简体   繁体   English

如何查找文件中十个最常用单词的频率?

[英]How to find frequency of ten most common words in a file?

I'm writing a function on Python that takes the name of a text file (as a string) as input.我正在 Python 上写一个 function ,它将文本文件的名称(作为字符串)作为输入。 The function should first determine how many times each word appears in the file. function 应该首先确定每个单词在文件中出现的次数。 Later, I will make a bar chart showing the frequency of the ten most common words in the file, and next to each bar is a second bar whose height is the frequency predicted by Zipf's Law.稍后,我将制作一个条形图,显示文件中最常见的十个单词的频率,每个条形旁边是第二个条形,其高度是 Zipf 定律预测的频率。 I have some code for the graph already but I need help with finding the most common words in a text file.我已经有一些图表代码,但我需要帮助来查找文本文件中最常见的单词。

def zipf_graph(text_file):
    import string
    file = open(text_file, encoding = 'utf8')
    text = file.read()
    file.close()

    #the following strips and removes punctuation and makes the words lowercase
    punc = string.punctuation + '’”—⎬⎪“⎫'
    new_text = text
    for char in punc:
        new_text = new_text.replace(char,'')
        new_text = new_text.lower()
    text_split = new_text.split()

I'm stuck here, I'm trying to find the most common strings in a list but I'm not sure where to go from here, the following is what I tried:我被困在这里,我试图在列表中找到最常见的字符串,但我不确定从这里到 go 的位置,以下是我尝试过的:

    words = text_split
    most_common = max(words, key = words.count)
    # print(most_common)

I also want to add the following code as it was suggested to help我还想添加以下代码,因为它被建议帮助

    # Sorting a list by frequency
    # Assumes you have your elements as (word, frequency) tuples
    # (Useful for the zipf function)
    words = [('the', 1), ('and', 1), ('test',2)]
    sorted(words, key = lambda x: x[1], reverse = True)

    # "Sorting" a dictionary by frequency
    # Assumes you have your elements as word:frequency
    # (Useful for the zipf function)
    words = dict()
    words['the'] = 1
    words['and'] = 1
    words['test'] = 2

    # This returns a list of just the most common words without their frequencies
    most_common_words = sorted(words, key = words.get, reverse = True)
    # print(most_common_words)

    # We can go back to the dictionary to get the frequencies
    for word in most_common_words:
        print(word, words[word])

zipf_graph('fortune.txt') #name of the file I chose to use 

I would suggest you use Counter from collections .我建议您使用collections中的Counter

from collections import Counter

text_split = ["a", "b", "c", "a", "c", "d", "a", "d", "b"]
word_and_freq = Counter(text_split)
top = word_and_freq.most_common(2)

print(top)

Interestingly, this returns the format you want.有趣的是,这会返回您想要的格式。

[("a", 3), ("b", 2)]

You can use the nltk library:您可以使用 nltk 库:

import nltk
words = ['words', 'in', 'the', 'file']
fd = nltk.FreqDist(words)
fd.most_common(10)

will give values in the format:将以以下格式给出值:

[('file', 1), ('words', 1), ('in', 1), ('the', 1)]

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

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