Skip to content

Changes to hashable dict #482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 12, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Optimized permutationdecoder
  • Loading branch information
Chipe1 committed Mar 31, 2017
commit 8c6e78e81fa5799ee239a7c3728f70e7e14f26bc
11 changes: 7 additions & 4 deletions text.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ def __init__(self, training_text, ciphertext=None):
def decode(self, ciphertext):
"""Search for a decoding of the ciphertext."""
self.ciphertext = canonicalize(ciphertext)
# reduce domain to speed up search
self.chardomain = {c for c in self.ciphertext if c is not ' '}
problem = PermutationDecoderProblem(decoder=self)
solution = search.best_first_graph_search(
problem, lambda node: self.score(node.state))
Expand All @@ -369,7 +371,8 @@ def score(self, code):

# remake code dictionary to contain translation for all characters
full_code = code.copy()
full_code.update({x:x for x in alphabet + ' ' if x not in code})
full_code.update({x:x for x in self.chardomain if x not in code})
full_code[' '] = ' '
text = translate(self.ciphertext, lambda c: full_code[c])

# add small positive value to prevent computing log(0)
Expand All @@ -387,7 +390,7 @@ def __init__(self, initial=None, goal=None, decoder=None):
self.decoder = decoder

def actions(self, state):
search_list = [c for c in alphabet if c not in state]
search_list = [c for c in self.decoder.chardomain if c not in state]
target_list = [c for c in alphabet if c not in state.values()]
# Find the best charater to replace
plainchar = argmax(search_list, key=lambda c: self.decoder.P1[c])
Expand All @@ -401,5 +404,5 @@ def result(self, state, action):
return new_state

def goal_test(self, state):
"""We're done when we get all 26 letters assigned."""
return len(state) >= 26
"""We're done when all letters in search domain are assigned."""
return len(state) >= len(self.decoder.chardomain)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy