{"id":327072,"date":"2022-01-10T08:45:00","date_gmt":"2022-01-10T08:45:00","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=327072"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=327072","title":{"rendered":"<span>\u0420\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u043c \u0437\u0430\u0434\u0430\u0447\u0443 T9 (predictive text)<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\" class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/76f\/d58\/f8c\/76fd58f8c7fc57b14d8f3a64b2188461.jpg\" width=\"800\" height=\"480\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/76f\/d58\/f8c\/76fd58f8c7fc57b14d8f3a64b2188461.jpg\" data-blurred=\"true\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! \u041d\u0430 \u0434\u043d\u044f\u0445 \u043a\u043e \u043c\u043d\u0435 \u043e\u0431\u0440\u0430\u0442\u0438\u043b\u0441\u044f \u0443\u0447\u0435\u043d\u0438\u043a \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u0438\u0437 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432, \u0433\u0434\u0435 \u044f \u0432\u044b\u0441\u0442\u0443\u043f\u0430\u044e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 frontend-\u043c\u0435\u043d\u0442\u043e\u0440\u0430, \u0441 \u043f\u0440\u043e\u0441\u044c\u0431\u043e\u0439 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c \u043e\u0434\u043d\u0443 \u0437\u0430\u0434\u0430\u0447\u0443. \u0421\u0443\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043b\u0430 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c:<\/p>\n<p><strong>\u041d\u0430\u0439\u0442\u0438 \u0432\u0441\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0435 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0439 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0439, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u043c <\/strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/T9_(predictive_text)\" rel=\"noopener noreferrer nofollow\"><strong>T9 (predictive text)<\/strong><\/a><\/p>\n<p><strong>\u0412\u0432\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435:<\/strong><\/p>\n<p>\u0424\u0430\u0439\u043b input.txt, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0446\u0438\u0444\u0440, \u0438\u043c\u0438\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u0432\u0432\u043e\u0434:<\/p>\n<pre><code>48 26624637 843 476877 63 5388377 66 3224 74663 539 9484 2 3278 222377 3428466279 63 96737 48 56657 87 46 843 3428466279 255 96737 2677377663464 86 843 73783623 63 5397876537 263 673377 8436 29 373783629 63 873 2 26678837 47 2 776472662253 6224463 8428 73234837 46788 786737 263 62647852837 3282 263 77684337 688788 46 2 873385 367628 2 644486273 47 2 37326 8428 226 22873 2 787664 63428483 366846625 73776673 3766 843 7533737 897422559 3327 67 467767 746784263 47 26 22273842833 79626542 9748464 638463 8428 462732737 77333 67 2738489 63 9748464 27 26672733 86 2 667625 638463 63 9748464 2 52648243 843 7762377 63 9748464 46 746784263 47 225533 78366472749 843 8376 625664 47 622274662559 8733 86 73337 86 2 666 778273 732826453<\/code><\/pre>\n<p>\u0424\u0430\u0439\u043b dictionary.txt, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u0441\u043f\u0438\u0441\u043e\u043a \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0438\u0445 \u0441\u043b\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0432\u0430 \u0438\u0437 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432\u044b\u0448\u0435.<\/p>\n<p><strong>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435:<\/strong><\/p>\n<p>\u0424\u0430\u0439\u043b output.txt, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0441\u043e\u0431\u0440\u0430\u043d\u044b \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0439.<\/p>\n<p><strong>\u0420\u0430\u0437\u0431\u043e\u0440 \u0437\u0430\u0434\u0430\u0447\u0438<\/strong><\/p>\n<p>\u041f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u043d\u0430 \u0443\u043c, \u0442\u0430\u043a \u044d\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Trie\" rel=\"noopener noreferrer nofollow\">\u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0434\u0435\u0440\u0435\u0432\u044c\u044f<\/a>. \u0410 \u043f\u0435\u0440\u0435\u0434 \u044d\u0442\u0438\u043c \u0441\u0442\u043e\u0438\u0442 \u043f\u043e\u044f\u0441\u043d\u0438\u0442\u044c \u0447\u0442\u043e \u0436\u0435 \u044d\u0442\u043e \u0442\u0430\u043a\u043e\u0435.<\/p>\n<p>\u041f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u2014 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u043a, \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0430\u044f \u0432 \u0432\u0438\u0434\u0435 \u0434\u0435\u0440\u0435\u0432\u0430, \u0433\u0434\u0435 \u043d\u0430 \u0440\u0435\u0431\u0440\u0430\u0445 \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u0441\u0438\u043c\u0432\u043e\u043b\u044b, \u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u044b \u043a\u0430\u043a \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0435. \u041a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438 \u0434\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435\u043c \u043d\u0438\u0436\u0435:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/394\/a24\/64a\/394a2464a2cca5bd1a738126785673e7.jpg\" width=\"543\" height=\"340\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/394\/a24\/64a\/394a2464a2cca5bd1a738126785673e7.jpg\" data-blurred=\"true\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0440\u043e\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u0432\u0438\u0434\u0435 \u0441\u0441\u044b\u043b\u043e\u043a \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u044b. \u041a\u0430\u0436\u0434\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u044b\u0447\u043d\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0442\u043e\u043c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u043e\u043d\u0430 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439, \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0441\u0432\u044f\u0437\u0430\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043b\u044e\u0431\u0430\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f, \u0437\u0430\u0432\u0438\u0441\u044f\u0449\u0430\u044f \u043e\u0442 \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<p>\u0414\u043b\u044f \u043b\u0430\u0442\u0438\u043d\u0441\u043a\u043e\u0433\u043e \u0430\u043b\u0444\u0430\u0432\u0438\u0442\u0430 \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e \u043f\u043e \u043f\u0440\u0438\u043d\u0438\u0446\u0438\u043f\u0443 \u043d\u0438\u0436\u0435.<\/p>\n<p>\u041f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0434\u0435\u0440\u0435\u0432\u043e, \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043a\u0430\u0440\u0442\u0443 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044f \u0431\u0443\u043a\u0432 \u0446\u0438\u0444\u0440\u0430\u043c \u0432 \u0440\u0430\u0441\u043a\u043b\u0430\u0434\u043a\u0435 \u043a\u043b\u0430\u0432\u0438\u0430\u0442\u0443\u0440\u044b \u043a\u043d\u043e\u043f\u043e\u0447\u043d\u043e\u0433\u043e \u0442\u0435\u043b\u0435\u0444\u043e\u043d\u0430:<\/p>\n<details class=\"spoiler\">\n<summary>\u041c\u0430\u043f\u043f\u0438\u043d\u0433<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"javascript\">export const keyMap: Record&lt;string, number> = {   a: 2,   b: 2,   c: 2,   d: 3,   e: 3,   f: 3,   g: 4,   h: 4,   i: 4,   j: 5,   k: 5,   l: 5,   m: 6,   n: 6,   o: 6,   p: 7,   q: 7,   r: 7,   s: 7,   t: 8,   u: 8,   v: 8,   w: 9,   x: 9,   y: 9,   z: 9, };<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043a \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430. \u0414\u043b\u044f \u044d\u0442\u0438\u0445 \u0446\u0435\u043b\u0435\u0439 \u0437\u0430\u0432\u0435\u0434\u0435\u043c \u043a\u043b\u0430\u0441\u0441 Trie, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0434\u0432\u0430 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u0430\u00a0<strong>insert\u00a0<\/strong>\u0438\u00a0<strong>getPredictions<\/strong>. \u0417\u0434\u0435\u0441\u044c \u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u0443\u0442\u0435\u043c \u043e\u0431\u0445\u043e\u0434\u0430 \u0434\u0435\u0440\u0435\u0432\u0430 \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441\u043e \u0441\u0442\u0440\u043e\u043a\u043e\u0439, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u0432\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0430, \u0430 \u0437\u0430\u0442\u0435\u043c \u0432 \u0441\u0443\u0444\u0444\u0438\u043a\u0441 \u0441\u0442\u0440\u043e\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0435 \u0443\u0437\u043b\u044b, \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435\u0441\u044f \u0432 \u0434\u0435\u0440\u0435\u0432\u0435. \u041c\u0435\u0442\u043e\u0434\u00a0<strong>getPredictions\u00a0<\/strong>\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u0443\u0442\u0435\u043c \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u043e\u0442 \u043a\u043e\u0440\u043d\u044f \u043f\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430\u043c \u0441\u043b\u043e\u0432\u0430. \u0415\u0441\u043b\u0438 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043a\u043e\u043d\u0435\u0447\u043d\u0430 \u2014 \u0442\u043e \u0441\u043b\u043e\u0432\u043e \u043d\u0430\u0439\u0434\u0435\u043d\u043e, \u0432 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0435\u0442.<\/p>\n<details class=\"spoiler\">\n<summary>\u041a\u043e\u0434 \u043a\u043b\u0430\u0441\u0441\u0430<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"javascript\">import { keyMap, rootGenerator } from \".\/helpers\"; import { Root, Prediction } from '.\/interfaces';  class Trie {   root: Root;    constructor() {     this.root = rootGenerator();   }    insert(word: string): void {     if (word.length === 0) throw new Error(\"A word has to be specified\");      let currentNode = this.root;          Array.from(word).forEach((letter, index) => {       const digit = keyMap[letter];       let isLastNode = false;        if (word.length === index + 1) isLastNode = true;       if (!digit) throw new Error(\"Not a valid digit\");       if (!currentNode.children) currentNode.children = {};       if (!currentNode.children[digit]) currentNode.children[digit] = rootGenerator();        currentNode = currentNode.children[digit] as Root;        if (!isLastNode) currentNode.predictions.deep.push(word);     });      currentNode.predictions.current.push(word);   }    getPredictions(keyString: string): Prediction | undefined {     const state = rootGenerator().predictions;      let currentNode = this.root;     let predictions;      Array.from(keyString).forEach((nodeKey) => {       if (!currentNode.children || !currentNode.children[nodeKey]) {         predictions = state;         return;       }              currentNode = currentNode.children[nodeKey] as Root;       predictions = currentNode.predictions;     });      return predictions;   } }  export default Trie;<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0434\u0435\u0440\u0435\u0432\u0430 \u0438 \u0440\u0430\u0437\u0431\u043e\u0440\u0430 \u0441\u043b\u043e\u0432\u0430\u0440\u044f. \u0422\u0443\u0442 \u043d\u0435\u0442 \u043a\u0430\u043a\u0438\u0445-\u0442\u043e \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0431\u0435\u0437 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0435\u0432.<\/p>\n<details class=\"spoiler\">\n<summary>\u041f\u0430\u0440\u0441\u0438\u043d\u0433<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"javascript\">import { readFileSync } from \"fs\";  import Trie from \".\/trie\";  import { rootGenerator } from \".\/helpers\"; import { Prediction } from \".\/interfaces\";  class TrieParser {   trie: Trie;   filePath: string;    constructor(filePath: string) {     this.trie = new Trie();     this.filePath = filePath;   }    createTrie(words?: string[]): void {     const wordArray = words || this.parseDictionary();  \/\/ Creating trie from words array     wordArray.forEach((word) => {       this.trie.insert(word);     });   }    parseDictionary(): string[] {     const filePath = this.filePath;     const data = readFileSync(filePath, {       encoding: \"utf8\",     });     const regex = \/\\r?\\n\/;     const array = data.toString().split(regex);      return array;   }    getPredictions(keyString: string): Prediction | undefined {     if (!keyString) return rootGenerator().predictions;     return this.trie.getPredictions(keyString);   } }  export default TrieParser;<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u0414\u0435\u043b\u043e \u0437\u0430 \u043c\u0430\u043b\u044b\u043c \u2014 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f, \u043a\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0438 \u0432\u0441\u0435\u0445 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0445 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0439. \u041a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043a \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0442\u0430\u043a\u0438\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u2014\u00a0<a href=\"http:\/\/homepage.math.uiowa.edu\/~goodman\/22m150.dir\/2007\/Permutation%20Generation%20Methods.pdf\" rel=\"noopener noreferrer nofollow\">\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043a\u0443\u0447\u0438<\/a>, \u043e\u0434\u043d\u0430\u043a\u043e \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u043d \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b, \u0430 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u2014\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Cartesian_product\" rel=\"noopener noreferrer nofollow\">\u0434\u0435\u043a\u0430\u0440\u0442\u043e\u0432\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/a>, \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"javascript\">export function cartesian&lt;T>(...words: T[][]): T[][] {   \/\/ iteratively get the cartesian product   return words.reduce&lt;T[][]>(     \/\/ part - cartesian product of the first few arrays from words     (part, array) =>       part.flatMap((cartesianPart) =>         \/\/cartesianPart is an array-prefix of one of the elements of the cartesian product         array.map((element) => [...cartesianPart, element])       ),     [[]]   ); }<\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u0441\u0430\u043c\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f, \u0438 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b. \u041a\u043e\u0434 \u0441 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u043c\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043d\u0438\u0436\u0435:<\/p>\n<details class=\"spoiler\">\n<summary>\u0424\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043b\u0438\u0441\u0442\u0438\u043d\u0433<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"javascript\">import { readFileSync, createWriteStream, unlinkSync, existsSync } from \"fs\";  import { cartesian } from \".\/utils\/cartesian\";  import TrieParser from \".\/trie\/trie-parser.js\";  const dictionaryFilePath = \"src\/data\/dictionary.txt\"; const combinationsFilePath = \"src\/data\/combinations.txt\"; const outputFilePath = \"src\/data\/output.txt\";  const trieDictionaryInstance = new TrieParser(dictionaryFilePath);  trieDictionaryInstance.createTrie();  export function main() {   try {     if (existsSync(outputFilePath)) unlinkSync(outputFilePath);     \/\/ create file stream     const output = createWriteStream(outputFilePath, {       flags: \"a\",     });     const rawData = readFileSync(combinationsFilePath, { encoding: \"utf-8\" })       .toString()       .split(\/\\r?\\n\/);      for (const rawCombination of rawData) {       const combinations = rawCombination.split(\" \");       const predictionArray: string[][] = combinations.map((combination) => {         \/\/ Get predictions for each combination in the string         const result = trieDictionaryInstance.getPredictions(combination);         const targetArray = result?.current.flatMap((item) =>           Array.isArray(item) ? item : [item]         );         return targetArray;       }) as string[][];       \/\/ Get cartesian products       const possibleProducts = (         [...cartesian(...(predictionArray as [string[]]))] as string[][]       ).map((permutation) => permutation.join(\" \"));        for (const permutation of possibleProducts) {         output.write(permutation + \"\\r\");       }     }      output.end();   } catch (e) {     console.error(e);   } }  main();<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u041a\u043e\u0434 \u0437\u0430\u0434\u0430\u0447\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043d\u0430 <a href=\"https:\/\/github.com\/eyudinkov\/t9-trie.git\" rel=\"noopener noreferrer nofollow\">\u0433\u0438\u0442\u0445\u0430\u0431<\/a>. \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435!<\/p>\n<\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/599029\/\"> https:\/\/habr.com\/ru\/post\/599029\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\" class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! \u041d\u0430 \u0434\u043d\u044f\u0445 \u043a\u043e \u043c\u043d\u0435 \u043e\u0431\u0440\u0430\u0442\u0438\u043b\u0441\u044f \u0443\u0447\u0435\u043d\u0438\u043a \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u0438\u0437 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432, \u0433\u0434\u0435 \u044f \u0432\u044b\u0441\u0442\u0443\u043f\u0430\u044e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 frontend-\u043c\u0435\u043d\u0442\u043e\u0440\u0430, \u0441 \u043f\u0440\u043e\u0441\u044c\u0431\u043e\u0439 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c \u043e\u0434\u043d\u0443 \u0437\u0430\u0434\u0430\u0447\u0443. \u0421\u0443\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043b\u0430 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c:<\/p>\n<p><strong>\u041d\u0430\u0439\u0442\u0438 \u0432\u0441\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0435 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0439 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0439, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u043c <\/strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/T9_(predictive_text)\" rel=\"noopener noreferrer nofollow\"><strong>T9 (predictive text)<\/strong><\/a><\/p>\n<p><strong>\u0412\u0432\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435:<\/strong><\/p>\n<p>\u0424\u0430\u0439\u043b input.txt, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0446\u0438\u0444\u0440, \u0438\u043c\u0438\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u0432\u0432\u043e\u0434:<\/p>\n<pre><code>48 26624637 843 476877 63 5388377 66 3224 74663 539 9484 2 3278 222377 3428466279 63 96737 48 56657 87 46 843 3428466279 255 96737 2677377663464 86 843 73783623 63 5397876537 263 673377 8436 29 373783629 63 873 2 26678837 47 2 776472662253 6224463 8428 73234837 46788 786737 263 62647852837 3282 263 77684337 688788 46 2 873385 367628 2 644486273 47 2 37326 8428 226 22873 2 787664 63428483 366846625 73776673 3766 843 7533737 897422559 3327 67 467767 746784263 47 26 22273842833 79626542 9748464 638463 8428 462732737 77333 67 2738489 63 9748464 27 26672733 86 2 667625 638463 63 9748464 2 52648243 843 7762377 63 9748464 46 746784263 47 225533 78366472749 843 8376 625664 47 622274662559 8733 86 73337 86 2 666 778273 732826453<\/code><\/pre>\n<p>\u0424\u0430\u0439\u043b dictionary.txt, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u0441\u043f\u0438\u0441\u043e\u043a \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0438\u0445 \u0441\u043b\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0432\u0430 \u0438\u0437 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432\u044b\u0448\u0435.<\/p>\n<p><strong>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435:<\/strong><\/p>\n<p>\u0424\u0430\u0439\u043b output.txt, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0441\u043e\u0431\u0440\u0430\u043d\u044b \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0439.<\/p>\n<p><strong>\u0420\u0430\u0437\u0431\u043e\u0440 \u0437\u0430\u0434\u0430\u0447\u0438<\/strong><\/p>\n<p>\u041f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u043d\u0430 \u0443\u043c, \u0442\u0430\u043a \u044d\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Trie\" rel=\"noopener noreferrer nofollow\">\u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0434\u0435\u0440\u0435\u0432\u044c\u044f<\/a>. \u0410 \u043f\u0435\u0440\u0435\u0434 \u044d\u0442\u0438\u043c \u0441\u0442\u043e\u0438\u0442 \u043f\u043e\u044f\u0441\u043d\u0438\u0442\u044c \u0447\u0442\u043e \u0436\u0435 \u044d\u0442\u043e \u0442\u0430\u043a\u043e\u0435.<\/p>\n<p>\u041f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u2014 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u043a, \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0430\u044f \u0432 \u0432\u0438\u0434\u0435 \u0434\u0435\u0440\u0435\u0432\u0430, \u0433\u0434\u0435 \u043d\u0430 \u0440\u0435\u0431\u0440\u0430\u0445 \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u0441\u0438\u043c\u0432\u043e\u043b\u044b, \u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u044b \u043a\u0430\u043a \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0435. \u041a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438 \u0434\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435\u043c \u043d\u0438\u0436\u0435:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0440\u043e\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u0432\u0438\u0434\u0435 \u0441\u0441\u044b\u043b\u043e\u043a \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u044b. \u041a\u0430\u0436\u0434\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u044b\u0447\u043d\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0442\u043e\u043c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u043e\u043d\u0430 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439, \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0441\u0432\u044f\u0437\u0430\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043b\u044e\u0431\u0430\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f, \u0437\u0430\u0432\u0438\u0441\u044f\u0449\u0430\u044f \u043e\u0442 \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<p>\u0414\u043b\u044f \u043b\u0430\u0442\u0438\u043d\u0441\u043a\u043e\u0433\u043e \u0430\u043b\u0444\u0430\u0432\u0438\u0442\u0430 \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e \u043f\u043e \u043f\u0440\u0438\u043d\u0438\u0446\u0438\u043f\u0443 \u043d\u0438\u0436\u0435.<\/p>\n<p>\u041f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0434\u0435\u0440\u0435\u0432\u043e, \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043a\u0430\u0440\u0442\u0443 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044f \u0431\u0443\u043a\u0432 \u0446\u0438\u0444\u0440\u0430\u043c \u0432 \u0440\u0430\u0441\u043a\u043b\u0430\u0434\u043a\u0435 \u043a\u043b\u0430\u0432\u0438\u0430\u0442\u0443\u0440\u044b \u043a\u043d\u043e\u043f\u043e\u0447\u043d\u043e\u0433\u043e \u0442\u0435\u043b\u0435\u0444\u043e\u043d\u0430:<\/p>\n<details class=\"spoiler\">\n<summary>\u041c\u0430\u043f\u043f\u0438\u043d\u0433<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"javascript\">export const keyMap: Record&lt;string, number> = {   a: 2,   b: 2,   c: 2,   d: 3,   e: 3,   f: 3,   g: 4,   h: 4,   i: 4,   j: 5,   k: 5,   l: 5,   m: 6,   n: 6,   o: 6,   p: 7,   q: 7,   r: 7,   s: 7,   t: 8,   u: 8,   v: 8,   w: 9,   x: 9,   y: 9,   z: 9, };<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043a \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430. \u0414\u043b\u044f \u044d\u0442\u0438\u0445 \u0446\u0435\u043b\u0435\u0439 \u0437\u0430\u0432\u0435\u0434\u0435\u043c \u043a\u043b\u0430\u0441\u0441 Trie, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0434\u0432\u0430 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u0430\u00a0<strong>insert\u00a0<\/strong>\u0438\u00a0<strong>getPredictions<\/strong>. \u0417\u0434\u0435\u0441\u044c \u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u0443\u0442\u0435\u043c \u043e\u0431\u0445\u043e\u0434\u0430 \u0434\u0435\u0440\u0435\u0432\u0430 \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441\u043e \u0441\u0442\u0440\u043e\u043a\u043e\u0439, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u0432\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0430, \u0430 \u0437\u0430\u0442\u0435\u043c \u0432 \u0441\u0443\u0444\u0444\u0438\u043a\u0441 \u0441\u0442\u0440\u043e\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0435 \u0443\u0437\u043b\u044b, \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435\u0441\u044f \u0432 \u0434\u0435\u0440\u0435\u0432\u0435. \u041c\u0435\u0442\u043e\u0434\u00a0<strong>getPredictions\u00a0<\/strong>\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u0443\u0442\u0435\u043c \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u043e\u0442 \u043a\u043e\u0440\u043d\u044f \u043f\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430\u043c \u0441\u043b\u043e\u0432\u0430. \u0415\u0441\u043b\u0438 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043a\u043e\u043d\u0435\u0447\u043d\u0430 \u2014 \u0442\u043e \u0441\u043b\u043e\u0432\u043e \u043d\u0430\u0439\u0434\u0435\u043d\u043e, \u0432 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0435\u0442.<\/p>\n<details class=\"spoiler\">\n<summary>\u041a\u043e\u0434 \u043a\u043b\u0430\u0441\u0441\u0430<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"javascript\">import { keyMap, rootGenerator } from \".\/helpers\"; import { Root, Prediction } from '.\/interfaces';  class Trie {   root: Root;    constructor() {     this.root = rootGenerator();   }    insert(word: string): void {     if (word.length === 0) throw new Error(\"A word has to be specified\");      let currentNode = this.root;          Array.from(word).forEach((letter, index) => {       const digit = keyMap[letter];       let isLastNode = false;        if (word.length === index + 1) isLastNode = true;       if (!digit) throw new Error(\"Not a valid digit\");       if (!currentNode.children) currentNode.children = {};       if (!currentNode.children[digit]) currentNode.children[digit] = rootGenerator();        currentNode = currentNode.children[digit] as Root;        if (!isLastNode) currentNode.predictions.deep.push(word);     });      currentNode.predictions.current.push(word);   }    getPredictions(keyString: string): Prediction | undefined {     const state = rootGenerator().predictions;      let currentNode = this.root;     let predictions;      Array.from(keyString).forEach((nodeKey) => {       if (!currentNode.children || !currentNode.children[nodeKey]) {         predictions = state;         return;       }              currentNode = currentNode.children[nodeKey] as Root;       predictions = currentNode.predictions;     });      return predictions;   } }  export default Trie;<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0434\u0435\u0440\u0435\u0432\u0430 \u0438 \u0440\u0430\u0437\u0431\u043e\u0440\u0430 \u0441\u043b\u043e\u0432\u0430\u0440\u044f. \u0422\u0443\u0442 \u043d\u0435\u0442 \u043a\u0430\u043a\u0438\u0445-\u0442\u043e \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0431\u0435\u0437 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0435\u0432.<\/p>\n<details class=\"spoiler\">\n<summary>\u041f\u0430\u0440\u0441\u0438\u043d\u0433<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"javascript\">import { readFileSync } from \"fs\";  import Trie from \".\/trie\";  import { rootGenerator } from \".\/helpers\"; import { Prediction } from \".\/interfaces\";  class TrieParser {   trie: Trie;   filePath: string;    constructor(filePath: string) {     this.trie = new Trie();     this.filePath = filePath;   }    createTrie(words?: string[]): void {     const wordArray = words || this.parseDictionary();  \/\/ Creating trie from words array     wordArray.forEach((word) => {       this.trie.insert(word);     });   }    parseDictionary(): string[] {     const filePath = this.filePath;     const data = readFileSync(filePath, {       encoding: \"utf8\",     });     const regex = \/\\r?\\n\/;     const array = data.toString().split(regex);      return array;   }    getPredictions(keyString: string): Prediction | undefined {     if (!keyString) return rootGenerator().predictions;     return this.trie.getPredictions(keyString);   } }  export default TrieParser;<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u0414\u0435\u043b\u043e \u0437\u0430 \u043c\u0430\u043b\u044b\u043c \u2014 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f, \u043a\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0438 \u0432\u0441\u0435\u0445 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0445 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0439. \u041a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043a \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0442\u0430\u043a\u0438\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u2014\u00a0<a href=\"http:\/\/homepage.math.uiowa.edu\/~goodman\/22m150.dir\/2007\/Permutation%20Generation%20Methods.pdf\" rel=\"noopener noreferrer nofollow\">\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043a\u0443\u0447\u0438<\/a>, \u043e\u0434\u043d\u0430\u043a\u043e \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u043d \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b, \u0430 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u2014\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Cartesian_product\" rel=\"noopener noreferrer nofollow\">\u0434\u0435\u043a\u0430\u0440\u0442\u043e\u0432\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/a>, \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"javascript\">export function cartesian&lt;T>(...words: T[][]): T[][] {   \/\/ iteratively get the cartesian product   return words.reduce&lt;T[][]>(     \/\/ part - cartesian product of the first few arrays from words     (part, array) =>       part.flatMap((cartesianPart) =>         \/\/cartesianPart is an array-prefix of one of the elements of the cartesian product         array.map((element) => [...cartesianPart, element])       ),     [[]]   ); }<\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u0441\u0430\u043c\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f, \u0438 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b. \u041a\u043e\u0434 \u0441 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u043c\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043d\u0438\u0436\u0435:<\/p>\n<details class=\"spoiler\">\n<summary>\u0424\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043b\u0438\u0441\u0442\u0438\u043d\u0433<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"javascript\">import { readFileSync, createWriteStream, unlinkSync, existsSync } from \"fs\";  import { cartesian } from \".\/utils\/cartesian\";  import TrieParser from \".\/trie\/trie-parser.js\";  const dictionaryFilePath = \"src\/data\/dictionary.txt\"; const combinationsFilePath = \"src\/data\/combinations.txt\"; const outputFilePath = \"src\/data\/output.txt\";  const trieDictionaryInstance = new TrieParser(dictionaryFilePath);  trieDictionaryInstance.createTrie();  export function main() {   try {     if (existsSync(outputFilePath)) unlinkSync(outputFilePath);     \/\/ create file stream     const output = createWriteStream(outputFilePath, {       flags: \"a\",     });     const rawData = readFileSync(combinationsFilePath, { encoding: \"utf-8\" })       .toString()       .split(\/\\r?\\n\/);      for (const rawCombination of rawData) {       const combinations = rawCombination.split(\" \");       const predictionArray: string[][] = combinations.map((combination) => {         \/\/ Get predictions for each combination in the string         const result = trieDictionaryInstance.getPredictions(combination);         const targetArray = result?.current.flatMap((item) =>           Array.isArray(item) ? item : [item]         );         return targetArray;       }) as string[][];       \/\/ Get cartesian products       const possibleProducts = (         [...cartesian(...(predictionArray as [string[]]))] as string[][]       ).map((permutation) => permutation.join(\" \"));        for (const permutation of possibleProducts) {         output.write(permutation + \"\\r\");       }     }      output.end();   } catch (e) {     console.error(e);   } }  main();<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u041a\u043e\u0434 \u0437\u0430\u0434\u0430\u0447\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043d\u0430 <a href=\"https:\/\/github.com\/eyudinkov\/t9-trie.git\" rel=\"noopener noreferrer nofollow\">\u0433\u0438\u0442\u0445\u0430\u0431<\/a>. \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435!<\/p>\n<\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/599029\/\"> https:\/\/habr.com\/ru\/post\/599029\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-327072","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/327072","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=327072"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/327072\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=327072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=327072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=327072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}