繁体   English   中英

编写一个返回文本文件中最常见单词列表的 Python 函数?

[英]Writing a Python Function that returns a list of the most common words in a text file?

我有点难以弄清楚这一点。 此功能应该执行以下/遵守以下准则:

def mostCommonWords(filename, N):
    return "stub"
- Read the file from filename in your function and returns a dictionary 
with the frequency of each word as its value.
- Words are separated by whitespace characters, but do not include
the following punctuation characters (,.!?;). You can assume contractions
count as one word (i.e. "don't", "you'll", etc. are one word).
- The split and strip functions may be useful.
- You can assume contractions count as one word 
(i.e. "don't", "you'll", etc. are one word).
- Your function should open the file for reading, and close
the file before returning.

我已经完成了辅助功能:

def wordFrequency(filename):
    frequency = {}
    file = open(filename, 'r')
    for line in file.readlines():
        for word in line.strip().split():
            if word not in frequency:
                frequency[word] = 0
            frequency[word] += 1
        file.close()
    return frequency

但是,我不确定如何从这里开始。 有人可以提供一些指导吗?

我想下一步是学习如何将结果从高到低排序,然后决定一种向用户表示此信息的方式。 可能最简单的方法是打印它。

我猜 N 参数旨在限制您打印的值(字)数量?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM