Python Forum

Full Version: filtering a string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,
I am looking for a way to filter words out of a string. I need it to work like this:
The string contains known words and unknown words and I need to divide both. For example:

input(word something) or input(word word something word)

word should go into a variable and something into another

Any ideas?

Thanks
If it only a few words, put them each in their own list, then test if the word exists in one or the other list. 

good_words = ['good', 'better', 'best', 'great']
bad_words = ['bad', 'worst', 'terrible', 'evil']

reply = input("Enter a word: ")

if reply in good_words:
    a_reply = reply
elif reply in bad_words:
    a_reply = reply
else:
    a_reply = "Not in my vocabulary"
That is not that what I am looking for there are more words that need to be filtered like:
hello my name is

known words are: hello, name
unknown words are: my, is

I need both in a variable because I need to decide with them witch "if" to use but still need the words in another variable that are not known
You could look at NLTK. Along with my original example, that gives you both ends of the spectrum.  Other than that, you need to be more specific as to what you are looking to do.
@hello_its_me, something like this?

vocabulary = ["hello", "name"]
words = input("Enter words: ")

known_words = []
unknown_words = []

for word in words.split(" "):
    if word in vocabulary:
        if word not in known_words:
            known_words.append(word)
    elif word not in unknown_words:
        unknown_words.append(word)

print("Known words: " + str(known_words))
print("Unknown words: " + str(unknown_words))
That is what you're looking for, though.  Loop through the sentence, word-by-word, and build up a result list of what your expected output is.
I will try them out later and let you know but thanks for the help