nlp分词之TextBlob

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')

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值