> 文档中心 > 【自然语言处理】-nltk库学习笔记(一)

【自然语言处理】-nltk库学习笔记(一)


句子切分(Sentence Tokenize)

nltk的分词是句子级别的,所以对于一篇文档首先要将文章按句子进行分割,然后句子进行分词

from nltk.tokenize import sent_tokenizetext = """Hello Mr. Smith, how are you doing today? The weather is great, and city is awesome.The sky is pinkish-blue. You shouldn't eat cardboard"""tokenized_text = sent_tokenize(text)print(tokenized_text)
['Hello Mr. Smith, how are you doing today?', 'The weather is great, and \ncity is awesome.The sky is pinkish-blue.', "You shouldn't eat cardboard"]

单词切分(Word Tokenize)

import nltksent = "Study hard and improve every day."token = nltk.word_tokenize(sent)print(token)
['Study', 'hard', 'and', 'improve', 'every', 'day', '.']

移除标点符号

对每个切词调用该函数,移除字符串中的标点符号,string.punctuation包含了所有的标点符号,从切词中把这些标点符号替换为空格。

python的string模块下的 punctuation 包含所有的英文标点符号,所以用replace()一下就可以去除

注意:

string.punctuation中的标点符号只有英文

#所有的标点字符import stringstring.punctuation'!"#$%&\'()*+,-./:;?@[\\]^_`{|}~'

去除中文标点符号
如果是中文文本,可以调用zhon包的zhon.hanzi.punctuation函数即可得到中文的标点符号集合

英文的

import strings = 'a,sbch.:usx/'S = s.translate(str.maketrans(string.punctuation, " " * len(string.punctuation)))print(S)
a sbch  usx 
import stringstri = 'today is friday, so happy..!!!'punctuation_string = string.punctuationprint("所有的英文标点符号:", punctuation_string)for i in punctuation_string:    stri = stri.replace(i, '')print(stri)
所有的英文标点符号: !"#$%&'()*+,-./:;?@[\]^_`{|}~today is friday so happy

如果是中文的捏

from zhon.hanzi import punctuationstr = '今天周五,下班了,好开心呀!!'punctuation_str = punctuationprint("中文标点符合:", punctuation_str)for i in punctuation:    str = str.replace(i, '')print(str)
中文标点符合: "#$%&'()*+,-/:;<=>@[\]^_`{|}~⦅⦆「」、 、〃〈〉《》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏﹑﹔·!?。。今天周五下班了好开心呀

string模块

在对字符串操作时,如果感觉自己写的很复杂时,可以试试string模块,里面有很多实用的属性。

>>>import string>>>string.punctuation#所有的标点字符'!"#$%&\'()*+,-./:;?@[\\]^_`{|}~'>>>string.whitespace#所有的空白字符' \t\n\r\x0b\x0c'>>>string.ascii_uppercase#所有的大写字母'ABCDEFGHIJKLMNOPQRSTUVWXYZ'>>>string.ascii_lowercase#所有的小写字母'abcdefghijklmnopqrstuvwxyz'>>>string.hexdigits#所有的十六进制字符'0123456789abcdefABCDEF'

zhon库

学习笔记——zhon库的简介、安装、使用方法之详细攻略

import reimport zhon.hanzirst = re.findall(zhon.hanzi.sentence, '我买了一辆车。妈妈做的菜,很好吃!')print(rst)
['我买了一辆车。', '妈妈做的菜,很好吃!']

移除停用词

将数据转换为计算机可以理解的内容的过程称为预处理。预处理的主要形式之一是过滤掉无用的数据。在自然语言处理中,无用的单词(数据)称为停用词。
停用词是指搜索引擎已编程忽略的常用词(例如“the”,“a”,“an”,“in”)。
我们不希望这些单词占用我们数据库中的空间,或占用宝贵的处理时间。为此,我们可以通过存储要停止使用的单词的列表轻松删除它们。

关于安装NLTK语料库

这个是详细介绍

import nltknltk.download()

【自然语言处理】-nltk库学习笔记(一)

关于NLTK_DATA下载的坑

经过不懈的努力终于搞好了(这些问题可能别人也会遇到)
OSError: No such file or directory: 'C:\Users\2019\AppData\Roaming\nltk_data\corpora\stopword
解决方案

使用split()函数进行分词

import nltkfrom nltk.corpus import stopwordsstop_words = stopwords.words("english")text = """Hello Mr. Smith, how are you doing today? The weather is great, and city is awesome. """word_tokens = nltk.tokenize.word_tokenize(text.strip())filtered_word = [w for w in word_tokens if not w in stop_words]print(filtered_word)
['Hello', 'Mr.', 'Smith', ',', 'today', '?', 'The', 'weather', 'great', ',', 'city', 'awesome', '.']