{ "cells": [ { "cell_type": "markdown", "id": "4b1d288c-50c0-4da2-9ea9-bf5adb06b034", "metadata": {}, "source": [ "# Respell\n", "Respell receives as input a text as a string type, and substitute all the occurrences of a targeted word with a replacement as a string type chosen by the user." ] }, { "cell_type": "code", "execution_count": 1, "id": "691152f6-8e85-4fc2-8c98-848448ee959a", "metadata": {}, "outputs": [], "source": [ "from nltk.tokenize import word_tokenize" ] }, { "cell_type": "code", "execution_count": 5, "id": "2a46fe37-b62f-482a-a040-9f7a5d39fc0e", "metadata": {}, "outputs": [], "source": [ "# text, target, and replacement are string types\n", "def respell(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 = 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" ] }, { "cell_type": "markdown", "id": "63374a00-6091-484d-9ffd-ebf79d13cdc1", "metadata": {}, "source": [ "This function in itself could be understood as a filter to process and alter texts. By targeting specific words and replacing them, either for another word, for specific characters or for blank spaces, the user of the tool can intervene inside a text. One could break down the meaning of a text or create new narrative meanings by exposing its structure, taking out or highlighting specific and meaningful words and detaching such text from its original context. \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 poetic one." ] }, { "cell_type": "markdown", "id": "1fde0e28-f253-4992-96d1-0497da2959f6", "metadata": {}, "source": [ "# Examples" ] }, { "cell_type": "code", "execution_count": 3, "id": "6c80de23-d4ad-45a7-8b2f-b07c4ff2e549", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'potato is potato'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "respell(\"life is life\",\"life\",\"potato\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "772ae978-5551-486e-add1-0a5a660a0bff", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'🥙 is 🥙'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "respell(\"life is life\",\"life\",\"🥙\")" ] } ], "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.7.3" } }, "nbformat": 4, "nbformat_minor": 5 }