当前位置: 首页>>代码示例>>Python>>正文


Python Stats.process_text方法代码示例

本文整理汇总了Python中stats.Stats.process_text方法的典型用法代码示例。如果您正苦于以下问题:Python Stats.process_text方法的具体用法?Python Stats.process_text怎么用?Python Stats.process_text使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在stats.Stats的用法示例。


在下文中一共展示了Stats.process_text方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: signal_handler

# 需要导入模块: from stats import Stats [as 别名]
# 或者: from stats.Stats import process_text [as 别名]
class Pool:

	def signal_handler(self, signal, frame):
		self.end = True
		print 'You pressed Ctrl+C!'
		

	def __init__(self, ciphertext, stats_filenames, dict_file, max_pop = 10, params = default_params, alphabet = default_alphabet, stat_file = None, naive_key = True):
	
		self.params = params
		self.alphabet = alphabet
		self.single_stats_filename, self.double_stats_filename = stats_filenames
		self.ciphertext = ciphertext
		self.max_pop = max_pop
		
		self.exp_stats = Stats(self.alphabet)
		self.exp_stats.load_singlef_from_file(self.single_stats_filename)
		self.exp_stats.load_doublef_from_file(self.double_stats_filename)

		self.ciph_stats = Stats(self.alphabet)
		self.ciph_stats.process_text(self.ciphertext)
		
		self.initialize(naive_key)
		self.stat_file = None
				
		if stat_file != None:
			self.stat_file = open(stat_file, "w")
			self.stat_file.write("gen\tworst\tavg\tbest\tderiv\n")
			
		self.sliding_table = []
		self.end = False
		
		self.dictionary = Dictionary(dict_file)
		self.temp_stats = Stats(self.alphabet)
		
		signal.signal(signal.SIGINT, self.signal_handler)


	def compute_sliding_deriv(self, current_value, maxlen):
		
		self.sliding_table.append(current_value)
		
		slen = len(self.sliding_table)
		
		if slen > maxlen:
			self.sliding_table = self.sliding_table[slen - maxlen:]
			
		slen = len(self.sliding_table)
		
		return (self.sliding_table[slen - 1] - self.sliding_table[0]) / float(slen)  

	''' the smaller the better '''
	def get_fitness(self, key):
	
		deciphered = key.encdec(self.ciphertext)
		
		
		self.temp_stats.process_text(deciphered)
		
		#print deciphered.encode("utf-8")

		corr =  self.exp_stats.compute_difference(self.temp_stats)
		
		return corr
		#return self.dictionary.count(deciphered) / 100.0 + corr * 0.1
		
		
	def initialize(self, naive):

		self.keys = []
		
		naive_key = Key(self.alphabet)
		naive_key.compute_naive(self.exp_stats, self.ciph_stats)
		naive_key.reverse()
		
		for i in xrange(0, self.max_pop):
			if naive:
				new_key = naive_key.clone()
				new_key.shuffle(3)
			else:
				new_key = Key(self.alphabet)
				new_key.randomize()

			self.keys.append(new_key)
		
	def process_mutations(self):
	
		count = int((self.params["mutations_percent"] * len(self.keys)) / 100)
		maxp = len(self.keys) - 1
		
		for i in xrange(0, count):
			p = random.randint(0, maxp)
			n = self.keys[p].clone()
			n.shuffle(self.params["mutation_swap_count"])
			self.keys.append(n)
	
	def make_step(self):
	
		crossovers = int(self.params["crossover_percent"] * self.max_pop / 100)
		
#.........这里部分代码省略.........
开发者ID:pinkeen,项目名称:subst-cracker,代码行数:103,代码来源:pool.py


注:本文中的stats.Stats.process_text方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。