用python 列出文章中使用最频繁的诗歌单词,并给出它们的出现次数

问题描述:

用python 列出文章中使用最频繁的诗歌单词,并给出它们的出现次数
1个回答 分类:英语 2014-12-06

问题解答:

我来补答
这个用python2.7测试通过.import urllib2
import re
from collections import Counter
def get_data(url):
    resp = urllib2.urlopen(url).read()
    return resp
def analyse(text, n=1):
    ''' show the n most common words in text '''
    res = Counter(re.split(r'\W+', text, flags=re.M)).most_common(n)
    print('words\ttimes')
    print('\n'.join([k+'\t'+str(v) for k,v in res]))
def main():
    data = get_data('http://www.umich.edu/~umfandsf/other/ebooks/alice30.txt')
    analyse(data, 1)
main()结果是:wordstimes
the1525可以自己调整给analyse()的参数n来控制打印多少频繁出现的单词.
 
 
展开全文阅读
剩余:2000