python自然语言处理 -读书笔记1

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zlp_zky/article/details/82992137
# -*- coding:utf-8 -*-
# __author__ = 'lipzhang'
import nltk
from nltk.book import *
# print(text1.concordance("monstrous"))#显示一个指 定单词的每一次出现,连同一些上下文一起显示
# print(text2.similar("monstrous"))#查看在text2中与该单词("monstrous")相似的词。
#print(text2.common_contexts(["monstrous", "very"]))#们研究两个或两个以上的词共同的上下文
#text4.dispersion_plot(["citizens", "democracy", "freedom", "duties", "America"])#判断词在文本中的位置:从文本开头算起在它前面有多少词。这个位置信息 可以用离散图表示。每一个竖线代表一个单词,每一行代表整个文本。
# print(len(text3) / len(set(text3)))#展示每个字平均被使用了多少次
# print(len((set(text3))))#展示这本书中一共使用了多少个不同的词汇,包括标点符号
# print(text3.count("smote"))#展示这本书中smote出现的次数
# print(100 * text4.count('a') / len(text4))#,计算一个特定的词在文本中占据的百分比


def lexical_diversity(text):#展示每个字平均被使用了多少次
    return len(text) / len(set(text))
def percentage(count, total):#计算百分比
    return 100 * count / total
#sent1...9是已经预定义好的词汇链表
fdist1=FreqDist(text1)#计算出来 的《白鲸记》中的总的词数
vocabulary1 = list(fdist1.keys())#表达式keys()为我们提供了文本中所有不同类型的链表
#fdist1.plot(50, cumulative=True)#《白鲸记》中 50 个最常用词的累积频率图
#print(vocabulary1[:50])
#print(fdist1.hapaxes())#只出现了一次的词
V = set(text5)
long_words = [w for w in V if len(w) > 15]
print(sorted(long_words))#找出文本词汇表长度中超过 15个字符的词

fdist5 = FreqDist(text5)
print(fdist5.items())
print(sorted([w for w in set(text5) if len(w) > 7 and fdist5[w] > 7]))#找出文本词汇表长度中超过7个字符并且出现次数大于7次的词

# print(text4.collocations())#找到比我们基于单个词的频率预期得到的更频繁出现的双连词

# fdist =FreqDist([len(w) for w in text1])#可以查看文本中词长的分布,
# print(fdist.items())
# print(fdist.max())
# print(fdist.freq(3))


猜你喜欢

转载自blog.csdn.net/zlp_zky/article/details/82992137