Search code examples
pythonlistfileinputpunctuation

Remove conjunction from file.txt and punctuation from user input


I want to clean a string from user input from punctuation and conjunction. the conjunction is stored in the file.txt (Stop Word.txt)

I already tried this code:

f = open("Stop Word.txt", "r")

def message(userInput):
    punctuation = "!@#$%^&*()_+<>?:.,;/"
    words = userInput.lower().split()
    conjunction = f.read().split("\n")
    for char in words:
        punc = char.strip(punctuation)
        if punc in conjunction:
            words.remove(punc)
            print(words)

message(input("Pesan: "))

OUTPUT

when i input "Hello, how are you? and where are you?" 
i expect the output is [hello,how,are,you,where,are,you]
but the output is [hello,how,are,you?,where,are,you?]
or [hello,how,are,you?,and,where,are,you?]

Solution

  • Use list comprehension to construct words and check if the word is in your conjunction list:

    f = open("Stop Word.txt", "r")
    
    def message(userInput):
        punctuation = "!@#$%^&*()_+<>?:.,;/"
        words = userInput.lower().split()
        conjunction = f.read().split("\n")
        return [char.strip(punctuation) for char in words if char not in conjunction]
    
    print (message("Hello, how are you? and where are you?"))
    
    #['hello', 'how', 'are', 'you', 'where', 'are', 'you']