Beispiel #1
0
 def crack(self):
     result = []
     # 加载数据
     with open(os.path.join(current_dir, 'image_data.json'), 'r') as f:
         image_data = json.load(f)
     for letter in self.handle_split_image():
         letter_vector = build_vector(letter)
         guess = []
         for image in image_data:
             for x, y in image.items():
                 guess.append((distance_hanmming(y, letter_vector), x))
         guess.sort()
         result.append(guess[0][1])
     return ''.join(result)
 def crack(self):
     result = []
     # 加载数据
     with open(os.path.join(current_dir, 'image_data.json'), 'rb') as f:
         image_data = json.load(f)
     for letter in self.handle_split_image():
         letter_vector = build_vector(letter)
         guess = []
         for image in image_data:
             for x, y in image.items():
                 guess.append((distance_hanmming(y, letter_vector), x))
         guess.sort()
         neighbors = guess[:15]  # 距离最近的十五个向量
         class_votes = {}  # 投票
         for neighbor in neighbors:
             class_votes.setdefault(neighbor[-1], 0)
             class_votes[neighbor[-1]] += 1
         sorted_votes = sorted(class_votes.items(),
                               key=lambda x: x[1],
                               reverse=True)
         result.append(sorted_votes[0][0])
     return ''.join(result)