pythonの自然言語処理ツールnltk

732 ワード

nltkナチュラル言語処理ツールパッケージを使用すると、単語の頻度の統計を簡単に実現できます.以下のコードは、すべての単語の中で最も多くの出現回数を出力する最初の100単語を実現します.
__author__ = '20130907'
#coding:utf-8
import nltk
filename = r"C:\Users\20130907\Desktop\freqword\suuplysplit.txt"
f = open(filename,'r')
ofile = r"C:\Users\20130907\Desktop\freqword\freqword.txt"
of = open(ofile,'w')
words = []
line = f.readline()
while(line):
    words += line.split()
    line = f.readline()
#print words
#for w in words:
#    of.write(w)
of.write('
') freq_dist = nltk.FreqDist(words) #print freq_dist.keys()[:2] f.close() of.write('FreqWord
') for x in freq_dist.keys()[:100]:     of.write(x)     of.write(' ') of.close()