set.seed(2026)
word_types <- c("<START>","the","corpus","analysis","shows",
"very","significant","results","<END>")
# Transition probability matrix: P(next word | current word)
transitions <- matrix(
c(
0.00,0.80,0.10,0.05,0.00,0.00,0.00,0.00,0.05, # <START>
0.00,0.00,0.40,0.35,0.00,0.00,0.00,0.00,0.25, # the
0.00,0.10,0.00,0.00,0.60,0.00,0.00,0.00,0.30, # corpus
0.00,0.00,0.00,0.00,0.70,0.00,0.00,0.00,0.30, # analysis
0.00,0.00,0.00,0.00,0.00,0.50,0.20,0.20,0.10, # shows
0.00,0.00,0.00,0.00,0.00,0.00,0.90,0.00,0.10, # very
0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.90,0.10, # significant
0.00,0.10,0.10,0.10,0.00,0.00,0.00,0.00,0.70, # results
0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,1.00 # <END>
),
nrow = length(word_types), byrow = TRUE,
dimnames = list(word_types, word_types)
)
generate_bigram_sentence <- function(trans, max_len = 20) {
words <- "<START>"
current <- "<START>"
repeat {
probs <- trans[current, ]
next_w <- sample(colnames(trans), 1, prob = probs)
if (next_w == "<END>" || length(words) > max_len) break
words <- c(words, next_w)
current <- next_w
}
paste(words[-1], collapse = " ")
}
bigram_sents <- replicate(10, generate_bigram_sentence(transitions))
for (i in seq_along(bigram_sents)) cat(sprintf(" %2d. %s\n", i, bigram_sents[i]))