统计词频,使用matplotlib画图验证齐普夫定律
程序员文章站
2022-03-08 19:38:16
...
以下我是用了人民日报的数据和英文数据进行分词,这里我是用的是pkuseg这个分词工具,原因是当时我读了一篇关于翟天临的文章,关于计算相似度,里面使用的是这个工具,然后我是用的是哈工大的停用此表,统计词频、排序等进行一系列的文本与预处理。接下就是使用matplotlib画图啦。
#-*-coding:utf-8-*-
from collections import Counter
import pkuseg
import matplotlib.pyplot as plt
import numpy
#导入文件
with open("The People's Daily.txt",encoding='utf-8') as fp:
with open("English.txt",encoding='utf-8-sig') as fp1:
data = fp.read()
fp.close()
data1=fp1.read()
fp1.close()
#人民日报数据清理
def cleanWord(content):
seg = pkuseg.pkuseg()
text = seg.cut(content)
stopwords = []
with open("哈工大停用词表.txt",encoding="utf-8") as f:
stopwords = f.read()
new_text = []
for w in text:
if len(w)>1 and len(w)<3 and w >= u'\u4e00' and w <= u'\u9fa5'and w not in stopwords:
new_text.append(w)
return new_text
#英文数据处理
def cleanWord2(english):
seg = pkuseg.pkuseg()
text = seg.cut(english)
stopwords2 = []
with open("哈工大停用词表.txt",encoding="utf-8") as f:
stopwords2 = f.read()
yingwen_text = []
for yingwen in text:
if yingwen not in stopwords2:
yingwen_text.append(yingwen)
return yingwen_text
#主函数部分
if __name__ == "__main__":
#整理
word = cleanWord(data)
word1=cleanWord2(data1)
#统计
counter=Counter(word)
counter1 = Counter(word1)
#排序
counter_top=counter.most_common()
counter_top1=counter1.most_common()
print("人民日报词频统计:",counter)
print("人民日报词语长度:",len(counter_top))
print("英文词频统计:",counter1)
print("英文词的长度:",len(counter_top1))
# 绘图
#第一张图
plt.figure('散点图')
plt.rc('font',family='SimHei',size=13)
y=list(map(lambda y:y[1], counter_top[:]))
y1 = list(map(lambda y: y[1], counter_top1[:]))
plt.xlabel('rank')
plt.ylabel('frequency')
plt.scatter(range(len(y)),y ,s=1,c='red')
plt.scatter(range(len(y1)), y1, s=1, c='black')
plt.title('Word frequency vs rank')
#第二张图
plt.figure("折线图 ")
plt.rc('font', family='SimHei', size=13)
y = list(map(lambda y: y[1], counter_top[:]))
y1 = list(map(lambda y1: y1[1], counter_top1[:]))
numpy.seterr(divide='ignore')
a=numpy.log(range(len(y)))
b=numpy.log(y)
c=numpy.log(range(len(y1)))
d=numpy.log(y1)
plt.xlabel('log(rank)')
plt.ylabel('log(frequency)')
line, = plt.plot(a,b, label='人民日报', color='red')
line1, = plt.plot(c,d, label='英文单词', color='black')
plt.legend(handles=[line,line1, ], loc='best')
plt.title('Word log(frequency) vs log(rank)')
plt.show()
由于某种原因
图片敬请上传!!!