{ "cells": [ { "cell_type": "markdown", "id": "4baa72c7-b196-4658-b8bb-b8aaf73fd631", "metadata": {}, "source": [ "https://hub.xpub.nl/soupboat/~grgr/api/" ] }, { "cell_type": "markdown", "id": "c719aba0-0866-4ea1-a235-1675984f34ef", "metadata": { "tags": [] }, "source": [ "# ... and I whish that your question has been answered\n", "\n", "\n", "this tool includes two functions that intervene on text by replacing specific targeted words with either other words or single characters.\n", "The 2 main functions are based in a very simple mechanism, but the scope and results one could get by using them in different ways and with different intentions can be very compelling.\n", "One could break down the meaning of a text, by exposing its structure, taking out specific and meaningful words and detaching such text from its original context.\n", "If then, these words would be substituted for others, the text can be given a new meaning, reclaiming concepts or ideas.\n", "The words could be also erased and replaced for blank spaces in order to create a new narrative meaning with what is left on the page, as well as to create abstract compositions. \n", "This tool offers a broad spectrum of possibilities in which it can be used, from a very political and subversive use, to a more playful and artistic (or poetic) one." ] }, { "cell_type": "markdown", "id": "0cd66035-60a5-4df6-884d-1012b5fe1132", "metadata": {}, "source": [ "### import" ] }, { "cell_type": "code", "execution_count": 2, "id": "5ace74f9-7dde-4944-9b49-878020a38bae", "metadata": {}, "outputs": [], "source": [ "from flask import Flask, request, json, render_template, url_for, redirect\n", "from jinja2 import Environment, PackageLoader, select_autoescape\n", "from weasyprint import HTML, CSS\n", "from nltk.tokenize import word_tokenize\n", "\n", "\n", "import os \n", "import json" ] }, { "cell_type": "markdown", "id": "a85a6dfa-25b9-4cb0-808e-823f35bdf2cb", "metadata": {}, "source": [ "for the receipt printer:" ] }, { "cell_type": "code", "execution_count": 3, "id": "9a22628a-123f-48a6-8b6a-2bea1918acec", "metadata": {}, "outputs": [], "source": [ "from escpos.printer import Network\n" ] }, { "cell_type": "markdown", "id": "de935eee-c378-4867-b7a4-0753ebb555fc", "metadata": {}, "source": [ "### functions" ] }, { "cell_type": "code", "execution_count": 4, "id": "f4618f95-9cbb-45a4-ad29-a70febd77fdc", "metadata": {}, "outputs": [], "source": [ "# function: respell\n", "# '''replace all the occurrences of a targeted word in a given text'''\n", "# def swap(text, target, replacement):\n", "# result = text.replace(target, replacement)\n", "# return result\n", "\n", "def swap(text, target, replacement):\n", " target = target.lower()\n", " txt = word_tokenize(text)\n", " new = []\n", " \n", "# print(f\"swap target:'{target}'\")\n", " for w in txt:\n", " if w == target:\n", " w = replacement\n", " new = new + [w]\n", " elif w == target[0:1].upper() + target[1:]:\n", " w = replacement[0:1].upper() + replacement[1:] \n", " new = new + [w]\n", " elif w == target.upper():\n", " w = replacement.upper()\n", " new = new + [w]\n", " else:\n", " new = new + [w]\n", " text = ' '.join(new)\n", " final= text.replace(' .','.').replace(' ,',',').replace(' :',':').replace(' ;',';').replace('< ','<').replace(' >','>').replace(' / ','/').replace('& ','&')\n", " return final\n", "\n", "# function: stitch\n", "# '''replace all the occurrences of a target word with a single character that is repeated as many times as the length of the target'''\n", "## example: stitch('halo stitch this', 'this', '*') ----> result:'halo stitch ****'\n", "def mend(text, target, replacement):\n", " target = target.lower()\n", " txt = word_tokenize(text)\n", " new = []\n", " \n", " for w in txt:\n", " if w == target:\n", " w = len(w)*replacement\n", " new = new + [w]\n", " elif w == target[0].upper() + target[1:]:\n", " w = len(w)*replacement\n", " new = new + [w]\n", " elif w== target.upper():\n", " w = len(w)*replacement \n", " new = new + [w]\n", " else:\n", " new = new + [w]\n", " text = ' '.join(new)\n", " final= text.replace(' .','.').replace(' ,',',').replace(' :',':').replace(' ;',';').replace('< ','<').replace(' >','>').replace(' / ','/').replace('& ','&')\n", " return final\n", "\n", "\n", "# reveal \n", "def underline(text,Group):\n", " txt = word_tokenize(text)\n", " \n", " txt_linebr = []\n", " for token in txt:\n", " if token == '<':\n", " continue\n", " elif token == 'br/':\n", " token='
'\n", " txt_linebr.append(token)\n", " elif token == '>':\n", " continue\n", " else:\n", " txt_linebr.append(token) \n", " new = []\n", " group = Group.split(\",\")\n", " for w in txt_linebr:\n", " if w=='
':\n", " new = new + [w]\n", " elif w not in group:\n", " w = len(w) * ' '\n", " new = new + [w]\n", " elif w in group :\n", " new = new + [w]\n", " text = ' '.join(new)\n", " final= text.replace(' .','.').replace(' ,',',').replace(' :',':').replace(' ;',';').replace('< ','<').replace(' >','>').replace(' / ','/').replace('& ','&')\n", " return final\n", "\n", "#function: txt_br\n", "#to read the line breaks in a .txt file, returns string readable in html \n", "def txt_br(any_text):\n", " lines= any_text.readlines()\n", " replaced = ''\n", " for line in lines:\n", " replaced+= line.replace('\\n', '
') \n", " return replaced" ] }, { "cell_type": "markdown", "id": "29b47df7-5323-41d0-8785-1f992bd38563", "metadata": {}, "source": [ "### flask " ] }, { "cell_type": "markdown", "id": "4c1b7998-8610-43ba-99a0-6f46b3e433d4", "metadata": {}, "source": [ "!pwd\n", "todo: adjust the width of the text on the right" ] }, { "cell_type": "code", "execution_count": null, "id": "70e167cd-929a-4323-9b15-c3926bf28b59", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " * Serving Flask app '__main__' (lazy loading)\n", " * Environment: production\n", "\u001b[31m WARNING: This is a development server. Do not use it in a production deployment.\u001b[0m\n", "\u001b[2m Use a production WSGI server instead.\u001b[0m\n", " * Debug mode: off\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " * Running on http://127.0.0.1:9087/ (Press CTRL+C to quit)\n", "127.0.0.1 - - [17/Dec/2021 11:43:28] \"GET /~grgr/api/and_i_wish_that_your_question_has_been_answered/ HTTP/1.0\" 200 -\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "result is:KYRIAKOS MITSOTAKIS, GREEK PRIME MINISTER

I understand that in the Netherlands you have a culture of asking direct questions to politicians, which I very much respect.
What I will not accept is that, in this office, you will insult me, or the Greek people, with accusations and expressions that are not supported by material facts when this country has been dealing with a migration crisis of unprecedented intensity, has been saving hundreds, if not thousands of people at sea.
We just rescued 250 people in danger of drowning south of Crete, we are doing this every single day rescuing people at sea, while, at the same time, we are intercepting boats that come from Turkey, as we have the right to do in accordance with European regulations and waiting for the Turkish Coast Guard to come and pick them up and return them to Turkey.
So, rather than putting the blame on Greece, you should put the blame on those who have been instrumentalizing migration systematically pushing people in to a desperate situation from a safe country, because I need to remind you that people who are in Turkey are not in danger, their life is not in danger and you should put the blame on others and not us.
We have a tough, but fair, policy on migration, we have processed and given the right to protection in Greece to 50,000 people, including tens of thousands of Afghans, in accordance…
Allow me. Have you visited the new camps on our islands ? Have you been to Samos ? … No listen to me, you have not been to Samos… No you have not been…
Please…Look, you will not come into this building and insult me.
Am I very clear on this ?
I am answering now and you will not interrupt me, in the same way that I listened to you very carefully.
If you go to Samos, you will find an impeccable camp, with impeccable conditions, funded by EU money, with clean facilities, with playgrounds for…the children to play, no comparison to what we had in the past.
This is our policy, we will stand by it, and I will not accept anyone pointing the finger to this government and accusing it of inhumane behavior.



MARK RUTTE, DUTCH PRIME MINISTER

I am absolutely convinced that this prime minister and this government is applying the highest standards and the fact that they have immediately launched an investigation on the issue of the pushbacks is testimony of that.
I will now go back on the situation of 2015 and 2016 when we had many people dying on the Aegean Sea trying to get from Turkey into Greece and then to Germany, Sweeden, the Netherlands etc. And I am happy that Germany and we -were holding at that time the rotating presidency of the EU- were able to negotiate the EU and Turkey agreement.
By which indeed Turkey is a safe country for people to stay.
And Turkey at this moment is hosting over 3 million Syrian refugees in the South of Turkey in camps but also in the local communities.
What this country is trying to do is to defend the outer borders of the European Union.
It is a lot of tasks that countries have who are lying on the outside like Italy, Spain, Hungary, Slovenia, but also Poland and Greece, and there is an extremely difficult situation.
What I don ’ t want again is for people to take boats that are not fully equipped to pass the Mediterranean or to pass the Aegean Sea, to die in those circumstances.
I want them to stay there, to be safe, and then we are willing as European Union to take a fair share of people from Africa, from Turkey – refugees, in line with the plans devised in 2015 and 2016.
So this is my answer and I wish that your question has been answered.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "127.0.0.1 - - [17/Dec/2021 11:43:31] \"GET /~grgr/api/archive/ HTTP/1.0\" 200 -\n" ] } ], "source": [ "app = Flask(__name__)\n", "\n", "\n", "\n", "# @app.route(f\"/~grgr/api/\")\n", "# def replace():\n", " \n", "# text=request.values.get('text', '')\n", "# target = request.values.get('target', '')\n", "# replacement = request.values.get('replacement', '')\n", "# title = f\"

{request.values.get('title', '')}

\"\n", "# txt_result = f\"

{swap(text, target, replacement)}

\"\n", "# contents= title + txt_result\n", " \n", "# with open(\"./assets/outputs/db.txt\", \"a\") as db:\n", "# db.write(contents+ \" \\n\")\n", " \n", "# with open (\"archive_section.html\", \"a\") as output:\n", "# print(contents, file=output)\n", " \n", "# return render_template(\"index.html\",\n", "# text=text,\n", "# contents=contents)\n", "\n", "\n", " \n", "\n", "# archive page\n", "@app.route(f\"/~grgr/api/archive/\", methods=['GET'])\n", "def archive():\n", " with open(\"./assets/question.txt\", 'r') as q_file:\n", " question = txt_br(q_file)\n", " #read the json file\n", " with open(\"archive.json\", \"r\") as file:\n", " archive_input = json.loads(file.read())\n", " archive = archive_input['archive']\n", " answers=[]\n", "# # create a list with all the sections contents from the json\n", " for section in archive:\n", " answers.append(section['answers']) \n", " \n", " return render_template(\"archive.html\",\n", " answers = answers,\n", " question=question)\n", "\n", "\n", "\n", "# main page with the form:\n", "temp_changes=[]\n", "\n", "@app.route(\"/~grgr/api/and_i_wish_that_your_question_has_been_answered/\", methods=['GET', 'POST'])\n", "def interact():\n", " target = request.values.get('target', '')\n", " f_replacement = request.values.get('f_replacement','')\n", " function = request.values.get('function', 'replace') \n", " text = request.values.get('text','')\n", " result = ''\n", " \n", " with open(\"./assets/question.txt\", 'r') as q_file:\n", " question = txt_br(q_file)\n", " \n", " with open(\"./assets/mitsotakis.txt\", 'r') as file:\n", " source = txt_br(file)\n", " \n", " if request.method == 'POST': \n", " if function == 'mend': \n", " stitch = f_replacement\n", " if ' ' in stitch:\n", " space = stitch.replace(' ','  ')\n", " result= mend(text, target, space)\n", " else:\n", " result= mend(text, target, stitch)\n", " elif function == 'underline':\n", " popup =underline(source, target)\n", " result= popup.replace(' ','  ')\n", " else:\n", " replacement = f_replacement\n", " result = swap(text, target, replacement)\n", " print('target is:'+ target)\n", " # now save temporary all the changes in the list changes\n", " temp_changes.append(result)\n", "\n", " if not text:\n", " # the first time we open the url the text is not saved, so plz flask, use the original source as text!\n", " if function == 'mend':\n", " result = mend(source, target, f_replacement)\n", " elif function== 'underline':\n", " popup = underline(source, target)\n", " result= popup.replace(' ','  ')\n", " \n", " else:\n", " result = swap(source, target, f_replacement)\n", " \n", " print('result is:'+result)\n", "\n", " return render_template('postform.html', question=question, text=text, result=result, source= source, function=function, f_replacement=f_replacement)\n", "\n", "\n", "\n", "#save the changes to the archive\n", "@app.route(\"/~grgr/api/save/\", methods=['GET', 'POST'])\n", "def save_to_archive():\n", " if request.method == 'POST':\n", " \n", " with open(\"./assets/question.txt\", 'r') as q_file:\n", " question = txt_br(q_file)\n", " \n", " with open(\"archive.json\", \"r\") as file:\n", " archive_input = json.loads(file.read())\n", " archive = archive_input['archive']\n", " i = len(temp_changes)-1\n", " while i >= 0:\n", " last_change = temp_changes[i]\n", " if last_change:\n", " last_change= temp_changes[i]\n", " \n", " section = {\"question\":question, \n", " \"answers\": last_change}\n", " archive.append(section)\n", " print('saved')\n", " break\n", " else:\n", " i = i-1 \n", " \n", " #print to the archive \n", " with open(\"archive.json\", \"w\") as file:\n", " file.write(json.dumps({\"archive\":archive}))\n", " \n", " #print with the line printer \n", "# with open('/dev/usb/lp0', 'w') as lp:\n", "# print(last_change, file=lp) \n", "# print_last = last_change.replace('  ',' ').replace('
', '\\n')\n", "# print(f'last print is:{print_last}')\n", " \n", " #for ethernet receipt printer\n", "# kitchen = Network(\"192.168.1.140\") #Printer IP Address\n", "# kitchen.text(print_last) \n", " \n", "# temp_changes.clear()iii\n", " \n", " return redirect(\"https://hub.xpub.nl/soupboat/~grgr/api/and_i_wish_that_your_question_has_been_answered/\")\n", "\n", "app.run(port=9087)" ] }, { "cell_type": "code", "execution_count": 48, "id": "6c18d410-7bb3-41a2-a3a6-1b101a7e02be", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "temp= ['']\n", "print(len(temp))" ] }, { "cell_type": "markdown", "id": "aa7e5557-f1eb-40d9-9f7d-a160096dd13c", "metadata": {}, "source": [ "# " ] }, { "cell_type": "markdown", "id": "35dc1723-75e4-4a21-bb63-6cfcfb84be28", "metadata": {}, "source": [ "## teeeeeesstttssssss" ] }, { "cell_type": "code", "execution_count": null, "id": "940e6b9f-3645-46a5-a9ae-25f6aa2d5b00", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 5, "id": "540e00da-7e8e-44cf-8b09-7d793e8892c7", "metadata": {}, "outputs": [], "source": [ "from weasyprint import HTML, CSS\n", "\n", "# with is nice cause it auto-closes the file once \"outside\" the with block\n", "with open (\"test.html\", \"a\") as output:\n", " print (\"
\", file=output)\n",
    "    print (sent + \" \", file=output)\n",
    "    print (\"
\", file=output)\n", "HTML(filename=\"test.html\").write_pdf('./test.pdf')" ] }, { "cell_type": "code", "execution_count": 106, "id": "c1d35ebc-6445-4a94-8f6a-70aa6d39d57c", "metadata": {}, "outputs": [], "source": [ "def ifcap(text, target, replacement):\n", " result=[]\n", " index= 0\n", " for w in text.split():\n", " w_lower = w.lower()\n", " if w_lower == target and w.isupper()==true:\n", " while w[index].isupper() == true and index\n" ] } ], "source": [ "with open(\"./assets/mitsotakis.txt\", 'r') as source:\n", " print(source)" ] }, { "cell_type": "code", "execution_count": null, "id": "9e5435ae-d71e-4463-b4fd-73a64d37fb59", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 44, "id": "7a35bd62-5909-4ade-b546-4c7f4f0f849e", "metadata": {}, "outputs": [], "source": [ "sentence= \"stitches //Have something <\\repairing\\ have HAVE\"" ] }, { "cell_type": "code", "execution_count": 17, "id": "2a2f38f2-18a1-46e1-a39d-bfa75bf780a8", "metadata": {}, "outputs": [], "source": [ "# stitch function, replace a target with stitches\n", "\n", "def change(text, target, replacement):\n", " result = text.replace(target, replacement)\n", " return result" ] }, { "cell_type": "code", "execution_count": 30, "id": "4b88817c-df89-4b8a-9d64-75e27fb54a06", "metadata": {}, "outputs": [], "source": [ "#updated swap function!\n", "from nltk.tokenize import word_tokenize\n", "\n", "def stitch(text, target, replacement):\n", " target = target.lower()\n", " txt = word_tokenize(text)\n", " new = []\n", " \n", " for w in txt:\n", " if w == target:\n", " w = len(w)*replacement\n", " new = new + [w]\n", " elif w == target[0].upper() + target[1:]:\n", " w = len(w)*replacement\n", " new = new + [w]\n", " elif w== target.upper():\n", " w = len(w)*replacement \n", " new = new + [w]\n", " else:\n", " new = new + [w]\n", " text = ' '.join(new)\n", " return text" ] }, { "cell_type": "code", "execution_count": null, "id": "2f868523-f1aa-47d5-872e-c0a224be5b49", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 54, "id": "501d662a-665c-400e-a4b0-954b5fccb4dd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'stitches //Have something