简体   繁体   English

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

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

I'm having a bit of difficulty figuring this out.我有点难以弄清楚这一点。 This function is supposed to do the following/adhere to the following guidelines:此功能应该执行以下/遵守以下准则:

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.

I have the helper function completed:我已经完成了辅助功能:

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

However, I am unsure how to go from here.但是,我不确定如何从这里开始。 Could anyone provide some guidance?有人可以提供一些指导吗?

I guess the next step would be to learn how to sort your result from highest to lowest, and then decide a way to represent this information to the user.我想下一步是学习如何将结果从高到低排序,然后决定一种向用户表示此信息的方式。 Probably the simplest would be to print it.可能最简单的方法是打印它。

I guess the N parameter intends to limit how many values(words) you print?我猜 N 参数旨在限制您打印的值(字)数量?

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

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