新知榜官方账号
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小白入门。跟着博主的脚步,每天进步一点点哟!
相关工具
相关文章
推荐
用Deepseek写AI绘图提示词,像呼吸一样简单!
2025-02-19 16:12
你以为AI绘画是黑科技?其实早成了“路边摊生意”!
2025-02-19 10:15
Flux爆火,全网最全面最详细的Flux使用教程!
2025-02-18 14:19
用AI如何创作音乐,实战教学来啦!
2025-02-17 17:23
MidJourney让你秒变绘画大神,从零开始画哪吒!
2025-02-17 14:56
AI应用新境界:让人工智能成为你的得力助手
2025-02-14 15:45
AI短片革命:当创作遇上智能,人人都能成为导演
2025-02-14 14:53
AI狂潮下的人类职场:是失业危机还是进化契机?
2025-02-13 16:53
开启影视创作新纪元,效率提升 10 倍的神器来了!
2025-02-13 15:11
深度解析DeepSeek:当AI技术照进创作产业的未来
2025-02-12 17:16