new NILOD(dbnullable)
The NILOD model constructor.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
db |
string | sqlite.Database |
<nullable> |
null | A filepath whereto load and save the NILOD model in the disk, or a SQLite database instance to use as the NILOD model's internal database. |
Methods
(static) loadSave(save, filepathnon-null, encodingopt) → (nullable) {NILOD}
Loads a save retrieved by NILOD#getSave. Unfortunately this
cannot be done without a file to store the save to (before opening
it with the database object).
Returns the loaded database. If there is already a file or directory at NILOD#loadSave.filepath, nothing will be done, and _null_ will be returned instead, to avoid damaging existing files.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
save |
string | Buffer | The save, usually a base64-encoded string (as NILOD#getSave usually returns saves encoded as base64). To use another encoding, or buffers, please see the [encoding] section. | ||
filepath |
string | The path to a file that can be created, to be used as the database file. Unfortunately, SQLite databases may not be loaded in any other way. | ||
encoding |
string |
<optional> |
base64 | The encoding of the save. Defaults to _'base64'_. |
Returns:
- Type
- NILOD
allEdgesBackward(nodeId) → {Promise.<Array.<Edge>>}
All edges that link _to_ the given node, from
some other node in the model's graph.
Parameters:
| Name | Type | Description |
|---|---|---|
nodeId |
string | The ID of the reference node in question. |
Returns:
- Type
- Promise.<Array.<Edge>>
allEdgesForward(nodeId) → {Promise.<Array.<Edge>>}
All edges that link _from_ the given node to
some other node in the model's graph.
Parameters:
| Name | Type | Description |
|---|---|---|
nodeId |
string | The ID of the reference node in question. |
Returns:
- Type
- Promise.<Array.<Edge>>
fetchEdges() → {Promise.<Array.<NILOD#Edge>>}
Fetchs a list of all the edges in the model's internal
SQLite graph.
Returns:
- Type
- Promise.<Array.<NILOD#Edge>>
fetchHistory() → {Promise.<Array.<NILOD#OutputHistory>>}
Fetches a list of output history from the internal
SQLite database.
Returns:
- Type
- Promise.<Array.<NILOD#OutputHistory>>
fetchNodes() → {Promise.<Array.<NILOD#Node>>}
Fetchs a list of all nodes from the internal SQL graph.
Returns:
- Type
- Promise.<Array.<NILOD#Node>>
findEdge(from, to) → {Promise.<?NILOD#Edge>}
Finds the particular edge between two nodes.
Returns null if no edge connects both nodes.
Parameters:
| Name | Type | Description |
|---|---|---|
from |
string | The ID of the node the edge stems *from*. |
to |
string | The ID of the node the edge ends in*to*. |
Returns:
- Type
- Promise.<?NILOD#Edge>
findEdgesBetween(word1, word2) → {Promise.<Array.<NILOD#Edge>>}
Finds all edges that link a word to another, regardless
of whichever nodes such words come from.
Parameters:
| Name | Type | Description |
|---|---|---|
word1 |
string | The 'from' word. |
word2 |
string | The 'to' word. |
Returns:
- Type
- Promise.<Array.<NILOD#Edge>>
generate(samples, config) → {NILOD#GenerationStatus}
Attempts to generate a good-looking sentence, by manufacturing
N random sentences, and giving each one a rating, which in turn is
predicted from previously generated sentences (output history).
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
samples |
integer | 30 | The number of samples (random sentences) to generate and rate. |
config |
object | A configuration object that can hold properties for the config arguments of both NILOD#tryOnce (for per-sample generation) and NILOD#rateText (for sample rating). Please see the respective methods' _config_ arguments for more details on such properties. |
Returns:
getNode(id) → {Promise.<?NILOD#Node>}
Retrieves a node by ID. Returns null if no node is found with the given ID.
Parameters:
| Name | Type | Description |
|---|---|---|
id |
string | The ID of this node in particular. |
Returns:
- Type
- Promise.<?NILOD#Node>
getSave()
Retrieves a base64 save of the SQLite database. Can be loaded using
the NILOD#loadSave 'static' member.
rateText(text, config) → {Promise.<number>}
Predicts a rating to a given sentence or text, based on output
history (and order in the tokenized text). This rating is the
resolve value of the returned Promise.
Parameters:
| Name | Type | Description |
|---|---|---|
text |
string | The text to rate. |
config |
object | The rating configuration |
Properties:
| Name | Type | Description |
|---|---|---|
wordSimilarity |
function | A word similarity function; a function that returns 1 for equal strings, and gradually lower numbers for gradually more different strings. Defaults to Levenshtein-based similarity. |
orderDistanceGradient |
function | A gradient function; computes the falloff of weight of a word, when compared with a word N tokens apart in the comparison sentence. Should be 1 for distance 0, and lower numbers (usually tending toward 0) for higher distances. |
Returns:
- Type
- Promise.<number>
train(sentences) → {Promise}
Trains this model's Markov-like generator graph. If you specify a filename to
its database, you may use an external sqlite database viewer, like the sqlite
command-line utility, to view (and even edit) the graph's data (nodes and
edges).
Returns a Promise that is resolved when training is finished.
Parameters:
| Name | Type | Description |
|---|---|---|
sentences |
Array.<string> | A list of sentences with the which to train the model's generator. |
Returns:
- Type
- Promise
tryOnce(configopt) → {Promise.<NILOD#SentenceAttempt>}
Generates a random sentence based on the
given Markov data.
Returns a Promise that is resolved once the sentence is generated, returning the Attempt.
Parameters:
| Name | Type | Attributes | Description | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
config |
Object |
<optional> |
The settings for this attempt.
Properties
|
Returns:
- Type
- Promise.<NILOD#SentenceAttempt>
Type Definitions
Edge
A graph edge, arguably the most vital part of the internal NILOD graph.
Type:
- Object
Properties:
| Name | Type | Description |
|---|---|---|
from |
string | The graph node the edge stems *from*. |
to |
string | The graph node the edge ends in*to*. |
fromWord |
string | The particular word of the graph node the edge stems from. |
toWord |
string | The particular word of the graph node the edge ends onto. |
separator |
string | The string that delimits the fromWord and toWord, i.e. the separator between both nodes as per this edge. |
count |
integer | The number of occurences of this particular edge that were found during training. Used as a probabilistic measure during sentence generation. |
GenerationStatus
Properties:
| Name | Type | Description |
|---|---|---|
best |
NILOD#OutputSample | The predicted best sample, out of the samples generated. |
worst |
NILOD#OutputSample | The predicted worst sample, out of the samples generated. _Laughable._ |
bestResult |
string | The predicted best sample's result sentence as a string. |
scoreboard |
Array.<NILOD#OutputSample> | A sorted list of samples, in descending order of rating. |
Node
A node from a NILOD model graph.
Nodes are simple structures that contain the bulk of
word data. Those words are not compressed by a
dictionary, but further redundancy is avoided via
the usage of edges.
Each item is a word. In NILOD, the array is always only
2 words long.
Type:
- Array.<string>
Properties:
| Name | Type | Description |
|---|---|---|
id |
string | The ID of this particular node. |
OutputHistory
A single sentence generated by the NILOD model at any point in time.
Type:
- Object
Properties:
| Name | Type | Description |
|---|---|---|
id |
string | The ID of this sentence in particular. Usually a mnemonic formed by common words joined by hyphens. |
data |
string | The text that forms this sentence. |
rating |
number | The average rating of this sentence, usually ranging from -1 to 1. |
rateCount |
number | The number of ratings. Used to average them into the NILOD#OutputHistory.rating property. |
seedID |
string | The ID of the node that seeded this particular sentence. |
OutputSample
Properties:
| Name | Type | Description |
|---|---|---|
rating |
number | The rating of this sample. |
attempt |
NILOD#SentenceAttempt | The attempt wrapped by this sample. |
SentenceAttempt
A sentence generated by the NILOD model.
Type:
- Object
Properties:
| Name | Type | Attributes | Description |
|---|---|---|---|
success |
boolean | Whether this attempt was successful (i.e. no errors). | |
seedNode |
string |
<nullable> |
The ID of the node that was used as the seed (i.e. the starting point for this sentence). |
seedText |
string |
<nullable> |
The string representation of the seed node (see above). |
seedQuery |
string |
<nullable> |
The original query from the which the node was deduced (or randomly picked). |
output |
string |
<nullable> |
The output of this query, or null if unsuccessful. |
outputWords |
Array.<string> | The words that were joined to compose the output property. | |
outputID |
string |
<nullable> |
The ID of this sentence generation, used when rating it. |
reasonCode |
string |
<nullable> |
An upper-case, shorthand string that allows for quick mnemonic identification and comparison of error codes. |
reasonMessage |
string |
<nullable> |
A human-readable reason describing the error. |
reasonNumber |
Number | An integer used to identify this particular reason. | |
applyRating |
function | A method that allows rating this particular generated sentence. Takes a single argument (a rating, usually between -1 and 1), and returns a Promise that is resolved when the underlying SQL database is updated accordingly. |