new Predictionary()
Constructs a Predictionary word prediction class. It's possible to manage multiple internal dictionaries, retrieve predictions
(suggestions) for a given input and learn/refine the dictionaries from user input.
Members
DEFAULT_DICTIONARY_KEY :string
Default dictionary key, if no key is specified.
Type:
- string
Methods
(static) instance() → {Predictionary}
Constructs a new instance of Predictionary
Returns:
- Type
- Predictionary
addDictionary(dictionaryKey, wordsopt)
Add a new internal dictionary for predictions.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
dictionaryKey |
string | the key of the dictionary to add | |
words |
Array |
<optional> |
Optional array of words (string) that should be added to the new dictionary. |
addWord(element, dictionaryKeyopt)
Add a single new word/element to a dictionary.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
element |
string | Object | the element to add, can be either a plain word as a string or an object containing the properties object.word (word to add as string) and object.rank (number specifying the rank of the word, a lower rank causes the word to be ranked to front as a suggestion). | ||
dictionaryKey |
string |
<optional> |
Predictionary#DEFAULT_DICTIONARY_KEY | optional key of the dictionary to add the element. |
addWords(elements, dictionaryKeyopt)
Add multiple new words/elements to a dictionary.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
elements |
string | Object | the elements to add, can be an Array of either plain words (string) or of objects containing the properties object.word (word to add as string) and object.rank (number specifying the rank of the word, a lower rank causes the word to be ranked to front as a suggestion). | ||
dictionaryKey |
string |
<optional> |
Predictionary#DEFAULT_DICTIONARY_KEY | optional key of the dictionary to add the element. |
applyPrediction(input, chosenPrediction, optionsopt) → {string}
Apply chosen suggestion to a given input, e.g. input = "this is an ap" and chosenPrediction = "Apple" results in
"this is an Apple". Calling this function automatically refines the saved frequency of the chosen word making it
more likely to be suggested in the future.
Parameters:
Name | Type | Attributes | Description | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
input |
string | the current input string (e.g. from textfield) | |||||||||||||||||
chosenPrediction |
string | the chosen prediction which should be applied to the input string | |||||||||||||||||
options |
Object |
<optional> |
options object containing additional properties.
Properties
|
Returns:
the given input with the applied suggestion
- Type
- string
delete(inputOrWord, optionsopt)
Deletes a single word from one or all dictionaries.
Parameters:
Name | Type | Attributes | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
inputOrWord |
string | a single word or longer string where the last word will be deleted in the dictionaries. | |||||||||||||
options |
Object |
<optional> |
Object for options
Properties
|
dictionariesToJSON() → {string}
Exports all dictionaries to a JSON string. Subsequently the dictionaries can be imported using the resulting string
and Predictionary#loadDictionaries.
Returns:
JSON string representing all currently loaded dictionaries.
- Type
- string
dictionaryToJSON(dictionaryKeyopt) → {string}
Exports a single dictionary to a JSON string. Subsequently the dictionary can be imported using the resulting string
and Predictionary#loadDictionary.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
dictionaryKey |
string |
<optional> |
Predictionary#DEFAULT_DICTIONARY_KEY | the key of the dictionary to export. |
Returns:
JSON string representing the currently loaded dictionary with specified dictionaryKey.
- Type
- string
getDictionaryKeys(onlyEnabledopt) → {Array.<string>}
Returns a list of currently loaded dictionary keys.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
onlyEnabled |
boolean |
<optional> |
if true only keys of dictionaries that are enabled are returned. See e.g. Predictionary#useDictionaries |
Returns:
- Type
- Array.<string>
getWords(dictionaryKeyopt) → {Array.<string>}
Retrieves saved words of a single or all loaded dictionaries.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
dictionaryKey |
string |
<optional> |
key of the dictionary from which the words should be retrieved, if not set all dictionaries are used. |
Returns:
array of saved words (string) for the dictionary with the given key.
- Type
- Array.<string>
hasWord(word, dictionaryKeyopt, matchCaseopt) → {boolean}
Test if a given word exists a single or all loaded dictionaries.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
word |
string | to test if existing | |
dictionaryKey |
string |
<optional> |
key of the dictionary to check, if not set all dictionaries are used |
matchCase |
boolean |
<optional> |
if set (true) the word is searched case-sensitive, otherwise case-insensitive (default) |
Returns:
true if the given word is existing
- Type
- boolean
isUsingOnlyDefaultDictionary() → {boolean}
Returns true if only the default dictionary is used (key Predictionary#DEFAULT_DICTIONARY_KEY).
Returns:
- Type
- boolean
learn(chosenWord, previousWordopt, addToDictionaryopt)
Updates the frequencies for given words, making them to be more likely suggested in the future.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
chosenWord |
string | a suggestion/word the user has chosen | |
previousWord |
string |
<optional> |
the previous word of the chosen suggestion |
addToDictionary |
string |
<optional> |
the key of the dictionary where new words should be added. Automatically determined, if not specified. |
learnFromInput(input, dictionaryKeyopt) → {boolean}
Learns from input text while the user is typing. This method can be called with e.g. the value of a text input
field for every character the user is typing.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
input |
string | the text string to learn with. The second last and third last words are learned. | |
dictionaryKey |
string |
<optional> |
the key of the dictionary where new words should be added. Automatically determined, if not specified. |
Returns:
true if something was learned, false if not
- Type
- boolean
learnFromText(text, dictionaryKeyopt)
Learns words and transitions from a given text/phrase.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
text |
string | the text to learn from | ||
dictionaryKey |
string |
<optional> |
Predictionary#DEFAULT_DICTIONARY_KEY | the key of the dictionary where the words should be learned/added. |
loadDictionaries(dictionariesJSON)
Loads all dictionaries from a JSON string that was previously exported by Predictionary#dictionariesToJSON.
This method replaces/deletes all currently loaded dictionaries!
Parameters:
Name | Type | Description |
---|---|---|
dictionariesJSON |
string | json string representing dictionaries, exported by Predictionary#dictionariesToJSON |
loadDictionary(dictionaryJSON, dictionaryKeyopt)
Loads a single dictionary from a JSON string that was previously exported by Predictionary#dictionaryToJSON.
If the given dictionaryKey already exists, the existing dictionary is replaced.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
dictionaryJSON |
string | json string representing a dictionary, exported by Predictionary#dictionaryToJSON | ||
dictionaryKey |
string |
<optional> |
Predictionary#DEFAULT_DICTIONARY_KEY | the key for which the dictionary should be imported. |
parseWords(importString, optionsopt)
Import words from a plain string (e.g. text file).
Parameters:
Name | Type | Attributes | Description | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
importString |
string | a plain text string (e.g. from a text file) | |||||||||||||||||||||||||||||||
options |
Object |
<optional> |
options object containing additional properties. The default properties are suited for
a plain text string in format: "word1;word2;word3;...", setting rankPosition=1 would be suited for
a plain text in format: "word1 rank1;word2 rank2;word3 rank3;...".
Properties
|
predict(input, optionsopt) → {Array.<string>}
Returns word suggestions for a given input. Automatically detects if the last word should be completed (last
character is not space) or if a next word should be suggested (last character is space).
Parameters:
Name | Type | Attributes | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
input |
string | string for which the predictions should be calculated, e.g. the value of a text input where the user is typing. | ||||||||||||||||
options |
Object |
<optional> |
options object containing additional properties.
Properties
|
Returns:
list of words that are predictions/suggestions for the given input, ordered by relevance.
- Type
- Array.<string>
predictCompleteWord(input, optionsopt) → {Array.<string>}
Returns word suggestions for a given input. Last word is assumed to be incomplete and has to be completed.
Parameters:
Name | Type | Attributes | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
input |
string | string for which the predictions should be calculated, e.g. the value of a text input where the user is typing. | ||||||||||||||||
options |
Object |
<optional> |
options object containing additional properties.
Properties
|
Returns:
list of words that are predictions/suggestions for the given input, ordered by relevance.
- Type
- Array.<string>
predictNextWord(input, optionsopt) → {Array.<string>}
Returns word suggestions for a given input. Last word is assumed to be complete and suggestions for the next words
are calculated.
Parameters:
Name | Type | Attributes | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
input |
string | string for which the predictions should be calculated, e.g. the value of a text input where the user is typing. | ||||||||||||||||
options |
Object |
<optional> |
options object containing additional properties.
Properties
|
Returns:
list of words that are predictions/suggestions for the given input, ordered by relevance.
- Type
- Array.<string>
useAllDictionaries()
Use all currently loaded dictionaries for predictions.
useDictionaries(dictionaryKeys)
Use a set of dictionaries for predictions, specified by an array of dictionaryKeys.
Parameters:
Name | Type | Description |
---|---|---|
dictionaryKeys |
Array | an array of strings, specifying the dictionaryKeys to use |
useDictionary(dictionaryKey)
Use only a singe loaded dictionary for predictions.
Parameters:
Name | Type | Description |
---|---|---|
dictionaryKey |
string | the key of the dictionary to use |