Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ff8fc03

Browse files
Chipe1norvig
authored andcommitted
Added PermutationDecoder to notebook (aimacode#507)
1 parent 4caca95 commit ff8fc03

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

text.ipynb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,64 @@
364364
"decoded_message = decoder.decode(ciphertext)\n",
365365
"print('The decoded message is', '\"' + decoded_message + '\"')"
366366
]
367+
},
368+
{
369+
"cell_type": "markdown",
370+
"metadata": {},
371+
"source": [
372+
"### Permutation Decoder\n",
373+
"Now let us try to decode messages encrypted by a general monoalphabetic substitution cipher. The letters in the alphabet can be replaced by any permutation of letters. For example if the alpahbet consisted of `{A B C}` then it can be replaced by `{A C B}`, `{B A C}`, `{B C A}`, `{C A B}`, `{C B A}` or even `{A B C}` itself. Suppose we choose the permutation `{C B A}`, then the plain text `\"CAB BA AAC\"` would become `\"ACB BC CCA\"`. We can see that Caesar cipher is also a form of permutation cipher where the permutation is a cyclic permutation. Unlike the Caesar cipher, it is infeasible to try all possible permutations. The number of possible permutations in Latin alphabet is `26!` which is of the order $10^{26}$. We use graph search algorithms to search for a 'good' permutation."
374+
]
375+
},
376+
{
377+
"cell_type": "code",
378+
"execution_count": 10,
379+
"metadata": {
380+
"collapsed": true
381+
},
382+
"outputs": [],
383+
"source": [
384+
"%psource PermutationDecoder"
385+
]
386+
},
387+
{
388+
"cell_type": "markdown",
389+
"metadata": {},
390+
"source": [
391+
"Each state/node in the graph is represented as a letter-to-letter map. If there no mapping for a letter it means the letter is unchanged in the permutation. These maps are stored as dictionaries. Each dictionary is a 'potential' permutation. We use the word 'potential' because every dictionary doesn't necessarily represent a valid permutation since a permutation cannot have repeating elements. For example the dictionary `{'A': 'B', 'C': 'X'}` is invalid because `'A'` is replaced by `'B'`, but so is `'B'` because the dictionary doesn't have a mapping for `'B'`. Two dictionaries can also represent the same permutation e.g. `{'A': 'C', 'C': 'A'}` and `{'A': 'C', 'B': 'B', 'C': 'A'}` represent the same permutation where `'A'` and `'C'` are interchanged and all other letters remain unaltered. To ensure we get a valid permutation a goal state must map all letters in the alphabet. We also prevent repetions in the permutation by allowing only those actions which go to new state/node in which the newly added letter to the dictionary maps to previously unmapped letter. These two rules togeter ensure that the dictionary of a goal state will represent a valid permutation.\n",
392+
"The score of a state is determined using word scores, unigram scores, and bigram scores. Experiment with different weightages for word, unigram and bigram scores and see how they affect the decoding."
393+
]
394+
},
395+
{
396+
"cell_type": "code",
397+
"execution_count": 11,
398+
"metadata": {
399+
"collapsed": false
400+
},
401+
"outputs": [
402+
{
403+
"name": "stdout",
404+
"output_type": "stream",
405+
"text": [
406+
"\"ahed world\" decodes to \"shed could\"\n",
407+
"\"ahed woxld\" decodes to \"shew atiow\"\n"
408+
]
409+
}
410+
],
411+
"source": [
412+
"ciphertexts = ['ahed world', 'ahed woxld']\n",
413+
"\n",
414+
"pd = PermutationDecoder(canonicalize(flatland))\n",
415+
"for ctext in ciphertexts:\n",
416+
" print('\"{}\" decodes to \"{}\"'.format(ctext, pd.decode(ctext)))"
417+
]
418+
},
419+
{
420+
"cell_type": "markdown",
421+
"metadata": {},
422+
"source": [
423+
"As evident from the above example, permutation decoding using best first search is sensitive to initial text. This is because not only the final dictionary, with substitutions for all letters, must have good score but so must the intermediate dictionaries. You could think of it as performing a local search by finding substitutons for each letter one by one. We could get very different results by changing even a single letter because that letter could be a deciding factor for selecting substitution in early stages which snowballs and affects the later stages. To make the search better we can use different definition of score in different stages and optimize on which letter to substitute first."
424+
]
367425
}
368426
],
369427
"metadata": {

0 commit comments

Comments
 (0)