Replacing internet slang with for loop in Python

Viewed 33

I am rather new to Python and I'm not really sure why my code is not working.

I have a large Twitter dataset in which I want to replace slang with words from a dictionary (as a CSV) I got.

Reading the Data as a dictionary:

TestDict = pd.read_csv("C:/Users/lukas/Desktop/Script_Output/Converted_Slang_CSV.csv", sep =";", index_col = 0, header = None, skiprows = 1).to_dict()

Trying to replace the words with a for loop:

def change_words(input):
    words = input.split()
    new_words = []
    for word in words:
        if word.lower in TestDict:
            word = TestDict[word.lower()]
        new_words.append(word)
    new_text = " ".join(new_words)
    return new_text

If I feed the function with for example "atm" I expect to get "at the moment" back, as this phrase is in the dictionary. But instead, I just get "atm" back.

change_words("atm")

This will just bring back "atm" and not "at the moment". It also doesn't work if I apply it to my Twitter Dataset.

0 Answers
Related