4

I have searched quite thoroughly and have not found a suitable answer. I am new to Python/Programming, so I appreciate any advice I can get:

I am trying to search user input strings for certain key words. For example, we'll say filtering out profanity. From my research, I have been able to make the following dummy example:

Swear = ("curse", "curse", "curse") #Obviously not typing actual swear words, created a set
Userinput = str.lower(input("Tell me about your day: "))

if Userinput in Swear:
     print("Quit Cursing!")
else:
     print("That sounds great!")

Using the above, if the user enters an exact word form the set as their entire string, it will print "quit cursing"; however, if the user enters "curses" or "I like to say curse" it will print "That sounds great!"

Ultimately what I need is to be able to search the entire string for a key word, not an exact match of the entire string. Ex: "I went to the park and felt like screaming curses" should return true for a match.

b4hand
  • 9,550
  • 4
  • 44
  • 49
MoJo2015
  • 43
  • 1
  • 1
  • 5
  • What's the issue?This is a working code!!!!!!!!!1 – vks Dec 04 '14 at 05:54
  • As I said beneath the code, it only works if the user inputs the exact string from the list and only that. If the user inputs "curse" it will return true, if the user inputs "curses" or "i like to curse" it will return false (I realize the formatting and some syntax got dropped and that the code works) – MoJo2015 Dec 04 '14 at 06:01
  • Side note: you should read PEP 8 so that you use the recommended style convention. Here, you should use variables names with lower case letters: the form you are using in the question is reserved by convention for class names, which makes the code a little strange. – Eric O. Lebigot Dec 04 '14 at 06:21
  • Thank you EOL, I found what you are talking about and added it to my reading list. I appreciate it :) – MoJo2015 Dec 04 '14 at 06:26

3 Answers3

9
Swear = ["curse", "curse", "curse"]

for i in Swear:
    if i in Userinput:
        print 'Quit Cursing!'

You should read up on the differences between lists and tuples.

Community
  • 1
  • 1
caleb.breckon
  • 1,336
  • 18
  • 42
  • 1
    For longer texts (i.e. if `UserInput` is long), if efficiency is sought after, then a different method would be more efficient, where `UserInput` is read only once (typically with `re.finditer("\w+", UserInput)`). – Eric O. Lebigot Dec 04 '14 at 06:24
  • This works perfectly, thank you Caleb. I will go over the link you added as well =) – MoJo2015 Dec 04 '14 at 06:24
  • @EOL - very true. From what I gathered from their post, I thought the most intuitive answer would be best. – caleb.breckon Dec 04 '14 at 06:28
  • @MoJo2015 It checks for every word in Swear list and prints each time. In this case if i give input as curse it will print 'Quit Cursing!' three times. Is that you want or it should show message just once ? – Tanveer Alam Dec 04 '14 at 06:40
  • 1
    @TanveerAlam he said that was just an example. He will presumably have unique words in the swear list, or he can insert a break. – caleb.breckon Dec 04 '14 at 06:42
  • @caleb.breckon But it will check for each word in the list even if it finds the match, which has the complexity of O(n). You should try to fix that. – Tanveer Alam Dec 04 '14 at 06:44
  • @TanveerAlam like I said, I think they need to learn loops and conditionals before they start worrying about Big O. – caleb.breckon Dec 04 '14 at 06:46
  • 1
    @caleb.breckon Its about quality and efficient answer that makes SO amazing and helps in learning better way than other place. – Tanveer Alam Dec 04 '14 at 06:48
  • Ah yes I did see that this was looping, I added a break to stop it from spamming. I also used an else comment so if I did* swear it would print both from the if and else statement. break seems to have fixed it. @EOL mentioned re.finditer which I found some docs on and have been tinkering with but i think regular expressions are a bit too much for me right now. What is this big O? – MoJo2015 Dec 04 '14 at 07:11
  • @MoJo2015 `Break` works fine but using `break` in both `if` and `else` condition looks ugly. See my updated answer I have added an example of `re.finditer`. 'Big O' notation is used in Computer Science to describe the performance or complexity of an algorithm. You will many documentation on web for Big O. It would be really helpful to analyse our code. – Tanveer Alam Dec 04 '14 at 12:15
1
Swear = ("curse", "curse", "curse") 
Userinput = str.lower(raw_input("Tell me about your day: "))

if any(Userinput.find(s)>=0 for s in Swear):
     print("Quit Cursing!")
else:
     print("That sounds great!")

Result:

Tell me about your day: curse
Quit Cursing!

Tell me about your day: cursing
That sounds great!

Tell me about your day: curses
Quit Cursing!

Tell me about your day: I like curse
Quit Cursing!

Using Regular Expression:

Pattern used is r"\bcurse[\w]*".

Swear = ("curse", "curse", "curse") 
Userinput = str.lower(raw_input("Tell me about your day: "))

if any(match.group() for match in re.finditer(r"\bcurse[\w]*", Userinput)) :
     print("Quit Cursing!")
else:
     print("That sounds great!")

finditer(pattern, string, flags=0)
    Return an iterator over all non-overlapping matches in the
    string.  For each match, the iterator returns a match object.

    Empty matches are included in the result.
Tanveer Alam
  • 5,185
  • 4
  • 22
  • 43
  • This almost works for me except it doesn't work if the user inputs more than one word "I like to curse". Maybe I'm doing something wrong? Thank you for helping though. raw_input doesn't work for me but I believe they took that out of newer version of python. Edit: caleb.breckon's answer worked for me – MoJo2015 Dec 04 '14 at 06:11
  • 1
    That would be `if any(UserInput.find(s) >= 0 for s in Swear)`, which is not only more direct, but also faster, since it does not construct a list first for all the Swear words: the form I advise to use stops at the first Swear word, so it's more efficient and to the point. – Eric O. Lebigot Dec 04 '14 at 06:18
  • @EOL - Thanks. It was bad to iterate it all to search as i was trying any function. – Tanveer Alam Dec 04 '14 at 06:21
1

You can use sets, if only you want to check the existance of swear words,

a_swear_set = set(Swear)

if a_swear_set & set(Userinput.split()):
     print("Quit Cursing!")
else:
     print("That sounds great!")
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • Gave this one a try, works for the most part but if I put in "I scream curses" it will say "that sound's great"! caleb.breckon's seems to give me what I was looking for. I really appreciate the help. – MoJo2015 Dec 04 '14 at 06:15
  • I see know. It wont work for plurals. Thanks for pointing that out. – Marcin Dec 04 '14 at 06:37