Creative Writing for Children

Stories are used all over the world by parents and adults alike to spend quality time with their children. Moreover, stories can also help children to learn and improve their creativity, imagination, and linguistics. A way to improve this activity is by providing children an opportunity to develop their own stories. This will greatly enhance their skill in creative writing while also having fun learning it.

R with its text generation algorithm allows the development of the automatic text generator. This will help teachers and adults alike on self-creating a set of writing tasks to children. In this article, we will discuss a step-by-step on developing a text generator for creative writing activity using Markov Chain Algorithm in R. Further exploration of this project may lead to the development of an educational app to improve children’s creative writing.

Markov Chain Algorithm

Markov Chain is a mathematical model of stochastic process that predicts the condition of the next state (e.g. will it rain tomorrow?) based on the condition of the previous one. Using this principle, the Markov Chain can predict the next word based on the last word typed. It models the transition probability between states, where in NLP each state is represented by terms/words. Markov Chains is a simple yet effective method to create a text generation model1.

Libraries

We will be using the following packages for building text generator using Markov Chain algorithm:

Dataset

Markov Chain model will generate a text output as good as the text input. Therefore, we should use a decent literature in order to build a decent text generator. We will be using The Little Prince Corpus dataset from The AMR Bank. This corpus is an annotation of the novel The Little Prince by Antoine de Saint-Exupéry, published in 1943.

The above format is a simplified version of The Little Prince Corpus without metadata. We will be using only rows of sentences (labeled by index before the sentence).

Data Pre-processing

Like most of NLP tasks, data cleaning or preprocessing took a major part of the process. Below, we will perform several data cleaning steps below to obtain one file containing sequences of words from The Little Prince story.

#>  [1] "once"        "when"        "i"           "was"         "six"        
#>  [6] "years"       "old"         "i"           "saw"         "a"          
#> [11] "magnificent" "picture"     "in"          "a"           "book"       
#> [16] ","           "called"      "true"        "stories"     "from"       
#> [21] "nature"      ","           "about"       "the"         "primeval"   
#> [26] "forest"      "."

Once we have the cleaned data, we can continue to build the text generation model.

Model Fitting

We will use markovchainFit() from markovchain package. The function will take our data as a sequence of words and will learn the probability of occurence of the following word based on the previous word. The default setting will only take one word before (1-gram) for the calculation. Although we can also build 2 or 3 words before for higher predictive performace (for predictive text) but with bigger computation. Because we only aim to builda text generator for random initial words, we can use the 1-gram setting.

RCreate Text Generator

This function below will generate n-random words which can be used to initialize a sentence. Below is the description of the functions:

  1. num = number of random sentences to generate
  2. first_word = first word of each sentences, obtained from the vocabulary (data)
  3. n = number of random words per sentence

With a guidance from the teachers and caregivers, students may be given task to finish such senteces or to create a paragraph on specific topics. This allow students to develop creativity in story telling and writing.

#> [1] "I saw the flower"
#> [1] "I were some use"
#> [1] "I do not show"
#> [1] "I was a flower"
#> [1] "I could fly to"

For more guided challange, you may ask students to combine those uncomplete sentences with a set of random words. The function below will generate n-random words from the vocabulary. You can set the random number generator by setting an integer number in the seed parameter.

#>  [1] "telescope"   "ways"        "man"         "resemblance" "mechanic"   
#>  [6] "clad"        "perplexed"   "twentyeight" "force"       "stretch"

Students may finish or even improve the omitted words with their imagination. This allows students to practice their writing skills and develop their curiousity in vocabulary and grammar.

This model may be far from perfection for generating a sentence. But with better and a lot more data we may improve the performance of this model. For further discussion on text generation and text prediction using Markov Chain model, you can go to the following link. The article “Text Generation With Markov Chains” will discuss the logic behind Markov Chain model and demonstrate a more sophisticated usage of Markov Chain in Business Practice.

Happy learning and exploring!


  1. Adyatama, A. 2020. “Algotech: Text Generation With Markov Chains”. Published online April 2, 2020.