新知榜官方账号
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小白入门。跟着博主的脚步,每天进步一点点哟!
相关工具
相关文章
推荐
中国首款3A游戏上线,《黑神话:悟空》出圈!
2024-08-21 13:46
盘点15款AI配音工具,短视频配音有救了!
2024-08-12 17:11
短视频文案没创意?10大AI写作工具来帮你!
2024-08-05 16:23
Midjourney发布V6.1版本,我已分不清AI和现实了!
2024-08-01 15:03
我发现了一款国产AI绘画神器,免费易上手!
2024-07-25 16:40
7位AI考生做今年高考题,能过一本线吗?
2024-07-19 17:17
世界上第一所AI学校来了,80亿人只需要1位老师?
2024-07-18 17:12
Sora首部AI广告片上线,广告从业者危险了!
2024-06-27 13:44
OpenAI与中国说拜拜,国产AI如何接棒?
2024-06-26 15:18
人与AI会产生爱情吗,专家发话了!
2024-06-17 17:28