# -*- coding: UTF-8 -*- import nltk from nltk.book import * len_text2 = len(text2) len_dis_text2 = len(set(text2)) print("在text2中有" + str(len_text2) + "个词") print("在text2中有" + str(len_dis_text2) + "个不同的词") print("因为言情小说词汇多样性得分是8.3,幽默小的词汇多样性得分是4.3,所以是幽默小说文体中的词汇更丰富") text5.collocations() word_li = [w for w in text5 if len(w) == 4] fdist = FreqDist(word_li) sorted_word_li = sorted(fdist.keys(), key=lambda x: fdist[x], reverse=True) for w in sorted_word_li: print "%s\t%d; " % (w, fdist[w]), print ("") def percent(word,text): freq = len([w for w in text if w == word]) * 1.0 / len(text)*100 return freq print (str(round(percent("JOIN",text5),3))+"%")