Sophocles is a fun way to give your computer some creative writing lessons with AI. Current functionality enables it to write a (mostly nonsensical) story based on Sherlock Holmes, or write a short article on a topic of your choice based on two Wikipedia articles. It's built on @karpathy's vanilla RNN, min-char-rnn as well as on on @weixsong's documentation.


Minimal character-level language model with a Vanilla Recurrent Neural Network, in Python/numpy

Reference page The Unreasonable Effectiveness of Recurrent Neural Networks

Train RNN Model

  • Install numpy (basis for the CNN) pip install numpy
  • Install Wikipedia (package for parsing Wikipedia articles) pip install wikipedia
  • Install argparse if you don't already have it pip install argparse
  • Run this code, and follow the instructions onscreen python
  • If you want iterations to be printed to the terminal, use python --iter


  • If you type "novel" as an input to Sophocles, the script will use the Sherlock Holmes novel A Study in Scarlet to train its neural network, from the file stud.txt.

  • If you type "Wikipedia" as an input, you'll be asked to provide the names of three Wikipedia articles (spaces are allowed). Sophocles will scrape the intro section of each article and use these intros to train its neural network.

  • Advanced users only: If you want to use more of each Wikipedia article, or perhaps a different section of it, start by looking at how Wikipedia can be accessed through json.

Output of this model

The output of this model is a couple of text files, each containing 5000 characters of writing generated by the computer. The first is generated after 100k iterations and is called Sophocles100k.txt; the second is generated after 400k iterations and is called Sophocles400k.txt. Remember that if you want to keep these outputs for later, rename the files and/or move them out of this script's directory - otherwise they'll be overwritten!

If you wish to change the length of the output text, change the last number on this line in

      sample_ix = sample(hprev, inputs[0], 5000)

Built With

Share this project: