2

What I am trying to do in my program is to have the program open a file with many different words inside it.

Receive a user input and check if any word inside the file is in user input.

Inside the file redflags.txt:

happy
angry 
ball 
jump

each word on a different line.

For example if the user input is "Hey I am a ball" then it will print redflag. If the user input is "Hey this is a sphere" then it will print noflag.

Redflags = open("redflags.txt")

data = Redflags.read()
text_post = raw_input("Enter text you wish to analyse")
words = text_post.split() and text_post.lower()

if data in words:
  print("redflag")
else:
 print("noflag")      
void
  • 2,571
  • 2
  • 20
  • 35
Aranya
  • 23
  • 4
  • For starters, this: `words = text_post.split() and text_post.lower()` isn't doing what you think it is doing. You want `words = text_post.lower().split()`. – juanpa.arrivillaga Jun 29 '17 at 05:38

3 Answers3

1

This should do the trick! Sets are generally much faster than lookups for list comparisons. Sets can tell you the intersection in this case (overlapping words), differences, etc. We consume the file that has a list of words, remove newline characters, lowercase, and we have our first list. The second list is created from the user input and split on spacing. Now we can perform our set intersection to see if any common words exist.

# read in the words and create list of words with each word on newline
# replace newline chars and lowercase
words = [word.replace('\n', '').lower() for word in open('filepath_to_word_list.txt', 'r').readlines()]
# collect user input and lowercase and split into list
user_input = raw_input('Please enter your words').lower().split()

# use set intersection to find if common words exist and print redflag
if set(words).intersection(set(user_input)):
    print('redflag')
else:
    print('noflag')
datawrestler
  • 1,527
  • 15
  • 17
  • I think https://stackoverflow.com/a/20756176/1141389 is probably a better way to go for getting the words from the file. See my answer below. – Wesley Bowman Jun 29 '17 at 06:06
1
with open('redflags.txt', 'r') as f:
    # See this post: https://stackoverflow.com/a/20756176/1141389
    file_words = f.read().splitlines()

# get the user's words, lower case them all, then split
user_words = raw_input('Please enter your words').lower().split()

# use sets to find if any user_words are in file_words
if set(file_words).intersection(set(user_words)):
    print('redflag')
else:
    print('noredflag')
Wesley Bowman
  • 1,366
  • 16
  • 35
  • Instead of using the `set`, you could do a list generator as well, but sets are nice, especially if later you do not want any duplicate values. – Wesley Bowman Jun 29 '17 at 06:04
0

I would suggest you to use list comprehensions

Take a look at this code which does what you want (I will explain them below):

Redflags = open("redflags.txt")

data = Redflags.readlines()

data = [d.strip() for d in data]

text_post = input("Enter text you wish to analyse:")
text_post = text_post.split()

fin_list = [i for i in data if i in text_post]


if (fin_list):
    print("RedFlag")
else:
    print("NoFlag")

Output 1:

Enter text you wish to analyse:i am sad
NoFlag

Output 2:

Enter text you wish to analyse:i am angry
RedFlag

So first open the file and read them using readlines() this gives a list of lines from file

>>> data = Redflags.readlines()
['happy\n', 'angry \n', 'ball \n', 'jump\n']

See all those unwanted spaces,newlines (\n) use strip() to remove them! But you can't strip() a list. So take individual items from the list and then apply strip(). This can be done efficiently using list comprehensions.

data = [d.strip() for d in data]

Also why are you using raw_input() In Python3 use input() instead.

After getting and splitting the input text.

fin_list = [i for i in data if i in text_post]

I'm creating a list of items where i (each item) from data list is also in text_pos list. So this way I get common items which are in both lists.

>>> fin_list
['angry']           #if my input is "i am angry"

Note: In python empty lists are considered as false,

>>> bool([])
False

While those with values are considered True,

>>> bool(['some','values'])
True

This way your if only executes if list is non-empty. Meaning 'RedFlag' will be printed only when some common item is found between two lists. You get what you want.

void
  • 2,571
  • 2
  • 20
  • 35