05 奇妙的Python库之【textblob(文本处理)】

2021-09-08 10:27:05 浏览数 (1)

简介

TextBlob 是一款 Pythonic 的文本处理工具,用于处理文本数据,它提供了一个简单的 API,用于潜入常见的自然语言处理(NLP)任务,如词性标注、名词短语提取、情感分析、分类等

TextBlob是一个用Python编写的开源的文本处理库。它可以用来执行很多自然语言处理的任务,比如,词性标注,名词性成分提取,情感分析,文本翻译,等等,仅为英文分析。

中文则可以使用SnowNLP,能够方便的处理中文文本内容,是受到了TextBlob的启发而写的。

功能

  • 名词短语提取
  • 词性标记
  • 情绪分析
  • 分类(朴素贝叶斯,决策树)
  • 由Google翻译提供的语言翻译和检测标记化(将文本分为单词和句子)
  • 单词和短语的频率
  • 单词变形(复数和单数)和词形化
  • 拼写校正
  • 通过扩展添加新的模型或语言
  • WordNet整合

实战

  • 安装
代码语言:javascript复制
pip install textblob

利用textblob的TextBlob方法实现分句

代码语言:javascript复制
import textblob
text1 = "No matter how many characters are available for your password you should be sure to use every one of them. " 
        "The more characters available for your password and the more you use makes it that much harder to figure out the combination. " 
        "Always make use of all characters available for a strong and secure password."
#1.利用textblob的TextBlob生成一个模型
blob1 = textblob.TextBlob(text1)

#sentences方法进行分句
sentences1 = blob1.sentences
print("1.分句是:",sentences1)

  • 运行结果
代码语言:javascript复制
1.分句是: [Sentence("No matter how many characters are available for your password you should be sure to use every one of them."), Sentence("The more characters available for your password and the more you use makes it that much harder to figure out the combination."), Sentence("Always make use of all characters available for a strong and secure password.")]

情感分析

(1)积极(polarity) / 消极 值越大,越积极(-1,1)

(2)主观(subjectivity)/客观 值越大,越主观(0,1)

注:生成的是俩个数值

  • 积极的
代码语言:javascript复制
import textblob

text = "JacksonYee is very handsome "
blob = textblob.TextBlob(text)
result_sentiment = blob.sentiment
print(result_sentiment)

代码语言:javascript复制
Sentiment(polarity=0.65, subjectivity=1.0)

  • 消极的
代码语言:javascript复制
import textblob

text = "mike is very ugly "
blob = textblob.TextBlob(text)
result_sentiment = blob.sentiment
print(result_sentiment)


代码语言:javascript复制
Sentiment(polarity=-0.9099999999999999, subjectivity=1.0)

总结

textblob有着很多功能,欢迎大家前去探索!!!

参考文档:https://textblob.readthedocs.io/en/dev/

0 人点赞