NLTK的内置函数
(1) concordance函数 给出一个指定单词每一次出现,连同上下文一起显示。
>>>text1.concordance('monstrous')
(2) similar函数 查找文中上下文结构相似的词,如the___pictures 和 the___size 等。
>>> text1.similar("monstrous")
(3) common_contexts 函数 检测、查找两个或两个以上的词共同的上下文。
>>> text2.common_contexts(["monstrous", "very"])
be_glad am_glad a_pretty is_pretty a_lucky
>>>
2. 词语离散图
判断词在文本中的位置:从文本开头算起在它前面有多少词。这个位置信息可以用离散图表示。
>>> text4.dispersion_plot(["citizens", "democracy", "freedom", "duties", "America"])
>>>
3. 词语计数
>>>len(text3)
44764
4. 文本-->词表 并排序
sorted(set(text3))
5. 词汇丰富度
>>> from __future__ import division
>>> len(text3) / len(set(text3))
16.050197203298673
>>>
6. 词在文本中出现的次数和百分比
>>> text3.count("smote")
5
>>> 100 * text4.count('a') / len(text4)
1.4643016433938312
>>>
7. 索引列表
(1) 表示元素位置的数字叫做元素的索引。
>>> text1[50]
'grammars'
>>>
(2) 找出一个词第一次出现的索引。
>>> text1.index('grammars')
50
>>>
8. 切片 可以获取到文本中的词汇(文本片段)。
>>>text1[100:120]['and', 'to', 'teach', 'them', 'by', 'what', 'name', 'a', 'whale', '-', 'fish', 'is', 'to', 'be', 'called', 'in', 'our', 'tongue', 'leaving', 'out']
>>>
9. NLTK 频率分布类中定义的函数
例子 描述
fdist = FreqDist(samples) 创建包含给定样本的频率分布
fdist.inc(sample) 增加样本
fdist['monstrous'] 计数给定样本出现的次数
fdist.freq('monstrous') 给定样本的频率
fdist.N() 样本总数
fdist.keys() 以频率递减顺序排序的样本链表
for sample in fdist: 以频率递减的顺序遍历样本
fdist.max() 数值最大的样本
fdist.tabulate() 绘制频率分布表
fdist.plot() 绘制频率分布图
fdist.plot(cumulative=True) 绘制累积频率分布图
fdist1 < fdist2 测试样本在fdist1 中出现的频率是否小于fdist2
text1.concordance("monstrous") # 搜索单词,并显示上下文
text1.similar("monstrous") # 搜索具有相似上下文的单词
text2.common_context(["monstrous", "very"]) #两个或两个以上的词的共同的上下文
text4.dispersion_plot(["citizens", "democracy", "freedom", "duties", "America"]) # 将语料按时间顺序拼接,此命令即可画出这些单词在语料中的位置,可以用来研究随时间推移语言使用上的变化
text3.generate() # 根据语料3的词序列统计信息生成随机文本【计算机写SCI论文的原理?】
len(text3) / len(set(text3)) # 计算平均词频 或者叫 词汇丰富度
100* text3.count("smote") / len(text3) # 计算特定词在文本中的百分比
标识符: All words
类型:Unique words
FreqDist(text1).keys()[:50] # 查看text1中频率最高的前50个词,FreeDist([])用来计算列表中元素的频率
FreqDist(text1).hapaxes() # 查看频率为1的词
bigrams(['more', 'is', 'said', 'than', 'done']) # 构造双连词,即[('more', 'is'), ('is', 'said'), ('said', 'than'), ('than', 'done')]
text4.collocations() # 返回文本中的双连词
fdist = FreqDist(samples) 创建包含给定样本的频率分布
fdist.inc(sample) 增加样本
fdist['monstrous'] 计数给定样本出现的次数
fdist.freq('monstrous') 给定样本的频率
fdist.N() 样本总数
fdist.keys() 以频率递减顺序排序的样本链表
for sample in fdist: 以频率递减的顺序遍历样本
fdist.max() 数值最大的样本
fdist.tabulate() 绘制频率分布表
fdist.plot() 绘制频率分布图
fdist.plot(cumulative=True) 绘制累积频率分布图
fdist1 < fdist2 测试样本在 fdist1 中出现的频率是否小于 fdist2
gutenberg.raw(fileid) # 给出原始文本内容
gutenberg.words(fileid) # 词数
gutenberg.sents(fileid) # 句数
wordlists = PlaintextCorpusReader(corpus_root, '.*') # 读入自己的语料库
cfdist= ConditionalFreqDist(pairs) 从配对链表中创建条件频率分布
cfdist.conditions() 将条件按字母排序
cfdist[condition] 此条件下的频率分布
cfdist[condition][sample] 此条件下给定样本的频率
cfdist.tabulate() 为条件频率分布制表
cfdist.tabulate(samples, conditions) 指定样本和条件限制下制表
cfdist.plot() 为条件频率分布绘图
cfdist.plot(samples, conditions) 指定样本和条件限制下绘图
cfdist1 < cfdist2 测试样本在 cfdist1 中出现次数是否小于在 cfdist2 中出现次数
条件概率的应用:
# -*- encoding: utf-8 -*-
import nltk
def generate_model(cfdist, word, num=15):
for i in range(num):
print word
word = cfdist[word].max()
text = nltk.corpus.genesis.words('english-kjv.txt')
bigrams = nltk.bigrams(text)
cfd = nltk.ConditionalFreqDist(bigrams)
print cfd['living']
generate_model(cfd, 'living')
nltk.corpus.stopwords.words('english') # stop words, 停用词
nltk.corpus.names # 姓名
wordnet.synsets('car') # 同义词集
wordnet.lemmas('car') # 获取所有包含词car的词条
# 找到文本中最长的词
maxlen = max(len(word) for word in text)
[word for word in text if len(word) == maxlen] # 熟悉并经常使用
lengths = map(len, nltk.corpus.brown.sents(categories="news"))
avg = sum(lengths) / len(lengths)
set() # 后台已经做了索引,集合成员地查找尽可能使用set
matplotlib # 绘图工具
NetworkX # 网络可视化