自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python文本預(yù)處理,試試BAT大佬總結(jié)的實(shí)用代碼!

開發(fā) 后端
本文將討論文本預(yù)處理的基本步驟,旨在將文本信息從人類語言轉(zhuǎn)換為機(jī)器可讀格式以便用于后續(xù)處理。此外,本文還將進(jìn)一步討論文本預(yù)處理過程所需要的工具。

[[350859]]

本文將討論文本預(yù)處理的基本步驟,旨在將文本信息從人類語言轉(zhuǎn)換為機(jī)器可讀格式以便用于后續(xù)處理。此外,本文還將進(jìn)一步討論文本預(yù)處理過程所需要的工具。

當(dāng)拿到一個(gè)文本后,首先從文本正則化(text normalization) 處理開始。常見的文本正則化步驟包括:

  •  將文本中出現(xiàn)的所有字母轉(zhuǎn)換為小寫或大寫
  •  將文本中的數(shù)字轉(zhuǎn)換為單詞或刪除這些數(shù)字
  •  刪除文本中出現(xiàn)的標(biāo)點(diǎn)符號、重音符號以及其他變音符號
  •  刪除文本中的空白區(qū)域
  •  擴(kuò)展文本中出現(xiàn)的縮寫
  •  刪除文本中出現(xiàn)的終止詞、稀疏詞和特定詞
  •  文本規(guī)范化(text canonicalization)

下面將詳細(xì)描述上述文本正則化步驟。

將文本中出現(xiàn)的字母轉(zhuǎn)化為小寫

示例1:將字母轉(zhuǎn)化為小寫

Python 實(shí)現(xiàn)代碼: 

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

輸出: 

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

刪除文本中出現(xiàn)的數(shù)字

如果文本中的數(shù)字與文本分析無關(guān)的話,那就刪除這些數(shù)字。通常,正則化表達(dá)式可以幫助你實(shí)現(xiàn)這一過程。

示例2:刪除數(shù)字

Python 實(shí)現(xiàn)代碼:      

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

輸出: 

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

刪除文本中出現(xiàn)的標(biāo)點(diǎn)

以下示例代碼演示如何刪除文本中的標(biāo)點(diǎn)符號,如 [!”#$%&’()*+,-./:;<=>?@[\]^_`{|}~] 等符號。

示例3:刪除標(biāo)點(diǎn)

Python 實(shí)現(xiàn)代碼: 

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

輸出: 

  1. This is an example of string with punctuation 

刪除文本中出現(xiàn)的空格

可以通過 strip()函數(shù)移除文本前后出現(xiàn)的空格。

示例4:刪除空格

Python 實(shí)現(xiàn)代碼: 

  1. input_str = “ \t a string example\t “  
  2. input_strinput_str = input_str.strip()  
  3. input_str 

輸出: 

  1. ‘a string example’ 

符號化(Tokenization)

符號化是將給定的文本拆分成每個(gè)帶標(biāo)記的小模塊的過程,其中單詞、數(shù)字、標(biāo)點(diǎn)及其他符號等都可視為是一種標(biāo)記。在下表中(Tokenization sheet),羅列出用于實(shí)現(xiàn)符號化過程的一些常用工具。

刪除文本中出現(xiàn)的終止詞

終止詞(Stop words) 指的是“a”,“a”,“on”,“is”,“all”等語言中最常見的詞。這些詞語沒什么特別或重要意義,通??梢詮奈谋局袆h除。一般使用 Natural Language Toolkit(NLTK) 來刪除這些終止詞,這是一套專門用于符號和自然語言處理統(tǒng)計(jì)的開源庫。

示例7:刪除終止詞

實(shí)現(xiàn)代碼: 

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

輸出: 

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

此外,scikit-learn 也提供了一個(gè)用于處理終止詞的工具:    

  1. from sklearn.feature_extraction.stop_words import ENGLISH_STOP_WORDS 

同樣,spaCy 也有一個(gè)類似的處理工具: 

  1. from spacy.lang.en.stop_words import STOP_WORDS 

刪除文本中出現(xiàn)的稀疏詞和特定詞

在某些情況下,有必要?jiǎng)h除文本中出現(xiàn)的一些稀疏術(shù)語或特定詞。考慮到任何單詞都可以被認(rèn)為是一組終止詞,因此可以通過終止詞刪除工具來實(shí)現(xiàn)這一目標(biāo)。

詞干提?。⊿temming)

詞干提取是一個(gè)將詞語簡化為詞干、詞根或詞形的過程(如 books-book,looked-look)。當(dāng)前主流的兩種算法是 Porter stemming 算法(刪除單詞中刪除常見的形態(tài)和拐點(diǎn)結(jié)尾) 和 Lancaster stemming 算法。

示例 8:使用 NLYK 實(shí)現(xiàn)詞干提取

實(shí)現(xiàn)代碼: 

  1. from nltk.stem import PorterStemmer  
  2. from nltk.tokenize import word_tokenize  
  3. stemmerPorterStemmer()  
  4. input_str=”There are several types of stemming algorithms.”  
  5. input_str=word_tokenize(input_str)  
  6. for word in input_str:  
  7.     print(stemmer.stem(word)) 

輸出: 

  1. There are sever type of stem algorithm. 

詞形還原(Lemmatization)

詞形還原的目的,如詞干過程,是將單詞的不同形式還原到一個(gè)常見的基礎(chǔ)形式。與詞干提取過程相反,詞形還原并不是簡單地對單詞進(jìn)行切斷或變形,而是通過使用詞匯知識(shí)庫來獲得正確的單詞形式。

當(dāng)前常用的詞形還原工具庫包括: NLTK(WordNet Lemmatizer),spaCy,TextBlob,Pattern,gensim,Stanford CoreNLP,基于內(nèi)存的淺層解析器(MBSP),Apache OpenNLP,Apache Lucene,文本工程通用架構(gòu)(GATE),Illinois Lemmatizer 和 DKPro Core。

示例 9:使用 NLYK 實(shí)現(xiàn)詞形還原

實(shí)現(xiàn)代碼:    

  1. from nltk.stem import WordNetLemmatizer  
  2. from nltk.tokenize import word_tokenize  
  3. lemmatizer=WordNetLemmatizer()  
  4. input_str=”been had done languages cities mice”  
  5. input_str=word_tokenize(input_str)  
  6. for word in input_str:  
  7.     print(lemmatizer.lemmatize(word)) 

輸出: 

  1. be have do language city mouse 

詞性標(biāo)注(POS)

詞性標(biāo)注旨在基于詞語的定義和上下文意義,為給定文本中的每個(gè)單詞(如名詞、動(dòng)詞、形容詞和其他單詞) 分配詞性。當(dāng)前有許多包含 POS 標(biāo)記器的工具,包括 NLTK,spaCy,TextBlob,Pattern,Stanford CoreNLP,基于內(nèi)存的淺層分析器(MBSP),Apache OpenNLP,Apache Lucene,文本工程通用架構(gòu)(GATE),F(xiàn)reeLing,Illinois Part of Speech Tagger 和 DKPro Core。

示例 10:使用 TextBlob 實(shí)現(xiàn)詞性標(biāo)注

實(shí)現(xiàn)代碼: 

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

輸出: 

  1. [(‘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’)] 

詞語分塊(淺解析)

詞語分塊是一種識(shí)別句子中的組成部分(如名詞、動(dòng)詞、形容詞等),并將它們鏈接到具有不連續(xù)語法意義的高階單元(如名詞組或短語、動(dòng)詞組等) 的自然語言過程。常用的詞語分塊工具包括:NLTK,TreeTagger chunker,Apache OpenNLP,文本工程通用架構(gòu)(GATE),F(xiàn)reeLing。

示例 11:使用 NLYK 實(shí)現(xiàn)詞語分塊

第一步需要確定每個(gè)單詞的詞性。

實(shí)現(xiàn)代碼: 

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

輸出: 

  1. [(‘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’)] 

二部就是進(jìn)行詞語分塊

實(shí)現(xiàn)代碼: 

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

輸出: 

  1. (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)  
  2. of/IN John/NNP) 

也可以通過 result.draw() 函數(shù)繪制句子樹結(jié)構(gòu)圖,如下圖所示。     

命名實(shí)體識(shí)別(Named Entity Recognition)

命名實(shí)體識(shí)別(NER) 旨在從文本中找到命名實(shí)體,并將它們劃分到事先預(yù)定義的類別(人員、地點(diǎn)、組織、時(shí)間等)。

常見的命名實(shí)體識(shí)別工具如下表所示,包括:NLTK,spaCy,文本工程通用架構(gòu)(GATE) -- ANNIE,Apache OpenNLP,Stanford CoreNLP,DKPro核心,MITIE,Watson NLP,TextRazor,F(xiàn)reeLing 等。

示例 12:使用 TextBlob 實(shí)現(xiàn)詞性標(biāo)注

實(shí)現(xiàn)代碼: 

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

輸出: 

  1. (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 ./.) 

共指解析 Coreference resolution(回指分辨率 anaphora resolution)

代詞和其他引用表達(dá)應(yīng)該與正確的個(gè)體聯(lián)系起來。Coreference resolution 在文本中指的是引用真實(shí)世界中的同一個(gè)實(shí)體。如在句子 “安德魯說他會(huì)買車”中,代詞“他”指的是同一個(gè)人,即“安德魯”。常用的 Coreference resolution 工具如下表所示,包括 Stanford CoreNLP,spaCy,Open Calais,Apache OpenNLP 等。

搭配提?。–ollocation extraction)

搭配提取過程并不是單獨(dú)、偶然發(fā)生的,它是與單詞組合一同發(fā)生的過程。該過程的示例包括“打破規(guī)則 break the rules”,“空閑時(shí)間 free time”,“得出結(jié)論 draw a conclusion”,“記住 keep in mind”,“準(zhǔn)備好 get ready”等。

示例 13:使用 ICE 實(shí)現(xiàn)搭配提取

實(shí)現(xiàn)代碼: 

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

輸出: 

  1. [“on the line”] 

關(guān)系提?。≧elationship extraction)

關(guān)系提取過程是指從非結(jié)構(gòu)化的數(shù)據(jù)源 (如原始文本)獲取結(jié)構(gòu)化的文本信息。嚴(yán)格來說,它確定了命名實(shí)體(如人、組織、地點(diǎn)的實(shí)體) 之間的關(guān)系(如配偶、就業(yè)等關(guān)系)。例如,從“昨天與 Mark 和 Emily 結(jié)婚”這句話中,我們可以提取到的信息是 Mark 是 Emily 的丈夫。  

總結(jié)

本文討論文本預(yù)處理及其主要步驟,包括正則化、符號化、詞干化、詞形還原、詞語分塊、詞性標(biāo)注、命名實(shí)體識(shí)別、共指解析、搭配提取和關(guān)系提取。還通過一些表格羅列出常見的文本預(yù)處理工具及所對應(yīng)的示例。在完成這些預(yù)處理工作后,得到的結(jié)果可以用于更復(fù)雜的 NLP 任務(wù),如機(jī)器翻譯、自然語言生成等任務(wù)。 

 

責(zé)任編輯:龐桂玉 來源: 機(jī)器學(xué)習(xí)算法與Python學(xué)習(xí)
相關(guān)推薦

2020-12-23 11:08:10

Python代碼文本

2021-03-28 08:57:57

Python 文本數(shù)據(jù)

2024-12-20 13:00:00

Python文本清洗預(yù)處理

2021-11-03 09:00:00

深度學(xué)習(xí)自然語言機(jī)器學(xué)習(xí)

2016-12-15 13:47:29

C語言預(yù)處理命令

2011-06-21 10:00:21

預(yù)處理指令

2020-07-08 13:46:25

Python數(shù)據(jù)分析預(yù)處理

2017-08-24 09:35:06

深度學(xué)習(xí)向量化Hash Trick

2024-01-03 16:01:23

2024-05-15 15:27:39

2020-08-31 08:25:06

Python時(shí)間模塊開發(fā)

2022-04-24 10:12:25

Python軟件包代碼

2016-12-20 16:07:13

Python數(shù)據(jù)預(yù)處理

2019-01-28 17:42:33

Python數(shù)據(jù)預(yù)處理數(shù)據(jù)標(biāo)準(zhǔn)化

2016-12-18 15:03:57

Python Scikit Lea數(shù)據(jù)

2021-08-23 17:49:02

代碼開發(fā)模型

2021-08-24 23:23:35

Python工具開發(fā)

2010-01-19 18:52:08

VB.NET處理數(shù)據(jù)行

2022-09-26 12:17:14

clamp() 函數(shù)CSS

2021-07-17 22:41:53

Python數(shù)據(jù)技術(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號