Options
All
  • Public
  • Public/Protected
  • All
Menu

A Dictionary is used to define, add/remove patterns to/from, and generate suggestions from suggestion scopes. As someone using this library, the Dictionary class will likely be the primary interface between your code and the functionality autosuggestion has to offer.

Generally speaking, a Dictionary is a collection of named [[Trie|scopes]] containing patterns (words or phrases) which can be easily matched with partially completed input patterns 1. For example, if we want to generate suggestions based on a list of colors, we can

// create a new dictionary
const myDictionary = new Dictionary()

// define a named context with some colors
myDictionary.define('myColors', ['red', 'rose', 'pink'])

now, we can generate suggestions given input,

const inputText = 'r'

// match input text with potential completions in the 'myColors' context
const colorSuggestions = myDictionary.suggest(inputText, 'myColors')
// outputs: red, rose

If we don't know all the patterns for a scope upfront, we can dynamically add,

myDictionary.add('myColors', ['salmon', 'violet', 'avocado'])

or remove,

myDictionary.remove('myColors', ['red', 'salmon'])

[1] Each named scope is a special type of trie indexed by a string-valued name. What makes this type of trie special is that each node not only points to the set of next character candidates, but also points to the set of next word candidates. This makes it, essentially, a trie of tries allowing us to perform phrase-based matches in addition to word-based matches.

Hierarchy

  • Dictionary

Index

Constructors

Properties

Methods

Constructors

constructor

Properties

lookahead

lookahead: number = 0

Configures how many consecutive lookups to resolve given a suggestion with a lookup term which immediately follows the input pattern.

For example, if there exists the following pattern

// assume the scopes for the 'color', 'size', & 'shape' scope groups have been defined.
one two <color> <size> <shape>

then, given an input of ['one', 'two'] & lookahead=2, the resulting suggestions would be,

one two green big <shape>
one two red big <shape>
one two green small <shape>
...

scopes

scopes: Map<string, Scope> = ...

A map of scope names to corresponding scopes.

Methods

add

  • add(scopeName: string, patterns: Pattern[]): void
  • Adds patterns to an existing scope.

    Parameters

    • scopeName: string

      an existing scope name.

    • patterns: Pattern[]

      array of patterns to add.

    Returns void

define

  • Defines a new named scope and populates it with optionally provided patterns.

    Parameters

    • scopeName: string

      the new scope name.

    • patterns: Pattern[] = ...

      optional initial scope patterns.

    Returns Scope

remove

  • remove(scopeName: string, patterns: Pattern[]): void
  • Removes patterns from an existing scope.

    Parameters

    • scopeName: string

      an existing scope name.

    • patterns: Pattern[]

      array of patterns to remove.

    Returns void

suggest

  • suggest(tokens: string | string[], scopeNames?: string[]): Suggestion[]
  • Generates suggestions from specified scopes given an array of input tokens.

    This method expects that the input tokens have already been tokenized by the calling code. In particular, multi-word inputs, which are expected to be matched against phrase-based patterns which have been added to a scope, must be broken up into a array of words (rather than just an raw string with words separated by spaces).

    Parameters

    • tokens: string | string[]

      a single input word token or array of input word tokens.

    • scopeNames: string[] = ...

    Returns Suggestion[]

Legend

  • Constructor
  • Property
  • Method
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Private property
  • Private method
  • Property

Generated using TypeDoc