Submit your answers to tleise@amherst.edu by Friday.

In this lab we will explore an application of the Metropolis-Hastings algorithm as an example of applying MCMC methods to decode a simple encryption scheme.

This material is adapted from Example 10.15 in Robert P. Dobrow’s book (www.people.carleton.edu/~dobrow/Probability/).

Adrbzf Fmysxbqdec Aykfrf (Simple Encryption Scheme)

We can code a message using a simple substitution cipher: each letter stands for a different letter in the alphabet, for example replace \(s\) with \(a\), \(i\) with \(d\), \(m\) with \(r\), etc, as was done in the title of this section. To decode a message, one could try all possible permutations of the alphabet (all the possible ciphers if we ignore all punctuation, numbers, and other symbols in the text), but that would require \(27!\approx10^{28}\) tests to see which yields a sensible answer, which is computationally infeasible.

A more efficient approach is to narrow down the likely candidates by noting that certain pairs of letters tend to occur with much higher frequency than others. If we know the typical transition probabilities of successive letter pairs, we can use that to help determine the cipher. For example, take a large set of writings (the “reference text”), count the number of times each possible letter pair occurs (including spaces, but ignoring all other punctuation). Dobrow used the complete works of Jane Austen to generate such a count, stored in a \(27\times 27\) matrix \(M\). For instance, the entry in the 1st row and 2nd column of \(M\) gives the number of times \(a\) was immediately followed by \(b\).

Step 1

Download the file AustenCount.txt which contains the counts of successive pairs of letters in the reference text, and save in the same folder as your R markdown file. Set the working directory in RStudio to this folder (using the Files tab or setwd(“pathtofolder”)), and then load the data matrix \(M\) into R:

M <- read.table("AustenCount.txt",header=F)
logM <- log(M + 1) # easier to compute product as sum of logs

Step 2

Download EncryptedMessage.txt and save to the same folder as the other files, and then read the encrypted message into RStudio:

fileName <- "EncryptedMessage.txt"
codemess <-readChar(fileName, file.info(fileName)$size)

For each coding function \(f\) mapping the coded letters back to the original letters, we can compute a score that quantifies how well the letter pair frequencies in the candidate decoded message (using \(f\) to decode) matches the observed frequencies, where \(N\) is the number of letters and spaces in the coded message \(c_1c_2\dots c_N\), treated as a string of characters: \[\text{score}(f)=\prod_{i=1}^{N-1}{M_{f(c_i),f(c_{i+1})}}.\] Coding functions \(f\) with high scores are good candidates for decryption because the score is higher when successive pair frequencies in the decoded message more closely match those in the reference text (think about this to make sure it makes sense to you).

We can define a probability distribution proportional to the scores as follows: \[\pi(f)=\frac{\text{score}(f)}{\sum_g{\text{score}(g)}}.\] We want to sample from \(\pi\), but the denominator in this expression has \(27!\) terms to sum, which sounds intractable. But we are saved by the fact that the Metropolis-Hastings formula involves a ratio \(\pi(f^\ast)/\pi(f)\), so the denominator cancels out.

Step 3

Working with the logarithm of the score is computationally easier. Define a function to calculate the log-score in R (later we apply exp to undo the log):

score <- function(code) {  # sum logs of frequencies of pairs 
  p <- 0
  for (i in 1:(nchar(code)-1)) {
    p <- p + logM[charIndex(substr(code, i, i)),charIndex(substr(code, i+1, i+1))]
  }
  p } # returns p=log(score(code))

Step 4

We also need some functions to convert letters and spaces into numbers, and to translate a coded message using a given code. The script ScriptForEncryptionLab.R with these functions should be downloaded from the course webpage and run in RStudio.

source("ScriptForEncryptionLab.R")

Metropolis algorithm

The Metropolis algorithm applied to this problem has the following steps:

  1. Start with any \(f\), say, the identify function that maps \(a\) to \(a\), \(b\) to \(b\), etc.

  2. As a convenient symmetric Markov chain to use here, pick two letters uniformly at random and switch the values that \(f\) assigns to these two symbols. Call this proposal state \(f^\ast\).

  3. Compute the acceptance function \[a(f,f^\ast)=\frac{\pi(f^\ast)}{\pi(f)}=\frac{\text{score}(f^\ast)}{\text{score}(f)}.\]

  4. Pick a uniformly distributed random number \(u\) between 0 and 1. If \(u\le a(f,f^\ast)\), accept \(f^\ast\); otherwise remain at \(f\).

This algorithm generates a Markov chain with equilibrium distribution \(\pi\). The \(f\) that will occur most frequently as the chain runs is the one with the largest value of \(\pi(f)\), which is the one with the greatest score. So we watch the chain for a sufficiently long time to see which \(f\) occurs the most often, and take it as the most likely coding function to decode the message.

We can monitor the chain as it runs, to see how well the current candidate for \(f\) does in decoding the message. We can stop the chain once the decoded message no longer has any gibberish.

Exercise 1

Run the R script to decipher the encrypted text. What is the decoded text?

curFunc <- 1:27 # uniform mapping
# calculate the score for curFunc
oldScore <- score(decrypt(codemess,curFunc))

# run 2000 iterations of the Metropolis-Hastings algorithm
for (iteration in 1:2500) {
  # sample two letters to swap (we didn't change spaces in this example)
  swaps <- sample(1:26,2) # leaving spaces fixed
  oldFunc <- curFunc
  
  # let curFunc be oldFunc but with two letters swapped
  curFunc[swaps[1]] <- oldFunc[swaps[2]]
  curFunc[swaps[2]] <- oldFunc[swaps[1]]
  
  newScore <- score(decrypt(codemess,curFunc))
  
  # decide whether to accept curFunc or to revert to oldFunc
  if (runif(1) > exp(newScore-oldScore)) {
    curFunc <- oldFunc
  } else  {
    oldScore <- newScore
  }
  
  # print out our decryption every 50 iterations
  if ((iteration %%  50) == 0) {
    print(iteration)
    print(decrypt(codemess,curFunc))
  }
}
## [1] 50
## [1] "lrob ive serg cehmttmth lrob ive lmryi bobeti m bag ajboyi yag ol bg afquamtiatfe nmiv gou gour battery mbpreyymth be nmiv ive lujjeyi cejmel ol gour arrohatfe gour fotfemi atx gour yejlmyv xmyxamt ol ive leejmthy ol oivery nere yufv ay io lorb ive hroutxnord ol xmyapprocaimot ot nvmfv yuffeexmth esetiy vase cumji yo mbbosacje a xmyjmde atx m vax toi dtont gou a botiv celore m leji ivai gou nere ive jayi bat mt ive norjx nvob m foujx eser ce presamjex ot io barrg"
## [1] 100
## [1] "lrot ive serh cegmnnmng lrot ive lmryi toteni m tah aftoyi yah ol th ajquamnianje bmiv hou hour tannery mtdreyymng te bmiv ive luffeyi cefmel ol hour arroganje hour jonjemi anx hour yeflmyv xmyxamn ol ive leefmngy ol oivery bere yujv ay io lort ive grounxborp ol xmyaddrocaimon on bvmjv yujjeexmng eseniy vase cumfi yo mttosacfe a xmyfmpe anx m vax noi pnobn hou a toniv celore m lefi ivai hou bere ive fayi tan mn ive borfx bvot m joufx eser ce dresamfex on io tarrh"
## [1] 150
## [1] "frot ide serh cexmnnmnx frot ide fmrli toteni m tah aytoli lah of th abquamnianbe jmid hou hour tannerl mtprellmnx te jmid ide fuyyeli ceymef of hour arroxanbe hour bonbemi ang hour leyfmld gmlgamn of ide feeymnxl of oiderl jere lubd al io fort ide xroungjorv of gmlapprocaimon on jdmbd lubbeegmnx esenil dase cumyi lo mttosacye a gmlymve ang m dag noi vnojn hou a tonid cefore m feyi idai hou jere ide yali tan mn ide joryg jdot m bouyg eser ce presamyeg on io tarrh"
## [1] 200
## [1] "frot ide cerh sexannanx frot ide farli toteni a tuh uytoli luh of th ubqmuaniunbe jaid hom homr tunnerl atprellanx te jaid ide fmyyeli seyaef of homr urroxunbe homr bonbeai ung homr leyfald galguan of ide feeyanxl of oiderl jere lmbd ul io fort ide xromngjork of galupprosuiaon on jdabd lmbbeeganx ecenil duce smayi lo attocusye u galyake ung a dug noi knojn hom u tonid sefore a feyi idui hom jere ide yuli tun an ide joryg jdot a bomyg ecer se precuayeg on io turrh"
## [1] 250
## [1] "frot ise jerh dexannanx frot ise farli toteni a tuh uptoli luh of th uyqmuaniunye bais hom homr tunnerl atcrellanx te bais ise fmppeli depaef of homr urroxunye homr yonyeai ung homr lepfals galguan of ise feepanxl of oiserl bere lmys ul io fort ise xromngbork of galuccroduiaon on bsays lmyyeeganx ejenil suje dmapi lo attojudpe u galpake ung a sug noi knobn hom u tonis defore a fepi isui hom bere ise puli tun an ise borpg bsot a yompg ejer de crejuapeg on io turrh"
## [1] 300
## [1] "fros ite jerd bexannanx fros ite farli soseni a sud upsoli lud of sd uyqmuaniunye hait dom domr sunnerl ascrellanx se hait ite fmppeli bepaef of domr urroxunye domr yonyeai ung domr lepfalt galguan of ite feepanxl of oiterl here lmyt ul io fors ite xromnghork of galuccrobuiaon on htayt lmyyeeganx ejenil tuje bmapi lo assojubpe u galpake ung a tug noi knohn dom u sonit before a fepi itui dom here ite puli sun an ite horpg htos a yompg ejer be crejuapeg on io surrd"
## [1] 350
## [1] "fros ite kerd bexannanx fros ite farli soseni a sud upsoli lud of sd uyqmuaniunye hait dom domr sunnerl ascrellanx se hait ite fmppeli bepaef of domr urroxunye domr yonyeai ung domr lepfalt galguan of ite feepanxl of oiterl here lmyt ul io fors ite xromnghorw of galuccrobuiaon on htayt lmyyeeganx ekenil tuke bmapi lo assokubpe u galpawe ung a tug noi wnohn dom u sonit before a fepi itui dom here ite puli sun an ite horpg htos a yompg eker be crekuapeg on io surrd"
## [1] 400
## [1] "fros ite kerp bexannanx fros ite farli soseni a sup udsoli lup of sp uyqmuaniunye hait pom pomr sunnerl ascrellanx se hait ite fmddeli bedaef of pomr urroxunye pomr yonyeai ung pomr ledfalt galguan of ite feedanxl of oiterl here lmyt ul io fors ite xromnghorw of galuccrobuiaon on htayt lmyyeeganx ekenil tuke bmadi lo assokubde u galdawe ung a tug noi wnohn pom u sonit before a fedi itui pom here ite duli sun an ite hordg htos a yomdg eker be crekuadeg on io surrp"
## [1] 450
## [1] "fros ite kerp bexannanx fros ite fardi soseni a sup ulsodi dup of sp uyvmuaniunye hait pom pomr sunnerd ascreddanx se hait ite fmlledi belaef of pomr urroxunye pomr yonyeai ung pomr delfadt gadguan of ite feelanxd of oiterd here dmyt ud io fors ite xromnghorw of gaduccrobuiaon on htayt dmyyeeganx ekenid tuke bmali do assokuble u gadlawe ung a tug noi wnohn pom u sonit before a feli itui pom here ite ludi sun an ite horlg htos a yomlg eker be crekualeg on io surrp"
## [1] 500
## [1] "fros ite kerp bexannanx fros ite fardi soseni a sup ulsodi dup of sp uyzmuaniunye hait pom pomr sunnerd ascreddanx se hait ite fmlledi belaef of pomr urroxunye pomr yonyeai ung pomr delfadt gadguan of ite feelanxd of oiterd here dmyt ud io fors ite xromnghorw of gaduccrobuiaon on htayt dmyyeeganx ekenid tuke bmali do assokuble u gadlawe ung a tug noi wnohn pom u sonit before a feli itui pom here ite ludi sun an ite horlg htos a yomlg eker be crekualeg on io surrp"
## [1] 550
## [1] "fros ite xerp bekannank fros ite fardi soseni a sup ulsodi dup of sp uyzmuaniunye hait pom pomr sunnerd ascreddank se hait ite fmlledi belaef of pomr urrokunye pomr yonyeai ung pomr delfadt gadguan of ite feelankd of oiterd here dmyt ud io fors ite kromnghorw of gaduccrobuiaon on htayt dmyyeegank exenid tuxe bmali do assoxuble u gadlawe ung a tug noi wnohn pom u sonit before a feli itui pom here ite ludi sun an ite horlg htos a yomlg exer be crexualeg on io surrp"
## [1] 600
## [1] "fros ite jerp bekannank fros ite fardi soseni a sup ulsodi dup of sp uyzmuaniunye hait pom pomr sunnerd ascreddank se hait ite fmlledi belaef of pomr urrokunye pomr yonyeai ung pomr delfadt gadguan of ite feelankd of oiterd here dmyt ud io fors ite kromnghorw of gaduccrobuiaon on htayt dmyyeegank ejenid tuje bmali do assojuble u gadlawe ung a tug noi wnohn pom u sonit before a feli itui pom here ite ludi sun an ite horlg htos a yomlg ejer be crejualeg on io surrp"
## [1] 650
## [1] "fros ite jery bekannank fros ite fardi soseni a suy ulsodi duy of sy upxmuaniunpe hait yom yomr sunnerd ascreddank se hait ite fmlledi belaef of yomr urrokunpe yomr ponpeai ung yomr delfadt gadguan of ite feelankd of oiterd here dmpt ud io fors ite kromnghorw of gaduccrobuiaon on htapt dmppeegank ejenid tuje bmali do assojuble u gadlawe ung a tug noi wnohn yom u sonit before a feli itui yom here ite ludi sun an ite horlg htos a pomlg ejer be crejualeg on io surry"
## [1] 700
## [1] "fros ite jery bekannank fros ite fardi soseni a suy ulsodi duy of sy upxmuaniunpe hait yom yomr sunnerd ascreddank se hait ite fmlledi belaef of yomr urrokunpe yomr ponpeai ung yomr delfadt gadguan of ite feelankd of oiterd here dmpt ud io fors ite kromnghorw of gaduccrobuiaon on htapt dmppeegank ejenid tuje bmali do assojuble u gadlawe ung a tug noi wnohn yom u sonit before a feli itui yom here ite ludi sun an ite horlg htos a pomlg ejer be crejualeg on io surry"
## [1] 750
## [1] "fros ite jery bekannank fros ite fardi soseni a suy ulsodi duy of sy upqmuaniunpe hait yom yomr sunnerd ascreddank se hait ite fmlledi belaef of yomr urrokunpe yomr ponpeai ung yomr delfadt gadguan of ite feelankd of oiterd here dmpt ud io fors ite kromnghorw of gaduccrobuiaon on htapt dmppeegank ejenid tuje bmali do assojuble u gadlawe ung a tug noi wnohn yom u sonit before a feli itui yom here ite ludi sun an ite horlg htos a pomlg ejer be crejualeg on io surry"
## [1] 800
## [1] "mrod ite very bekannank mrod ite marsi dodeni a duy uldosi suy om dy ucqfuaniunce hait yof yofr dunners adpressank de hait ite mfllesi belaem om yofr urrokunce yofr conceai ung yofr selmast gasguan om ite meelanks om oiters here sfct us io mord ite krofnghorw om gasupprobuiaon on htact sfcceegank evenis tuve bfali so addovuble u gaslawe ung a tug noi wnohn yof u donit bemore a meli itui yof here ite lusi dun an ite horlg htod a coflg ever be prevualeg on io durry"
## [1] 850
## [1] "mrod ite very bekannank mrod ite marsi dodeni a duy uldosi suy om dy ucqfuaniunce hait yof yofr dunners adpressank de hait ite mfllesi belaem om yofr urrokunce yofr conceai ung yofr selmast gasguan om ite meelanks om oiters here sfct us io mord ite krofnghorw om gasupprobuiaon on htact sfcceegank evenis tuve bfali so addovuble u gaslawe ung a tug noi wnohn yof u donit bemore a meli itui yof here ite lusi dun an ite horlg htod a coflg ever be prevualeg on io durry"
## [1] 900
## [1] "mrod ite very bekannank mrod ite marsi dodeni a duy uldosi suy om dy ucqfuaniunce hait yof yofr dunners adpressank de hait ite mfllesi belaem om yofr urrokunce yofr conceai ung yofr selmast gasguan om ite meelanks om oiters here sfct us io mord ite krofnghorw om gasupprobuiaon on htact sfcceegank evenis tuve bfali so addovuble u gaslawe ung a tug noi wnohn yof u donit bemore a meli itui yof here ite lusi dun an ite horlg htod a coflg ever be prevualeg on io durry"
## [1] 950
## [1] "mrod tie very bewannanw mrod tie marst dodent a duy uldost suy om dy ucqfuantunce hati yof yofr dunners adpressanw de hati tie mfllest belaem om yofr urrowunce yofr conceat ung yofr selmasi gasguan om tie meelanws om otiers here sfci us to mord tie wrofnghork om gasupprobutaon on hiaci sfcceeganw events iuve bfalt so addovuble u gaslake ung a iug not knohn yof u donti bemore a melt tiut yof here tie lust dun an tie horlg hiod a coflg ever be prevualeg on to durry"
## [1] 1000
## [1] "mrod tie very bewannanw mrod tie marst dodent a duy uldost suy om dy ucqfuantunce hati yof yofr dunners adpressanw de hati tie mfllest belaem om yofr urrowunce yofr conceat ung yofr selmasi gasguan om tie meelanws om otiers here sfci us to mord tie wrofnghork om gasupprobutaon on hiaci sfcceeganw events iuve bfalt so addovuble u gaslake ung a iug not knohn yof u donti bemore a melt tiut yof here tie lust dun an tie horlg hiod a coflg ever be prevualeg on to durry"
## [1] 1050
## [1] "mrod tie very bewannanw mrod tie marst dodent a duy uldost suy om dy ucqfuantunce hati yof yofr dunners adpressanw de hati tie mfllest belaem om yofr urrowunce yofr conceat ung yofr selmasi gasguan om tie meelanws om otiers here sfci us to mord tie wrofnghork om gasupprobutaon on hiaci sfcceeganw events iuve bfalt so addovuble u gaslake ung a iug not knohn yof u donti bemore a melt tiut yof here tie lust dun an tie horlg hiod a coflg ever be prevualeg on to durry"
## [1] 1100
## [1] "drom tie very bewannanw drom tie darst moment a muy ulmost suy od my ucqfuantunce hati yof yofr munners ampressanw me hati tie dfllest belaed od yofr urrowunce yofr conceat ung yofr seldasi gasguan od tie deelanws od otiers here sfci us to dorm tie wrofnghork od gasupprobutaon on hiaci sfcceeganw events iuve bfalt so ammovuble u gaslake ung a iug not knohn yof u monti bedore a delt tiut yof here tie lust mun an tie horlg hiom a coflg ever be prevualeg on to murry"
## [1] 1150
## [1] "drom tie very bewannanw drom tie darst moment a muy ulmost suy od my ucqfuantunce hati yof yofr munners ampressanw me hati tie dfllest belaed od yofr urrowunce yofr conceat ung yofr seldasi gasguan od tie deelanws od otiers here sfci us to dorm tie wrofnghork od gasupprobutaon on hiaci sfcceeganw events iuve bfalt so ammovuble u gaslake ung a iug not knohn yof u monti bedore a delt tiut yof here tie lust mun an tie horlg hiom a coflg ever be prevualeg on to murry"
## [1] 1200
## [1] "drom the very bewannanw drom the darst moment a muy ulmost suy od my ucqfuantunce iath yof yofr munners ampressanw me iath the dfllest belaed od yofr urrowunce yofr conceat ung yofr seldash gasguan od the deelanws od others iere sfch us to dorm the wrofngiork od gasupprobutaon on ihach sfcceeganw events huve bfalt so ammovuble u gaslake ung a hug not knoin yof u month bedore a delt thut yof iere the lust mun an the iorlg ihom a coflg ever be prevualeg on to murry"
## [1] 1250
## [1] "drom the very bewannanw drom the darst moment a muy ulmost suy od my ucqfuantunce iath yof yofr munners ampressanw me iath the dfllest belaed od yofr urrowunce yofr conceat ung yofr seldash gasguan od the deelanws od others iere sfch us to dorm the wrofngiork od gasupprobutaon on ihach sfcceeganw events huve bfalt so ammovuble u gaslake ung a hug not knoin yof u month bedore a delt thut yof iere the lust mun an the iorlg ihom a coflg ever be prevualeg on to murry"
## [1] 1300
## [1] "drom the very bewannanw drom the darst moment a muy ulmost suy od my ucqfuantunce iath yof yofr munners ampressanw me iath the dfllest belaed od yofr urrowunce yofr conceat ung yofr seldash gasguan od the deelanws od others iere sfch us to dorm the wrofngiork od gasupprobutaon on ihach sfcceeganw events huve bfalt so ammovuble u gaslake ung a hug not knoin yof u month bedore a delt thut yof iere the lust mun an the iorlg ihom a coflg ever be prevualeg on to murry"
## [1] 1350
## [1] "drom the very bewannanw drom the darst moment a muy ulmost suy od my ucqfuantunce iath yof yofr munners ampressanw me iath the dfllest belaed od yofr urrowunce yofr conceat ung yofr seldash gasguan od the deelanws od others iere sfch us to dorm the wrofngiork od gasupprobutaon on ihach sfcceeganw events huve bfalt so ammovuble u gaslake ung a hug not knoin yof u month bedore a delt thut yof iere the lust mun an the iorlg ihom a coflg ever be prevualeg on to murry"
## [1] 1400
## [1] "drom the very beiannani drom the darst moment a muy ulmost suy od my ucqfuantunce wath yof yofr munners ampressani me wath the dfllest belaed od yofr urroiunce yofr conceat ung yofr seldash gasguan od the deelanis od others were sfch us to dorm the irofngwork od gasupprobutaon on whach sfcceegani events huve bfalt so ammovuble u gaslake ung a hug not known yof u month bedore a delt thut yof were the lust mun an the worlg whom a coflg ever be prevualeg on to murry"
## [1] 1450
## [1] "drom the very beainnina drom the dirst moment i muy ulmost suy od my ucqfuintunce with yof yofr munners impressina me with the dfllest belied od yofr urroaunce yofr conceit ung yofr seldish gisguin od the deelinas od others were sfch us to dorm the arofngwork od gisupprobution on which sfcceegina events huve bfilt so immovuble u gislike ung i hug not known yof u month bedore i delt thut yof were the lust mun in the worlg whom i coflg ever be prevuileg on to murry"
## [1] 1500
## [1] "drom the very befinninf drom the dirst moment i muy ulmost suy od my uczauintunce with yoa yoar munners impressinf me with the dallest belied od yoar urrofunce yoar conceit ung yoar seldish gisguin od the deelinfs od others were sach us to dorm the froangwork od gisupprobution on which sacceeginf events huve bailt so immovuble u gislike ung i hug not known yoa u month bedore i delt thut yoa were the lust mun in the worlg whom i coalg ever be prevuileg on to murry"
## [1] 1550
## [1] "drom the very befinninf drom the dirst moment i muy ulmost suy od my uczauintunce with yoa yoar munners impressinf me with the dallest belied od yoar urrofunce yoar conceit ung yoar seldish gisguin od the deelinfs od others were sach us to dorm the froangwork od gisupprobution on which sacceeginf events huve bailt so immovuble u gislike ung i hug not known yoa u month bedore i delt thut yoa were the lust mun in the worlg whom i coalg ever be prevuileg on to murry"
## [1] 1600
## [1] "drom the very befinninf drom the dirst moment i muy ulmost suy od my uczauintunce with yoa yoar munners impressinf me with the dallest belied od yoar urrofunce yoar conceit ung yoar seldish gisguin od the deelinfs od others were sach us to dorm the froangwork od gisupprobution on which sacceeginf events huve bailt so immovuble u gislike ung i hug not known yoa u month bedore i delt thut yoa were the lust mun in the worlg whom i coalg ever be prevuileg on to murry"
## [1] 1650
## [1] "drom the very befinninf drom the dirst moment i muy ulmost suy od my uczauintunce with yoa yoar munners impressinf me with the dallest belied od yoar urrofunce yoar conceit ung yoar seldish gisguin od the deelinfs od others were sach us to dorm the froangwork od gisupprobution on which sacceeginf events huve bailt so immovuble u gislike ung i hug not known yoa u month bedore i delt thut yoa were the lust mun in the worlg whom i coalg ever be prevuileg on to murry"
## [1] 1700
## [1] "drom the very befinninf drom the dirst moment i muy ulmost suy od my uczauintunce with yoa yoar munners impressinf me with the dallest belied od yoar urrofunce yoar conceit ung yoar seldish gisguin od the deelinfs od others were sach us to dorm the froangwork od gisupprobution on which sacceeginf events huve bailt so immovuble u gislike ung i hug not known yoa u month bedore i delt thut yoa were the lust mun in the worlg whom i coalg ever be prevuileg on to murry"
## [1] 1750
## [1] "drom the very befinninf drom the dirst moment i muy ulmost suy od my uczauintunce with yoa yoar munners impressinf me with the dallest belied od yoar urrofunce yoar conceit ung yoar seldish gisguin od the deelinfs od others were sach us to dorm the froangwork od gisupprobution on which sacceeginf events huve bailt so immovuble u gislike ung i hug not known yoa u month bedore i delt thut yoa were the lust mun in the worlg whom i coalg ever be prevuileg on to murry"
## [1] 1800
## [1] "drom the very beginning drom the dirst moment i muy ulmost suy od my uczauintunce with yoa yoar munners impressing me with the dallest belied od yoar urrogunce yoar conceit unf yoar seldish fisfuin od the deelings od others were sach us to dorm the groanfwork od fisupprobution on which sacceefing events huve bailt so immovuble u fislike unf i huf not known yoa u month bedore i delt thut yoa were the lust mun in the worlf whom i coalf ever be prevuilef on to murry"
## [1] 1850
## [1] "drom the very beginning drom the dirst moment i may almost say od my aczuaintance with you your manners impressing me with the dullest belied od your arrogance your conceit anf your seldish fisfain od the deelings od others were such as to dorm the grounfwork od fisapprobation on which succeefing events have built so immovable a fislike anf i haf not known you a month bedore i delt that you were the last man in the worlf whom i coulf ever be prevailef on to marry"
## [1] 1900
## [1] "drom the very beginning drom the dirst moment i may almost say od my aczuaintance with you your manners impressing me with the dullest belied od your arrogance your conceit anf your seldish fisfain od the deelings od others were such as to dorm the grounfwork od fisapprobation on which succeefing events have built so immovable a fislike anf i haf not known you a month bedore i delt that you were the last man in the worlf whom i coulf ever be prevailef on to marry"
## [1] 1950
## [1] "drom the very beginning drom the dirst moment i may almost say od my aczuaintance with you your manners impressing me with the dullest belied od your arrogance your conceit anf your seldish fisfain od the deelings od others were such as to dorm the grounfwork od fisapprobation on which succeefing events have built so immovable a fislike anf i haf not known you a month bedore i delt that you were the last man in the worlf whom i coulf ever be prevailef on to marry"
## [1] 2000
## [1] "from the very beginning from the first moment i may almost say of my aczuaintance with you your manners impressing me with the fullest belief of your arrogance your conceit and your selfish disdain of the feelings of others were such as to form the groundwork of disapprobation on which succeeding events have built so immovable a dislike and i had not known you a month before i felt that you were the last man in the world whom i could ever be prevailed on to marry"
## [1] 2050
## [1] "from the very beginning from the first moment i may almost say of my acjuaintance with you your manners impressing me with the fullest belief of your arrogance your conceit and your selfish disdain of the feelings of others were such as to form the groundwork of disapprobation on which succeeding events have built so immovable a dislike and i had not known you a month before i felt that you were the last man in the world whom i could ever be prevailed on to marry"
## [1] 2100
## [1] "from the very beginning from the first moment i may almost say of my acjuaintance with you your manners impressing me with the fullest belief of your arrogance your conceit and your selfish disdain of the feelings of others were such as to form the groundwork of disapprobation on which succeeding events have built so immovable a dislike and i had not known you a month before i felt that you were the last man in the world whom i could ever be prevailed on to marry"
## [1] 2150
## [1] "from the very beginning from the first moment i may almost say of my acjuaintance with you your manners impressing me with the fullest belief of your arrogance your conceit and your selfish disdain of the feelings of others were such as to form the groundwork of disapprobation on which succeeding events have built so immovable a dislike and i had not known you a month before i felt that you were the last man in the world whom i could ever be prevailed on to marry"
## [1] 2200
## [1] "from the very beginning from the first moment i may almost say of my acquaintance with you your manners impressing me with the fullest belief of your arrogance your conceit and your selfish disdain of the feelings of others were such as to form the groundwork of disapprobation on which succeeding events have built so immovable a dislike and i had not known you a month before i felt that you were the last man in the world whom i could ever be prevailed on to marry"
## [1] 2250
## [1] "from the very beginning from the first moment i may almost say of my acquaintance with you your manners impressing me with the fullest belief of your arrogance your conceit and your selfish disdain of the feelings of others were such as to form the groundwork of disapprobation on which succeeding events have built so immovable a dislike and i had not known you a month before i felt that you were the last man in the world whom i could ever be prevailed on to marry"
## [1] 2300
## [1] "from the very beginning from the first moment i may almost say of my acquaintance with you your manners impressing me with the fullest belief of your arrogance your conceit and your selfish disdain of the feelings of others were such as to form the groundwork of disapprobation on which succeeding events have built so immovable a dislike and i had not known you a month before i felt that you were the last man in the world whom i could ever be prevailed on to marry"
## [1] 2350
## [1] "from the very beginning from the first moment i may almost say of my acquaintance with you your manners impressing me with the fullest belief of your arrogance your conceit and your selfish disdain of the feelings of others were such as to form the groundwork of disapprobation on which succeeding events have built so immovable a dislike and i had not known you a month before i felt that you were the last man in the world whom i could ever be prevailed on to marry"
## [1] 2400
## [1] "from the very beginning from the first moment i may almost say of my acquaintance with you your manners impressing me with the fullest belief of your arrogance your conceit and your selfish disdain of the feelings of others were such as to form the groundwork of disapprobation on which succeeding events have built so immovable a dislike and i had not known you a month before i felt that you were the last man in the world whom i could ever be prevailed on to marry"
## [1] 2450
## [1] "from the very beginning from the first moment i may almost say of my acquaintance with you your manners impressing me with the fullest belief of your arrogance your conceit and your selfish disdain of the feelings of others were such as to form the groundwork of disapprobation on which succeeding events have built so immovable a dislike and i had not known you a month before i felt that you were the last man in the world whom i could ever be prevailed on to marry"
## [1] 2500
## [1] "from the very beginning from the first moment i may almost say of my acquaintance with you your manners impressing me with the fullest belief of your arrogance your conceit and your selfish disdain of the feelings of others were such as to form the groundwork of disapprobation on which succeeding events have built so immovable a dislike and i had not known you a month before i felt that you were the last man in the world whom i could ever be prevailed on to marry"

Exercise 2

Experiment with a text snippet of your choosing. For instance, how well does this method perform if the text is rather short, only a few words? Or from a different writing style than Austen?

Here is an example showing how to create an encrypted message with a randomly generated cipher:

message <- "life seems but a quick succession of busy nothings"
cipher <- sample(1:26)
codemess <- decrypt(message,cipher)
codemess
## [1] "yhgz fzznf bcr v qchoa fcoozffhdx dg bcfi xdrthxuf"

Your message needs to be in all lower case without any punctuation or numbers.