Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generating text with a markov chain in R.
library(markovchain)
text <- readLines('text.txt')
text <- text[nchar(text) > 0]
text <- gsub('.', ' .', text, fixed = TRUE)
text <- gsub(',', ' ,', text, fixed = TRUE)
text <- gsub('!', ' !', text, fixed = TRUE)
text <- gsub('(', '( ', text, fixed = TRUE)
text <- gsub(')', ' )', text, fixed = TRUE)
terms <- unlist(strsplit(text, ' '))
fit <- markovchainFit(data = terms)
plot(fit$estimate)
paste(markovchainSequence(n=50, markovchain=fit$estimate), collapse=' ')
#s <- createSequenceMatrix(terms, sanitize=FALSE)
#fit2 <- fitHigherOrder(s)
@hareentej2

This comment has been minimized.

Copy link

commented Apr 3, 2018

hello,
thanks for the contribution which is precise and effective . but can you please help me in understanding how the sequence of the title is starting from a particular term?
can we give a term as an input and start our sequence from there on wards??

@McCartneyAC

This comment has been minimized.

Copy link

commented Aug 26, 2018

you can work starting from a term by using code like this for your final step. t0 represents "term zero" or the initial term.

paste(markovchainSequence(n = 100, markovchain=update_fit$estimate, t0 = "start", include.t0 = TRUE ), collapse = ' ')

does anyone know how to change the depth so that it iterates over bi- or tri-grams rather than going discretely step by step? markovify in python seems to be able to do this but I can't figure out how to do it with markovchain in R.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.