自然语言处理(1)之NLTK与PYTHON

题记: 由于现在的项目是搜索引擎,所以不由的对自然语言处理产生了好奇,再加上一直以来都想学Python,只是没有机会与时间。碰巧这几天在亚马逊上找书时发现了这本《Python自然语言处理》,瞬间觉得这对我同时入门自然语言处理与Python有很大的帮助。所以最近都会学习这本书,也写下这些笔记。

1. NLTK简述

NLTK模块及功能介绍

语言处理任务 NLTK模块 功能描述
获取语料库 nltk.corpus 语料库和词典的标准化接口
字符串处理 nltk.tokenize,nltk.stem 分词、句子分解、提取主干
搭配研究 nltk.collocations t-检验,卡方,点互信息
词性标示符 nltk.tag n-gram,backoff,Brill,HMM,TnT
分类 nltk.classify,nltk.cluster 决策树,最大熵,朴素贝叶斯,EM,k-means
分块 nltk.chunk 正则表达式,n-gram,命名实体
解析 nltk.parse 图标,基于特征,一致性,概率性,依赖项
语义解释 nltk.sem,nltk.inference λ演算,一阶逻辑,模型检验
指标评测 nltk.metrics 精度,召回率,协议系数
概率与估计 nltk.probability 频率分布,平滑概率分布
应用 nltk.app,nltk.chat 图形化的关键词排序,分析器,WordNet查看器,聊天机器人
语言学领域的工作 nltk.toolbox 处理SIL工具箱格式的数据

2. NLTK安装

  我的Python版本是2.7.5,NLTK版本2.0.4

 DESCRIPTION
The Natural Language Toolkit (NLTK) is an open source Python library
for Natural Language Processing. A free online book is available.
(If you use the library for academic research, please cite the book.) Steven Bird, Ewan Klein, and Edward Loper ().
Natural Language Processing with Python. O'Reilly Media Inc.
http://nltk.org/book @version: 2.0.

安装步骤跟http://www.nltk.org/install.html 一样

1. 安装Setuptools: http://pypi.python.org/pypi/setuptools

  在页面的最下面setuptools-5.7.tar.gz

2. 安装 Pip: 运行 sudo easy_install pip(一定要以root权限运行)

3. 安装 Numpy (optional): 运行 sudo pip install -U numpy

4. 安装 NLTK: 运行 sudo pip install -U nltk

5. 进入python,并输入以下命令

 :chapter2 rcf$ python
Python 2.7. (default, Mar , ::)
[GCC 4.2. Compatible Apple LLVM 5.0 (clang-500.0.)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
>>> nltk.download()

当出现以下界面进行nltk_data的下载

也可直接到 http://nltk.googlecode.com/svn/trunk/nltk_data/index.xml 去下载数据包,并拖到Download Directory。我就是这么做的。

最后在Python目录运行以下命令以及结果,说明安装已成功

 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
text2: Sense and Sensibility by Jane Austen
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

3. NLTK的初次使用

  现在开始进入正题,由于本人没学过python,所以使用NLTK也就是学习Python的过程。初次学习NLTK主要使用的时NLTK里面自带的一些现有数据,上图中已由显示,这些数据都在nltk.book里面。

3.1 搜索文本

concordance:搜索text1中的monstrous

 >>> text1.concordance("monstrous")
Building index...
Displaying of 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

similar:查找text1中与monstrous相关的所有词语

 >>> text1.similar("monstrous")
Building word-context index...
abundant candid careful christian contemptible curious delightfully
determined doleful domineering exasperate fearless few gamesome
horrible impalpable imperial lamentable lazy loving

dispersion_plot:用离散图判断词在文本的位置即偏移量

 >>> text4.dispersion_plot(["citizens","democracy","freedom","duties","America"])

3.2 计数词汇

len:获取长度,即可获取文章的词汇个数,也可获取单个词的长度

 >>> len(text1)   #计算text1的词汇个数

 >>> len(set(text1)) #计算text1 不同的词汇个数

 >>> len(text1[])   #计算text1 第一个词的长度
 

sorted:排序

 >>> sent1
['Call', 'me', 'Ishmael', '.']
>>> sorted(sent1)
['.', 'Call', 'Ishmael', 'me']

3.3 频率分布

nltk.probability.FreqDist

 >>> fdist1=FreqDist(text1)    #获取text1的频率分布情况
>>> fdist1         #text1具有19317个样本,但是总体有260819个值
<FreqDist with samples and outcomes>
>>> keys=fdist1.keys()
>>> keys[:] #获取text1的前50个样本
[',', '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']
 >>> fdist1.items()[:]      #text1的样本分布情况,比如','出现了18713次,总共的词为260819
[(',', ), ('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', )]
 >>> fdist1.hapaxes()[:]   #text1的样本只出现一次的词
['!\'"', '!)"', '!*', '!--"', '"...', "',--", "';", '):', ');--', ',)', '--\'"', '---"', '---,', '."*', '."--', '.*--', '.--"', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
3 >>> fdist1['!\'"']
4 1

>>> fdist1.plot(,cumulative=True) #画出text1的频率分布图

3.4 细粒度的选择词

 >>> long_words=[w for w in set(text1) if len(w) > ]  #获取text1内样本词汇长度大于15的词并按字典序排序
>>> sorted(long_words)
['CIRCUMNAVIGATION', 'Physiognomically', 'apprehensiveness', 'cannibalistically', 'characteristically', 'circumnavigating', 'circumnavigation', 'circumnavigations', 'comprehensiveness', 'hermaphroditical', 'indiscriminately', 'indispensableness', 'irresistibleness', 'physiognomically', 'preternaturalness', 'responsibilities', 'simultaneousness', 'subterraneousness', 'supernaturalness', 'superstitiousness', 'uncomfortableness', 'uncompromisedness', 'undiscriminating', 'uninterpenetratingly']
>>> fdist1=FreqDist(text1) #获取text1内样本词汇长度大于7且出现次数大于7的词并按字典序排序
>>> sorted([wforwin set(text5) if len(w) > and fdist1[w] > ]) ['American', 'actually', 'afternoon', 'anything', 'attention', 'beautiful', 'carefully', 'carrying', 'children', 'commanded', 'concerning', 'considered', 'considering', 'difference', 'different', 'distance', 'elsewhere', 'employed', 'entitled', 'especially', 'everything', 'excellent', 'experience', 'expression', 'floating', 'following', 'forgotten', 'gentlemen', 'gigantic', 'happened', 'horrible', 'important', 'impossible', 'included', 'individual', 'interesting', 'invisible', 'involved', 'monsters', 'mountain', 'occasional', 'opposite', 'original', 'originally', 'particular', 'pictures', 'pointing', 'position', 'possibly', 'probably', 'question', 'regularly', 'remember', 'revolving', 'shoulders', 'sleeping', 'something', 'sometimes', 'somewhere', 'speaking', 'specially', 'standing', 'starting', 'straight', 'stranger', 'superior', 'supposed', 'surprise', 'terrible', 'themselves', 'thinking', 'thoughts', 'together', 'understand', 'watching', 'whatever', 'whenever', 'wonderful', 'yesterday', 'yourself']

3.5 词语搭配和双连词

用bigrams()可以实现双连词

 >>> bigrams(['more','is','said','than','done'])
[('more', 'is'), ('is', 'said'), ('said', 'than'), ('than', 'done')]
>>> text1.collocations()
Building collocations list
Sperm Whale; Moby Dick; White Whale; old man; Captain Ahab; sperm
whale; Right Whale; Captain Peleg; New Bedford; Cape Horn; cried Ahab;
years ago; lower jaw; never mind; Father Mapple; cried Stubb; chief
mate; white whale; ivory leg; one hand

3.6 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的类情况. 使用type可以查看变量类型,使用help()可以获取类的属性以及方法。以后想要获取具体的方法可以使用help(),这个还是很好用的。

 >>> type(text1)
<class 'nltk.text.Text'>
>>> help('nltk.text.Text')
Help on class Text in nltk.text: nltk.text.Text = class Text(__builtin__.object)
| A wrapper around a sequence of simple (string) tokens, which is
| intended to support initial exploration of texts (via the
| interactive console). Its methods perform a variety of analyses
| on the text's contexts (e.g., counting, concordancing, collocation
| discovery), and display the results. If you wish to write a
| program which makes use of these analyses, then you should bypass
| the ``Text`` class, and use the appropriate analysis function or
| class directly instead.
|
| A ``Text`` is typically initialized from a given document or
| corpus. E.g.:
|
| >>> import nltk.corpus
| >>> from nltk.text import Text
| >>> moby = Text(nltk.corpus.gutenberg.words('melville-moby_dick.txt'))
|
| Methods defined here:
|
| __getitem__(self, i)
|
| __init__(self, tokens, name=None)
| Create a Text object.
|
| :param tokens: The source text.
| :type tokens: sequence of str
|
| __len__(self)
|
| __repr__(self)
| :return: A string representation of this FreqDist.
| :rtype: string
|
| collocations(self, num=, window_size=)
| Print collocations derived from the text, ignoring stopwords.
|
| :seealso: find_collocations
| :param num: The maximum number of collocations to print.
| :type num: int
| :param window_size: The number of tokens spanned by a collocation (default=)
| :type window_size: int
|
| common_contexts(self, words, num=)
| Find contexts where the specified words appear; list
| most frequent common contexts first.
|
| :param word: The word used to seed the similarity search
| :type word: str
| :param num: The number of words to generate (default=)
| :type num: int
| :seealso: ContextIndex.common_contexts()

4. 语言理解的技术

1. 词意消歧

2. 指代消解

3. 自动生成语言

4. 机器翻译

5. 人机对话系统

6. 文本的含义

5. 总结

虽然是初次接触Python,NLTK,但是我已经觉得他们的好用以及方便,接下来就会深入的学习他们。

自然语言处理(1)之NLTK与PYTHON的更多相关文章

  1. python自然语言处理函数库nltk从入门到精通

    1. 关于Python安装的补充 若在ubuntu系统中同时安装了Python2和python3,则输入python或python2命令打开python2.x版本的控制台:输入python3命令打开p ...

  2. Python自然语言处理实践: 在NLTK中使用斯坦福中文分词器

    http://www.52nlp.cn/python%E8%87%AA%E7%84%B6%E8%AF%AD%E8%A8%80%E5%A4%84%E7%90%86%E5%AE%9E%E8%B7%B5-% ...

  3. 自然语言处理1——语言处理与Python(内含纠错)

    学习Python自然语言处理,记录一下学习笔记. 运用Python进行自然语言处理需要用到nltk库,关于nltk库的安装,我使用的pip方式. pip nltk 或者下载whl文件进行安装.(推荐p ...

  4. 自然语言23_Text Classification with NLTK

    QQ:231469242 欢迎喜欢nltk朋友交流 https://www.pythonprogramming.net/text-classification-nltk-tutorial/?compl ...

  5. 自然语言20_The corpora with NLTK

    QQ:231469242 欢迎喜欢nltk朋友交流 https://www.pythonprogramming.net/nltk-corpus-corpora-tutorial/?completed= ...

  6. 自然语言19.1_Lemmatizing with NLTK(单词变体还原)

    QQ:231469242 欢迎喜欢nltk朋友交流 https://www.pythonprogramming.net/lemmatizing-nltk-tutorial/?completed=/na ...

  7. 自然语言14_Stemming words with NLTK

    https://www.pythonprogramming.net/stemming-nltk-tutorial/?completed=/stop-words-nltk-tutorial/ # -*- ...

  8. 自然语言13_Stop words with NLTK

    https://www.pythonprogramming.net/stop-words-nltk-tutorial/?completed=/tokenizing-words-sentences-nl ...

  9. hanlp自然语言处理包的基本使用--python

    hanlp拥有:中文分词.命名实体识别.摘要关键字.依存句法分析.简繁拼音转换.智能推荐. 这里主要介绍一下hanlp的中文分词.命名实体识别.依存句法分析,这里就不介绍具体的hanlp的安装了,百度 ...

随机推荐

  1. 如何高性能的给UIImageView加个圆角

    文/natewang(简书作者)原文链接:http://www.jianshu.com/p/268f3839d2e6著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 其实你只需要的是圆角 ...

  2. (组合数学3.1.1.2)UVA 10098 Generating Fast(使用字典序思想产生所有序列)

    /* * UVA_10098.cpp * * Created on: 2013年10月8日 * Author: Administrator */ #include <iostream> # ...

  3. ruby 安装 运行

    Ruby基础 一 简介 1.Ruby在windows平台下的安装 (1)下载地址:http://rubyinstaller.org/downloads/ (2)安装过程 这里我们选择安装路径为 D:\ ...

  4. 【每日一linux命令8】添加新的工作组(groupadd)

    groupadd (字意add group)增加一个新的工作组. 语法:groupadd 选项 用户组名 选项: -g 指定新建工作组的ID -r 创建系统工作组,系统工作组的ID小于500 -k 覆 ...

  5. php中应该哪怕被打断腿都要记得的几个函数

    php中应该哪怕被打断腿都要记得的几个函数: substr() 截取字符串 : explode() 使用一个字符串分割另一个字符串 : implode() 将数组用预定的字符连接成字符串: 下面有一个 ...

  6. 浙江大学PAT上机题解析之1015. 德才论 (25)

    宋代史学家司马光在<资治通鉴>中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人.凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人 ...

  7. cocos2d-x项目过程记录(ios和android设备的适配)

    (原创作品,欢迎转载,注明出处,谢谢:http://www.cnblogs.com/binxindoudou/admin/EditPosts.aspx?postid=3213645) 1.原理分析的博 ...

  8. [转] 使用CodeViz生成C/C++函数调用关系图

    运行环境:虚拟机下的Ubuntu 11.04 结合Graphviz工具,使用CodeViz可以生成直观和漂亮的C/C++程序函数之间的调用关系图. 1.安装graphviz 在安装CodeViz之前, ...

  9. C++学习路线

    已经确定做C++后台的工作了,因此,要对C++要越来越熟悉才行,今天,在此列出学习和温习C++书籍的顺序,从而由浅入深地学习C++. 1. <C++ primer> 2. <Acce ...

  10. c# 使用递归 循环遍历导航树结构 并解析

    1.数据书库结构 1 家用电器 0 一级菜单 2 手机.数码.京东通信 0 一级菜单 3 电脑.办公 0 一级菜单 4 家具.家居.厨房 0 一级菜单 5 男装.女装.童装.内衣 0 一级菜单 6 个 ...