统计词频

 1 #统计词频,可以根据需要统计句子或文本
 2 txt = 'this is a student there is a bag'
 3 words = txt.split()
 4 dict = {}
 5 for w in words:
 6     if w not in dict:
 7         dict[w] = 1
 8     else:
 9         dict[w] = dict[w] + 1
10 print dict

 

posted @ 2014-02-05 10:02  cdsj  阅读(147)  评论(0编辑  收藏  举报