python中的字母频率

时间:2011-02-28 23:39:43

标签: python

我需要制作一个打印出文字中字母频率的程序 文件并将该频率与python中的另一个频率进行比较。

到目前为止,我能够打印出一封信的次数,但是 我得到的百分比频率是错误的。我认为这是因为我只需要计算我的程序 删除所有空格和其他空格中文件中的字母数 字符。

def addLetter (x):
    result = ord(x) - ord(a)
    return result


#start of the main program
#prompt user for a file

while True:
    speech = raw_input("Enter file name:")

    wholeFile = open(speech, 'r+').read()
    lowlet = wholeFile.lower()
    letters= list(lowlet)
    alpha = list('abcdefghijklmnopqrstuvwxyz')
    n = len(letters)
    f = float(n)
    occurrences = {}
    d = {}


    #number of letters
    for x in alpha:
        occurrences[x] = letters.count(x)
        d[x] =(occurrences[x])/f
    for x in occurrences:
        print x, occurrences[x], d[x]

这是输出

Enter file name:dems.txt
a 993 0.0687863674148
c 350 0.0242449431976
b 174 0.0120532003325
e 1406 0.0973954003879
d 430 0.0297866444999
g 219 0.015170407315
f 212 0.0146855084511
i 754 0.0522305347742
h 594 0.0411471321696
k 81 0.00561097256858
j 12 0.000831255195345
m 273 0.0189110556941
l 442 0.0306178996952
o 885 0.0613050706567
n 810 0.0561097256858
q 9 0.000623441396509
p 215 0.0148933222499
s 672 0.0465502909393
r 637 0.0441257966196
u 305 0.021127736215
t 1175 0.0813937378775
w 334 0.0231366029371
v 104 0.00720421169299
y 212 0.0146855084511
x 13 0.000900526461624
z 6 0.000415627597672
Enter file name:

程序会在列中打印,但我不确定如何在此处显示。

“a”的频率应为.0878

3 个答案:

答案 0 :(得分:2)

您可以使用translator recipe删除alpha以外的所有字符。 由于这样做会导致letters只包含alpha中的字符,n现在是正确的分母。

然后,您可以使用collections.defaultdict(int)来计算字母的出现次数:

import collections
import string

def translator(frm='', to='', delete='', keep=None):
    # Python Cookbook Recipe 1.9
    # Chris Perkins, Raymond Hettinger
    if len(to) == 1: to = to * len(frm)
    trans = string.maketrans(frm, to)
    if keep is not None:
        allchars = string.maketrans('', '')
        # delete is expanded to delete everything except
        # what is mentioned in set(keep)-set(delete)
        delete = allchars.translate(allchars, keep.translate(allchars, delete))
    def translate(s):
        return s.translate(trans, delete)
    return translate

alpha = 'abcdefghijklmnopqrstuvwxyz'
keep_alpha=translator(keep=alpha)

while True:
    speech = raw_input("Enter file name:")
    wholeFile = open(speech, 'r+').read()
    lowlet = wholeFile.lower()
    letters = keep_alpha(lowlet)
    n = len(letters)
    occurrences = collections.defaultdict(int)    
    for x in letters:
        occurrences[x]+=1
    for x in occurrences:
        print x, occurrences[x], occurrences[x]/float(n)

答案 1 :(得分:2)

import collections
import re
from __future__ import division

file1 = re.subn(r"\W", "", open("file1.txt", "r").read())[0].lower()
counter1 = collections.Counter(file1)
for k, v in counter1.iteritems():
   counter1[k] = v / len(file1)

file2 = re.subn(r"\W", "", open("file2.txt", "r").read())[0].lower()
counter2 = collections.Counter(file2)
for k, v in counter2.iteritems():
   counter2[k] = v / len(file2)

注意:需要Python 2.7。

答案 2 :(得分:0)

我认为这是一种非常直接的方式:

while True:
    speech = raw_input("Enter file name:")

    wholeFile = open(speech, 'r+').read()
    lowlet = wholeFile.lower()

    alphas = 'abcdefghijklmnopqrstuvwxyz'

    # lets set default values first
    occurrences = {letter : 0 for letter in alphas }
    # occurrences = dict(zip(alphas, [0]*len(alphas))) # for python<=2.6

    # total number of valid letters
    total = 0

    # iter everything in the text
    for letter in lowlet:
        # if it is a valid letter then it is in occurrences
        if letter in occurrences:
            # update counts
            total += 1
            occurrences[letter] += 1

    # now print the results:
    for letter, count in occurrences.iteritems():
        print letter, (1.0*count/total)

如您所知,在计算频率之前,您需要文本中的有效字母总数。要么在处理之前过滤文本,要么将过滤与处理结合起来,这就是我在这里所做的。