Professional Documents
Culture Documents
NLTK
NLTK
1. 词语索引
>>>text1.concordance('monstrous')
>>> text1.similar("monstrous")
2. 词语离散图
判断词在文本中的位置:从文本开头算起在它前面有多少词。这个位置信息可以用
离散图表示。
3. 词语计数
>>>len(text3)
44764
4. 文本-->词表 并排序
sorted(set(text3))
5. 词汇丰富度
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 论文的原理?】
条件概率的应用:
import nltk
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 的词条
下载、读取、处理网络文本
from urllib import urlopen
url = "http://www.gutenberg.org/files/2554/2554.txt"
raw = urlopen(url).read()
url = "http://news.bbc.co.uk/2/hi/health/2284783.stm"
html = urlopen(url).read()
raw = nltk.clean_html(html) # 清除 html 标记,但导航等内容还是无法清除
import feedparser
blog = feedparser.parse("http://languagelog.ldc.upenn.edu/nll/?feed=atom")
blog['feed']['title']
post = blog.entries[2]
tokens = nltk.word_tokenize(raw) # 分词
text = nltk.Text(tokens) # 下一步才能使用 text.collocations()等函数
# 解码
import codecs
f = codecs.open(path, encoding='latin2')
# 正则
re.findall(r'^.*(ing|ly|ed|ious|ies|ive|es|s|ment)$', 'processing') ==> ['ing']
re.findall(r'^.*(?:ing|ly|ed|ious|ies|ive|es|s|ment)$', 'processing') ==> ['processing']
# 查找上、下位词
hobbies_learned = nltk.Text(brown.words(categories=['hobbies', 'learned']))
hobbies_learned.findall(r"<\w*> <and> <other> <\w*s>")
将得到:
speed and other activities; water and other liquids; tomb and other
landmarks; Statues and other monuments; pearls and other jewels;
charts and other items; roads and other features; figures and other
objects; military and other areas; demands and other factors;
# 词干提取
tokens = nltk.word_tokenize(raw)
porter = nltk.PorterStemmer()
lancaster = nltk.LancasterStemmer()
[porter.stem(t) for t in tokens]
# 词形归并
wnl = nltk.WordNetLemmatizer()
[wnl.lemmatize(t) for t in tokens]
# 分词
nltk.regexp_tokenize()
# Python 过程风格与声明风格
# 找到文本中最长的词
matplotlib # 绘图工具
NetworkX # 网络可视化