热门搜素: 电商 教育 视频 健康
新知榜公众号 随时随地查看资讯 手机访问更加便捷
首页 > 行业观察 > TextBlob的使用方法介绍
TextBlob的使用方法介绍
新知榜官方账号
2023-12-04 08:21:27
本文介绍了TextBlob的使用方法,这是一个用Python编写的开源的文本处理库。它可以用来执行很多自然语言处理的任务,比如,词性标注,名词性成分提取,情感分析,文本翻译,等等。
TextBlob是一个用Python编写的开源的文本处理库。它可以用来执行很多自然语言处理的任务,比如,词性标注,名词性成分提取,情感分析,文本翻译,等等。
Github地址:https://github.com/sloria/TextBlob
官方文档:https://textblob.readthedocs.io/en/dev/
安装:pip install textblob
配置国内源安装:pip install textblob -i https://pypi.tuna.tsinghua.edu.cn/simple
blob.tags[('I','PRP'),('love','VBP'),('natural','JJ'),('language','NN'),('processing','NN'),('I','PRP'),('am','VBP'),('not','RB'),('like','IN'),('fish','NN')]
np=blob.noun_phrasesforwinnp:print(w)naturallanguageprocessing
forsentenceinblob.sentences:print(sentence+'------>'+str(sentence.sentiment.polarity))Ilovenaturallanguageprocessing!------>0.3125iamnotlikeyou!------>0.05.
token=blob.wordsforwintoken:print(w)IlovenaturallanguageprocessingIamnotlikefishsentence=blob.sentencesforsinsentence:print(s)Ilovenaturallanguageprocessing!Iamnotlikefish!
token=blob.wordsforwintoken:#变复数print(w.pluralize())#变单数print(w.singularize())weIlovelovenaturalsnaturallanguageslanguageprocessingsprocessingweIamsamnotsnotlikeslikefishfish
fromtextblobimportWordw=Word('went')print(w.lemmatize('v'))w=Word('octopi')print(w.lemmatize())gooctopus
fromtextblob.wordnetimportVERBword=Word('octopus')syn_word=word.synsetsforsyninsyn_word:print(syn)Synset('octopus.n.01')Synset('octopus.n.02')#指定返回的同义词集为动词syn_word1=Word("hack").get_synsets(pos=VERB)forsyninsyn_word1:print(syn)Synset('chop.v.05')Synset('hack.v.02')Synset('hack.v.03')Synset('hack.v.04')Synset('hack.v.05')Synset('hack.v.06')Synset('hack.v.07')Synset('hack.v.08')#查看synset(同义词集)的具体定义Word("beautiful").definitions['delightingthesensesorexcitingintellectualoremotionaladmiration','(ofweather)highlyenjoyable']
sen='Ilvoenaturllanguageprocessing!'sen=TextBlob(sen)print(sen.correct())Ilovenaturelanguageprocessing!#Word.spellcheck()返回拼写建议以及置信度w1=Word('good')w2=Word('god')w3=Word('gd')print(w1.spellcheck())print(w2.spellcheck())print(w3.spellcheck())[('good',1.0)][('god',1.0)][('go',0.586139896373057),('god',0.23510362694300518),('d',0.11658031088082901),('g',0.03626943005181347),('ed',0.009067357512953367),('rd',0.006476683937823834),('nd',0.0038860103626943004),('gr',0.0025906735751295338),('sd',0.0006476683937823834),('md',0.0006476683937823834),('id',0.0006476683937823834),('gdp',0.0006476683937823834),('ga',0.0006476683937823834),('ad',0.0006476683937823834)]
text=TextBlob('Ilvoenaturllanguageprocessing!')print(text.parse())I/PRP/B-NP/Olvoe/NN/I-NP/Onaturl/NN/I-NP/Olanguage/NN/I-NP/Oprocessing/NN/I-NP/O!/./O/O
text=TextBlob('Ilvoenaturllanguageprocessing!')print(text.ngrams(n=2))[WordList(['I','lvoe']),WordList(['lvoe','naturl']),WordList(['naturl','language']),WordList(['language','processing'])]
参考:https://textblob.readthedocs.io/en/dev/classifiers.html#classifiers
代码已上传:
欢迎关注同名微信公众号:AI小白入门。跟着博主的脚步,每天进步一点点哟!
相关工具
相关文章
相关快讯
短视频知识人物影响力榜
新知榜独家 {{faTime.effecttime}}发布
总榜
人物 领域 粉丝数 影响力指数
{{item.manIndex}}
{{item.nickname}} {{item.field}}
{{item.fs}}
{{item.effect}}