当前位置: 首页 > 工具软件 > TextBlob > 使用案例 >

nlp分词之TextBlob

丌官瀚
2023-12-01

TextBlob

TextBlob是用于处理文本数据的Python(2和3)库。它提供了一个一致的API,可用于深入研究普通自然语言处理(NLP)任务,例如词性标记,名词短语提取,情感分析等。
主要用于英文的分词,不适用于中文

安装TextBlob
可以在PyCharm开发工具中Python Console窗口用pip install textblob

词性标注

from textblob import TextBlob
wiki = TextBlob("Python is a high-level, general-purpose programming language.")
print(wiki.tags)

输出:
[('Python', 'NNP'), ('is', 'VBZ'), ('a', 'DT'), ('high-level', 'JJ'), 
('general-purpose', 'JJ'), ('programming', 'NN'), ('language', 'NN')]

名词短语提取

from textblob import TextBlob
wiki = TextBlob("Python is a high-level, general-purpose programming language.")
print(wiki.tags)

输出:
['python']

情感分析
polarity:情感积极消极在[-1,1]之间,越接近-1越消极,越接近1越积极
subjectivity:主观客观在[0,1]之间,越接近1越主观

from textblob import TextBlob
testimonial = TextBlob("Textblob is amazingly simple to use. What great fun!")
print(testimonial.sentiment)

输出:
Sentiment(polarity=0.39166666666666666, subjectivity=0.4357142857142857)

符号化
分词,分句

text = "Beautiful is better than ugly. "\
    " Explicit is better than implicit. "\
    "Simple is better than complex. "\
# 利用textblob实现分句
blob = TextBlob(text)
sentences = blob.sentences
print("1分句:",sentences)

words_list = [] #声明一个list存储所有的分词结果
for sentence in sentences:
    words_list.append(sentence.words)
    print(sentence.words)
print("2 分词:",words_list)

1分句: [Sentence("Beautiful is better than ugly."), Sentence("Explicit is better than implicit."), Sentence("Simple is better than complex.")]
['Beautiful', 'is', 'better', 'than', 'ugly']
['Explicit', 'is', 'better', 'than', 'implicit']
['Simple', 'is', 'better', 'than', 'complex']
2 分词: [WordList(['Beautiful', 'is', 'better', 'than', 'ugly']), WordList(['Explicit', 'is', 'better', 'than', 'implicit']), WordList(['Simple', 'is', 'better', 'than', 'complex'])]

拼写校正

b=TextBlob("I havv goood speling!")
print(b.correct())

输出:
I have good spelling!

获取单词和名词短语频率
两种方法获取单词中名词或名词短语的出现频率
1、通过word_counts字典
2、通过count()

monty = TextBlob("We are no longer the Knights who say Ni. We are now the Knights who say Ekki ekki ekki PTANG.")
print("ekki:",monty.word_counts['ekki'])
print("ekki:",monty.words.count('ekki'))

#是否区分大小写
print("ekki_sensitive:",monty.words.count('ekki',case_sensitive=True))

输出:
ekki: 3
ekki: 3
ekki_sensitive: 2

机器翻译
这个需要翻墙,演示不了了

en_text = "I think KFC is not good. "
en_blob = textblob.TextBlob(en_text)
zh_text = en_blob.translate(from_lang='en',to='zh-CN')

 类似资料: