python统计字词练习

时间:2023-03-09 01:36:40
python统计字词练习

方法一:

 import operator
from nltk.corpus import stopwords
stop_words = stopwords.words('English')#目的是去除人称代词等,注意根据编译提示下载相应库 speech_text = '''
He is a good boy
She is a good girl
We are very nice
Hello boy hello boy
hello girl hello girl
hello dog
hello cat
hello pig
'''
speech = speech_text.lower().split()
dic = {}
for word in speech:
if word not in dic:
dic[word] = 1 #给词典赋值
else:
dic[word] = dic[word] + 1
swd = sorted(dic.items(), key = operator.itemgetter(1),reverse = True)
#stop_words
for k,v in swd:
if k not in stop_words:
print(k,v) print(swd)

方法二:

 import operator
from nltk.corpus import stopwords
stop_words = stopwords.words('English')#目的是去除人称代词等,注意根据编译提示下载相应库 speech_text = '''
He is a good boy
She is a good girl
We are very nice
Hello boy hello boy
hello girl hello girl
hello dog
hello cat
hello pig
'''
speech = speech_text.lower().split()
from collections import Counter
c = Counter(speech)
for sw in stop_words:
del c[sw]
print(c.most_common(10)) #打印前10项