Skip to content
Prev Previous commit
Next Next commit
Update text.py
  • Loading branch information
antmarakis authored Mar 1, 2017
commit 9ba524ca9c8bd01193af6bf4a3d8d4f526ec252e
20 changes: 10 additions & 10 deletions text.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,25 +153,25 @@ def query(self, query_text, n=10):
return heapq.nlargest(n, ((self.total_score(qwords, docid), docid) for docid in docids))

def score(self, word, docid):
"Compute a score for this word on the document with this docid."
"""Compute a score for this word on the document with this docid."""
# There are many options; here we take a very simple approach
return (log(1 + self.index[word][docid]) /
log(1 + self.documents[docid].nwords))

def total_score(self, words, docid):
"Compute the sum of the scores of these words on the document with this docid."
"""Compute the sum of the scores of these words on the document with this docid."""
return sum(self.score(word, docid) for word in words)

def present(self, results):
"Present the results as a list."
"""Present the results as a list."""
for (score, docid) in results:
doc = self.documents[docid]
print(
("{:5.2}|{:25} | {}".format(100 * score, doc.url,
doc.title[:45].expandtabs())))

def present_results(self, query_text, n=10):
"Get results for the query and present them."
"""Get results for the query and present them."""
self.present(self.query(query_text, n))


Expand Down Expand Up @@ -264,7 +264,7 @@ def maketrans(from_, to_):


def encode(plaintext, code):
"Encodes text, using a code which is a permutation of the alphabet."
"""Encodes text, using a code which is a permutation of the alphabet."""
trans = maketrans(alphabet + alphabet.upper(), code + code.upper())

return translate(plaintext, trans)
Expand Down Expand Up @@ -293,7 +293,7 @@ def __init__(self, training_text):
self.P2 = CountingProbDist(bigrams(training_text), default=1)

def score(self, plaintext):
"Return a score for text based on how common letters pairs are."
"""Return a score for text based on how common letters pairs are."""

s = 1.0
for bi in bigrams(plaintext):
Expand All @@ -302,15 +302,15 @@ def score(self, plaintext):
return s

def decode(self, ciphertext):
"Return the shift decoding of text with the best score."
"""Return the shift decoding of text with the best score."""

list_ = [(self.score(shift), shift)
for shift in all_shifts(ciphertext)]
return max(list_, key=lambda elm: elm[0])[1]


def all_shifts(text):
"Return a list of all 26 possible encodings of text by a shift cipher."
"""Return a list of all 26 possible encodings of text by a shift cipher."""

yield from (shift_encode(text, i) for i, _ in enumerate(alphabet))

Expand Down Expand Up @@ -339,7 +339,7 @@ def __init__(self, training_text, ciphertext=None):
self.P2 = NgramTextModel(2, training_text) # By letter pair

def decode(self, ciphertext):
"Search for a decoding of the ciphertext."
"""Search for a decoding of the ciphertext."""
self.ciphertext = ciphertext
problem = PermutationDecoderProblem(decoder=self)
return search.best_first_tree_search(
Expand Down Expand Up @@ -368,5 +368,5 @@ def actions(self, state):
succs = [extend(state, plainchar, cipherchar)] # ???? # noqa

def goal_test(self, state):
"We're done when we get all 26 letters assigned."
"""We're done when we get all 26 letters assigned."""
return len(state) >= 26