簡體   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