欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python 笔记:nltk (标记英文单词词性等)

发布时间:2025/4/5 python 43 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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 (标记英文单词词性等)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。