word清理代码_使用NLTK进行文本数据清理

点击蓝字 |关注我们

e064eb6a151a3b0eaa883506adbb01b9.png

介绍

NLTK是一个字符串处理库,它将字符串作为输入。输出采用字符串或字符串列表的形式。该库提供了大量算法,这些算法主要有助于实际研究。可以在不同的输出变式之间进行比较,选择最适合的方法。还有其他库,例如spaCy,CoreNLP,PyNLPI,Polyglot但是NLTK和spaCy使用最广泛,在此我使用了非常适合学习的NLTK库进行学习介绍。

从网站上抓取的数据大部分采用原始文本形式。在分析数据或为其拟合模型之前,需要清除此数据。清理文本数据对于突出显示您希望机器学习系统使用的属性是必要的。清理(或预处理)数据通常包括多个步骤。现在我将介绍实际使用中较为常见且几乎必须做的几种。

删除额外空格

大多数时候,您拥有的文本数据在单词之间、句子之后或之前可能包含额外的空格。因此,首先我们将使用正则表达式从每个句子中删除这些额外的空格。

代码和结果实例如下:

import redoc = "NLP  is an interesting     field.  "new_doc = re.sub(" +"," ", doc)print(new_doc)

4eb7bc11df05b283d8eb06742322c0e4.png

案例规范化(大小写处理)

由于 python 是一种大小写敏感语言,因此它将以不同的方式对待 NLP 和 nlp。可以使用以下方法轻松地将字符串转换为大写或者小写,代码与结果实例如下

import stringtext = "Hello! How are you!! I'm very excited that you're going for a trip to Europe!! Yayy!"text_clean = "".join([i.lower() for i in text if i not in string.punctuation])print(text_clean)

9f98db18597d4c12f65235c309bd2907.png

分词标记

由于原来的语料为一个个长的句子,在实际分析时可能需要将其进行分割处理,并且有时候需要做一些标记,在这里介绍三种方法

1.word_tokenize:它是分隔单词和标点符号的通用标记器。撇号在这里不被视为标点符号。

text = "Hello! How are you!! I'm very excited that you're going for a trip to Europe!! Yayy!"nltk.tokenize.word_tokenize(text)

b6f619226770c96906479d257e89a071.png

2.TweetTokenizer:这是在处理来自社交媒体的文本数据时特别使用的,这些内容包括 #,@,表情。

在这里观察突出显示的部分,并对单词标记

text = "Hello! How are you!! I'm very excited that you're going for a trip to Europe!! Yayy!"from nltk.tokenize import TweetTokenizertweet = TweetTokenizer()tweet.tokenize(text)

9cb877cb742ae353e5b68bb8c1ca176b.png

3.regexp_tokenize:当我们想要分离我们感兴趣的词语时,可以使用它,例如从推文中提取所有#号标签、推文地址或文本中的超链接。

import rea = 'What are your views related to US elections @nitin'print(re.split('@', a))

0bc542906c2bce4473afd8f2f6d69ff1.png

删除非索引字(stopwords)

由于原语料中可能包含一些非语词,如:我,他,她,但是,是,现在,有,等等,这并没有增加数据的意义。因此,必须删除这些单词,这有助于减少我们数据中的功能。在标记文本后,这些内容将被删除。

import nltkimport stringstopwords = nltk.corpus.stopwords.words('english')text = "Hello! How are you!! I'm very excited that you're going for a trip to Europe!! Yayy!"text_new = "".join([i for i in text if i not in string.punctuation])print(text_new)words = nltk.tokenize.word_tokenize(text_new)print(words)words_new = [i for i in words if i not in stopwords]print(words_new)

f8fbf5fa93de501a5aefbd142b06ea22.png

Lemmatization(归一化) 与 Stemming(词干化)

1.stemming(词干化):一种将词变成词根形式的技术。它只是从单词中删除后缀。词干不一定是词典的一部分,即不一定表示含义。

import nltkimport stringstopwords = nltk.corpus.stopwords.words('english')text = "Hello! How are you!! I'm very excited that you're going for a trip to Europe!! Yayy!"text_new = "".join([i for i in text if i not in string.punctuation])# print(text_new)words = nltk.tokenize.word_tokenize(text_new)# print(words)words_new = [i for i in words if i not in stopwords]print(words_new)ps = nltk.PorterStemmer()w = [ps.stem(word) for word in words_new]print(w)

438d15fca4c5bc2945e638976ffcae1b.png

2. Lemmatization(归一化):将这个词作为名为Lemma的根形。它有助于把单词带到他们的字典形式。默认情况下,它应用于名词。它更准确,因为它使用更明智的分析,基于上下文创建具有类似含义的单词组,因此它很复杂,需要更多时间。这用于我们需要保留上下文信息的地方。

import nltkimport stringstopwords = nltk.corpus.stopwords.words('english')text = "Hello! How are you!! I'm very excited that you're going for a trip to Europe!! Yayy!"text_new = "".join([i for i in text if i not in string.punctuation])# print(text_new)words = nltk.tokenize.word_tokenize(text_new)# print(words)words_new = [i for i in words if i not in stopwords]print(words_new)wn = nltk.WordNetLemmatizer()w = [wn.lemmatize(word) for word in words_new]print(w)

3c23f4d08ece37676cfc7b5b5d87ba53.png

总结

这些是在实际中应用较多的的数据清洁技术,灵活使用这些可以使我们的文本数据更利于用于分析和模型构建。但是实际中不必执行所有这些步骤进行清洁,可以根据实际的数据和需求挑选一部分。

本文作者:

4b2bfcfd742c0881e850d9ff8d8ea5c9.png

52b44a6add387728490915c0da7f96c3.png

指导老师:

6a7d8000777a03748434071ae9509c4b.png

长按关注我们~

17298d04e23685fd76cde1941c64a6c3.png

欢迎关注微信公众号”沈浩老师“

b71ebc759d190cea9a92e185365df627.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值