NLP 文本预处理 Python 代码

在获得文本之后

将所有字母转换为小写或大写

目录

在获得文本之后

将所有字母转换为小写或大写

将数字转换为单词或删除数字

删除标点、重音符号和其他音调符号

删除空格

扩展缩写词

删除停止词、稀疏词和特定词

文本规范化---单词减为词干、词根或词干的过程

词性标记

分块是一种自然的语言过程,用于识别组成部分把句子(名词、动词、形容词等)联系起来具有离散语法意义的顺序单位(名词组或短语、动词组等)。

命名实体识别

词搭配提取

 


Python 代码:

input_str = ”The 5 biggest countries by population in
2017 are China, India, United States, Indonesia, and
Brazil.”
input_str = input_str.lower()
print(input_str)

输出:

the 5 biggest countries by population in 2017 are china, india, united states, indonesia, and brazil.

将数字转换为单词或删除数字

2. 删除数字

删除与分析无关的数字。通常,正则表达式用于删除数字。

Python 代码:

import re
input_str = ’Box A contains 3 red and 5 white balls,
while Box B contains 4 red and 2 blue balls.’
result = re.sub(r’\d+’, ‘’, input_str)
print(result)

输出:

Box A contains red and white balls, while Box B contains red and blue balls.

删除标点、重音符号和其他音调符号

3. 删除标点符号

以下代码删除了这组符号:[!”#$%&’()*+,-./:;<=>?@[\]^_`{|}~]:

Python 代码:

import string
input_str = “This &is [an] example? {of} string.
with.? punctuation!!!!” # Sample string
result = input_str.translate(string.maketrans(“”,””),
string.punctuation)
print(result)

输出:

This is an example of string with punctuation

删除空格

要删除前导空格和结束空格,可以使用 strip() 函数。

Python 代码:

input_str = “ \t a string example\t “
input_str = input_str.strip()
input_str

输出:

‘a string example’

扩展缩写词

删除停止词、稀疏词和特定词

停止词”是语言中最常见的词,如“the”、“a”,“开”、“是”、“全部”。这些词没有重要意义,而且通常从文本中删除。可以使用自然语言工具包(NLTK),一套用于符号和统计自然语言处理。

Python 代码:

input_str = “NLTK is a leading platform for building
Python programs to work with human language data.”
stop_words = set(stopwords.words(‘english’))
from nltk.tokenize import word_tokenize
tokens = word_tokenize(input_str)
result = [i for i in tokens if not i in stop_words]
print (result)

输出:

[‘NLTK’, ‘leading’, ‘platform’, ‘building’, ‘Python’, ‘programs’, ‘work’, ‘human’, ‘language’, ‘data’, ‘.’]

文本规范化---单词减为词干、词根或词干的过程

Stemming 是将单词减为词干、词根或词干的过程。根表单(例如,books-book、looked-look)。主要两个算法是 Porter stemming algorithm(从单词中删除常见的词尾)和 Lancaster stemming algorithm(一种更具攻击性的词干算法)。

Python 代码:

from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize
stemmer= PorterStemmer()
input_str=”There are several types of stemming
algorithms.”
input_str=word_tokenize(input_str)
for word in input_str:
print(stemmer.stem(word))

输出:

There are sever type of stem algorithm.

7. Lemmatization

Lemmatization 与 Stemming 功能相似,但作用相反。它不是简单地切掉词干。相反,它使用词汇知识获取正确单词基本形式的基础。

Python 代码:

from nltk.stem import WordNetLemmatizer
from nltk.tokenize import word_tokenize
lemmatizer=WordNetLemmatizer()
input_str=”been had done languages cities mice”
input_str=word_tokenize(input_str)
for word in input_str:
print(lemmatizer.lemmatize(word))

输出:

be have do language city mouse

词性标记

一部分旨在将词性部分分配给基于它的给定文本(如名词、动词、形容词和其他)定义及其上下文。

Python 代码:

input_str=”Parts of speech examples: an article, to
write, interesting, easily, and, of”
from textblob import TextBlob
result = TextBlob(input_str)
print(result.tags)

输出:

[(‘Parts’, u’NNS’), (‘of’, u’IN’), (‘speech’, u’NN’), (‘examples’, u’NNS’), (‘an’, u’DT’), (‘article’, u’NN’), (‘to’, u’TO’), (‘write’, u’VB’), (‘interesting’, u’VBG’), (‘easily’, u’RB’), (‘and’, u’CC’), (‘of’, u’IN’)]

分块----名词组或短语、动词组等

是一种自然的语言过程,用于识别组成部分把句子(名词、动词、形容词等)联系起来具有离散语法意义的顺序单位(名词组或短语、动词组等)。

Python 代码:

input_str=”A black television and a white stove were
bought for the new apartment of John.”
from textblob import TextBlob
result = TextBlob(input_str)
print(result.tags)

输出:

[(‘A’, u’DT’), (‘black’, u’JJ’), (‘television’, u’NN’), (‘and’, u’CC’), (‘a’, u’DT’), (‘white’, u’JJ’), (‘stove’, u’NN’), (‘were’, u’VBD’), (‘bought’, u’VBN’), (‘for’, u’IN’), (‘the’, u’DT’), (‘new’, u’JJ’), (‘apartment’, u’NN’), (‘of’, u’IN’), (‘John’, u’NNP’)]

Python 代码:

reg_exp = “NP: {<DT>?<JJ>*<NN>}”
rp = nltk.RegexpParser(reg_exp)
result = rp.parse(result.tags)
print(result)

输出:

(S (NP A/DT black/JJ television/NN) and/CC (NP a/DT white/JJ stove/NN) were/VBD bought/VBN for/IN (NP the/DT new/JJ apartment/NN) of/IN John/NNP)

命名实体识别

命名实体识别(NER)旨在在文本中查找命名实体并将其分为预先定义的类别(人员姓名,地点、组织、时间等)。

Python 代码:

from nltk import word_tokenize, pos_tag, ne_chunk
input_str = “Bill works for Apple so he went to Boston
for a conference.”
print ne_chunk(pos_tag(word_tokenize(input_str)))

输出:

(S (PERSON Bill/NNP) works/VBZ for/IN Apple/NNP so/IN he/PRP went/VBD to/TO (GPE Boston/NNP) for/IN a/DT conference/NN ./.)

词搭配提取

 

搭配是经常出现在一起的单词组合。例如 “break the rules,” “free time,” “draw a conclusion,” “keep in mind,” “get ready,” 等。

Python 代码:

input=[“he and Chazz duel with all keys on the line.”]
from ICE import CollocationExtractor
extractor =
CollocationExtractor.with_collocation_pipeline(“T1” ,
bing_key = “Temp”,pos_check = False)
print(extractor.get_collocations_of_length(input,
length = 3))

输出:

[“on the line”]

猜你喜欢

转载自blog.csdn.net/weixin_40245436/article/details/87873896