{"id":19388478,"url":"https://github.com/vuldin/trie","last_synced_at":"2025-04-23T23:31:40.149Z","repository":{"id":31476425,"uuid":"127830930","full_name":"vuldin/trie","owner":"vuldin","description":"Rank text by best match to seeded words and phrases","archived":false,"fork":false,"pushed_at":"2024-06-12T20:12:31.000Z","size":1656,"stargazers_count":3,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T23:49:12.962Z","etag":null,"topics":["autocomplete","match","phrase","search","stem","trie","typeahead","word"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/vuldin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","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":"2018-04-03T01:03:11.000Z","updated_at":"2024-07-25T17:10:26.000Z","dependencies_parsed_at":"2024-11-10T10:12:53.350Z","dependency_job_id":"4d0904cf-de48-42c7-9004-bb76efd621d9","html_url":"https://github.com/vuldin/trie","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vuldin%2Ftrie","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vuldin%2Ftrie/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vuldin%2Ftrie/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vuldin%2Ftrie/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vuldin","download_url":"https://codeload.github.com/vuldin/trie/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250532069,"owners_count":21446109,"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":["autocomplete","match","phrase","search","stem","trie","typeahead","word"],"created_at":"2024-11-10T10:12:46.229Z","updated_at":"2025-04-23T23:31:39.710Z","avatar_url":"https://github.com/vuldin.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @vuldin/trie\n\nA trie implementation with a focus on matching phrases.\nThis package is available as an ES and CommonJS module.\nAdd it to an existing project via `npm`:\n\n```\nnpm i @vuldin/trie\n```\n\n## Why?\n\nKeep in mind that this library is an excuse for me to mess with trie data structure more than anything.\nFor more info, see the [wiki](https://en.wikipedia.org/wiki/Trie) and other articles such as [this](https://medium.com/basecs/trying-to-understand-tries-3ec6bede0014).\n\nMost trie implementations focus on each node being a letter in a word.\nBut nodes in this library are word stems instead.\nWord stems are used in place of the actual words to enable matches on similarity and intent (ie. `jumps` is treated the same as `jump`)\nCommon words are also removed from phrases, both when adding and finding phrases in the trie dataset.\n\nThis concept could be expanded upon to eventually be apart of a search tool.\nPhrases could be matched to the text a user enters, and these phrases could be recommended and/or used for autocomplete or typeahead.\n\n## Usage\n\nThe first step is to instantiate the trie:\n\n```\nimport Trie from '@vuldin/trie'\n// or\nconst Trie = require('@vuldin/trie')\n\nconst trie = new Trie()\n```\n\nThen `trie` can be used to add phrases. The API is flexible, allowing for single words, multiple phrases, and chaining:\n\n```\ntrie.add('Hello.')\ntrie.add('Here is a test sentence that contains some common words in English.')\ntrie.add('Strings can. Contain multiple sentences.')\ntrie.add('add function calls').add('can be chained')\n```\n\nOnce the dataset is ready, you can search it with `find`:\n\n```\n// finding a phrase\ntrie.find('test sentence')   // { count: 2, exact: true }\n// common words are ignored\ntrie.find('test a sentence') // { count: 2, exact: true }\n```\n\nThe `find` result is very basic at the moment.\nBut in this current state, the count can be used to show the user how relevant a given dataset is to the given phrase.\n\n### Parsing text\n\nStrings sent to this library (whether during intial data structure creation or during lookup) go through several parsing steps.\nFirst the string is broken up into phrases/sentences.\nThen each sentence is stripped of all common words.\nRemaining uncommon words are finally converted to their stem form to remove any differences during later comparisons that relate to tense or plural forms.\nThe lower case version of these stems are then used to generate the data structure (details on this data structure below).\nThe following text:\n\n```\nHere is a test sentence that contains some common words in English.\n```\n\nbecomes:\n\n```\n['here', 'is', 'test', 'sentenc', 'contain', 'common', 'word', 'english']\n```\n\n### Data structure\n\nThe following data structure is used to ensure that phrases contained in the text can be matched.\nBut since we also want to match on any (uncommon) word, each of these words is additionally added to the root node.\nThis means that each node is added to the trie the same number of times as the place it holds in the array.\nThe tradeoff is that we create a larger data structure but have a more functional lookup (by any uncommon word or phrase).\n\n**A visual example of this behaviour will be added soon**\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvuldin%2Ftrie","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvuldin%2Ftrie","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvuldin%2Ftrie/lists"}