def generate_frequency_table(text,charset):
'''
Generate a character frequency table for a given text
and charset as dict with character or string as key and
frequency of appearance as value expressed as a decimal
percentage
text - A sample of plaintext to analyze for frequency data
charset - (list of strings) The set of items to count in the plaintext
such as ['a','b','c', ... 'z','aa','ab','ac', ... 'zz']
'''
freq_table = {}
text_len = 0
for char in charset:
freq_table[char] = 0
for char in text:
if char in charset:
freq_table[char] += 1
text_len += 1
for multigraph in filter(lambda x: len(x)>1,charset):
freq_table[multigraph] = string.count(text, multigraph)
# Normalize frequencies with length of text
for key in freq_table.keys():
if text_len != 0:
freq_table[key] /= float(text_len)
else:
freq_table[key] = 0
return freq_table
评论列表
文章目录