本文整理汇总了Python中mapper.Mapper.get_mapping_from方法的典型用法代码示例。如果您正苦于以下问题:Python Mapper.get_mapping_from方法的具体用法?Python Mapper.get_mapping_from怎么用?Python Mapper.get_mapping_from使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mapper.Mapper
的用法示例。
在下文中一共展示了Mapper.get_mapping_from方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Cryptsession
# 需要导入模块: from mapper import Mapper [as 别名]
# 或者: from mapper.Mapper import get_mapping_from [as 别名]
class Cryptsession(object):
def __init__(self):
#TODO Move frequency stuff to two separate models of same type. one for reference and one for ciphertext
#map that stores the frequeny model for the plainText
#self.symbol_ref = {}
#self.bigram_ref = {}
#load the reference frequencies from frequencies/<language>/
self.reference = ReferenceModel.for_language("english")
#map that stores the absolute frequencies of letters in the ciphertext
#init the dictionary with 0s
# self.symbol_counts = dict.fromkeys(unicode(string.ascii_uppercase),0.0)
# #map that stores the frequency model for the ciphertext
# #init the dictionary with 0s
# self.symbol_freqs = dict.fromkeys(unicode(string.ascii_uppercase),0.0)
#map to store the assumed mappings between symbol in ciphertext and symbol in plaintext
#self.substitutions = {}
self.mapper = Mapper()
#blacklist of punctuation characters
# self.blacklist = [u"É",u"!", u".", u",", u"|", u"?", u":", u";",u"+", u"-",u"\"",u"§",u"$",u"%",u"&",u"/",u"(",u")",u"=",u"[",u"]",u"{",u"}",u"@",u"1",u"2",u"3",u"4",u"5",u"6",u"7",u"8",u"9",u"0"]
# #map that stores absolute word frequencies
# self.word_counts = {}
# self.word_freqs = {}
# self.bigram_counts = {}
# self.bigram_freqs = {}
# self.trigram_counts = {}
# self.trigram_freqs = {}
def show_most_frequent_symbols(self,n=5):
smbls = self.ciphertext.get_letter_freqs()
smbls = sorted(smbls, key=lambda x: x[1], reverse=True)
print "=== %d most frequent symbols ===" % n
for i in range(n):
symbol = smbls[i][0]
out = u"{} ({:.2f} %)".format(symbol.upper(), smbls[i][1])
#if there is a known mapping, print it
if self.mapper.has_mapping_from(symbol):
plain = self.mapper.get_mapping_from(symbol)
out += u" --> {} ({:.2f} %)".format(plain.lower(), self.reference.get_letter_freq(plain))
print out
def show_most_frequent_bigrams(self,n=5):
bgrms = self.ciphertext.get_bigram_freqs()
bgrms = sorted(bgrms, key=lambda x: x[1], reverse=True)
print "=== %d most frequent bigrams ===" %n
for i in range(n):
bgrm = bgrms[i][0]
out = u"{} ({:.2f} %)".format(bgrm.upper(), bgrms[i][1])
#print bigram mapping (using current mappings)
plainbgrm=u""
#for each letter in the bigram
for symbol in bgrm:
#check if we have a mapping
if self.mapper.has_mapping_from(symbol):
plainbgrm+=self.mapper.get_mapping_from(symbol)
else:
#if we do not have a mapping use ?
plainbgrm+=u"?"
#if none of the bigram letters has a mapping don't show bigram-mapping
if plainbgrm.count(u"?") < len(bgrm):
out+= u" --> {}".format(plainbgrm.lower())
print out
def show_most_frequent_trigrams(self, n=5):
trgrms = self.ciphertext.get_trigram_freqs()
trgrms = sorted(trgrms, key=lambda x: x[1], reverse=True)
print "=== %d most freqent trigrams ===" %n
for i in range(n):
trgrm = trgrms[i][0]
out = u"{} ({:.2f} %)".format(trgrm.upper(), trgrms[i][1])
#print trigram mapping (using current mappings)
plaintrgrm=u""
#for each letter in the trigram
for symbol in trgrm:
#check if we have a mapping
if self.mapper.has_mapping_from(symbol):
plaintrgrm+=self.mapper.get_mapping_from(symbol)
else:
#if we do not have a mapping use ?
plaintrgrm+=u"?"
#if none of the trigram letters has a mapping don't show trigram-mapping
if plaintrgrm.count(u"?") < len(trgrm):
out+= u" --> {}".format(plaintrgrm.lower())
print out
def show_most_frequent_words(self, n=5):
cwords = self.c.word_counts.items()
cwords = sorted(cwords, key=lambda x: x[1], reverse=True)
print "=== %d most frequent words ===" % n
for i in range(n):
word = cwords[i][0]
out = u"{} ({:.2f} %)".format(word.upper(), cwords[i][1])
#print word mapping (using current mappings)
plainword=u""
#for each letter in the word
for symbol in word:
#check if we have a mapping
if self.mapper.has_mapping_from(symbol):
plainword+=self.mapper.get_mapping_from(symbol)
#.........这里部分代码省略.........