【自然语言处理篇】--以NLTK为基础讲解自然语?处理的原理

一、前述

Python上著名的?然语?处理库?带语料库,词性分类库?带分类,分词,等等功能强?的社区?持,还有N多的简单版wrapper。

二、文本预处理

1、安装nltk

pip install -U nltk

安装语料库 (一堆对话,一对模型)

import nltk
nltk.download()

2、功能一览表:

3、文本处理流程

4、Tokenize 把长句?拆成有“意义”的?部件

import jieba
seg_list = jieba.cut("我来到北北京清华?大学", cut_all=True)
print "Full Mode:", "/ ".join(seg_list) # 全模式
seg_list = jieba.cut("我来到北北京清华?大学", cut_all=False)
print "Default Mode:", "/ ".join(seg_list) # 精确模式
seg_list = jieba.cut("他来到了了?网易易杭研?大厦") # 默认是精确模式
print ", ".join(seg_list)
seg_list = jieba.cut_for_search("?小明硕?士毕业于中国科学院计算所,后在?日本京都?大学深造")
# 搜索引擎模式
print ", ".join(seg_list)

结果:

【全模式】: 我/ 来到/ 北北京/ 清华/ 清华?大学/ 华?大/ ?大学
【精确模式】: 我/ 来到/ 北北京/ 清华?大学
【新词识别】:他, 来到, 了了, ?网易易, 杭研, ?大厦
(此处,“杭研”并没有在词典中,但是也被Viterbi算法识别出来了了)
【搜索引擎模式】: ?小明, 硕?士, 毕业, 于, 中国, 科学, 学院, 科学院, 中国科学院, 计算,
计算所, 后, 在, ?日本, 京都, ?大学, ?日本京都?大学, 深造

社交?络语?的tokenize:

import re
emoticons_str = r"""
(?:
[:=;] # 眼睛
[oO\-]? # ?鼻?子
[D\)\]\(\]/\\OpP] # 嘴
)"""
regex_str = [
emoticons_str,
r‘<[^>]+>‘, # HTML tags
r‘(?:@[\w_]+)‘, # @某?人
r"(?:\#+[\w_]+[\w\‘_\-]*[\w_]+)", # 话题标签
r‘http[s]?://(?:[a-z]|[0-9]|[[email protected]&amp;+]|[!*\(\),]|(?:%[0-9a-f][0-9a-f]))+‘,
# URLs
r‘(?:(?:\d+,?)+(?:\.?\d+)?)‘, # 数字
r"(?:[a-z][a-z‘\-_]+[a-z])", # 含有 - 和 ‘ 的单词
r‘(?:[\w_]+)‘, # 其他
r‘(?:\S)‘ # 其他
]

正则表达式对照表
http://www.regexlab.com/zh/regref.htm

这样能处理社交语言中的表情等符号:

tokens_re = re.compile(r‘(‘+‘|‘.join(regex_str)+‘)‘, re.VERBOSE | re.IGNORECASE)
emoticon_re = re.compile(r‘^‘+emoticons_str+‘$‘, re.VERBOSE | re.IGNORECASE)
def tokenize(s):
return tokens_re.findall(s)
def preprocess(s, lowercase=False):
tokens = tokenize(s)
if lowercase:
tokens = [token if emoticon_re.search(token) else token.lower() for token in
tokens]
return tokens
tweet = ‘RT @angelababy: love you baby! :D http://ah.love #168cm‘
print(preprocess(tweet))
# [‘RT‘, ‘@angelababy‘, ‘:‘, ‘love‘, ‘you‘, ‘baby‘,
# ’!‘, ‘:D‘, ‘http://ah.love‘, ‘#168cm‘]

5、词形归?化

Stemming 词?提取:?般来说,就是把不影响词性的inflection的?尾巴砍掉
walking 砍ing = walk
walked 砍ed = walk
Lemmatization 词形归?:把各种类型的词的变形,都归为?个形式
went 归? = go
are 归? = be

>>> from nltk.stem.porter import PorterStemmer
>>> porter_stemmer = PorterStemmer()
>>> porter_stemmer.stem(‘maximum’)
u’maximum’
>>> porter_stemmer.stem(‘presumably’)
u’presum’
>>> porter_stemmer.stem(‘multiply’)
u’multipli’
>>> porter_stemmer.stem(‘provision’)
u’provis’
>>> from nltk.stem import SnowballStemmer
>>> snowball_stemmer = SnowballStemmer(“english”)
>>> snowball_stemmer.stem(‘maximum’)
u’maximum’
>>> snowball_stemmer.stem(‘presumably’)
u’presum’
>>> from nltk.stem.lancaster import LancasterStemmer
>>> lancaster_stemmer = LancasterStemmer()
>>> lancaster_stemmer.stem(‘maximum’)
‘maxim’
>>> lancaster_stemmer.stem(‘presumably’)
‘presum’
>>> lancaster_stemmer.stem(‘presumably’)
‘presum’
>>> from nltk.stem.porter import PorterStemmer
>>> p = PorterStemmer()
>>> p.stem(‘went‘)
‘went‘
>>> p.stem(‘wenting‘)
‘went‘

6、词性Part-Of-Speech

>>> import nltk
>>> text = nltk.word_tokenize(‘what does the fox say‘)
>>> text
[‘what‘, ‘does‘, ‘the‘, ‘fox‘, ‘say‘]
>>> nltk.pos_tag(text)
[(‘what‘, ‘WDT‘), (‘does‘, ‘VBZ‘), (‘the‘, ‘DT‘), (‘fox‘, ‘NNS‘), (‘say‘, ‘VBP‘)]

7、Stopwords

?先记得在console??下载?下词库
或者 nltk.download(‘stopwords’)
from nltk.corpus import stopwords
# 先token?一把,得到?一个word_list
# ...
# 然后filter?一把
filtered_words =
[word for word in word_list if word not in stopwords.words(‘english‘)]

8、?条?本预处理流?线

三、自然语言处理应用。

实际上预处理就是将文本转换为Word_List,自然语言处理再转变成计算机能识别的语言。

自然语言处理有以下几个应用:情感分析,?本相似度, ?本分类

原文地址:https://www.cnblogs.com/LHWorldBlog/p/9279051.html

时间: 2024-08-25 05:00:18

【自然语言处理篇】--以NLTK为基础讲解自然语?处理的原理的相关文章

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

自然语言处理(1)之NLTK与PYTHON 题记: 由于现在的项目是搜索引擎,所以不由的对自然语言处理产生了好奇,再加上一直以来都想学Python,只是没有机会与时间.碰巧这几天在亚马逊上找书时发现了这本<Python自然语言处理>,瞬间觉得这对我同时入门自然语言处理与Python有很大的帮助.所以最近都会学习这本书,也写下这些笔记. 1. NLTK简述 NLTK模块及功能介绍 语言处理任务 NLTK模块 功能描述 获取语料库 nltk.corpus 语料库和词典的标准化接口 字符串处理 nl

这篇 博客将讲解如何删除目录以及删除文

这篇 博客将讲解如何删除目录以及删除文件. 删除目录:rmdir (remove directory) 可以使用rmdir 命令删除一个目录.必须离开目录,并且目录必须为空目录,不然提示删除失败. 例:在目录B下,有一个C文件夹,我们mkdir D 创建一个新的文件夹D,在使用rmdir D 删除文件夹D,显示结果如下: 在下例中,我们想要删除文件夹C,命令行上输入rmdir C ,结果如下,会发现出现,rmdir: failed to remove `C': Directory not emp

人工智能之 NLP 自然语言处理篇(1)

(1) NLP 介绍 NLP 是什么? NLP (Natural Language Processing) 自然语言处理,是计算机科学.人工智能和语言学的交叉学科,目的是让计算机处理或"理解"自然语言.自然语言通常是指一种自然地随文化演化的语言,如汉语.英语.日语. NLP 可以用来做什么?以及它的应用领域是什么? 文本朗读(Text to speech)/ 语音合成(Speech synthesis) 语音识别(Speech recognition) 中文自动分词(Chinese w

转载一篇比较详细的讲解html,css的一篇文章,很长

转载自这里,转载请注明出处. DIV+CSS系统学习笔记回顾 第一部分 HTML 第一章 职业规划和前景 职业方向规划定位: web前端开发工程师 web网站架构师 自己创业 转岗管理或其他 web前端开发的前景展望: 未来IT行业企业需求最多的人才 结合最新的html5抢占移动端的市场 自己创业做老板 随着互联网的普及web开发成为企业的宠儿和核心 web职业发展目标: 第一.梳理知识架构 负责内容的HTML 负责外观的css(层叠样式表) 负责行为的js ps切图 第二.分解目标(起步阶段.

自然语言13_Stop words with NLTK

https://www.pythonprogramming.net/stop-words-nltk-tutorial/?completed=/tokenizing-words-sentences-nltk-tutorial/ Stop words with NLTK The idea of Natural Language Processing is to do some form of analysis, or processing, where the machine can underst

自然语言19.1_Lemmatizing with NLTK

https://www.pythonprogramming.net/lemmatizing-nltk-tutorial/?completed=/named-entity-recognition-nltk-tutorial/ Lemmatizing with NLTK A very similar operation to stemming is called lemmatizing. The major difference between these is, as you saw earlie

自然语言23_Text Classification with NLTK

https://www.pythonprogramming.net/text-classification-nltk-tutorial/?completed=/wordnet-nltk-tutorial/ Text Classification with NLTK Now that we're comfortable with NLTK, let's try to tackle text classification. The goal with text classification can

自然语言14_Stemming words with NLTK

https://www.pythonprogramming.net/stemming-nltk-tutorial/?completed=/stop-words-nltk-tutorial/ Stemming words with NLTK The idea of stemming is a sort of normalizing method. Many variations of words carry the same meaning, other than when tense is in

自然语言20_The corpora with NLTK

https://www.pythonprogramming.net/nltk-corpus-corpora-tutorial/?completed=/lemmatizing-nltk-tutorial/ The corpora with NLTK In this part of the tutorial, I want us to take a moment to peak into the corpora we all downloaded! The NLTK corpus is a mass