{"id":18524139,"url":"https://github.com/m-elbably/symspell-ex","last_synced_at":"2025-04-09T11:31:56.578Z","repository":{"id":37766215,"uuid":"373277235","full_name":"m-elbably/symspell-ex","owner":"m-elbably","description":"Distributed spelling correction \u0026 fuzzy search based on symmetric delete spelling correction algorithm (SymSpell)","archived":false,"fork":false,"pushed_at":"2023-10-12T10:21:06.000Z","size":903,"stargazers_count":6,"open_issues_count":1,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-24T05:22:53.062Z","etag":null,"topics":["algorithm","nlp","spelling-correction"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/m-elbably.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-06-02T19:11:46.000Z","updated_at":"2024-04-01T12:14:41.000Z","dependencies_parsed_at":"2024-11-06T17:44:29.233Z","dependency_job_id":"05a915e0-b0ff-43e1-a9f3-5da1d7ec78c8","html_url":"https://github.com/m-elbably/symspell-ex","commit_stats":{"total_commits":25,"total_committers":2,"mean_commits":12.5,"dds":0.07999999999999996,"last_synced_commit":"4f15e04a6c052364b997f8ef669ec9a81f32370f"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m-elbably%2Fsymspell-ex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m-elbably%2Fsymspell-ex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m-elbably%2Fsymspell-ex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m-elbably%2Fsymspell-ex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/m-elbably","download_url":"https://codeload.github.com/m-elbably/symspell-ex/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247721921,"owners_count":20985084,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","nlp","spelling-correction"],"created_at":"2024-11-06T17:39:42.559Z","updated_at":"2025-04-09T11:31:56.108Z","avatar_url":"https://github.com/m-elbably.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# **SymSpellEx**\n\nSpelling correction \u0026 Fuzzy search based on *Symmetric Delete Spelling Correction algorithm* (SymSpell)\n\n\u003e Work in progress, need more optimizations\n\n[![Node Version](https://img.shields.io/node/v/symspell-ex.svg)](https://nodejs.org)\n[![npm version](https://img.shields.io/npm/v/symspell-ex/latest.svg)](https://www.npmjs.com/package/symspell-ex)\n[![Build Status](https://github.com/m-elbably/symspell-ex/workflows/symspell-ex/badge.svg)](https://github.com/m-elbably/symspell-ex/workflows/symspell-ex/badge.svg)\n[![Coverage Status](https://coveralls.io/repos/github/m-elbably/symspell-ex/badge.svg?branch=main\u0026t=pKbxsM)](https://coveralls.io/github/m-elbably/symspell-ex?branch=main)\n[![License](https://img.shields.io/github/license/m-elbably/symspell-ex.svg)](https://raw.githubusercontent.com/m-elbably/symspell-ex/master/LICENSE)\n\n## Installation\n    npm install symspell-ex --save\n\n\u003e Changes v1.1.1\n\u003e - Tokenization support\n\u003e - Term frequency should be provided for training and terms should be unique\n\u003e - Correct function return different object (`Correction` object) \n\u003e - Hash table implemented in redis store instead of normal list structure\n\u003e - Enhanced testing code and coverage\n\u003e - Fixed bugs in lookup\n\n## Features\n- ***Very fast***\n- ***Word suggestions***\n- ***Word correction***\n- ***Multiple languages supported*** - _The algorithm, and the implementation are language independent_ \n- ***Extendable*** - _Edit distance and data stores can be implemented to extend library functionalities_\n\n## Usage\n\n### Training\n\nFor single term training you can use `add` function:\n```typescript\nimport {SymSpellEx, MemoryStore} from 'symspell-ex';\n\nconst LANGUAGE = 'en';\n// Create SymSpellEx instnce and inject store new store instance\nsymSpellEx = new SymSpellEx(new MemoryStore());\nawait symSpellEx.initialize();\n// Train data\nawait symSpellEx.add(\"argument\", LANGUAGE);\nawait symSpellEx.add(\"computer\", LANGUAGE);\n```\n\nFor multiple terms (Array) you can use `train` function:\n```typescript\nconst terms = ['argument', 'computer'];\nawait symSpellEx.train(terms, 1, LANGUAGE);\n```\n\n### Searching\n`search` function can be used to get multiple suggestions if available up to the `maxSuggestions` value\n\nArguments:\n- `input` _String_ (Wrong/Invalid word we need to correct)\n- `language` _String_ (Language to be used in search)  \n- `maxDistance` _Number_, _optional_, default = `2` (Maximum distance for suggestions)\n- `maxSuggestions` _Number_, _optional_, default = `5` (Maximum suggestions number to return)\n\nReturn: `Array\u003cSuggetion\u003e` Array of suggestions\n\nExample\n```typescript\nawait symSpellEx.search('argoments', 'en');\n```\nExample `Suggestion Object`:\n```json\n{\n  \"term\": \"argoments\",\n  \"suggestion\": \"arguments\",  \n  \"distance\": 2,\n  \"frequency\": 155\n}\n```\n\n### Correction\n`correct` function can be used to get the best suggestion for input word or sentence in terms of edit distance and frequency\n\nArguments:\n- `input` _String_ (Wrong/Invalid word we need to correct)\n- `language` _String_ (Language to be used in search)\n- `maxDistance` _Number_, _optional_, default = `2` (Maximum distance for suggestions)\n\nReturn: `Correction` object which contains original `input` and corrected `output` string, with array of suggestions  \n\nExample\n```typescript\nawait symSpellEx.correct('Special relatvity was orignally proposed by Albert Einstein', 'en');\n```\n\nReturns this `Correction` object:\n\u003e This output is totally depending on the quality of the training data that was push into the store\n```json\n{\n  \"suggestions\": [],\n  \"input\": \"Special relatvity was orignally proposed by Albert Einstein\",\n  \"output\": \"Special relativity was originally proposed by Albert Einstein\"\n}\n```\n\n## Computational Complexity\nThe algorithm has constant time O(1) time, independent of the dictionary size, but depend on the average term length and maximum edit distance,\n_Hash Table_ is used to store all search entries which has an average search time complexity of O(1).\n\n## Why the algorithm is fast?\n\n### Pre-calculation\nin training phase all possible spelling error variants as generated (deletes only) and stored in hash table\n\n\u003e 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.\n\u003e _(Using RedisStore makes it easy to train and build once, then search and correct from any external source)_\n \n### Symmetric Delete Spelling Correction\nIt allows a tremendous reduction of the number of spelling error candidates to be pre-calculated (generated and added to hash table), \nwhich then allows O(1) search while getting spelling suggestions.\n\n## Library Design\n![Lib Diagram](./res/images/diagram.svg \"SymSpellEx\")\n\n### Tokenizer\nThis interface can be implemented to provide a different tokenizer for the library\n\nInterface type\n```typescript\nexport interface Tokenizer {\n    tokenize(input: string): Array\u003cToken\u003e;\n}\n```\n\n### EditDistance\nThis interface can be implemented to provide more algorithms to use to calculate edit distance between two words\n\u003e **[Edit Distance](https://en.wikipedia.org/wiki/Edit_distance)** \n\u003e 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\n\nInterface type\n```typescript\ninterface EditDistance {\n    name: String;\n    calculateDistance(source: string, target: string): number;\n}\n```\n\n### DataStore\nThis interface can be implemented to provide additional method to store data other than built-in stores (Memory, Redis)\n\nData store should handle storage for these 2 data types:\n- **Terms**: List data structure to store terms and retrieve it by index\n- **Entries**: Hash Table data structure to store dictionary entries and retrieve data by term (_Key_)\n\n\u003e Data store should also handle storage for multiple languages and switch between them\n\nInterface type\n```typescript\nexport interface DataStore {\n    name: string;\n    initialize(): Promise\u003cvoid\u003e;\n    isInitialized(): boolean;\n    setLanguage(language: string): Promise\u003cvoid\u003e;\n    pushTerm(value: string): Promise\u003cnumber\u003e;\n    getTermAt(index: number): Promise\u003cstring\u003e;\n    getTermsAt(indexes: Array\u003cnumber\u003e): Promise\u003cArray\u003cstring\u003e\u003e;\n    getEntry(key: string): Promise\u003cArray\u003cnumber\u003e\u003e;\n    getEntries(keys: Array\u003cstring\u003e): Promise\u003cArray\u003cArray\u003cnumber\u003e\u003e\u003e;\n    setEntry(key: string, value: Array\u003cnumber\u003e): Promise\u003cboolean\u003e;\n    hasEntry(key: string): Promise\u003cboolean\u003e;\n    maxEntryLength(): Promise\u003cnumber\u003e;\n    clear(): Promise\u003cvoid\u003e;\n}\n```\n\n#### Built-in data stores\n- Memory: Stores data in memory, using array structure for terms and high speed hash table [(megahash)](https://www.npmjs.com/package/megahash)\nto manage dictionary entries\n\u003e May be limited by node process memory limits, which can be overridden\n\n- Redis: Stores data into Redis database using list structure to store terms and hash to store dictionary data\n\u003e Very efficient way to train and store data, it will allow accessing by multiple processes and/or machines, \n\u003e also dumping and migrating data will be easy\n\nTODO\n\n- [x] Tokenization\n- [x] Sentence correction\n- [x] Bulk data training  \n- [ ] Word Segmentation\n- [ ] Domain specific correction\n\n#### References\n* [Symmetric Delete Spelling Correction](https://wolfgarbe.medium.com/1000x-faster-spelling-correction-algorithm-2012-8701fcd87a5f)\n* [SymSpell](https://github.com/wolfgarbe/SymSpell)\n\n#### License\nMIT\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fm-elbably%2Fsymspell-ex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fm-elbably%2Fsymspell-ex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fm-elbably%2Fsymspell-ex/lists"}