NLP初步——TextBlob

2022年5月21日13:42:23 发表评论 556 views

安装 pip install textblob

(1)处理文本数据的python库、功能:词性标注、名词短语抽取、情感分析、分类、翻译。

<pre>#-*- coding:utf-8 -*-

#  词性标注, 名词短语抽取,情感分析

import textblob
text ="Join thousands of learners from around the world who are improving their English listening skills with our online courses. " \
       "Join thousands of learners from around the world who are improving their English listening skills with our online courses."   # 必须后面句号后面有空格才能分句
blob = textblob.TextBlob(text)
sentences = blob.sentences
print("分句1",sentences)

# 从分句到分词
words_list  = []
for sentence in sentences:
       words_list.append(sentence.words)
       print(sentence.words)

# 统计单词/短语 词频
counts = blob.word_counts['the']
print('词频统计the的次数',counts)
noun_count = blob.noun_phrases.count('join',case_sensitive=False)
print('名词world出现的次数',noun_count)  #  改成非名词后出现为0 online,the0,world0,their0 courses0 有点问题

# 词性标注:
tags= blob.tags
print("tags of words", tags)   # 'NNP' is noun

# 情感分析:
text2 = "NLP is amazing and simple to study"  # 0.3     0.62
# text2 = "NLP is easy and simple to study"   # 0.21    0.59
blob2 = textblob.TextBlob(text2)
result = blob2.sentiment
print(result)

# 机器翻译
en_text = text
en_blob = textblob.TextBlob(en_text)
zh_text = en_blob.translate(from_lang='en',to='zh-CN')   # 这个要连接网站
print(zh_text)</pre>

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: