安装:pip install textblob
如果下载速度太慢,可以配置国内源安装:pip install textblob -i https://pypi.tuna.tsinghua.edu.cn/simple
In [2]:
!pip install textblob
Requirement already satisfied: textblob in /opt/conda/lib/python3.6/site-packages
Requirement already satisfied: nltk>=3.1 in /opt/conda/lib/python3.6/site-packages (from textblob)
Requirement already satisfied: six in /opt/conda/lib/python3.6/site-packages (from nltk>=3.1->textblob)
You are using pip version 9.0.1, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
import nltk
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
nltk.download('brown')
nltk.download('wordnet')
下载不了可以通过百度网盘下载:
链接:https://pan.baidu.com/s/1n4AX-_GAI-SVBo9hhCTeVA
提取码:9utb
下载完后将文件夹放到本地路径:C:\Users\wangchaojie\AppData\Roaming\nltk_data
其中wangchaojie为我们自己计算机名称
from textblob import TextBlob
text = 'I love natural language processing! I am not like fish!'
blob = TextBlob(text)
print(blob.tags)
Out[13]:
[('I', 'PRP'),
('love', 'VBP'),
('natural', 'JJ'),
('language', 'NN'),
('processing', 'NN'),
('I', 'PRP'),
('am', 'VBP'),
('not', 'RB'),
('like', 'IN'),
('fish', 'NN')]
np = blob.noun_phrases
for w in np:
print(w)
natural language processing
In [15]:
for sentence in blob.sentences:
print(sentence + '------>' + str(sentence.sentiment.polarity))
I love natural language processing!------>0.3125
I am not like fish!------>0.0
token = blob.words
for w in token:
print(w)
I
love
natural
language
processing
I
am
not
like
fish
sentence = blob.sentences
for s in sentence:
print(s)
I love natural language processing!
I am not like fish!
In [18]:
token = blob.words
for w in token:
# 变复数
print(w.pluralize())
# 变单数
print(w.singularize())
we
I
love
love
naturals
natural
languages
language
processings
processing
we
I
ams
am
nots
not
likes
like
fish
fish
from textblob import Word
w = Word('went')
print(w.lemmatize('v'))
w = Word('octopi')
print(w.lemmatize())
go
octopus
In [22]:
from textblob.wordnet import VERB
word = Word('octopus')
syn_word = word.synsets
for syn in syn_word:
print(syn)
Synset('octopus.n.01')
Synset('octopus.n.02')
指定返回的同义词集为动词
In [23]:
syn_word1 = Word("hack").get_synsets(pos=VERB)
for syn in syn_word1:
print(syn)
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')
查看synset(同义词集)的具体定义
In [24]:
Word("beautiful").definitions
Out[24]:
['delighting the senses or exciting intellectual or emotional admiration',
'(of weather) highly enjoyable']
In [25]:
sen = 'I lvoe naturl language processing!'
sen = TextBlob(sen)
print(sen.correct())
I love nature language processing!
Word.spellcheck()返回拼写建议以及置信度
In [26]:
w1 = Word('good')
w2 = Word('god')
w3 = Word('gd')
print(w1.spellcheck())
print(w2.spellcheck())
print(w3.spellcheck())
[('good', 1.0)]
[('god', 1.0)]
[('go', 0.586139896373057), ('god', 0.23510362694300518), ('d', 0.11658031088082901), ('g', 0.03626943005181347), ('ed', 0.009067357512953367), ('rd', 0.006476683937823834), ('nd', 0.0038860103626943004), ('gr', 0.0025906735751295338), ('sd', 0.0006476683937823834), ('md', 0.0006476683937823834), ('id', 0.0006476683937823834), ('gdp', 0.0006476683937823834), ('ga', 0.0006476683937823834), ('ad', 0.0006476683937823834)]
In [27]:
text = TextBlob('I lvoe naturl language processing!')
print(text.parse())
I/PRP/B-NP/O lvoe/NN/I-NP/O naturl/NN/I-NP/O language/NN/I-NP/O processing/NN/I-NP/O !/./O/O
In [28]:
text = TextBlob('I lvoe naturl language processing!')
print(text.ngrams(n=2))
[WordList(['I', 'lvoe']), WordList(['lvoe', 'naturl']), WordList(['naturl', 'language']), WordList(['language', 'processing'])]
另外,代码已经上传github:https://github.com/yuquanle/StudyForNLP/blob/master/NLPtools/TextBlobDemo.ipynb