2 min read

Randomized Hobbit

@wrathematics pointed me to his ngram R package for constructing and simulating from n-grams from text.

I’d recently grabbed the text of the hobbit, and so I applied it to that text, with amusing results.

Here’s the code I used to grab the text.

library(XML)
stem <- "http://www.5novels.com/classics/u5688"
hobbit <- NULL
for(i in 1:74) {
```r
cat(i,"\n")
if(i==1) {
    url <- paste0(stem, ".html")
} else {
    url <- paste0(stem, "_", i, ".html")
}

x <- htmlTreeParse(url, useInternalNodes=TRUE)
xx <- xpathApply(x, "//p", xmlValue)
hobbit <- c(hobbit, gsub("\r", "", xx[-length(xx)]))
Sys.sleep(0.5)
```
}

Then calculate the ngrams with n=2.

library(ngram)
ng2 <- ngram(hobbit, n=2)

Simulate some number of words with babble(). If you use the seed argument, you can get the result reproducibly.

babble(ng2, 48, seed=53482175)
into trees, and then bore to the Mountain to go through?" groaned the hobbit. "Well, are you doing, And where are you doing, And where are you?" it squeaked, as it was no answer. They were surly and angry and puzzled at finding them here in their holes

Update: @wrathematics suggested that I mix two texts, so here’s a bit from the Hobbit in the Hat (The Hobbit with 59× Cat in the Hat – up-sampled to match lengths.) But there’s maybe not enough overlap between the two texts to get much of a mixture.

"I am Gandalf," said the fish. This is no way at all!
already off his horse and among the goblin and the dragon, who had remained behind to guard the door. "Something is outside!" Bilbo's heart jumped into his boat on to sharp rocks below; but there was a good game, Said our fish No! No! Those Things should not fly.