Looping through Python string in search for keywords -


i have created little program counts kewords in string. see, keywords stored in txt file. today realized if word in string repeats keyword counter wont increase value. particulary in case "wrong" keyword in txt file , in result count variable wil 1 not 2.

how make work, repeating words counted too?

source_text = 'this wrong. wrong you?'  source_words = source_text.split() count = 0      word_list = [] open('pozit.txt') inputfile:     line in inputfile:         word_list.append(line.strip())  word in word_list:     if word in source_words:         count += 1 

you can use .count():

with open('pozit.txt') inputfile:     count = 0     line in inputfile:         count += line.count('wrong') 

if want words in linguistic sense, have @ nltk's tokenizer module.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -