import jieba
def ciping():
    article = open('../爬取QQ空间/qq_word1.txt','r', encoding="utf-8").read()
    dele = {'。','!','?','的','“','”','(',')',' ','》','《',','}
   # jieba.add_word()
   words = list(jieba.cut(article))
   articleDict = {}
   articleSet = set(words)-dele
   for w in articleSet:
        if len(w)>1:
            articleDict[w] = words.count(w)
    articlelist = sorted(articleDict.items(),key = lambda x:x[1], reverse = True)
    for i in articlelist:
        print(i)
    return articlelist
def paint(articlelist):
    import matplotlib.pyplot as plt
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False
    x=[]
    y=[]
    lens = len(articlelist)
    print("总共有"+str(lens)+"个分词")
    for i in range(10):
        x.append(articlelist[i][0])
        y.append(articlelist[i][1])
    fig, ax = plt.subplots(figsize=(10, 7))
    ax.bar(x=x, height=y)
    ax.set_title("Word frequency", fontsize=15)
    plt.savefig("participle.png")
    plt.show()
if __name__ == '__main__':
    articlelists=ciping()
    paint(articlelists)