1.AC代码展示
给的参考答案没有对标点符号进行剔除,这里用正则表达式删除标点
import re
import jieba
fi=open("天龙八部-网络版.txt","r",encoding="utf-8")
fo1=open("天龙八部-汉字统计.txt","w",encoding="utf-8")
fo2=open("天龙八部-词语统计.txt","w",encoding="utf-8")
txt=fi.read()
newtxt1= re.sub("[\s]", "",txt)
newtxt2= re.sub("[\:;\-\s+\.\!\/_,$%^*(+\"\')\[\]\{\}\`]+|[\·\:;《》‘’“”+——!,。??、~@#¥%……&*()]+", "",txt)
d1={}
d2={}
for c in newtxt1:
d1[c]=d1.get(c,0)+1
ls1=[]
for key in d1:
ls1.append("{}:{}".format(key,d1[key]))
fo1.write(",".join(ls1))
words=jieba.lcut(newtxt2)for w in words:
d2[w]=d2.get(w,0)+1
ls2=[]
for key in d2:
ls2.append("{}:{}".format(key,d2[key]))
fo2.write(",".join(ls2))
fi.close()
fo1.close()
fo2.close()
2.出于兴趣
1.按词频排序
2.输出词云
import re
import jieba
import wordcloud
fi=open("天龙八部-网络版.txt","r",encoding="utf-8")
fo1=open("天龙八部-汉字统计.txt","w",encoding="utf-8")
fo2=open("天龙八部-词语统计.txt","w",encoding="utf-8")
txt=fi.read()
newtxt1= re.sub("[\s]", "",txt)
newtxt2= re.sub("[\:;\-\s+\.\!\/_,$%^*(+\"\')\[\]\{\}\`]+|[\·\:\;\‘\’《》“”+——!,。??、~@#¥%……&*()]+", "",txt)
d1={}
d2={}
for c in newtxt1:
d1[c]=d1.get(c,0)+1
d1_sort=sorted(d1.items(),key=lambda x:x[1],reverse=True)
ls1=[]
for dui in d1_sort:
ls1.append("{}:{}".format(dui[0],dui[1]))
fo1.write(",".join(ls1))
words=jieba.lcut(newtxt2)
for w in words:
d2[w]=d2.get(w,0)+1
d2_sort=sorted(d2.items(),key=lambda x:x[1],reverse=True)
ls2=[]
for dui in d2_sort:
ls2.append("{}:{}".format(dui[0],dui[1]))
fo2.write(",".join(ls2))
ls3=[]
for dui in d2_sort:
ls3.append(dui[0])
words=" ".join(ls3)
w=wordcloud.WordCloud(\
font_path="C:\\Windows\\Fonts\\simkai.ttf",\
width=1000,height=700,background_color="white",\
max_words=15)
w.generate(words)
w.to_file("天龙八部.png")
fi.close()
fo1.close()
fo2.close()
3.展示
图1. 生成的词云文件