首页 > 解决方案 > 关于 nltk 中的 vocab() 和 Text()

问题描述

以下代码有什么作用?即,只是最后两行。我不明白 Text() 做什么以及 vocab() 做什么?

import nltk
 def words(content):
     tokens = nltk.tokenize.word_tokenize(content) #Make the string into a list of words
     tokens = [w for w in tokens if not w in stop_words]  # remove the stop words
     tokens = [wordnet_lemmatizer.lemmatize(w) for w in tokens] #Lemmatization
     NLTKText = Text(tokens)## remove if nltk is restricted develop new method
     return NLTKText.vocab()

运行后我得到的是一些数据结构 FreqDist。

标签: python

解决方案


从包的文档nltkText()

围绕一系列简单(字符串)标记的包装器,旨在支持文本的初始探索(通过交互式控制台)。它的方法对文本的上下文进行各种分析(例如,计数、检索、搭配发现),并显示结果。

所以它只是给定字符串列表的一个包装器,提供一个简单的接口来分析给定的语料库。特别是,vocab()返回传递列表中标记(单词)的频率分布。更多关于FreqDist 这里

希望这能回答你的问题。


推荐阅读