Constructor
new NilodModel(jGraphopt)
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
jGraph |
NilodGraph | string |
<optional> |
null | A NILOD model save to be loaded. Omit for an empty model. |
Members
_obj :NilodGraph
The internal graph structure, which can
be used to export a JSON save.
Type:
edges :Object.<string, NDEdge>
The edges, where the keys are the edges' IDs.
Type:
- Object.<string, NDEdge>
nodes :Object.<string, NDNode>
The nodes, where the keys are the nodes' IDs.
Type:
- Object.<string, NDNode>
outputHistory :Array.<string>
A history of past generated sentences.
Type:
- Array.<string>
wordDictionary :Array.<string>
A bag of words, used to losslessly compress the graph.
Type:
- Array.<string>
Methods
(static) load(data) → {NilodModel}
Loads a NILOD model from a save.
Parameters:
| Name | Type | Description |
|---|---|---|
data |
string | object | The save, either as a plain object or a JSON string, of the model. |
Returns:
The loaded modek.
- Type
- NilodModel
allEdgesBackward(nodeId) → {Array.<NDEdge>}
Retrieves a list of all edges that stem from a given node.
Parameters:
| Name | Type | Description |
|---|---|---|
nodeId |
string | The ID of the node from the which to look for stemming edges. |
Returns:
- Type
- Array.<NDEdge>
allEdgesForward(nodeId) → {Array.<NDEdge>}
Retrieves a list of all edges that 'point to' a given node.
Parameters:
| Name | Type | Description |
|---|---|---|
nodeId |
string | The ID of the node to look behind, for edges 'pointing to' it. |
Returns:
- Type
- Array.<NDEdge>
applyRating(attemptID, rating) → {number}
Apply a rating vote to an output sentence.
Parameters:
| Name | Type | Description |
|---|---|---|
attemptID |
string | The ID of the output sentence. |
rating |
number | The rating, in no required scale, though a scale between -1 and 1 is preferable. |
Returns:
The new resulting averaged rating.
- Type
- number
encodeWord(word) → {integer}
Adds a word to the word bag, if it is not in the bag yet, and returns its index.
Parameters:
| Name | Type | Description |
|---|---|---|
word |
string | A word index. |
Returns:
The word's index in the word bag.
- Type
- integer
findEdge(from, to) → (nullable) {NDEdge}
Finds the edge between the specified nodes.
Parameters:
| Name | Type | Description |
|---|---|---|
from |
string | The ID of the previous node from this edge. |
to |
string | The ID of the next node from this edge. |
Returns:
The edge found, or null if none.
- Type
- NDEdge
findEdgesBetween(word1, word2) → {Array.<NDEdge>}
Finds all edges that link a word to another, regardless
of whichever nodes such words come from. The arguments
may or may not be encoded using the bag-of-words.
Parameters:
| Name | Type | Description |
|---|---|---|
word1 |
string | number | The 'from' word. |
word2 |
string | number | The 'to' word. |
Returns:
- Type
- Array.<NDEdge>
findNode(a, b) → (nullable) {NDNode}
Returns a list of all nodes containing both words.
Parameters:
| Name | Type | Description |
|---|---|---|
a |
string | integer | The first word in the node. |
b |
string | integer | The second word in the node. |
Returns:
The node found, or null if none.
- Type
- NDNode
findNodeWith(word) → (nullable) {NDNode}
Returns a list of all nodes containing the words.
Parameters:
| Name | Type | Description |
|---|---|---|
word |
string | integer | The word to be searched. |
Returns:
The first node found, or null if none.
- Type
- NDNode
generate(samples, config) → (nullable) {GenerationResults}
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. This config in particular may also contain an optional _sampleCallback_ property, called once after each sample is generated. |
Returns:
The results of generation, or null if unsuccessful.
- Type
- GenerationResults
getNodes()
Returns a map, from the ID of nodes, to the nodes, from
all nodes in the model's internal graph.
getWord(index) → {string}
Retrieves a word from the specified index in the word bag.
Parameters:
| Name | Type | Description |
|---|---|---|
index |
integer | A word bag index. |
Returns:
The word in the word bag.
- Type
- string
rateText(text, config) → {number}
Predicts a rating to a given sentence or text, based on output
history (and order in the tokenized text).
Parameters:
| Name | Type | Description | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
text |
string | The text to rate. | |||||||||
config |
object | The rating configuration
Properties
|
Returns:
The determined rating.
- Type
- number
save(json) → {object|string}
Returns the graph, optionally serialized as JSON.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
json |
boolean | true | Whether to return the graph in JSON encoding or as a plain object. |
Returns:
The graph, duh.
- Type
- object | string
setRating(attemptID, rating)
Sets an output sentence's rating.
Parameters:
| Name | Type | Description |
|---|---|---|
attemptID |
string | The ID of the output sentence. |
rating |
number | The new rating, in no required scale, though a scale between -1 and 1 is preferable. |
train(sentences) → {void}
Trains this model's Markov-like generator graph.
Parameters:
| Name | Type | Description |
|---|---|---|
sentences |
Array.<string> | A list of sentences from the which to train the model's generator. |
Returns:
- Type
- void
tryOnce(configopt) → {NDSampleOutput}
Generates a random sentence based on the
given Markov data.
Returns the result, in the NDSampleOutput record format.
Parameters:
| Name | Type | Attributes | Description | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
config |
Object |
<optional> |
The settings for this attempt.
Properties
|
Returns:
- Type
- NDSampleOutput