点击蓝字 |关注我们
介绍
NLTK是一个字符串处理库,它将字符串作为输入。输出采用字符串或字符串列表的形式。该库提供了大量算法,这些算法主要有助于实际研究。可以在不同的输出变式之间进行比较,选择最适合的方法。还有其他库,例如spaCy,CoreNLP,PyNLPI,Polyglot但是NLTK和spaCy使用最广泛,在此我使用了非常适合学习的NLTK库进行学习介绍。
从网站上抓取的数据大部分采用原始文本形式。在分析数据或为其拟合模型之前,需要清除此数据。清理文本数据对于突出显示您希望机器学习系统使用的属性是必要的。清理(或预处理)数据通常包括多个步骤。现在我将介绍实际使用中较为常见且几乎必须做的几种。
删除额外空格
大多数时候,您拥有的文本数据在单词之间、句子之后或之前可能包含额外的空格。因此,首先我们将使用正则表达式从每个句子中删除这些额外的空格。
代码和结果实例如下:
import redoc = "NLP is an interesting field. "new_doc = re.sub(" +"," ", doc)print(new_doc)
案例规范化(大小写处理)
由于 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)
分词标记
由于原来的语料为一个个长的句子,在实际分析时可能需要将其进行分割处理,并且有时候需要做一些标记,在这里介绍三种方法
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)
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)
3.regexp_tokenize:当我们想要分离我们感兴趣的词语时,可以使用它,例如从推文中提取所有#号标签、推文地址或文本中的超链接。
import rea = 'What are your views related to US elections @nitin'print(re.split('@', a))
删除非索引字(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)
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)
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)
总结
这些是在实际中应用较多的的数据清洁技术,灵活使用这些可以使我们的文本数据更利于用于分析和模型构建。但是实际中不必执行所有这些步骤进行清洁,可以根据实际的数据和需求挑选一部分。
本文作者:
指导老师:
长按关注我们~
欢迎关注微信公众号”沈浩老师“