本站内容均来自兴趣收集,如不慎侵害的您的相关权益,请留言告知,我们将尽快删除.谢谢.
TextBlob简介
TextBlob是一个用Python编写的开源的文本处理库。是自然语言工具包(NLTK)库的一个包装器,目的是抽象其复杂性。它可以用来执行很多自然语言处理的任务,比如,词性标注,名词性成分提取,情感分析,文本翻译,等等。
主要特性:
名词短语提取
词性标记
情绪分析
分类
由 Google 翻译提供的翻译和检测
标记(将文本分割成单词和句子)
词句、短语频率
解析
n-gram
词变化(复数和单数化)和词形化
拼写校正
通过扩展添加新模型或语言
WordNet 集成
TextBlob的安装
直接执行
pip
install
textblob
即可进行安装。但是执行如下代码可能会报错:
from textblob import TextBlob text = 'I love natural language processing! I am not like fish!' blob = TextBlob(text) D:\CodeHub\NLP\venv\Scripts\python.exe D:/CodeHub/NLP/test_new.py Traceback (most recent call last): File "D:\CodeHub\NLP\venv\lib\site-packages\textblob\decorators.py", line 35, in decorated return func(*args, **kwargs) File "D:\CodeHub\NLP\venv\lib\site-packages\textblob\tokenizers.py", line 57, in tokenize return nltk.tokenize.sent_tokenize(text) File "D:\CodeHub\NLP\venv\lib\site-packages\nltk\tokenize\__init__.py", line 105, in sent_tokenize tokenizer = load('tokenizers/punkt/{0}.pickle'.format(language)) File "D:\CodeHub\NLP\venv\lib\site-packages\nltk\data.py", line 868, in load opened_resource = _open(resource_url) File "D:\CodeHub\NLP\venv\lib\site-packages\nltk\data.py", line 993, in _open return find(path_, path + ['']).open() File "D:\CodeHub\NLP\venv\lib\site-packages\nltk\data.py", line 701, in find raise LookupError(resource_not_found) LookupError: ********************************************************************** Resource punkt not found. Please use the NLTK Downloader to obtain the resource: >>> import nltk >>> nltk.download('punkt') For more information see: https://www.nltk.org/data.html Attempted to load tokenizers/punkt/english.pickle Searched in: - 'C:\\Users\\qw.TCENT/nltk_data' - 'D:\\CodeHub\\NLP\\venv\\nltk_data' - 'D:\\CodeHub\\NLP\\venv\\share\\nltk_data' - 'D:\\CodeHub\\NLP\\venv\\lib\\nltk_data' - 'C:\\Users\\qw.TCENT\\AppData\\Roaming\\nltk_data' - 'C:\\nltk_data' - 'D:\\nltk_data' - 'E:\\nltk_data' - '' **********************************************************************
解决方案是,安装好nltk,并下载好nltk_data。
TextBlob
的使用
from textblob import TextBlob from textblob import Word text = 'I love natural language processing! I am not like fish!' blob = TextBlob(text) print(blob.words) # 分词 print(blob.tags) # 词性标注 print(blob.noun_phrases) # 短语抽取 # 分句+计算句子情感值 # 使用TextBlob情感分析的结果,以元组的方式进行返回,形式如(polarity, subjectivity). # polarity 是一个范围为 [-1.0 , 1.0 ] 的浮点数, 正数表示积极,负数表示消极。 # subjectivity 是一个 范围为 [0.0 , 1.0 ] 的浮点数,其中 0.0 表示 客观,1.0表示主观的。 for sentence in blob.sentences: print(sentence + '------>' + str(sentence.sentiment.polarity)) # 词语变形(Words Inflection) w = Word("apple") print(w.pluralize()) # 变复数 print(w.pluralize().singularize()) # 变单数 # 词干化(Words Lemmatization) w = Word('went') print(w.lemmatize('v')) w = Word('octopi') print(w.lemmatize()) # 拼写纠正(Spelling Correction) sen = 'I lvoe naturl language processing!' sen = TextBlob(sen) print(sen.correct()) # 句法分析(Parsing) text = TextBlob('I lvoe naturl language processing!') print(text.parse()) # N-Grams text = TextBlob('I lvoe naturl language processing!') print(text.ngrams(n=2))
参考链接:
Github地址: https://github.com/sloria/TextBlob
Be First to Comment