Search code examples
pythonloopsstop-words

remove stopwords using for loop in python


I am recently studying python loop, and I want to try if I can use for loop to remove stop words and punctuation.

However, my code doesn't work and it shows punctuation is not defined. (the stopwords can be removed thanks to the comment)

I know it can be achieved using list comprehension and there are a lot of answers in StackOverflow, but I want to know how would it be achieved using for loop.

The code I used for practice is below:

texts = 'I love python, From John'

stopword = ['i', 'from']
punctuations = ','

texts = texts.split()

empty_list = []
    
for text in texts:
    text = text.lower()
    if text not in stopword and punctuation not in punctuations:
        empty_list.append(text)
        print(empty_list)

the expected output would be love python John

thanks for the help


Solution

  • Punctuation can be attached to words, so user replace.

    Then split the text into words, convert them to lower and for each word check if it exist in stopwords

    text = 'I love python, From John'
    stopwords = ['i', 'from']
    punctuations = [',']
    
    # first remove punctuations
    for p in punctuations:
        text = text.replace(p, "")
    # then split the text string and for each element check if it exist in stopwords
    
    result = []
    for word in text.split():
        if word.lower() not in stopwords:
            result.append(word)
    
    # join into string
    result = " ".join(result)
    

    Result:

    'love python John'