python 笔记:nltk (标记英文单词词性等)
生活随笔
收集整理的这篇文章主要介绍了
python 笔记:nltk (标记英文单词词性等)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
1 单词切分
import nltkcontent = 'She sells seashells on the seashore. The seashells she sells are seashells, she is sure.'tokens = nltk.word_tokenize(content) print(tokens) #['She', 'sells', 'seashells', 'on', 'the', 'seashore', '.', 'The', 'seashells', 'she', 'sells', 'are', 'seashells', ',', 'she', 'is', 'sure', '.']1.1 词性划分
pos_tags = nltk.pos_tag(tokens) print(pos_tags) ''' [('She', 'PRP'), ('sells', 'VBZ'), ('seashells', 'NNS'), ('on', 'IN'), ('the', 'DT'), ('seashore', 'NN'), ('.', '.'), ('The', 'DT'), ('seashells', 'NNS'), ('she', 'PRP'), ('sells', 'VBZ'), ('are', 'VBP'), ('seashells', 'NNS'), (',', ','), ('she', 'PRP'), ('is', 'VBZ'), ('sure', 'JJ'), ('.', '.')] '''1.1.1 词性表
2 词性还原
import nltk.stem as ns# 词型还原:复数名词->单数名词 ;分词->动词原型 lemmatizer = ns.WordNetLemmatizer()word = 'leaves' # 将名词还原为单数形式 #'n'表示是一个名词(noun) n_lemma = lemmatizer.lemmatize(word, pos='n') print(n_lemma) #leaf# 将动词还原为原型形式 #'v'表示是一个动词(verb) v_lemma = lemmatizer.lemmatize(word, pos='v') print(v_lemma) #leave 《新程序员》:云原生和全面数字化实践50位技术专家共同创作,文字、视频、音频交互阅读总结
以上是生活随笔为你收集整理的python 笔记:nltk (标记英文单词词性等)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: python笔记 xpinyin
- 下一篇: python笔记:jieba(中文分词)