I have a text file in french that I want to count its most occurring words, without taking in consideration stop words. Here's the code:
with open('./text_file.txt', 'r', encoding='utf8') as f:
s = f.read()
num_chars = len(s)
num_lines = s.count('\n')
#call split with no arguments
words = s.split()
d = {}
for w in words:
if w in d:
d[w] += 1
else:
d[w] = 1
num_words = sum(d[w] for w in d)
lst = [(d[w],w) for w in d]
lst.sort()
lst.reverse()
# nltk treatment
from nltk.corpus import stopwords # Import the stop word list
from nltk.tokenize import wordpunct_tokenize
stop_words = set(stopwords.words('french')) # creating a set makes the searching faster
print (stop_words)
print ([word for word in lst if word not in stop_words])
print('\n The 50 most frequent words are /n')
i = 1
for count, word in lst[:50]:
print('%2s. %4s %s' %(i,count,word))
i+= 1
This returns the most occurred words including the stop words. Any better idea how to do this?