Spelling correction & Fuzzy search based on Symmetric Delete Spelling Correction algorithm (SymSpell)
Work in progress, optimizations needs to be applied
npm install symspell-ex --save
- Very fast
- Word suggestions
- Word correction
- Multiple languages supported - The algorithm, and the implementation are language independent
- Extendable - Edit distance and data stores can be implemented to extend library functionalities
For single term training you can use add
function:
import {SymSpellEx, MemoryStore} from 'symspell-ex';
const LANGUAGE = 'en';
// Create and initialize memory store (for simplicity)
const store = new MemoryStore();
await store.initialize();
// Create SymSpellEx instnce and inject store into it
symSpellEx = new SymSpellEx(store);
// Train data
await symSpellEx.add("argument", LANGUAGE);
await symSpellEx.add("computer", LANGUAGE);
For multiple terms (Array) you can use train
function:
const terms = ['argument', 'computer'];
await symSpellEx.train(terms, LANGUAGE);
search
function can be used to get multiple suggestions if available up to the maxSuggestions
value
Arguments:
input
String (Wrong/Invalid word we need to correct)language
String (Language to be used in search)maxDistance
Number, optional, default =2
(Maximum distance for suggestions)maxSuggestions
Number, optional, default =5
(Maximum suggestions number to return)
Return: Array<Suggetion>
Array of suggestions
Example
await symSpellEx.search('argoments', 'en');
correct
function can be used to get the best suggestion for input in terms of edit distance and frequency
Arguments:
input
String (Wrong/Invalid word we need to correct)language
String (Language to be used in search)maxDistance
Number, optional, default =2
(Maximum distance for suggestions)
Return: Suggetion
Suggestion object
Example
await symSpellEx.correct('argoments', 'en');
Example Result Suggestion Object
:
{
"term": "arguments",
"distance": 2,
"frequency": 155
}
The algorithm has constant time O(1) time, independent of the dictionary size, but depend on the average term length and maximum edit distance, Hash Table is used to store all search entries which has an average search time complexity of O(1).
in training phase all possible spelling error variants as generated (deletes only) and stored in hash table
This makes the algorithm very fast, but it also required a large memory footprint, and the training phase takes a considerable amount of time to build the dictionary first time. (Using RedisStore makes it easy to train and build once, then search and correct from any external source)
It allows a tremendous reduction of the number of spelling error candidates to be pre-calculated (generated and added to hash table), which then allows O(1) search while getting spelling suggestions.
This interface can be implemented to provide more algorithms to use to calculate edit distance between two words
Edit Distance is a way of quantifying how dissimilar two strings (e.g., words) are to one another by counting the minimum number of operations required to transform one string into the other
Interface type
interface EditDistance {
name: String;
calculateDistance(source: string, target: string): number;
}
This interface can be implemented to provide additional method to store data other than built-in stores (Memory, Redis)
Data store should handle storage for these 2 data types:
- Terms: List data structure to store terms and retrieve it by index
- Entries: Hash Table data structure to store dictionary entries and retrieve data by term (Key)
Data store should also handle storage for multiple languages and switch between them
Interface type
interface DataStore {
name: string;
initialize(): Promise<void>;
setLanguage(language: string): Promise<void>;
// List data structure
pushTerm(key: string): Promise<number>;
getTermAt(index: number): Promise<string>;
// Hash table data structure
getEntry(key: string): Promise<Array<number>>;
getManyEntries(keys: Array<string>): Promise<Array<Array<number>>>;
setEntry(key: string, value: Array<number>): Promise<boolean>;
hasEntry(key: string): Promise<boolean>;
maxEntryLength(): Promise<number>;
clear(): Promise<void>;
}
- Memory: Stores data in memory, using array structure for terms and high speed hash table (megahash) to manage dictionary entries
May be limited by node process memory limits, which can be overridden
- Redis: Stores data into Redis database using list structure to store terms and hash to store dictionary data
Very efficient way to train and store data, it will allow accessing by multiple processes and/or machines, also dumping and migrating data will be easy
TODO
- Tokenization
- Word Segmentation
- Sentence correction
- Domain specific correction
- Bulk data training
MIT