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

简介:

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

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 ,corpus_root = '/Users/libinggen/Documents/workspace/Python/robot/txt' ,wordlists = PlaintextCorpusReader(corpus_root, '.*') ,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

目录
相关文章
|
JavaScript 前端开发
vue项目打包后使用reverse-sourcemap反编译到源码(详解版)
vue项目打包后使用reverse-sourcemap反编译到源码(详解版)
3577 0
|
存储 安全 Java
【Java】学习J.U.C中的ConCurrentHashmap源码分析
学习了HashMap知识点,了解到HashMap不是线程安全的,因为HashMap在高并发情况下可能会出现环形链表,造成下一次读操作形成死循环。所以,要使用ConcurrentHashMap线程安全的方法。 不多说了直接学习。每天进步一点点,加油。
427 0
【Java】学习J.U.C中的ConCurrentHashmap源码分析
|
数据可视化 Shell Linux
[Android电量] 耗电信息统计服务battery / BatteryStats
通过执⾏battery命令(不需要root) adb命令获取电量量消耗信息 获取整个设备的电量量消耗信息 获取某个apk的电量量消耗信息 batterystats使用步骤 通过执⾏battery命令(不需要root) 通过 adb shell dumpsys battery,返回结果后有电池的...
8136 0
|
Python 移动开发 网络协议
|
9天前
|
域名解析 人工智能 Linux
|
19天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17690 63
|
17天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
3天前
|
人工智能 安全 网络安全
|
7天前
|
云安全 数据采集 人工智能
|
8天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1398 5

热门文章

最新文章