自然语言处理库——TextBlob
? ? ? ? TextBlob(https://textblob.readthedocs.io/en/dev/index.html)是一個用于處理文本數據的Python庫。它提供一個簡單的API,可用于深入研究常見的NLP任務,如詞性標注、名詞短語提取、情感分析、文本翻譯、分類等。
官方文檔:https://textblob.readthedocs.io/en/dev/
目錄
1. 情感分析
2.詞性標注
3.?分詞和分句
4.?名詞短語列表
5. 詞形還原及詞干提取
(1)單復數
(2)Word 類
(3)WordNet:獲取近義詞
6. 拼寫矯正
(1)直接矯正
(2)Word 拼寫檢查
7. 單詞詞頻
(1)單詞詞頻
(2)短語頻次
8.?翻譯及語言檢測語言
1. 情感分析
? ? ? ? 情感指的是隱藏在句子中的觀點,極性(polarity)定義句子中的消極性或積極性,主觀性(subjectivity)暗示句子的表達的含糊的、還是肯定的。
? ? ? ? 返回一個元組?Sentiment(polarity,?subjectivity).?
? ? ? ?polarity: [-1.0, 1.0].? ? ?-1.0 消極,1.0積極
? ? ? subjectivity: [0.0, 1.0]? ? ? 0.0 表示客觀,1.0表示主觀.
from textblob import TextBlobtext = "Textblob is amazingly simple to use. What great fun!" blob = TextBlob(text) # 創建一個textblob對象 from textblob import TextBlobresult = blob.sentiment # Sentiment(polarity=0.39166666666666666, subjectivity=0.4357142857142857)polarity = blob.sentiment.polarity # 0.391666666666666662.詞性標注
wiki = TextBlob("Python is a high-level, general-purpose programming language.") tag = wiki.tags# [('Python', 'NNP'), ('is', 'VBZ'), ('a', 'DT'), ('high-level', 'JJ'), ('general-purpose', 'JJ'), ('programming', 'NN'), ('language', 'NN')]3.?分詞和分句
blob = TextBlob("Beautiful is better than ugly. ""Explicit is better than implicit. ""Simple is better than complex.")word = blob.words sentence = blob.sentences''' ['Beautiful', 'is', 'better', 'than', 'ugly', 'Explicit', 'is', 'better', 'than', 'implicit', 'Simple', 'is', 'better', 'than', 'complex'][Sentence("Beautiful is better than ugly."), Sentence("Explicit is better than implicit."), Sentence("Simple is better than complex.")] '''4.?名詞短語列表
list = wiki.noun_phrases# ['python']5. 詞形還原及詞干提取
(1)單復數
? ? ? ?singularize() 變單數, pluralize()變復數,用在對名詞進行處理,且會考慮特殊名詞單復數形式
sentence = TextBlob('Use 4 spaces per indentation level.') word = sentence.wordsdanshu = word[2].singularize() # space fushu = word[-1].pluralize() # levels(2)Word 類
? ? ?lemmatize() 方法? 對單詞進行詞形還原,名詞找單數,動詞找原型。所以需要一次處理名詞,一次處理動詞。
from textblob import Wordw1 = Word('apples') result1 = w1.lemmatize() # 默認只處理名詞 applew2 = Word('went') result2 = w2.lemmatize("v") # 對動詞原型處理 go(3)WordNet:獲取近義詞
# 1.獲取近義詞 from textblob import Word from textblob.wordnet import VERB result1 = Word("hack").synsets result2 = Word("hack").get_synsets(pos=VERB) #get_synsets(): 只查找 該詞作為 動詞 的集合,參數為空時和synsets方法相同''' result1:[Synset('hack.n.01'), Synset('machine_politician.n.01'), Synset('hack.n.03'), Synset('hack.n.04'), Synset('cab.n.03'), Synset('hack.n.06'), Synset('hack.n.07'), Synset('hack.n.08'), Synset('chop.v.05'), Synset('hack.v.02'), Synset('hack.v.03'), Synset('hack.v.04'), Synset('hack.v.05'), Synset('hack.v.06'), Synset('hack.v.07'), Synset('hack.v.08')]result2:[Synset('chop.v.05'), Synset('hack.v.02'), Synset('hack.v.03'), Synset('hack.v.04'), Synset('hack.v.05'), Synset('hack.v.06'), Synset('hack.v.07'), Synset('hack.v.08')] '''2. 獲取近義詞的定義 defi = result1[1].definition() # 獲取定義#defi結果: a politician who belongs to a small clique that controls a political party for private rather than public ends3. 獲取單詞本身的定義 defi = Word("octopus").definitions# ['tentacles of octopus prepared as food', 'bottom-living cephalopod having a soft oval body with eight long tentacles']6. 拼寫矯正
(1)直接矯正
b = TextBlob("I havv goood speling!") b_corr = b.correct() print(b_corr) # I have good spelling!(2)Word 拼寫檢查
? ? ? word.spellcheck()方法,返回帶有拼寫建議的(word,confidence)元組列表
from textblob import Word w = Word('falibility') w_ = w.spellcheck() print(w_) # [('fallibility', 1.0)]7. 單詞詞頻
(1)單詞詞頻
monty = TextBlob("We are no longer the Knights who say Ni. ""We are now the Knights who say Ekki ekki ekki PTANG.")#(1)方式1 counts = monty.word_counts['ekki'] # 不區分大小寫 print(counts) # 3 #(2)方式2 counts2 = monty.words.count('ekki') print(counts2) # 3#(3)方式3 counts3 = monty.words.count('ekki', case_sensitive=True) # 設置大小寫敏感,默認不區分 print(counts3) # 2(2)短語頻次
counts4 = wiki.noun_phrases.count('python') # 短語頻次 print(counts4) # 18.?翻譯及語言檢測語言
en_blob = TextBlob('Simple is better than complex.') lang = en_blob.translate(to='es') # from_lang默認 en print(lang) # TextBlob("Simple es mejor que complejo.")chinese_blob = TextBlob("美麗優于丑陋") lang = chinese_blob.translate(from_lang="zh-CN", to='en') print(lang) # TextBlob("Beautiful is better than ugly")?
總結
以上是生活随笔為你收集整理的自然语言处理库——TextBlob的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自然语言处理库——NLTK
- 下一篇: word2vec原理(一): 词向量、C