jieba nltk 进行中英文分词

Jieba、NLTK等中英文分词工具进行分词
建议:中文分词使用 jieba(SnowNlp、THULAC、NLPIR、StanfordCoreNLP)进行分词,英文使用 NLTK进行分词;还有git上的一个英文文本分词(无空格)模块wordninja
1.中文分词
1.jieba分词
import jieba import re
Chinese=‘央视315晚会曝光湖北省知名的神丹牌、莲田牌“土鸡蛋”实为普通鸡蛋冒充,同时在商标上玩猫腻,分别注册“鲜土”、注册“好土”商标,让消费者误以为是“土鸡蛋”。3月15日晚间,新京报记者就此事致电湖北神丹健康食品有限公司方面,其工作人员表示不知情,需要了解清楚情况,截至发稿暂未取得最新回应。新京报记者还查询发现,湖北神丹健康食品有限公司为农业产业化国家重点龙头企业、高新技术企业,此前曾因涉嫌虚假宣传“中国最大的蛋品企业”而被罚6万元。’
str=re.sub(’[^\w]’,’’,chinese) #使用正则去符号,之后都是用这个str字符串
seg_list=jieba.cut(s_list, cut_all=False) #精确模式 print(’/’.join(seg_list))

2.nltk分词
import nltk import re
english=‘H:\\自然语言处理\\Experiment2\\English.txt’ with open(english,‘r’,encoding=‘utf-8’) as file:
u=file.read() str=re.sub(’[^\w ]’,’’,u) print(nltk.word_tokenize(str)) print(nltk.pos_tag(nltk.word_tokenize(str)))
#对分完词的结果进行词性标注

3.模块wordninja 分词
下面简单以实例看一下它的功能: ‘’’ https://github.com/yishuihanhan/wordninja ‘’’
import wordninja
print(wordninja.split(‘derekanderson’) print(wordninja.split(‘imateapot’) print(wordninja.split(‘wethepeopleoftheunitedstatesinordertoformamoreperfectunionestablishjusticeinsuredomestictranquilityprovideforthecommondefencepromotethegeneralwelfareandsecuretheblessingsoflibertytoourselvesandourposteritydoordainandestablishthisconstitutionfortheunitedstatesofamerica’) print(wordninja.split(‘littlelittlestar’)
结果如下: [‘derek’, ‘anderson’] [‘im’, ‘a’, ‘teapot’] [‘we’, ‘the’, ‘people’, ‘of’, ‘the’, ‘united’, ‘states’, ‘in’, ‘order’, ‘to’, ‘form’, ‘a’, ‘more’, ‘perfect’, ‘union’, ‘establish’, ‘justice’, ‘in’, ‘sure’, ‘domestic’, ‘tranquility’, ‘provide’, ‘for’, ‘the’, ‘common’, ‘defence’, ‘promote’, ‘the’, ‘general’, ‘welfare’, ‘and’, ‘secure’, ‘the’, ‘blessings’, ‘of’, ‘liberty’, ‘to’, ‘ourselves’, ‘and’, ‘our’, ‘posterity’, ‘do’, ‘ordain’, ‘and’, ‘establish’, ‘this’, ‘constitution’, ‘for’, ‘the’, ‘united’, ‘states’, ‘of’, ‘america’] [‘little’, ‘little’, ‘star’]

发布了114 篇原创文章 · 获赞 18 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/WangYouJin321/article/details/103969016