Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
filtering a string
#1
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
Reply
#2
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"
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
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
Reply
#4
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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
@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))
Reply
#6
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.
Reply
#7
I will try them out later and let you know but thanks for the help
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020