本文介绍了Python-在文本文件中查找单词列表的单词频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图加快我的项目以计算单词频率的速度.我有360多个文本文件,我需要获取单词的总数以及另一个单词列表中每个单词出现的次数.我知道如何使用单个文本文件执行此操作.

I am trying to speed up my project to count word frequencies. I have 360+ text files, and I need to get the total number of words and the number of times each word from another list of words appears. I know how to do this with a single text file.

>>> import nltk
>>> import os
>>> os.chdir("C:\Users\Cameron\Desktop\PDF-to-txt")
>>> filename="1976.03.txt"
>>> textfile=open(filename,"r")
>>> inputString=textfile.read()
>>> word_list=re.split('\s+',file(filename).read().lower())
>>> print 'Words in text:', len(word_list)
#spits out number of words in the textfile
>>> word_list.count('inflation')
#spits out number of times 'inflation' occurs in the textfile
>>>word_list.count('jobs')
>>>word_list.count('output')

获取通货膨胀",工作",产出"个体的频率太繁琐了.我可以将这些单词放入列表中并同时查找列表中所有单词的出现频率吗?

Its too tedious to get the frequencies of 'inflation', 'jobs', 'output' individual. Can I put these words into a list and find the frequency of all the words in the list at the same time? Basically this with Python.

示例:代替此:

>>> word_list.count('inflation')
3
>>> word_list.count('jobs')
5
>>> word_list.count('output')
1

我想这样做(我知道这不是真正的代码,这是我寻求帮助的内容):

I want to do this (I know this isn't real code, this is what I'm asking for help on):

>>> list1='inflation', 'jobs', 'output'
>>>word_list.count(list1)
'inflation', 'jobs', 'output'
3, 5, 1

我的单词列表将有10到20个词,因此我需要能够仅将Python指向单词列表以获取计数.如果输出能够复制并粘贴到excel电子表格中,文字作为列,频率作为行,也很好

My list of words is going to have 10-20 terms, so I need to be able to just point Python toward a list of words to get the counts of. It would also be nice if the output was able to be copy+paste into an excel spreadsheet with the words as columns and frequencies as rows

示例:

inflation, jobs, output
3, 5, 1

最后,有人可以帮助所有文本文件实现自动化吗?我认为我只是将Python指向该文件夹,它就可以针对每个360+文本文件从新列表中进行上述字数统计.似乎很容易,但是我有点卡住了.有什么帮助吗?

And finally, can anyone help automate this for all of the textfiles? I figure I just point Python toward the folder and it can do the above word counting from the new list for each of the 360+ text files. Seems easy enough, but I'm a bit stuck. Any help?

这样的输出将是很棒的: 文件名1 通货膨胀,工作,产出 3、5、1

An output like this would be fantastic: Filename1 inflation, jobs, output 3, 5, 1

Filename2
inflation, jobs, output
7, 2, 4

Filename3
inflation, jobs, output
9, 3, 5

谢谢!

推荐答案

如果我了解您的问题,则collections.Counter()已涵盖此问题.

文档中的示例似乎符合您的问题.

The example from the docs would seem to match your problem.

# Tally occurrences of words in a list
cnt = Counter()
for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
    cnt[word] += 1
print cnt


# Find the ten most common words in Hamlet
import re
words = re.findall('\w+', open('hamlet.txt').read().lower())
Counter(words).most_common(10)

在上面的示例中,您应该能够:

From the example above you should be able to do:

import re
import collections
words = re.findall('\w+', open('1976.03.txt').read().lower())
print collections.Counter(words)

EDIT 天真的方法来展示一种方法.

EDIT naive approach to show one way.

wanted = "fish chips steak"
cnt = Counter()
words = re.findall('\w+', open('1976.03.txt').read().lower())
for word in words:
    if word in wanted:
        cnt[word] += 1
print cnt

这篇关于Python-在文本文件中查找单词列表的单词频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 17:22