自然语言处理1——语言处理与Python(内含纠错)
学习Python自然语言处理,记录一下学习笔记。
运用Python进行自然语言处理需要用到nltk库,关于nltk库的安装,我使用的pip方式。
pip nltk
或者下载whl文件进行安装。(推荐pip方式,简单又适用)。
安装完成后就可以使用该库了,但是还需要下载学习所需要的数据。启动ipython,键入下面两行代码:
>>>import nltk
>>>nltk.download()
就会出现下面的一个界面:

选择book,选择好文件夹,(我选择的是E:\nltk_data)。下载数据。
下载完成后,可以验证一下下载成功与否:
>>> from nltk.book import *
*** Introductory Examples for the NLTK Book ***
Loading text1, ..., text9 and sent1, ..., sent9
Type the name of the text or sentence to view it.
Type: 'texts()' or 'sents()' to list the materials.
text1: Moby Dick by Herman Melville 1851
text2: Sense and Sensibility by Jane Austen 1811
text3: The Book of Genesis
text4: Inaugural Address Corpus
text5: Chat Corpus
text6: Monty Python and the Holy Grail
text7: Wall Street Journal
text8: Personals Corpus
text9: The Man Who Was Thursday by G . K . Chesterton 1908
如果出现上面的文本,说明下载数据成功。
在进行下面的操作之前,一定要保证先导入数据(from nltk.book import *)
prac1:搜索文本:
1.concordance('要搜索的文本')
>>>text1.concordance('monstrous')
Displaying 11 of 11 matches:
ong the former , one was of a most monstrous size . ... This came towards us ,
ON OF THE PSALMS . " Touching that monstrous bulk of the whale or ork we have r
ll over with a heathenish array of monstrous clubs and spears . Some were thick
d as you gazed , and wondered what monstrous cannibal and savage could ever hav
that has survived the flood ; most monstrous and most mountainous ! That Himmal
they might scout at Moby Dick as a monstrous fable , or still worse and more de
th of Radney .'" CHAPTER 55 Of the Monstrous Pictures of Whales . I shall ere l
ing Scenes . In connexion with the monstrous pictures of whales , I am strongly
ere to enter upon those still more monstrous stories of them which are to be fo
ght have been rummaged out of this monstrous cabinet there is no telling . But
of Whale - Bones ; for Whales of a monstrous size are oftentimes cast up dead u
2.similar('文本'):搜索那些词出现在相似的上下文中:
>>>text1.similar('monstrous')
exasperate imperial gamesome candid subtly contemptible lazy part
pitiable delightfully domineering puzzled determined vexatious
modifies fearless christian horrible mouldy doleful
>>>text2.similar('monstrous')
very heartily so exceedingly a extremely good great remarkably
amazingly sweet as vast
可以看出text1和text2在使用monstrous这个词上在表达意思上完全不同,在text2中,monstrous有正面的意思。
3.common_contexts(['word1','word2'...]):研究共用2个或者2个以上的词汇的上下文。
>>>text2.common_contexts(['monstrous','very'])
a_lucky be_glad am_glad a_pretty is_pretty
4.dispersion_plot():位置信息离散图。每一列代表一个单词,每一行代表整个文本。(ps:该函数需要依赖numpy和matplotlib库)
>>>text4.dispersion_plot(['citizens','democracy','freedom','duties','America'])

piac2:计数词汇:
计数词汇主要函数为len(),sorted():用于排序,set():用于得到唯一的词汇,去除重复。这些函数的用法和Python中一样,不做重复。
piac3:简单的统计:
该部分中很多函数都不在适用于python3,有的用法需要自己改进,有的则完全不可用
1.频率分布:FreqDist(文本):统计文本中每个标识符出现的频率。该函数在Python3上使用需要改进。
例如我们在text1《白鲸记》中统计最常出现的50个词:
原始版本:
>>>fdist1=FreqDist(text1)
>>>vocabulary1=fdist1.keys()
>>>vocabulary[:50]
但是在Python3中却行不通了。我们需要自己对其进行排序;
>>>fdist1=FreqDist(text1)
>>>len(fdist1)
19317
>>>vocabulary1=sorted(fd.items(),key=lambda jj:jj[1],reverse=True)
>>>s=[]
>>>for i in range(len(vocabulary1)):
s.append(vocabulary1[i][0])
>>>print(s)
[',', 'the', '.', 'of', 'and', 'a', 'to', ';', 'in', 'that', "'", '-', 'his', 'it', 'I', 's', 'is', 'he', 'with', 'was',
'as', '"', 'all', 'for', 'this', '!', 'at', 'by', 'but', 'not', '--', 'him', 'from', 'be', 'on', 'so', 'whale', 'one',
'you', 'had', 'have', 'there', 'But', 'or', 'were', 'now', 'which', '?', 'me', 'like']
在本书中,凡是涉及到FreqDist的都需要对其进行改进操作。
2.细粒度选择词:这里需要用到Python的列表推导式。
例如:选择text1中长度大于15的单词:
>>>V=sorted([w for w in set(text1) if len(w)>15])
['CIRCUMNAVIGATION',
'Physiognomically',
'apprehensiveness',
'cannibalistically',
'characteristically',
'circumnavigating',
'circumnavigation',
'circumnavigations',
'comprehensiveness',
'hermaphroditical',
'indiscriminately',
'indispensableness',
'irresistibleness',
'physiognomically',
'preternaturalness',
'responsibilities',
'simultaneousness',
'subterraneousness',
'supernaturalness',
'superstitiousness',
'uncomfortableness',
'uncompromisedness',
'undiscriminating',
'uninterpenetratingly']
判断的条件还有:s.startwith(t)、s.endwith(t)、t in s、s.islower()、s.isupper()、s.isalpha():都是字母、s.isalnum():字母和数字、s.isdigit()、s.istitle()
3.词语搭配和双连词:collocations()函数可以帮助我们完成这一任务。
如查看text4中的搭配:
>>>text4.collocations()
United States; fellow citizens; four years; years ago; Federal
Government; General Government; American people; Vice President; Old
World; Almighty God; Fellow citizens; Chief Magistrate; Chief Justice;
God bless; every citizen; Indian tribes; public debt; one another;
foreign nations; political parties
本书的第一章中还有一个babelize_shell()翻译函数,键入后会出现下面错误:
NameError: name 'babelize_shell' is not defined
原因是因为该模块已经不再可用了。
利用Python的条件分支和循环就可以简单的来处理一些文本信息。
自然语言处理1——语言处理与Python(内含纠错)的更多相关文章
- 自然语言处理(1)之NLTK与PYTHON
自然语言处理(1)之NLTK与PYTHON 题记: 由于现在的项目是搜索引擎,所以不由的对自然语言处理产生了好奇,再加上一直以来都想学Python,只是没有机会与时间.碰巧这几天在亚马逊上找书时发现了 ...
- 常用脚本语言Perl,Python,Ruby,Javascript一 Perl,Python,Ruby,Javascript
常用脚本语言Perl,Python,Ruby,Javascript一 Perl,Python,Ruby,Javascript Javascript现阶段还不适合用来做独立开发,它的天下还是在web应用 ...
- Python语言学习之Python入门到进阶
人们常说Python语言简单,编写简单程序时好像也确实如此.但实际上Python绝不简单,它也是一种很复杂的语言,其功能特征非常丰富,能支持多种编程风格,在几乎所有方面都能深度定制.要想用好Pytho ...
- D17——C语言基础学PYTHON
C语言基础学习PYTHON——基础学习D17 20181014内容纲要: 1.jQuery介绍 2.jQuery功能介绍 (1)jQuery的引入方式 (2)选择器 (3)筛选 (4)文本操作 (5) ...
- D16——C语言基础学PYTHON
C语言基础学习PYTHON——基础学习D16 20180927内容纲要: 1.JavaScript介绍 2.JavaScript功能介绍 3.JavaScript变量 4.Dom操作 a.获取标签 b ...
- D15——C语言基础学PYTHON
C语言基础学习PYTHON——基础学习D15 20180926内容纲要: 1.CSS介绍 2.CSS的四种引入方式 3.CSS选择器 4.CSS常用属性 5.小结 6.练习 1 CSS介绍 层叠样式表 ...
- D14——C语言基础学PYTHON
C语言基础学习PYTHON——基础学习D14 20180919内容纲要: 1.html认识 2.常用标签 3.京东html 4.小结 5.练习(简易淘宝html) 1.html初识(HyperText ...
- D13——C语言基础学PYTHON
C语言基础学习PYTHON——基础学习D13 20180918内容纲要: 堡垒机运维开发 1.堡垒机的介绍 2.堡垒机的架构 3.小结 4.堡垒机的功能实现需求 1 堡垒机的介绍 百度百科 随着信息安 ...
- D12——C语言基础学PYTHON
C语言基础学习PYTHON——基础学习D12 20180912内容纲要: 1.数据库介绍 2.RDMS术语 3.MySQL数据库介绍和基本使用 4.MySQL数据类型 5.MySQL常用命令 6.外键 ...
随机推荐
- IT人才最容易犯的17个错误--人生警言
转载 记得刚参加工作时(那是97年),中国的IT刚刚兴起,那时,作为一个IT人士是一件很光荣的事,而那时的我正在做电气和电子相关的工作.99年第一次跳槽,进入了IT行业做软件开发.至今,中国的IT已经 ...
- wMy_Python ~储存相关~
str,int,list,tuple,dict 是类型调用之后会产生一个 实例 >>> brand=["李宁",'耐克','阿迪达斯','鱼C'] >> ...
- CPP,MATLAB实现牛顿插值
牛顿插值法的原理,在维基百科上不太全面,具体可以参考这篇文章.同样贴出,楼主作为初学者认为好理解的代码. function p=Newton1(x1,y,x2) %p为多项式估计出的插值 syms x ...
- magento获取页面url的办法还有magento的常用函数
<?php echo $this->getStoreUrl('checkout/cart');?> 获取结账页面的url:<?php echo $this->getUrl ...
- Objective-C语言介绍 、 Objc与C语言 、 面向对象编程 、 类和对象 、 属性和方法 、 属性和实例变量
1 第一个OC控制台程序 1.1 问题 Xcode是苹果公司向开发人员提供的集成开发环境(非开源),用于开发Mac OS X,iOS的应用程序.其运行于苹果公司的Mac操作系统下. 本案例要求使用集成 ...
- [转】:HTTP请求流程(一)----流程简介
http://www.cnblogs.com/stg609/archive/2008/07/06/1236966.html HTTP请求流程(一)----流程简介 最近一直在研究如何让asp.net实 ...
- linux查看系统的启动时间和运行时间
1. uptime命令 输出:09:32:17 up 8:41, 1 user, load average: 0.01, 0.00, 0.00 其中8:41代表系统已经运行8小时41分 2.查看/pr ...
- Binary Tree Traversal
1.Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' values. For ex ...
- asp.net网站 提示Ambiguous match found
在ASP.net中,每个aspx页面都会有一个.cs文件,(好像不可以多个aspx共用一个cs的,我前面就碰到这个问题), 在aspx页面中,我们会用到服务器控件,或html控件,这些控件的id命名时 ...
- Win7下安装双系统Centos,并修复Centos引导加载程序安装在U盘上的问题
1.使用U盘安装Centos时,磁盘分区划分要注意:系统(包含Win7)只能4个主分区,所以只能在删除一个主分区或者在扩展分区的空闲分区内建立目录. 2.Centos在安装步骤的最后,引导加载程序的选 ...