{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "from szyfrow.keyword_cipher import *\n", "from szyfrow.column_transposition import *\n", "from szyfrow.support.text_prettify import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "challenge_number = 5\n", "plaintext_a_filename = f'plaintext.{challenge_number}a.txt'\n", "plaintext_b_filename = f'plaintext.{challenge_number}b.txt'\n", "ciphertext_a_filename = f'ciphertext.{challenge_number}a.txt'\n", "ciphertext_b_filename = f'ciphertext.{challenge_number}b.txt'" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "ca = open(ciphertext_a_filename).read()\n", "sca = sanitise(ca)\n", "cb = open(ciphertext_b_filename).read()\n", "scb = sanitise(cb)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "Collapsed": "false" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "aile KeywordWrapAlphabet.from_a \n", "\n", "whike wecic notha vetim etopk adean ytrai necag entsw ithth ekonc onsdo utgro upsth eabek aoper ation cicma naget oredr uitan clrie fseve rakme mlers ofthe kindo knsdo uting assod iatio nanco neoft hemha smace amajo rlrea bthro ughsp ottin gsome thing thato urreg ukara gents hacno tnoti decth ekonc onpar tydon siste coftw entyt hreed ydkis tslut thegr oupar rivin ginki ndokn hacon kytwe ntyme mlers atfir stias sumec hehac misdo untec lutth ephot ograp hinth ekoda kpape raddo mpany ingth eirre porto nthev isitd onfir mecth enuml eriwi kkmab eenqu iries witho urkon conag entst oseei fthey havea nythi ngont heoth erthr eeanc wikkl riefy ouont hatne xttim etheg roupw astra dbeca sthey retur necso uthsp kitup ancvi sitec adhai nofes sexan cbent vikka gesak ongth etham eslut cespi tethe irdon tinui ngint erest inkan cmarb snoth ingol vious inthe irleh aviou rsugg estec anypa rtidu karpk anthe north erngr oupar enows tayin gwith sdout sinda newco nanca bekah asred ruite coneo fthem tospy onthe party ourai rmini stryd ontad tsare indre asing kyaka rmeca loutt hespy dkist sluta resti kknot prepa recto tekku swhat itist hatis worry ingth emthe yarep artid ukark yagit ateca loutt hepre sende atdai stora ncnow atdan ewcon ancth isdon derna louta dtivi tyine astan gkiaa ncess exmab esmew oncer ifiti sdonn edtec witha ircef endef orthe dapit aklut theya reref using totek kanci twikk lehar cforu stohe kpthe mifth eywon ttekk uswha twear esupp osect olepr otedt ingth eatta dhect ekegr amwas dopie clyan abeka agent inspa kcing hissi sterw orbsa tthet ekegr aphyo ffide ancga vehim addes sthem essag ewasc ekive recly oneof thesp ydkis tsanc ondem oreit wasac cress ectow ikhek minti rpitz ufert hespy dkist shave rever tecto asuls titut iondi pherf orthi smess agelu tlkod becit tocis guise theme ssage strud turet hatma ceita kotha rcert odrad bluti amgka cicic ithas asedr etmes sageh iccen inthe cedip herec textw hidhs ugges tstha tthes pydki stmis sioni sdkos ingon itsai msiam notsu rewha tthei nitia ksrcf stanc forlu tiwik kgola dbtot heair minis tryto asbif theyh avean ysugg estio nsiti sakso dkear fromt hedon tents ofthi smess ageth atthe germa nsare getti ngaki ttkes uspid iouss owewi kknee ctost epupo urown sedur ityiw ikkse ncmyn extlr iefin gusin gavig enere diphe rwith beyke ngtht hreem oreto fokko wpear k\n", "\n" ] } ], "source": [ "(word_a, wrap_a), score_a = keyword_break_mp(sca, fitness=Ptrigrams)\n", "print(word_a, wrap_a, '\\n')\n", "pa = keyword_decipher(ca, word_a, wrap_a)\n", "print(pa)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "'while wedid notha vetim etopl acean ytrai nedag entsw ithth elond onsco utgro upsth eakel aoper ation didma naget orecr uitan dbrie fseve ralme mbers ofthe linco lnsco uting assoc iatio nando neoft hemha smade amajo rbrea kthro ughsp ottin gsome thing thato urreg ulara gents hadno tnoti cedth elond onpar tycon siste doftw entyt hreec yclis tsbut thegr oupar rivin ginli ncoln hadon lytwe ntyme mbers atfir stias sumed hehad misco unted butth ephot ograp hinth eloca lpape racco mpany ingth eirre porto nthev isitc onfir medth enumb eriwi llmak eenqu iries witho urlon donag entst oseei fthey havea nythi ngont heoth erthr eeand willb riefy ouont hatne xttim etheg roupw astra ckeda sthey retur nedso uthsp litup andvi sited achai nofes sexan dkent villa gesal ongth etham esbut despi tethe ircon tinui ngint erest inlan dmark snoth ingob vious inthe irbeh aviou rsugg ested anypa rticu larpl anthe north erngr oupar enows tayin gwith scout sinca newdo nanda kelah asrec ruite doneo fthem tospy onthe party ourai rmini stryc ontac tsare incre asing lyala rmeda boutt hespy clist sbuta resti llnot prepa redto tellu swhat itist hatis worry ingth emthe yarep artic ularl yagit ateda boutt hepre sence atcai stora ndnow atcan ewdon andth iscon cerna bouta ctivi tyine astan gliaa ndess exmak esmew onder ifiti sconn ected witha irdef encef orthe capit albut theya reref using totel landi twill behar dforu stohe lpthe mifth eywon ttell uswha twear esupp osedt obepr otect ingth eatta chedt elegr amwas copie dbyan akela agent inspa lding hissi sterw orksa tthet elegr aphyo ffice andga vehim acces sthem essag ewasd elive redby oneof thesp yclis tsand oncem oreit wasad dress edtow ilhel minti rpitz ufert hespy clist shave rever tedto asubs titut ionci pherf orthi smess agebu tbloc kedit todis guise theme ssage struc turet hatma deita lotha rdert ocrac kbuti amgla didid ithas asecr etmes sageh idden inthe decip hered textw hichs ugges tstha tthes pycli stmis sioni sclos ingon itsai msiam notsu rewha tthei nitia lsrdf stand forbu tiwil lgoba cktot heair minis tryto askif theyh avean ysugg estio nsiti salso clear fromt hecon tents ofthi smess ageth atthe germa nsare getti ngali ttles uspic iouss owewi llnee dtost epupo urown secur ityiw illse ndmyn extbr iefin gusin gavig enere ciphe rwith keyle ngtht hreem oreto follo wpear l\\n'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "keyword_decipher(ca, 'akela', wrap_a)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "Collapsed": "false" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "akelbcdfghijmnopqrstuvwxyz \n", "\n", "while wedid notha vetim etopl acean ytrai nedag entsw ithth elond onsco utgro upsth eakel aoper ation didma naget orecr uitan dbrie fseve ralme mbers ofthe linco lnsco uting assoc iatio nando neoft hemha smade amajo rbrea kthro ughsp ottin gsome thing thato urreg ulara gents hadno tnoti cedth elond onpar tycon siste doftw entyt hreec yclis tsbut thegr oupar rivin ginli ncoln hadon lytwe ntyme mbers atfir stias sumed hehad misco unted butth ephot ograp hinth eloca lpape racco mpany ingth eirre porto nthev isitc onfir medth enumb eriwi llmak eenqu iries witho urlon donag entst oseei fthey havea nythi ngont heoth erthr eeand willb riefy ouont hatne xttim etheg roupw astra ckeda sthey retur nedso uthsp litup andvi sited achai nofes sexan dkent villa gesal ongth etham esbut despi tethe ircon tinui ngint erest inlan dmark snoth ingob vious inthe irbeh aviou rsugg ested anypa rticu larpl anthe north erngr oupar enows tayin gwith scout sinca newdo nanda kelah asrec ruite doneo fthem tospy onthe party ourai rmini stryc ontac tsare incre asing lyala rmeda boutt hespy clist sbuta resti llnot prepa redto tellu swhat itist hatis worry ingth emthe yarep artic ularl yagit ateda boutt hepre sence atcai stora ndnow atcan ewdon andth iscon cerna bouta ctivi tyine astan gliaa ndess exmak esmew onder ifiti sconn ected witha irdef encef orthe capit albut theya reref using totel landi twill behar dforu stohe lpthe mifth eywon ttell uswha twear esupp osedt obepr otect ingth eatta chedt elegr amwas copie dbyan akela agent inspa lding hissi sterw orksa tthet elegr aphyo ffice andga vehim acces sthem essag ewasd elive redby oneof thesp yclis tsand oncem oreit wasad dress edtow ilhel minti rpitz ufert hespy clist shave rever tedto asubs titut ionci pherf orthi smess agebu tbloc kedit todis guise theme ssage struc turet hatma deita lotha rdert ocrac kbuti amgla didid ithas asecr etmes sageh idden inthe decip hered textw hichs ugges tstha tthes pycli stmis sioni sclos ingon itsai msiam notsu rewha tthei nitia lsrdf stand forbu tiwil lgoba cktot heair minis tryto askif theyh avean ysugg estio nsiti salso clear fromt hecon tents ofthi smess ageth atthe germa nsare getti ngali ttles uspic iouss owewi llnee dtost epupo urown secur ityiw illse ndmyn extbr iefin gusin gavig enere ciphe rwith keyle ngtht hreem oreto follo wpear l\n", "\n" ] } ], "source": [ "word_a, score_a = simulated_annealing_break(sca, fitness=Ptrigrams,\n", " plain_alphabet=string.ascii_lowercase, cipher_alphabet=keyword_cipher_alphabet_of('akkad', wrap_alphabet=KeywordWrapAlphabet.from_largest))\n", "print(word_a, '\\n')\n", "pa = keyword_decipher(ca, word_a)\n", "print(pa)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "Collapsed": "false" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "while we did not have time to place any trained agents with the london scout groups the akela\n", "operation did manage to recruit and brief several members of the lincoln scouting association and\n", "one of them has made a major breakthrough spotting something that our regular agents had not noticed\n", "the london party consisted of twenty three cyclists but the group arriving in lincoln had only\n", "twenty members at first i assumed he had mis counted but the photograph in the local paper\n", "accompanying their report on the visit confirmed the number i will make enquiries with our london\n", "agents to see if they have anything on the other three and will brief you on that next time the\n", "group was tracked as they returned south split up and visited a chain of essex and kent villages\n", "along the thames but despite their continuing interest in landmarks nothing obvious in their\n", "behaviour suggested any particular plan the northern group are now staying with scouts in ca new don\n", "and akela has recruited one of them to spy on the part your air ministry contacts are increasingly\n", "alarmed about the spy c lists but are still not prepared to tell us what it is that is worrying them\n", "they are particularly agitated about the presence at caistor and now at ca new don and this concern\n", "about activity in east anglia and essex makes me wonder if it is connected with air defence for the\n", "capital but they are refusing to tell and it will be hard for us to help them if they wont tell us\n", "what we are supposed to be protecting the attached telegram was copied by an akela agent in spalding\n", "his sister works at the telegraphy office and gave him access the message was delivered by one of\n", "the spy c lists and once more it was addressed to wilhelm in tirpitz ufer the spy c lists have\n", "reverted to a substitution cipher for this message but blocked it to disguise the message structure\n", "that made it alot harder to crack but i am glad i did it has a secret message hidden in the\n", "deciphered text which suggests that the spy c list mission is closing on its aim siam not sure what\n", "the initials rdf stand for but i will go back to the air ministry to ask if they have any\n", "suggestions it is also clear from the contents of this message that the germans are getting a little\n", "suspicious so we will need to step up our own security i will send my next briefing using avi genere\n", "cipher with key length three more to follow pearl\n" ] } ], "source": [ "pa = prettify(keyword_decipher(sca, 'akela', wrap_alphabet=KeywordWrapAlphabet.from_a))\n", "print(pa)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "2336" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "open(plaintext_a_filename, 'w').write(pa)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "Collapsed": "false" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "wilhelm KeywordWrapAlphabet.from_last \n", "\n", "dearu nclew ilhel masyo uprom isedw ethor oughl yenjo yedou rvisi ttoyo urcou sinin linco lnand found itmos tinfo rmati vesto ptheb eauti fulca thedr alalm ostju stifi edour visit onits ownan dourh ostss hared withu ssome inter estin gdraw ingso fthet owers youme ntion edsto pthey graci ously allow edmet ocopy thesk etche sande xplai nedmu chabo uthow theto wersa rebui ltand whyst opasu suali wills endyo umyno tesan dsket chesv iaour dearf riend jessi cawho haspr omise dtoen suret heirs afede liver ystop reall yimus tcong ratul ateou rhost sinth eloca lscou tgrou psfor thema rvell ouswa yinwh ichth eyhav eorga nised ourtr avelt hough someo fthes couts haveb eenra therm oreso licit ousth anweh adexp ected askin grath eralo tofqu estio nsabo utour plans stopg enera llyih opeth eywer esati sfied witht heans werst hatwe gaveb utwef eelth atwea reatr iskof overs tayin gourw elcom esowe willr eturn tolon donon tuesd aysto peven thoug hweha veenj oyedo urtim etoge thert hereh asbee nsome debat eamon gtheg roupa boutw herew eshou ldvis itnex tstop there areso manyi ntere sting sites tovis italo ngthe majes ticth amese stuar ysowe haved ecide dtosp litin totwo group sstop ralfw illle adone party onato uroft heken tcoas twhil eiamv erymu chloo kingf orwar dtoex plori ngthe essex marsh essto pdole tmekn owify ouhav eabet terid eabut ihave beent oldth atcan ewdon sfift eenth centu rychu rchaf fords anele vatio nwith awide viewo fthes urrou nding lands inthi sothe rwise rathe rflat lands capea ndist hesit eofan other fasci natin gtowe rwhic hiwil lbece rtain toske tchfo ryous topfi nally ifyou havea nyfur therr eques tsfor speci ficin forma tiont henpe rhaps youco uldle aveam essag eform eatth epost offic ether estop karlm essag eends \n", "\n" ] } ], "source": [ "(word_b, wrap_b), score_b = keyword_break_mp(scb, fitness=Ptrigrams)\n", "print(word_b, wrap_b, '\\n')\n", "pb = keyword_decipher(cb, word_b, wrap_b)\n", "print(pb)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "Collapsed": "false" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dear uncle wilhelm as you promised we thoroughly enjoyed our visit to your cousin in lincoln and\n", "found it most informative stop the beautiful cathedral almost justified our visit on its own and our\n", "hosts shared with us some interesting drawings of the towers you mentioned stop they graciously\n", "allowed me to copy the sketches and explained much about how the towers are built and why stop as\n", "usual i will send you my notes and sketches via our dear friend jessica who has promised to ensure\n", "their safe delivery stop really i must congratulate our hosts in the local scout groups for the\n", "marvellous way in which they have organised our travel though some of the scouts have been rather\n", "more solicitous than we had expected asking rather alot of questions about our plans stop generally\n", "i hope they were satisfied with the answers that we gave but we feel that we are at risk of\n", "overstaying our welcome so we will return to london on tuesday stop eventhough we have enjoyed our\n", "time together there has been some debate among the group about where we should visit next stop there\n", "are so many interesting sites to visit along the majestic thames estuary so we have decided to split\n", "into two groups stop ralf will lead one party on at our of the kent coast while i am very much\n", "looking forward to exploring the essex marshes stop do let me know if you have a better idea but i\n", "have been told that ca new dons fifteenth century church afford san elevation with a wide view of\n", "the surrounding lands in this otherwise rather flat landscape and is the site of another fascinating\n", "tower which i will be certain to sketch for you stop finally if you have any further requests for\n", "specific information then perhaps you could leave a message for meat the post office there stop karl\n", "message ends\n" ] } ], "source": [ "pb = prettify(keyword_decipher(scb, word_b, wrap_b))\n", "print(pb)" ] }, { "cell_type": "code", "execution_count": 87, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "1783" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "open(plaintext_b_filename, 'w').write(pb)" ] }, { "cell_type": "code", "execution_count": 86, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "pb = \"\"\"dear uncle wilhelm as you promised we thoroughly enjoyed our visit to your cousin in lincoln and\n", "found it most informative stop the beautiful cathedral almost justified our visit on its own and our\n", "hosts shared with us some interesting drawings of the towers you mentioned stop they graciously\n", "allowed me to copy the sketches and explained much about how the towers are built and why stop as\n", "usual i will send you my notes and sketches via our dear friend jessica who has promised to ensure\n", "their safe delivery stop really i must congratulate our hosts in the local scout groups for the\n", "marvellous way in which they have organised our travel though some of the scouts have been rather\n", "more solicitous than we had expected asking rather a lot of questions about our plans stop generally\n", "i hope they were satisfied with the answers that we gave but we feel that we are at risk of\n", "overstaying our welcome so we will return to london on tuesday stop even though we have enjoyed our\n", "time together there has been some debate among the group about where we should visit next stop there\n", "are so many interesting sites to visit along the majestic thames estuary so we have decided to split\n", "into two groups stop ralf will lead one party on a tour of the kent coast while i am very much\n", "looking forward to exploring the essex marshes stop do let me know if you have a better idea but i\n", "have been told that canewdons fifteenth century church affords an elevation with a wide view of\n", "the surrounding lands in this otherwise rather flat landscape and is the site of another fascinating\n", "tower which i will be certain to sketch for you stop finally if you have any further requests for\n", "specific information then perhaps you could leave a message for me at the post office there stop karl\n", "message ends\"\"\"" ] }, { "cell_type": "code", "execution_count": 88, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "spb = sanitise(pb)" ] }, { "cell_type": "code", "execution_count": 89, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "['the',\n", " 'they',\n", " 'as',\n", " 'really',\n", " 'generally',\n", " 'even',\n", " 'there',\n", " 'ralf',\n", " 'do',\n", " 'finally',\n", " 'karl']" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pbs = pb.split()\n", "[pbs[i+1] for i, w in enumerate(pbs) if w == 'stop']" ] }, { "cell_type": "code", "execution_count": 106, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "'ttargetrdfk'" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pbs = pb.split()\n", "cat(pbs[i+1][0] for i, w in enumerate(pbs) if w == 'stop')" ] }, { "cell_type": "code", "execution_count": 90, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "'duwaypwteovtycilafim is tbcajovoioaohswusi dotty mst ga mtct sae mah ttab aws a uiwsymnasvodfjwhptet sds rim co hit lsgftmwiwthoottsotsh brms tw hear al oqaopsgihtwswtatwgbw ft waar ooo wsw wrt lot set wheottthbsdatgawwsvn st as mist vat mteswhdtsitgsrwlopoa to tk cwiavmlftetemsdlmkiy habibi hbttcfccaaewawvotsl it or fla its oaftwiwbctsfysfiyhaf rfs it py clam fm at pots k me'" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wcat(segment(cat(w[0] for w in pbs)))" ] }, { "cell_type": "code", "execution_count": 91, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "'en is or eh nui oo on i not on the a luu in twn uo his on rfhooethrleoohknxu boho run hts sie oy on kiuereharonhaeteuou on hoc roh a an hharurhofhcaeaooheax sa of ubulteoheaihnheauee her tif vue oeieoonutvheanuiohae oem hrb he hie thro an i oil hahsoeaeopnwrtaien an of he ohm eu ooo x hsa to eenfoaeduaeohaiehfnl ii if huan ht alan sh if naoh i eeo koot if oanueopnheooeeoeth of hta en'" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wcat(segment(cat(w[1] for w in pbs if len(w) > 1)))" ] }, { "cell_type": "code", "execution_count": 92, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "'sel pee teter eaedusnncrsryeufypu rhone li hr gr fees de tres ns gef we a hnnshyhstdneitaraeuf we top sxe god ghy miette fr any ed lfp soo to deeoyscegtosgyoeeptt deet peg een ser erde ehnpynnonleoergfkte et let eet sehdeyeiypsrtsftargd denser nesefehlrdeyhnyserst lens reti ypyereodsoadrrasdsyu d lil spy dtesewthddseyoedyypd use fsgeshdsrdnsntrdtlle pet tdd nnn rotr dyed us mer'" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wcat(segment(cat(w[-1] for w in reversed(pbs))))" ] }, { "cell_type": "code", "execution_count": 93, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "12" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max([len(w) for w in pbs])" ] }, { "cell_type": "code", "execution_count": 94, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "[21, 24, 19, 23, 45, 32, 22, 22, 24, 54, 24, 3]" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[len(s.strip().split()) for s in pb.split('stop')]" ] }, { "cell_type": "code", "execution_count": 95, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "'DeaR uncle wilhelm as you pRomiseD we thoRoughly enjoyeD ouR visit to youR cousin in lincoln anD FounD it most inFoRmative stop the beautiFul catheDRal almost justiFieD ouR visit on its own anD ouR hosts shaReD with us some inteResting DRawings oF the toweRs you mentioneD stop they gRaciously alloweD me to copy the sketches anD explaineD much about how the toweRs aRe built anD why stop as usual i will senD you my notes anD sketches via ouR DeaR FRienD jessica who has pRomiseD to ensuRe theiR saFe DeliveRy stop Really i must congRatulate ouR hosts in the local scout gRoups FoR the maRvellous way in which they have oRganiseD ouR tRavel though some oF the scouts have been RatheR moRe solicitous than we haD expecteD asking RatheR a lot oF questions about ouR plans stop geneRally i hope they weRe satisFieD with the answeRs that we gave but we Feel that we aRe at Risk oF oveRstaying ouR welcome so we will RetuRn to lonDon on tuesDay stop even though we have enjoyeD ouR time togetheR theRe has been some Debate among the gRoup about wheRe we shoulD visit next stop theRe aRe so many inteResting sites to visit along the majestic thames estuaRy so we have DeciDeD to split into two gRoups stop RalF will leaD one paRty on a touR oF the kent coast while i am veRy much looking FoRwaRD to exploRing the essex maRshes stop Do let me know iF you have a betteR iDea but i have been tolD that canewDons FiFteenth centuRy chuRch aFFoRDs an elevation with a wiDe view oF the suRRounDing lanDs in this otheRwise RatheR Flat lanDscape anD is the site oF anotheR Fascinating toweR which i will be ceRtain to sketch FoR you stop Finally iF you have any FuRtheR Requests FoR speciFic inFoRmation then peRhaps you coulD leave a message FoR me at the post oFFice theRe stop kaRl message enDs'" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tt = ''.maketrans('rdf', 'RDF')\n", "pbcap = wcat(pb.split()).translate(tt)\n", "spbcap = wcat(spb.split()).translate(tt)\n", "pbcap" ] }, { "cell_type": "code", "execution_count": 96, "metadata": { "Collapsed": "false" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Dea\n", "R uncle wilhelm as you p\n", "Romise\n", "D we tho\n", "Roughly enjoye\n", "D ou\n", "R visit to you\n", "R cousin in lincoln an\n", "D \n", "Foun\n", "D it most in\n", "Fo\n", "Rmative stop the beauti\n", "Ful cathe\n", "D\n", "Ral almost justi\n", "Fie\n", "D ou\n", "R visit on its own an\n", "D ou\n", "R hosts sha\n", "Re\n", "D with us some inte\n", "Resting \n", "D\n", "Rawings o\n", "F the towe\n", "Rs you mentione\n", "D stop they g\n", "Raciously allowe\n", "D me to copy the sketches an\n", "D explaine\n", "D much about how the towe\n", "Rs a\n", "Re built an\n", "D why stop as usual i will sen\n", "D you my notes an\n", "D sketches via ou\n", "R \n", "Dea\n", "R \n", "F\n", "Rien\n", "D jessica who has p\n", "Romise\n", "D to ensu\n", "Re thei\n", "R sa\n", "Fe \n", "Delive\n", "Ry stop \n", "Really i must cong\n", "Ratulate ou\n", "R hosts in the local scout g\n", "Roups \n", "Fo\n", "R the ma\n", "Rvellous way in which they have o\n", "Rganise\n", "D ou\n", "R t\n", "Ravel though some o\n", "F the scouts have been \n", "Rathe\n", "R mo\n", "Re solicitous than we ha\n", "D expecte\n", "D asking \n", "Rathe\n", "R a lot o\n", "F questions about ou\n", "R plans stop gene\n", "Rally i hope they we\n", "Re satis\n", "Fie\n", "D with the answe\n", "Rs that we gave but we \n", "Feel that we a\n", "Re at \n", "Risk o\n", "F ove\n", "Rstaying ou\n", "R welcome so we will \n", "Retu\n", "Rn to lon\n", "Don on tues\n", "Day stop even though we have enjoye\n", "D ou\n", "R time togethe\n", "R the\n", "Re has been some \n", "Debate among the g\n", "Roup about whe\n", "Re we shoul\n", "D visit next stop the\n", "Re a\n", "Re so many inte\n", "Resting sites to visit along the majestic thames estua\n", "Ry so we have \n", "Deci\n", "De\n", "D to split into two g\n", "Roups stop \n", "Ral\n", "F will lea\n", "D one pa\n", "Rty on a tou\n", "R o\n", "F the kent coast while i am ve\n", "Ry much looking \n", "Fo\n", "Rwa\n", "R\n", "D to explo\n", "Ring the essex ma\n", "Rshes stop \n", "Do let me know i\n", "F you have a bette\n", "R i\n", "Dea but i have been tol\n", "D that canew\n", "Dons \n", "Fi\n", "Fteenth centu\n", "Ry chu\n", "Rch a\n", "F\n", "Fo\n", "R\n", "Ds an elevation with a wi\n", "De view o\n", "F the su\n", "R\n", "Roun\n", "Ding lan\n", "Ds in this othe\n", "Rwise \n", "Rathe\n", "R \n", "Flat lan\n", "Dscape an\n", "D is the site o\n", "F anothe\n", "R \n", "Fascinating towe\n", "R which i will be ce\n", "Rtain to sketch \n", "Fo\n", "R you stop \n", "Finally i\n", "F you have any \n", "Fu\n", "Rthe\n", "R \n", "Requests \n", "Fo\n", "R speci\n", "Fic in\n", "Fo\n", "Rmation then pe\n", "Rhaps you coul\n", "D leave a message \n", "Fo\n", "R me at the post o\n", "F\n", "Fice the\n", "Re stop ka\n", "Rl message en\n", "Ds\n" ] } ], "source": [ "pbcap2 = cat(('\\n' + c) if c.isupper() else c for c in pbcap)\n", "print(pbcap2)" ] }, { "cell_type": "code", "execution_count": 97, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "'dearu nclew ilhel masyo uprom isedw ethor oughl yenjo yedou rvisi ttoyo urcou sinin linco lnand found itmos tinfo rmati vesto ptheb eauti fulca thedr alalm ostju stifi edour visit onits ownan dourh ostss hared withu ssome inter estin gdraw ingso fthet owers youme ntion edsto pthey graci ously allow edmet ocopy thesk etche sande xplai nedmu chabo uthow theto wersa rebui ltand whyst opasu suali wills endyo umyno tesan dsket chesv iaour dearf riend jessi cawho haspr omise dtoen suret heirs afede liver ystop reall yimus tcong ratul ateou rhost sinth eloca lscou tgrou psfor thema rvell ouswa yinwh ichth eyhav eorga nised ourtr avelt hough someo fthes couts haveb eenra therm oreso licit ousth anweh adexp ected askin grath eralo tofqu estio nsabo utour plans stopg enera llyih opeth eywer esati sfied witht heans werst hatwe gaveb utwef eelth atwea reatr iskof overs tayin gourw elcom esowe willr eturn tolon donon tuesd aysto peven thoug hweha veenj oyedo urtim etoge thert hereh asbee nsome debat eamon gtheg roupa boutw herew eshou ldvis itnex tstop there areso manyi ntere sting sites tovis italo ngthe majes ticth amese stuar ysowe haved ecide dtosp litin totwo group sstop ralfw illle adone party onato uroft heken tcoas twhil eiamv erymu chloo kingf orwar dtoex plori ngthe essex marsh essto pdole tmekn owify ouhav eabet terid eabut ihave beent oldth atcan ewdon sfift eenth centu rychu rchaf fords anele vatio nwith awide viewo fthes urrou nding lands inthi sothe rwise rathe rflat lands capea ndist hesit eofan other fasci natin gtowe rwhic hiwil lbece rtain toske tchfo ryous topfi nally ifyou havea nyfur therr eques tsfor speci ficin forma tiont henpe rhaps youco uldle aveam essag eform eatth epost offic ether estop karlm essag eends'" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wcat(chunks(spb, 5))" ] }, { "cell_type": "code", "execution_count": 98, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "'uwlomwrlouiounoddsoiobiarmuirtsnhsduernwotsenoyiywtykeeiuowoaidtuisoontvrfdiorentserplsgluthauuralahhvadrthossbamothhpdnhouoorsgahhridtstebfharfsnwmernnndongajometheetngawwusxpeoiegssoesheredepnoppweeyotnslvuofrxiexhoenyvtdtethnnthuufseoheosugsieeetsattnrinecleneosiyuarrsrinatesoemgmhtcrpmgs'" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cat(w[-1] for w in chunks(spb, 5))" ] }, { "cell_type": "code", "execution_count": 99, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "(87, 47, 35)" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "locR = [i for i, c in enumerate(spb) if c == 'r']\n", "locD = [i for i, c in enumerate(spb) if c == 'd']\n", "locF = [i for i, c in enumerate(spb) if c == 'f']\n", "len(locR), len(locD), len(locF)" ] }, { "cell_type": "code", "execution_count": 100, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "[(3, 586, 1169, 583),\n", " (34, 350, 666, 316),\n", " (50, 519, 988, 469),\n", " (124, 395, 666, 271),\n", " (163, 769, 1375, 606),\n", " (197, 337, 477, 140),\n", " (305, 337, 369, 32),\n", " (368, 769, 1170, 401),\n", " (370, 866, 1362, 496),\n", " (419, 830, 1241, 411),\n", " (440, 769, 1098, 329),\n", " (450, 798, 1146, 348),\n", " (485, 958, 1431, 473),\n", " (512, 830, 1148, 318),\n", " (522, 594, 666, 72),\n", " (558, 960, 1362, 402),\n", " (563, 866, 1169, 303),\n", " (976, 1173, 1370, 197),\n", " (1002, 1086, 1170, 84),\n", " (1011, 1193, 1375, 182),\n", " (1160, 1248, 1336, 88),\n", " (1165, 1256, 1347, 91)]" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "triples = []\n", "for r in locR:\n", " for d in locD:\n", " for f in locF:\n", " if d > r:\n", " if d - r == f - d:\n", " triples += [(r, d, f, d - r)]\n", "triples" ] }, { "cell_type": "code", "execution_count": 101, "metadata": { "Collapsed": "false" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dearunclewilhelma s youpromisedwet\n", "horoughlyenjoyedo u rvisittoyourco\n", "usininlincolnandf o unditmostinfor\n", "mativestopthebeau t ifulcathedrala\n", "lmostjustifiedour v isitonitsownan\n", "dourhostssharedwi t hussomeinteres\n", "tingdrawingsofthe t owersyoumentio\n", "nedstoptheygracio u slyallowedmeto\n", "copythesketchesan d explainedmucha\n", "bouthowthetowersa r ebuiltandwhyst\n", "opasusualiwillsen d youmynotesands\n", "ketchesviaourdear f riendjessicawh\n", "ohaspromisedtoens u retheirsafedel\n", "iverystopreallyim u stcongratulate\n", "ourhostsintheloca l scoutgroupsfor\n", "themarvellouswayi n whichtheyhaveo\n", "rganisedourtravel t houghsomeofthe\n", "scoutshavebeenrat h ermoresolicito\n", "usthanwehadexpect e daskingrathera\n", "lotofquestionsabo u tourplansstopg\n", "enerallyihopethey w eresatisfiedwi\n", "ththeanswersthatw e gavebutwefeelt\n", "hatweareatriskofo v erstayingourwe\n", "lcomesowewillretu r ntolondonontue\n", "sdaystopeventhoug h wehaveenjoyedo\n", "urtimetogetherthe r ehasbeensomede\n", "bateamongthegroup a boutwherewesho\n", "uldvisitnextstopt h erearesomanyin\n", "terestingsitestov i sitalongthemaj\n", "esticthamesestuar y sowehavedecide\n", "dtosplitintotwogr o upsstopralfwil\n", "lleadonepartyonat o urofthekentcoa\n", "stwhileiamverymuc h lookingforward\n", "toexploringtheess e xmarshesstopdo\n", "letmeknowifyouhav e abetterideabut\n", "ihavebeentoldthat c anewdonsfiftee\n", "nthcenturychurcha f fordsanelevati\n", "onwithawideviewof t hesurroundingl\n", "andsinthisotherwi s eratherflatlan\n", "dscapeandisthesit e ofanotherfasci\n", "natingtowerwhichi w illbecertainto\n", "sketchforyoustopf i nallyifyouhave\n", "anyfurtherrequest s forspecificinf\n", "ormationthenperha p syoucouldleave\n", "amessageformeatth e postofficether\n", "estopkarlmessagee n ds\n" ] } ], "source": [ "t = cat(c if c.isupper() else '.' for c in pbscap)\n", "col = 305 % 32\n", "tcs = chunks(spb, 32)\n", "stcs = [wcat([c[:col], c[col], c[col+1:]]) for c in tcs]\n", "print(lcat(stcs))" ] }, { "cell_type": "code", "execution_count": 102, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "780" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "results = []\n", "for l in range(1, 40):\n", " for i in range(l):\n", " cs = chunks(spb, l, fillvalue=' ')\n", " t = cat(c[i] for c in cs)\n", " f = Ptrigrams(t)\n", " results += [{'l': l, 'i': i, 't': t, 'f': f * l}]\n", "len(results)" ] }, { "cell_type": "code", "execution_count": 103, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "[{'l': 39,\n", " 'i': 37,\n", " 't': 'gnfsihalnpaseseasemerenhworttaragsods ',\n", " 'f': -5279.032049169729},\n", " {'l': 38,\n", " 'i': 35,\n", " 't': 'ocatteseoslsuteerefloatsoralycanaepsls ',\n", " 'f': -5213.417095225861},\n", " {'l': 39,\n", " 'i': 24,\n", " 't': 'mosdsepenjessoloitatmeegdemraceshotes ',\n", " 'f': -5197.69063368185},\n", " {'l': 1,\n", " 'i': 0,\n", " 't': 'dearunclewilhelmasyoupromisedwethoroughlyenjoyedourvisittoyourcousininlincolnandfounditmostinformativestopthebeautifulcathedralalmostjustifiedourvisitonitsownandourhostssharedwithussomeinterestingdrawingsofthetowersyoumentionedstoptheygraciouslyallowedmetocopythesketchesandexplainedmuchabouthowthetowersarebuiltandwhystopasusualiwillsendyoumynotesandsketchesviaourdearfriendjessicawhohaspromisedtoensuretheirsafedeliverystopreallyimustcongratulateourhostsinthelocalscoutgroupsforthemarvellouswayinwhichtheyhaveorganisedourtravelthoughsomeofthescoutshavebeenrathermoresolicitousthanwehadexpectedaskingratheralotofquestionsaboutourplansstopgenerallyihopetheyweresatisfiedwiththeanswersthatwegavebutwefeelthatweareatriskofoverstayingourwelcomesowewillreturntolondonontuesdaystopeventhoughwehaveenjoyedourtimetogethertherehasbeensomedebateamongthegroupaboutwhereweshouldvisitnextstoptherearesomanyinterestingsitestovisitalongthemajesticthamesestuarysowehavedecidedtosplitintotwogroupsstopralfwillleadonepartyonatourofthekentcoastwhileiamverymuchlookingforwardtoexploringtheessexmarshesstopdoletmeknowifyouhaveabetterideabutihavebeentoldthatcanewdonsfifteenthcenturychurchaffordsanelevationwithawideviewofthesurroundinglandsinthisotherwiseratherflatlandscapeandisthesiteofanotherfascinatingtowerwhichiwillbecertaintosketchforyoustopfinallyifyouhaveanyfurtherrequestsforspecificinformationthenperhapsyoucouldleaveamessageformeatthepostofficetherestopkarlmessageends',\n", " 'f': -4923.336122800165}]" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(sorted(results, key=lambda r: r['f']))[-4:]" ] }, { "cell_type": "code", "execution_count": 104, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rdfresults = [r for r in results if 'rdf' in r['t']]\n", "len(rdfresults)" ] }, { "cell_type": "code", "execution_count": 105, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "[{'l': 32,\n", " 'i': 17,\n", " 't': 'suotvttudrdfuulntheuwevrhrahiyooheecftsewispen',\n", " 'f': -6630.535098505313}]" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rdfresults" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [] } ], "metadata": { "jupytext": { "formats": "ipynb,md" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" } }, "nbformat": 4, "nbformat_minor": 4 }