So I have two txt files. one with a list of 2000 words and another with 1000 sentences. I also have a function that turns the list of words into a dictionary, with each word having a value of 0
So if the word list was : oranges bananas apples
The function returns:
{'oranges':0, 'bananas':0, 'apples':0}
I need to compare each sentence with this dictionary and increase the values for every word based on their frequency of the sentence.
So if the sentence was "I like apples, oranges, and bananas, but oranges are the best.", then the dictionary should contain:
{'oranges':2, 'bananas':1, 'apples':1}
To access the sentences from the file I used
file = open(sentences.txt)
lines = file.readlines()
Why not iterate over the words in each line? Suppose your dictionary is called words_dict
.
Then:
for line in file:
for word in line:
if word in words_dict:
words_dict[word] += 1
file.close()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.