ngn4-compiler
v0.2.2
Published
Sample compiler for ngn4
Downloads
1
Readme
#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
The easiest way to install it 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.
Global installation should work in Windows if you installed node.js and npm with the Windows installer provided on the official website.
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.
5. Test the stuff
There are some testing commands included when installed globally.
ngn4-test
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. This has to be run from the same directory as the file is in.
Example: ngn4-test mygenerator.json 16 full_name female
ngn4-generate
To test the results of a compile, you can use the ngn4-generate
command. It loads the full set from a JSON file and generates one or more names according to parameters. Providing no parameters after the JSON filename will give you a list of the set's contents.
Example: ngn4-test myset.json mycat/mygenerator 100 first_name male
Feel free to try this command with ngn4.io's generator set, which can be acquired like this: curl --compressed http://api.ngn4.io/d/set >set.json
If you don't have curl, copying the URL into your browser and CTRL-S-ing the result works too.
Change Log
0.2.2
- Changed that line from the ngn4-generate command that required ES6 and thus
wouldn't work on older node.js versions
0.2.1
- Readme edits
0.2.0
- The version info (-V/--version) now exits the program like it should.
- ngn4-test now also works with compiled single generators. You can try it with
what the data given from http://api.ngn4.io/d/me/elcor
- Added a new bin, ngn4-generate, that can generate names from a compiled set.
0.1.0
- Initial release