You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3.7 KiB

Respell

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.

In [1]:
from nltk.tokenize import word_tokenize
In [5]:
# text, target, and replacement are string types
def respell(text, target, replacement):
    target = target.lower()
    txt = word_tokenize(text)
    new = []
    
    for w in txt:
        if w == target:
            w = replacement
            new = new + [w]
        elif w == target[0:1].upper() + target[1:]:
            w = replacement[0:1].upper() + replacement[1:] 
            new = new + [w]
        elif w == target.upper():
            w = replacement.upper()
            new = new + [w]
        else:
            new = new + [w]
    text = ' '.join(new)
    final= text.replace(' .','.').replace(' ,',',').replace(' :',':').replace(' ;',';').replace('< ','<').replace(' >','>').replace(' / ','/').replace('& ','&')
    return final

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. 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.

Examples

In [3]:
respell("life is life","life","potato")
Out[3]:
'potato is potato'
In [4]:
respell("life is life","life","🥙")
Out[4]:
'🥙 is 🥙'