{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Creating a dictionary of text for Beatnik\n", "\n", "this python notebook is dedicated to list the scrabble value of given text." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Installing required Library " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pip install beatnik --upgrade" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Interactive text\n", "display the value corresponding to the word in realtime" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "142ea644a7c544cc92ad1e5bd4683532", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(Text(value='this is a line of text', description='input_text'), Output()), _dom_classes=…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from __future__ import print_function\n", "import ipywidgets as widgets\n", "from ipywidgets import interact, interactive, fixed, interact_manual\n", "from ipywidgets import Button, Layout, Textarea, HBox, VBox\n", "from beatnik import beatnik_simple\n", "from beatnik import beatnik_stack\n", "from beatnik import preprocess\n", "\n", "ACTION = {\n", " 5: 'PUSH',\n", " 6: 'DISCARD',\n", " 7: 'ADD',\n", " 8: 'INPUT',\n", " 9: 'OUTPUT',\n", " 10: 'SUBTRACT',\n", " 11: 'SWAP',\n", " 12: 'DUP',\n", " 13: 'SKIP_AHEAD_ZERO',\n", " 14: 'SKIP_AHEAD_NONZERO',\n", " 15: 'SKIP_BACK_ZERO',\n", " 16: 'SKIP_BACK_NONZERO',\n", " 17: 'STOP',\n", "}\n", "\n", "def scrabble(word):\n", " SCRABBLE = {\n", " 'A': 1,'B': 3,'C': 3,'D': 2,'E': 1,'F': 4,'G': 2,\n", " 'H': 4,'I': 1,'J': 8,'K': 5,'L': 1,'M': 3,'N': 1,\n", " 'O': 1,'P': 3,'Q': 10,'R': 1,'S': 1,'T': 1,'U': 1,\n", " 'V': 4,'W': 4,'X': 8,'Y': 4,'Z': 10}\n", " acc = 0\n", " for c in word.upper():\n", " acc += SCRABBLE[c]\n", " return acc\n", "\n", "def f(input_text):\n", " token = preprocess(input_text)\n", " for w in token:\n", " print('{:<12} {:<4} {:>2}'.format(w,scrabble(w),ACTION.get(scrabble(w), 'NOP')))\n", "\n", "interact(f, input_text='this is a line of text');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Interactive dictionary\n", "building a dictionary of values corresponding to words in realtime" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "48c0a524bdca4148b22abc464b563a57", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(Text(value='Sind Zeichen oder Programmierkenntnisse somit Texte, wenn Entscheidungsfindu…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "def f2(input_text):\n", " dic = []\n", " for a in range(50):\n", " dic.append([])\n", " token = preprocess(input_text)\n", " for w in token:\n", " temp_value = scrabble(w)\n", " lower_word = w.lower()\n", " if(lower_word not in dic[temp_value]):\n", " dic[temp_value].append(lower_word)\n", " for i in dic:\n", " i.sort()\n", " \n", " for _index,_action in enumerate(dic):\n", " if(len(_action)!=0):\n", " print(_index,ACTION.get(_index, 'NOP'),_action)\n", " \n", "interact(f2, input_text='''Sind Zeichen oder Programmierkenntnisse somit Texte, wenn Entscheidungsfindungen statt in Zusammenhang mit uns in einem größeren Kontext (also im System: Material) stehen?\n", "He Hazardous\n", "of we strong\n", "follow bacteria walks\n", "by town guy place\n", "Er stand neben mir,\n", "sprach er Sätze, dass nicht von mir versteht wurden.\n", "bis übermorgen, blieben die Sätze mir im Gedächtnis.\n", "fünf verschiedene Grammatiken, verwendente er.\n", "Schritt für Schritt. Ohne jede vernünftige Struktur, gabe er mir.\n", "Farbe, dass niemand viel findet, wurde gezeigt.\n", "aus der und die\n", "alle in alt\n", "und das land schiß\n", "sang der haß der flöhe für die hysterischen und ausgeschlossenen frauen\n", "Of neighborhood reputation:\n", "Doubted empty buses’ land A very cold girl Taxi of attempt\n", "By Paris’ gestures\n", "Man sagt, dass alle Übersetzungen nur für den Leser sind. Werke dienen nur der Hörerschaft, oder? Es gibt Werke, die dienen. Es gibt überdies Leben – das doppelsinnige Dichterische oder geheimnisvolle Bild. Es gilt: Wesentliche Übersetzer, das sind Dichter. Genug für den Augenblick, oder nicht? Darauf: translate!\n", "sing walfisch! vorhin loesten anderswo dicke boote ihre anker.\n", "Man sagt, dass alle Übersetzungen nur für den Leser sind. Genug, denn gegenüber gibt es eines Dichtung. tongue teilt doppelsinnig tongue teilt mutter tongue macht übel oder grundsätzlich nicht translate.\n", "mit Körperflüssigkeiten verschwimmen mit Resilienz Poren mit Erderwärmung nicht sich.\n", "''');" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.6" } }, "nbformat": 4, "nbformat_minor": 4 }