markovifyR : R wrapper for Markovify

Ref: https://github.com/jsvine/markovify

"Markovify is a simple, extensible Markov chain generator. Right now, its main use is for building Markov models of large corpora of text, and generating random sentences from that."

This package requires Python and markovify to be installed.

To install markovify in R you can run:

system("pip install markovify")

The following functions are implemented:

Installation

devtools::install_github("abresler/markovifyR")
options(width=120)

Usage

library(markovifyR)
library(dplyr)

Generate New Peter Linneman "Life Lessons""

Here we are going to showcase how to use the package to create new Life Lessons from my favorite professor from college Peter Linneman.

Step 1 -- Build the Corpus

data("linneman_lessons")

lessons <-
  linneman_lessons %>% 
  pull(textLesson)

lessons %>% str()

Step 2 -- Build the Model

markov_model <-
  generate_markovify_model(
    input_text = lessons,
    markov_state_size = 2L,
    max_overlap_total = 25,
    max_overlap_ratio = .85
  )

Step 3 -- Generate the Text

markovify_text(
  markov_model = markov_model,
  maximum_sentence_length = NULL,
  output_column_name = 'textLinnemanBot',
  count = 25,
  tries = 100,
  only_distinct = TRUE,
  return_message = TRUE
)

Step 4 -- Other Features

Generate random sentence starting with.

markovify_text(
  markov_model = markov_model,
  maximum_sentence_length = NULL,
  start_words = c("The", "You", "Life"),
  output_column_name = 'textLinnemanBot',
  count = 25,
  tries = 100,
  only_distinct = TRUE,
  return_message = TRUE
)

Look at corpus start-words

generate_start_words(markov_model = markov_model)


abresler/markovifyR documentation built on July 19, 2023, 7:06 p.m.