Valus Sulav
Valus Sulav

Reputation: 145

removing custom stop words form a phrase in python

I am trying to remove certain phrases and words form a user input before I further process the input and while trying to do that I'm running into a problem of getting an "index out of range" error and am completely stuck. How do I solve this? I get my input phrase as a string which I convert to a list to compare every word and I have my stop words as a predefined list.
Example inputs:
["well","you","know","the","weather","is","awful"]
["you", "know", "what", "i", "mean", "so", "just", "turn", "the", "lights", "on"]

#Gets user input and removes the selected stop words from it and returns a filtered phrase back.    
def stop_word_remover(phrase_list):

    stop_words_lst = ["yo", "so", "well", "um", "a", "the","you know", "i mean"]

    #initalize clean phrase string
    clean_input_phrase= ""

    #copying phrase_list into a new variable for stopword removal.
    Copy_phrase_list = list(phrase_list)

    #Cleanup loop

    for i in range(1,len(phrase_list)):
        has_stop_words = False

        for x in range(len(stop_words_lst)):
            has_stop_words = False

            #if one of the stop words matches the word passed by the first main loop      the  flag is raised.
            if (phrase_list[i-1]+" "+phrase_list[i]) == stop_words_lst[x].strip():
                has_stop_words = True    

            # this if statement adds the word of the phrase only if the flag is not raised thus making sure all the stop words are filtered out         
            if has_stop_words == True:
                Copy_phrase_list.remove(Copy_phrase_list[i-1])
                Copy_phrase_list.remove(Copy_phrase_list[i-1])

    #first for loop takes a individual words of the phrase given and makes a loop until the whole phrase goes through one word at a time
    for i in range(len(Copy_phrase_list)):
        #flag initialized for marking stop words
        has_stop_words = False

        #second loop takes all the stop words and compares them to the first word passed on by the first loop to sheck for a stop word
        for x in range(len(stop_words_lst)):
            #if one of the stop words matches the word passed by the first main loop the  flag is raised.
            if Copy_phrase_list[i] == stop_words_lst[x].strip():
            has_stop_words = True    

        # this if statement adds the word of the phrase only if the flag is not raised thus making sure all the stop words are filtered out        
        if has_stop_words == False:
            clean_input_phrase += str(Copy_phrase_list[i]) +" "


return clean_input_phrase

Upvotes: 1

Views: 7375

Answers (2)

Marc Poulin
Marc Poulin

Reputation: 66

Use the regular expression substitute function. Replace each match with an empty string.

stop_words_lst = ['yo', 'so', 'well', 'um', 'a', 'the', 'you know', 'i mean']
s = "you know what i mean so just turn the lights on"

import re
for w in stop_words_lst:
    pattern = r'\b'+w+r'\b'
    s = re.sub(pattern, '', s)
    print (s)

Upvotes: 5

geekpradd
geekpradd

Reputation: 418

You need to separate your word lists. One should be for single words and another should be for phrases.

single_word_list = ["yo", "so", "well", "um", "a", "the"]
phrase_list = ["you know", "i mean"]
for index, word in enumerate(Copy_phrase_list) :
    if word in single_word_lst:
        del Copy_phrase_list[index] 
    if word + " " + Copy_phrase_list[index+1] in phrase_list:
        del Copy_phrase_list[index] 
        del Copy_phrase_list[index+1] 
return " ".join(Copy_phrase_list) 

And then you need to convert copy_phrase_list to a string and return it.

Upvotes: 0

Related Questions