Fictio builds configurable models from lists of training strings and generates random names from pre-build models. It can be used to generate random names for fictional characters, places, etc. on web apps.

Building the Model

The idea with build phase is to create a small model from a list of training strings. The strings can be anything, but the more they are similar to the expected output, the better.

For example, to build a simple 2-gram model from a single file containing one name per line, the following code can be used:

const cities500Names = dataFileLoader.load('cities500Names.txt')
const chain = new Builder(2)
  .from(cities500Names)
  .build()

It can also be used to build a more complex n-gram model and with more than one file for input. For example, to build a 4-gram model from more files:

const fictionalPlaceNames = new Builder(4)
  .from(cities500Names)
  // the second file is much smaller than the first so a weight is used to give it more importance
  .from(fictionalPlaceNames, 10000)
  .build()

Optimizing the Model Size

The model size is growing exponentially with n-grams parameter and depends also on the training data. For using on web, this can be a problem, so the model needs to be optimized for size.

The optimized model can be obtained like this:

const optimizedModel = ModelSizeOptimizer.optimize(model)

The generated model is just a typescript object that can be saved to a file and used later.

Note: the generated model is typescript code like export const cities500Names3 = {...}, however, the javascript part can be easily extracted. If this is later used in a build pipeline (transpiled, etc.) the size will be affected accordingly, you may wish to exclude it from such optimizations.

Generating Names

The generator uses the model optimized for size obtained in the previous step. Now that we have an optimized model, we can get to generate random names from a model:

const nameGenerator = new Generator(optimizedModel)
console.log(nameGenerator.next())

Example of Output

The repo contains two example files with training data, one with city names and one with fictional place names.

Examples of names in the training data :

  • Aereth
  • Dosadi
  • Halvmork

Examples of generated names:

  • Amiamar
  • Ellicon
  • Hooletaluna
  • Trishnu
  • Pyrrhia

But also, (albeit for lower n-grams):

  • Fureraywauiu 🤔
  • Acaskagnyeserwer 🥺
  • Kanggemervadebikaynieza 😵

For more details see the GitHub README.