Advertisement
Guest User

Decode Encrypt Text

a guest
Aug 1st, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. """
  2. Try to match all the words in encrypt text
  3. """
  4. import re
  5.  
  6. ENCRYPT_TEXT = "Sm ppl cmprs txt msgs by rtnng only ths vwls tht bgn "
  7. ENCRYPT_TEXT += "a wrd and by rplcng dbld ltrs wth sngl ltrs"
  8. ENCRYPT_TEXT = ENCRYPT_TEXT.split()
  9.  
  10.  
  11. def main():
  12.     """
  13.    The main function
  14.    """
  15.     word_dict = {}
  16.     answer = {}
  17.  
  18.     # Build the dictionary by first letter
  19.     for capital in range(ord('a'), ord('z')+1):
  20.         word_dict[chr(capital)] = []
  21.  
  22.     with open("/usr/share/dict/words", "r") as dict_file:
  23.         for word in dict_file.readlines():
  24.             word = word.strip()
  25.             if len(word) > 0:
  26.                 word_dict[word[0].lower()].append(word)
  27.  
  28.     for capital in word_dict.keys():
  29.         word_dict[capital] = "\n".join(word_dict[capital])
  30.  
  31.     # For each word, try to find it in dictionary
  32.     for word in ENCRYPT_TEXT:
  33.         reg_re = word[0] + "".join([".*"+c for c in word[1:]]) + ".*"
  34.         reg_re = reg_re.lower()
  35.         answer[word] = re.findall(reg_re, word_dict[reg_re[0]])
  36.  
  37.     print answer
  38.  
  39. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement