python ⾃然语⾔处理(NLP )⼊门
本⽂简要介绍Python(NLP),使⽤Python的NLTK库。NLTK是Python的⾃然语⾔处理⼯具包,在NLP领域中,最常使⽤的⼀个Python 库。
什么是NLP?
简单来说,⾃然语⾔处理(NLP)就是开发能够理解⼈类语⾔的应⽤程序或服务。
这⾥讨论⼀些⾃然语⾔处理(NLP)的实际应⽤例⼦,如语⾳识别、语⾳翻译、理解完整的句⼦、理解匹配词的同义词,以及⽣成语法正确完整句⼦和段落。
这并不是NLP能做的所有事情。
NLP实现
搜索引擎: ⽐如⾕歌,Yahoo等。⾕歌搜索引擎知道你是⼀个技术⼈员,所以它显⽰与技术相关的结果;
社交⽹站推送:⽐如Facebook News Feed。如果News Feed算法知道你的兴趣是⾃然语⾔处理,就会显⽰相关的⼴告和帖⼦。语⾳引擎:⽐如Apple的Siri。
垃圾邮件过滤:如⾕歌垃圾邮件过滤器。和普通垃圾邮件过滤不同,它通过了解邮件内容⾥⾯的的深层意义,来判断是不是垃圾邮件。NLP库
下⾯是⼀些开源的⾃然语⾔处理库(NLP):
Natural language toolkit (NLTK);
Apache OpenNLP;
Stanford NLP suite;
Gate NLP library
其中⾃然语⾔⼯具包(NLTK)是最受欢迎的⾃然语⾔处理库(NLP),它是⽤Python编写的,⽽且背后有⾮常强⼤的社区⽀持。NLTK也很容易上⼿,实际上,它是最简单的⾃然语⾔处理(NLP)库。
在这个NLP教程中,我们将使⽤Python NLTK库。
安装 NLTK
如果您使⽤的是Windows/Linux/Mac,您可以使⽤pip安装NLTK:
打开python终端导⼊NLTK检查NLTK是否正确安装:
如果⼀切顺利,这意味着您已经成功地安装了NLTK库。⾸次安装了NLTK,需要通过运⾏以下代码来安装NLTK扩展包:pip install  nltk
1import  nltk
1import  nltk nltk.download()
1
2
3
这将弹出NLTK 下载窗⼝来选择需要安装哪些包:
您可以安装所有的包,因为它们的⼤⼩都很⼩,所以没有什么问题。
使⽤Python Tokenize⽂本
⾸先,我们将抓取⼀个web页⾯内容,然后分析⽂本了解页⾯的内容。
我们将使⽤urllib模块来抓取web页⾯:
从打印结果中可以看到,结果包含许多需要清理的HTML标签。
然后BeautifulSoup模块来清洗这样的⽂字:quest response = quest.urlopen('php/')html = ad()print (html)
1
2
3
4
5from  bs4 import  BeautifulSoup import  quest response = quest.urlopen('php/')html = ad()soup = BeautifulSoup(html,"html5lib")
1
hbase详解
2
3
4
5
6
这需要安装html5lib 模块
现在我们从抓取的⽹页中得到了⼀个⼲净的⽂本。
下⼀步,将⽂本转换为tokens,像这样:
统计词频
text已经处理完毕了,现在使⽤Python NLTK统计token的频率分布。
可以通过调⽤NLTK中的FreqDist()⽅法实现:
如果搜索输出结果,可以发现最常见的token是PHP。
您可以调⽤plot函数做出频率分布图:text  = _text(strip =True)print  (text )
1
2from  bs4 import  BeautifulSoup import  quest response = quest.urlopen('php/')html = ad()soup = BeautifulSoup(html,"html5lib")text = _text(strip=True )tokens = text.split()print  (tokens)
1
2
3
4
5
6
7
8
9from  bs4 import  BeautifulSoup import  quest import  nltk response = quest.urlopen('php/')html = ad()soup = BeautifulSoup(html,"html5lib")text = _text(strip=True )tokens = text.split()freq = nltk.FreqDist(tokens)for  key,val in  freq.items():    print  (str(key) + ':' + str(val))
1
2
3
4
5
6
7
8
9
10
11
12
13freq.plot(20, cumulative=False )# 需要安装matplotlib 库
1
2
这上⾯这些单词。⽐如of,a,an等等,这些词都属于停⽤词。
⼀般来说,停⽤词应该删除,防⽌它们影响分析结果。
处理停⽤词
NLTK⾃带了许多种语⾔的停⽤词列表,如果你获取英⽂停⽤词:
现在,修改下代码,在绘图之前清除⼀些⽆效的token:
最终的代码应该是这样的: 下⾯代码应该是 if token not in sr:from  pus import  stopwords stopwords.words('english')
1
2
3clean_tokens = list()sr = stopwords.words ('english')for  token  in tokens:    if  token  not in sr:        clean_tokens.append(token )
1
2
3
4python入门教程非常详细word
struct结构体类型
5
ascii编码叙述正确
现在再做⼀次词频统计图,效果会⽐之前好些,因为剔除了停⽤词:
使⽤NLTK Tokenize⽂本
在之前我们⽤split⽅法将⽂本分割成tokens,现在我们使⽤NLTK来Tokenize⽂本。
⽂本没有Tokenize之前是⽆法处理的,所以对⽂本进⾏Tokenize⾮常重要的。token化过程意味着将⼤的部件分割为⼩部件。你可以将段落tokenize成句⼦,将句⼦tokenize成单个词,NLTK分别提供了句⼦tokenizer和单词tokenizer。
maven命令怎么下载源码假如有这样这段⽂本:from bs4 import BeautifulSoup quest import nltk pus import stopwords response = quest.urlopen('php/')html = ad()soup = BeautifulSoup(html,"html5lib")text  = _text(strip=True)tokens = text .split()clean_tokens = list()sr = stopwords.words ('english')for  token  in tokens:    if  not token  in sr:        clean_tokens.append(token )freq = nltk.FreqDist(clean_tokens)for  key,val in freq.items ():    print (str(key) + ':' + str(val))
1
2
3
4
5
6
7
8
9
10
11
12
13
linux显示隐藏文件夹14
15
16
17
18freq.plot(20,cumulative=False)
1