#ngn4-compiler
About
This is a sample compiler for ngn4 meant to turn human-editable sample libraries into an importable JSON file for the generator. Its Compiler class can be used by a submodule, but it's primarily meant as a standalone program to load files from a simple folder structure as depicted above.
categoryid
├── generatorid
│ ├── samplefile1.txt
│ ├── samplefile2.txt
│ ├── ...
│ └── samplefileN.txt
├── generatorid.json
└── category.json
While the name generator engine (ngn4) and this tool is open source, the sample library I use for ngn4.io is not. The reason for that is firstly that I would need to contact a lot of authors of fan-made content, and secondly that I don't know if there's any demand for it. If you decide to create such a repository, please send me an e-mail and I'll look into putting together a pull request with what I can share.
Setup
Linux
The easiest way to install it on a Linux system is sudo npm install -g ngn4-compiler
. See ngn4-compiler -h
command help. If it's not installed globally, use the node
command on the directory of this module or its index.js with the -d
parameter followed by a relative or absolute path to the sample library.
Windows
If you're regularly using Windows for node.js development, then I'm sure you'll have a better solution. If not, then get the package and its dependencies with npm, put node.js' executable in that folder, and you can run node . -d path/to/library
when cd'd to that directory with cmd.
Usage: Standalone
1. Category
It will look at subdirectories for categories and they need to contain a category.json
file. It looks something like this.
{
"id": "me",
"name": "Mass Effect",
"meta": {
}
}
2. Generator
In the same folder as the category.json
file, it expects to find other JSON files for each thing it should generate for (e.g. a race/species). Below is one such file.
{
"id": "elcor",
"name": "Elcor",
"genders": [
"Male"
],
"parts": {
"first": {
"algo": "grammar",
"options": {},
"list": "first_g",
"format": "Aa"
}
},
"formats": {
"full_name": {
"name": "Full Name",
"format": "{first}"
}
},
"meta": {
}
}
3. Samples
In a folder with the same name as the id
field on the above JSON file, it will load all text files for sample data. For example, I have a canon_g.txt
file in there with the following content. You can add content to multiple lists in one file if they all use the same loader.
$loader tokens
$list first_g
# Source: Games & Canon
$group SC V1 CL VL EC
C a l y n
B u b i n
$group SC V1 C2 V2 CL EV
P e t o z i
$group SC V1 DCL VL EC
H a rr o t
$group SC V1 CCL VL EC
X e lt a n
$group SCC V1 CCL DVL EC
Th u nl oo n
The naming convention for the groups can be anything of your choosing, but I recommend using a consistent pattern that can be derived from reading the tokens.
4. Rinse and repeat
Repeat the appropriate step to add multiple categories, generators and sample files.
The meta
field in the JSON files is for any arbitrary data you would want to associate with the generators. It has no effect on the name generation itself.
I add a g
and s
in the sample files and list names when they're tokenized for grammar
and syllables
respectively. The other two generator algorithms don't use tokenized samples. That's only for the sake of readability.
The results may end up being a megabyte or more, but it does compress well with gzip.
ngn4-test
If installed globally, there is an ngn4-test
command that you can use. It loads sample files from a sub-folder with name matching the id
field in the JSON file given as parameter, and generates a name with the results. There are also 3 optional parameters: amount, format and gender.
Example: ngn4-test mygenerator.json 16 full_name female