Python文本預(yù)處理:步驟、使用工具及示例
本文將討論文本預(yù)處理的基本步驟,旨在將文本信息從人類語言轉(zhuǎn)換為機器可讀格式以便用于后續(xù)處理。此外,本文還將進一步討論文本預(yù)處理過程所需要的工具。當拿到一個文本后,首先從文本正則化(text normalization)?處理開始。常見的文本正則化步驟包括:
將文本中出現(xiàn)的所有字母轉(zhuǎn)換為小寫或大寫
將文本中的數(shù)字轉(zhuǎn)換為單詞或刪除這些數(shù)字
刪除文本中出現(xiàn)的標點符號、重音符號以及其他變音符號
刪除文本中的空白區(qū)域
擴展文本中出現(xiàn)的縮寫
刪除文本中出現(xiàn)的終止詞、稀疏詞和特定詞
文本規(guī)范化(text canonicalization)
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.import reinput_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.import stringinput_str = “This &is [an] example? {of} string. with.? punctuation!!!!” # Sample stringresult = input_str.translate(string.maketrans(“”,””), string.punctuation)print(result)
This is an example of string with punctuationinput_str = “ \t a string example\t “input_str = input_str.strip()input_str
‘a(chǎn) string example’
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_tokenizetokens = 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’, ‘.’]from?sklearn.feature_extraction.stop_words?import?ENGLISH_STOP_WORDSfrom?spacy.lang.en.stop_words?import?STOP_WORDS
from nltk.stem import PorterStemmerfrom nltk.tokenize import word_tokenizestemmer= 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.from nltk.stem import WordNetLemmatizerfrom nltk.tokenize import word_tokenizelemmatizer=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?mouseinput_str=”Parts of speech examples: an article, to write, interesting, easily, and, of”from textblob import TextBlobresult = TextBlob(input_str)print(result.tags)
[(‘Parts’,?u’NNS’),?(‘of’,?u’IN’),?(‘speech’,?u’NN’),?(‘examples’,?u’NNS’),?(‘a(chǎn)n’,?u’DT’),?(‘a(chǎn)rticle’,?u’NN’),?(‘to’,?u’TO’),?(‘write’,?u’VB’),?(‘interesting’,?u’VBG’),?(‘easily’,?u’RB’),?(‘a(chǎn)nd’,?u’CC’),?(‘of’,?u’IN’)]input_str=”A black television and a white stove were bought for the new apartment of John.”from textblob import TextBlobresult = TextBlob(input_str)print(result.tags)
[(‘A’,?u’DT’),?(‘black’,?u’JJ’),?(‘television’,?u’NN’),?(‘a(chǎn)nd’,?u’CC’),?(‘a(chǎn)’,?u’DT’),?(‘white’,?u’JJ’),?(‘stove’,?u’NN’),?(‘were’,?u’VBD’),?(‘bought’,?u’VBN’),?(‘for’,?u’IN’),?(‘the’,?u’DT’),?(‘new’,?u’JJ’),?(‘a(chǎn)partment’,?u’NN’),?(‘of’,?u’IN’),?(‘John’,?u’NNP’)]reg_exp = “NP: {? * }” 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)


from nltk import word_tokenize, pos_tag, ne_chunkinput_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?./.)

input=[“he and Chazz duel with all keys on the line.”]from ICE import CollocationExtractorextractor = CollocationExtractor.with_collocation_pipeline(“T1” , bing_key = “Temp”,pos_check = False)print(extractor.get_collocations_of_length(input,?length?=?3))
[“on the line”]
https://medium.com/@datamonsters/text-preprocessing-in-python-steps-tools-and-examples-bf025f872908
今天給大家推薦,七月在線【機器學習集訓營 第十二期】課程。
1
專業(yè)的教學模式
【機器學習集訓營?第十二期】,采取十二位一體的教學模式,包括12個環(huán)節(jié):“入學測評、直播答疑、布置作業(yè)、階段考試、畢業(yè)考核、一對一批改、線上線下結(jié)合、CPU&GPU雙云平臺、組織比賽、聯(lián)合認證、面試輔導、就業(yè)推薦”。
2
完善的實戰(zhàn)項目
只學理論肯定是不行的,學機器學習的核心是要做項目,本期集訓營共13大實戰(zhàn)項目.




3
專家級講師團隊
本期集訓營擁有超豪華講師團隊,學員將在這些頂級講師的手把手指導下完成本期課程的學習,挑戰(zhàn)40萬年薪。


授課老師、助教老師,多對一服務(wù)。從課上到課下,從專業(yè)輔導到日常督學、360度無死角為學員安心學習鋪平道路。陪伴式解答學員疑惑,為學員保駕護航。
4
六大課程特色


5
完善的就業(yè)服務(wù)
學員在完成所有的階段學習后,將會有一對一的就業(yè)服務(wù),包括簡歷優(yōu)化、面試求職輔導及企業(yè)內(nèi)推三大部分。
為了確保學員能拿到滿意的offer,七月在線還專門成立就業(yè)部,會專門為集訓營學員提供就業(yè)服務(wù),保證每一位學員都能拿到滿意的offer。
掃碼查看課程詳情,同時大家也可以去看看之前學員的面試經(jīng)驗分享。
戳↓↓“閱讀原文”查看課程詳情!
