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
Post a Comment