Class: NilodModel

NilodModel(jGraphopt)

The model that englobates (mostly) everything NILOD.

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.
Source:

Members

_obj :NilodGraph

The internal graph structure, which can be used to export a JSON save.
Type:
Source:

edges :Object.<string, NDEdge>

The edges, where the keys are the edges' IDs.
Type:
Source:

nodes :Object.<string, NDNode>

The nodes, where the keys are the nodes' IDs.
Type:
Source:

outputHistory :Array.<string>

A history of past generated sentences.
Type:
  • Array.<string>
Source:

wordDictionary :Array.<string>

A bag of words, used to losslessly compress the graph.
Type:
  • Array.<string>
Source:

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.
Source:
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.
Source:
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.
Source:
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.
Source:
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.
Source:
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.
Source:
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.
Source:
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.
Source:
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.
Source:
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.
Source:
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.
Source:

getWord(index) → {string}

Retrieves a word from the specified index in the word bag.
Parameters:
Name Type Description
index integer A word bag index.
Source:
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
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.
Source:
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.
Source:
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.
Source:

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.
Source:
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
Name Type Attributes Default Description
begin string <optional>
The seed query for this sentence generation attempt. Random by default.
maxWords integer <optional>
64 The maximum amount of words to be composed in the output.
maxChars integer <optional>
512 The maximum amount of characters to form the output.
Source:
Returns:
Type
NDSampleOutput