Fdist和前10个函数词
我需要写一个脚本,来找出所有内容词,并按出现频率从高到低排列。
我想要找出出现频率最高的10个内容词,所以不仅要列出我文本中出现频率最高的10个词,还需要把一些功能词(比如“和”、“或”,还有标点符号等)过滤掉。
我现在有的代码是这样的:
fileids=corpus.fileids ()
text=corpus.words(fileids)
wlist=[]
ftable=nltk.FreqDist (text)
wlist.append(ftable.keys () )
这段代码能给我一个很整齐的词汇列表,按出现频率从高到低排列,但我该怎么把功能词过滤掉呢?
谢谢。
1 个回答
1
你想要过滤掉一些特定的词,这些词叫做“停用词”。这里有个来自这个StackOverflow回答的核心思路:
你需要在你的代码中添加几行内容:
fileids=corpus.fileids ()
text=corpus.words(fileids)
添加以下几行:创建一个停用词的列表,并从你的文本中把它们过滤掉。
#get a list of the stopwords
stp = nltk.corpus.stopwords.words('english')
#from your text of words, keep only the ones NOT in stp
filtered_text = [w for w in text if not w in stp]
接下来,继续按照原来的方式进行。
wlist=[]
ftable=nltk.FreqDist (filtered_text)
wlist.append(ftable.keys () )
希望这对你有帮助。