独角兽企业重金招聘Python工程师标准>>>
.输入一个段落,分成句子(Punkt句子分割器)
importnltkimportnltk.datadefsplitSentence(paragraph):tokenizer=nltk.data.load('tokenizers/punkt/english.pickle')sentences=tokenizer.tokenize(paragraph)returnsentencesif__name__=='__main__':printsplitSentence("MynameisTom.Iamaboy.Ilikesoccer!")
结果为['My name is Tom.', 'I am a boy.', 'I like soccer!']
2.输入一个句子,分成词组
fromnltk.tokenizeimportWordPunctTokenizerdefwordtokenizer(sentence):#分段words=WordPunctTokenizer().tokenize(sentence)returnwordsif__name__=='__main__':printwordtokenizer("MynameisTom.")
结果为['My', 'name', 'is', 'Tom', '.']
如果觉得《利用NLTK进行分句分词》对你有帮助,请点赞、收藏,并留下你的观点哦!