学习笔记CB001:NLTK库、语料库、词概率、双连词、词典

清醒疯子 发布于 2018年02月12日
无人欣赏。

聊天机器人知识主要是自然语言处理。包括语言分析和理解、语言生成、机器学习、人机对话、信息检索、信息传输与信息存储、文本分类、自动文摘、数学方法、语言资源、系统评测。

NLTK库安装,pip install nltk 。执行python。下载书籍,import nltk,nltk.download(),选择book,点Download。下载完,加载书籍,from nltk.book import * 。输入text*书籍节点,输出书籍标题。搜索文本,text1.concordance("former”) 。搜索相关词,text1.similar("ship") 。查看词在文章的位置,text4.dispersion_plot(["citizens", "democracy", "freedom", "duties", "America"]) ,可以按Ctr+Z退出。继续尝试其他函数需要重新执行python,重新加载书籍。词统计,总字数 len(text1),文本所有词集合 set(text1),文本总词数 len(set(text4)),单词出现总次数 text4.count("is") ,统计文章词频从大到小排序到列表 FreqDist(text1),统计词频输出累计图 fdist1 = FreqDist(text1);fdist1.plot(50, cumulative=True),只出现一次的词 fdist1.hapaxes(),频繁双联词 text4.collocations() 。

自然语言处理关键点,词意理解、自动生成语言,机器翻译、人机对话(图灵测试,5分钟内回答提出问题的30%)。基于规则,完全从语法句法出发,照语言规则分析、理解。基于统计,收集大量语料数据,统计学习理解语言,得益于硬件(GPU)、大数据、深度学习的发展。

NLTK语料库,Gutenberg,nltk.corpus.gutenberg.fileids()。Gutenberg语料库文件标识符,import nltk,nltk.corpus.gutenberg.fileids()。Gutenberg语料库阅读器 nltk.corpus.gutenberg。输出文章原始内容 nltk.corpus.gutenberg.raw('chesterton-brown.txt') 。输出文章单词列表 nltk.corpus.gutenberg.words('chesterton-brown.txt') 。输出文章句子列表 nltk.corpus.gutenberg.sents('chesterton-brown.txt') 。网络文本语料库,网络和聊天文本,from nltk.corpus import webtext 。布朗语料库,按照文本分类好500个不同来源文本,from nltk.corpus import brown 。路透社语料库,1万多个新闻文档,from nltk.corpus import reuters 。就职演说语料库,55个总统的演说,from nltk.corpus import inaugural 。

语料库组织结构,散养式(孤立多篇文章)、分类式(按照类别组织,但没有交集)、交叉式(文章属多个类)、渐变式(语法随时间发生变化)。

语料库通用接口,文件 fileids(),分类 categories(),原始内容 raw(),词汇 words(),句子 sents(),指定文件磁盘位置 abspath(),文件流 open()。

加载自定义语料库,from nltk.corpus import PlaintextCorpusReader ,corpusroot = '/Users/libinggen/Documents/workspace/Python/robot/txt' ,wordlists = PlaintextCorpusReader(corpusroot, '.*') ,wordlists.fileids() 。

格式转换GBK2UTF8,iconv -f GBK -t UTF-8 安娜·卡列尼娜.txt > 安娜·卡列尼娜utf8.txt 。

条件分布,在一定条件下事件概率颁上。条件频率分布,指定条件下事件频率分布。

输出布朗语料库每个类别条件每个词概率:

# coding:utf-8

import sys
import importlib
importlib.reload(sys)
import nltk
from nltk.corpus import brown

# 链表推导式,genre是brown语料库里的所有类别列表,word是这个类别中的词汇列表
# (genre, word)就是类别加词汇对
genre_word = [(genre, word)
        for genre in brown.categories()
        for word in brown.words(categories=genre)
        ]

# 创建条件频率分布
cfd = nltk.ConditionalFreqDist(genre_word)

# 指定条件和样本作图
# cfd.tabulate(conditions=['news','adventure'], samples=[u'stock', u'sunbonnet', u'Elevated', u'narcotic', u'four', u'woods', u'railing', u'Until', u'aggression', u'marching', u'looking', u'eligible', u'electricity', u'$25-a-plate', u'consulate', u'Casey', u'all-county', u'Belgians', u'Western', u'1959-60', u'Duhagon', u'sinking', u'1,119', u'co-operation', u'Famed', u'regional', u'Charitable', u'appropriation', u'yellow', u'uncertain', u'Heights', u'bringing', u'prize', u'Loen', u'Publique', u'wooden', u'Loeb', u'963', u'specialties', u'Sands', u'succession', u'Paul', u'Phyfe'])

cfd.plot(conditions=['news','adventure'], samples=[u'stock', u'sunbonnet', u'Elevated', u'narcotic', u'four', u'woods', u'railing', u'Until', u'aggression', u'marching', u'looking', u'eligible', u'electricity', u'$25-a-plate', u'consulate', u'Casey', u'all-county', u'Belgians', u'Western', u'1959-60', u'Duhagon', u'sinking', u'1,119', u'co-operation', u'Famed', u'regional', u'Charitable', u'appropriation', u'yellow', u'uncertain', u'Heights', u'bringing', u'prize', u'Loen', u'Publique', u'wooden', u'Loeb', u'963', u'specialties', u'Sands', u'succession', u'Paul', u'Phyfe'])

利用条件频率分布,按照最大条件概率生成双连词,生成随机文本:

# coding:utf-8

import sys
import importlib
importlib.reload(sys)

import nltk

# 循环10次,从cfdist中取当前单词最大概率的连词,并打印出来
def generate_model(cfdist, word, num=10):
    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)

# 以the开头,生成随机串
generate_model(cfd, 'the')

词典资源,词或短语集合: 词汇列表语料库,所有英文单词,识别语法错误 nltk.corpus.words.words 。 停用词语料库,识别最频繁出现没有意义词 nltk.corpus.stopwords.words 。 发音词典,输出英文单词发音 nltk.corpus.cmudict.dict 。比较词表,多种语言核心200多个词对照,语言翻译基础 nltk.corpus.swadesh 。同义词集,面向语义英语词典,同义词集网络 WordNet 。

参考资料:

http://www.shareditor.com/blogshow/?blogId=63

http://www.shareditor.com/blogshow?blogId=64

http://www.shareditor.com/blogshow?blogId=65

欢迎推荐上海机器学习工作机会,我的微信:qingxingfengzi

暂无回复
登录 或者 注册
相关帖子