Advertisement
vkreal

AutoComplete

Jun 1st, 2022
838
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2.  
  3. // trie node to store characters for trie
  4. class TrieNode {
  5.     constructor() {
  6.         this.children = new Map();
  7.         this.word = '';
  8.         this.cost = -1;
  9.     }
  10. }
  11.  
  12. class Trie {
  13.     constructor() {
  14.         // create root node
  15.         this.root = new TrieNode();
  16.     }
  17.     // add word to trie
  18.     addWord(word, cost) {
  19.         let cur = this.root;
  20.         for (const c of word) {
  21.             if (!cur.children.has(c)) {
  22.                 cur.children.set(c, new TrieNode());
  23.             }
  24.             cur = cur.children.get(c);
  25.         }
  26.         cur.cost = cost;
  27.         cur.word = word;
  28.     }
  29.     // find words with prefix
  30.     find(word) {
  31.         let cur = this.root;
  32.         for (const c of word) {
  33.             if (!cur.children.has(c)) {
  34.                 return [];
  35.             }
  36.             cur = cur.children.get(c);
  37.         }
  38.         let res = [];
  39.         this.dfs(cur, res);
  40.         return res;
  41.     }
  42.     // dfs for words inside cur node
  43.     dfs(node, res) {
  44.         if (node.word) {
  45.             res.push({ word: node.word, cost: node.cost });
  46.         }
  47.         // if child exist dfs
  48.         for (let [key, child] of node.children) {
  49.             this.dfs(child, res);
  50.         }
  51.     }
  52. }
  53.  
  54. class AutoComplete {
  55.     constructor(words) {
  56.         this.trie = new Trie(words);
  57.     }
  58.     addWords(wordPairs) {
  59.         for (let i = 0; i < wordPairs.length; i++) {
  60.             let wordPair = wordPairs[i];
  61.             this.trie.addWord(wordPair.word, wordPair.cost);
  62.         }
  63.     }
  64.     find(text) {
  65.         return this.trie.find(text);
  66.     }
  67. }
  68.  
  69. class AutoCompleteTest {
  70.     constructor() {
  71.         this.numberOfWords = 0;
  72.         this.numberOfQuery = 0;
  73.         this.autoComplete = new AutoComplete();
  74.     }
  75.     runTest() {
  76.         this.parse();
  77.         this.autoComplete.addWords(this.words);
  78.  
  79.         const ans = [];
  80.         for (const qs of this.query) {
  81.             let temp = this.autoComplete.find(qs);
  82.             temp.sort((a, b) => {
  83.                 return a.cost - b.cost;
  84.             });
  85.             ans.push(temp);
  86.         }
  87.  
  88.         console.log(ans);
  89.  
  90.     }
  91.     parse() {
  92.         let data = this.getDataFileContent();
  93.  
  94.         let arr = data.split('\n');
  95.         this.numberOfWords = +arr.shift();
  96.         this.numberOfQuery = +arr.shift();
  97.  
  98.         this.query = [];
  99.         for (let i = 0; i < this.numberOfQuery; i++) {
  100.             this.query.push(arr.pop());
  101.         }
  102.  
  103.  
  104.         this.words = [];
  105.         for (let i = 0; i < arr.length; i++) {
  106.             this.words.push({ word: arr[i], cost: i + 1 })
  107.         }
  108.     }
  109.     getDataFileContent() {
  110.         const content = fs.readFileSync('dataTestFile.data');
  111.         return content.toString();
  112.     }
  113. }
  114.  
  115. let autoTest = new AutoCompleteTest();
  116. autoTest.runTest();
  117.  
  118. // catc:
  119. // catch (10)
  120. // art:
  121. // art (1)
  122. // arts (4)
  123. // articles (8)
  124. // article (12)
  125. // articulate (13)
  126. // da:
  127. // date (5)
  128. // day (7)
  129. // data (9)
  130. // z:
  131. // zone (2)
  132. // zip (3)
  133. // z (6)
  134. // zoom (11)
  135.  
  136. // [
  137. //     { word: 'art', cost: 1 },
  138. //     { word: 'zone', cost: 2 },
  139. //     { word: 'zip', cost: 3 },
  140. //     { word: 'arts', cost: 4 },
  141. //     { word: 'date', cost: 5 },
  142. //     { word: 'z', cost: 6 },
  143. //     { word: 'day', cost: 7 },
  144. //     { word: 'articles', cost: 8 },
  145. //     { word: 'data', cost: 9 },
  146. //     { word: 'catch', cost: 10 },
  147. //     { word: 'zoom', cost: 11 },
  148. //     { word: 'article', cost: 12 },
  149. //     { word: 'articulate', cost: 13 },
  150. //     { word: 'articulation', cost: 14 }
  151. //   ]
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement