predictionary
Version:
JavaScript dictionary-based word prediction library.
1 lines • 8.31 kB
JavaScript
var Predictionary=function(t){var e={};function n(r){if(e[r])return e[r].exports;var i=e[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)n.d(r,i,function(e){return t[e]}.bind(null,i));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="dist/",n(n.s=0)}([function(t,e,n){"use strict";n.r(e);var r={};r.createItem=function(t,e){if(!t)throw'parameter "word" must be specified.';var n,r=parseInt(e),i={w:t,f:0,t:{}};return(n=r)==parseInt(n)&&(i.r=r),i};var i=r;var o=function(){var t=this,e={},n=null,r=null;function o(t){if(!t)return[];var n=[];return e[t]&&n.push(e[t]),e[t.toLowerCase()]&&-1===n.indexOf(e[t.toLowerCase()])&&n.push(e[t.toLowerCase()]),e[t.toUpperCase()]&&-1===n.indexOf(e[t.toUpperCase()])&&n.push(e[t.toUpperCase()]),e[c(t)]&&-1===n.indexOf(e[c(t)])&&n.push(e[c(t)]),n}function a(t){var e=o(t);return e.length>0?e[0]:null}function c(t){return t.charAt(0).toUpperCase()+t.slice(1)}t.load=function(t){var n=JSON.parse(t);Object.keys(n).forEach(function(t){n[t].w=t}),e=n},t.toJSON=function(){var t=JSON.parse(JSON.stringify(e));return Object.keys(t).forEach(function(e){delete t[e].w}),JSON.stringify(t)},t.addWord=function(t,n){t&&(e[t]||(e[t]=i.createItem(t,n)))},t.addWords=function(e){if(!(e instanceof Array)||0===e.length)throw"words to add must be an array with at least one element.";e.forEach(function(e){t.addWord(e)})},t.deleteWord=function(t,n){Object.keys(e).forEach(function(r){if(n?t.toUpperCase()===r.toUpperCase():t===r)delete e[r];else{var i=e[r];Object.keys(i.t).forEach(function(e){(n?t.toUpperCase()===e.toUpperCase():t===e)&&delete i.t[e]})}})},t.contains=function(t,n){return n?!!e[t]:!!a(t)},t.predictCompleteWord=function(i,o){i=i||"",o=o||{};var a=[];if(Object.keys(e).forEach(function(t){0===t.toLowerCase().indexOf(i.toLowerCase())&&a.push(e[t])}),0===a.length&&i.length>1){var c=null;return(c=n&&r&&0===i.indexOf(n)?r:t.predictCompleteWord(i.substring(0,i.length-1),o)).forEach(function(t){t.fuzzyMatch=!0}),c}return n=i,r=a.map(function(t){return{word:t.w,frequency:t.f,rank:t.r}})},t.predictNextWord=function(t,e){var n=o(t),r=[];return n.forEach(function(t){Object.keys(t.t).forEach(function(e){r.push({word:e,frequency:t.t[e]})})}),r},t.learn=function(e,n,r){if(e&&(t.contains(e)||r)){r&&e&&!t.contains(e)&&t.addWord(e),r&&n&&!t.contains(n)&&t.addWord(n);var i=a(n),o=a(e);o.f++,i&&i.t&&(i.t[o.w]?i.t[o.w]++:i.t[o.w]=1)}},t.getWords=function(){return Object.keys(e)}};n.d(e,"instance",function(){return p});var a="[\\s\\.\\?!,]",c="[\\.\\?!,]",u="[\\.\\?!]";function s(){this.DEFAULT_DICTIONARY_KEY="DEFAULT_DICTIONARY_KEY";var t=this,e="PREDICT_METHOD_COMPLETE_WORD",n="PREDICT_METHOD_NEXT_WORD",r={},i=null;function a(i,o,a){var c=[];(o=o||{}).maxPredictions=o.maxPredictions||o.maxPredicitons||10,o.applyToInput=o.applyToInput||!1,Object.keys(r).forEach(function(t){var u=r[t];if(!u.disabled){var s=a===n?u.predictNextWord:a===e?u.predictCompleteWord:null;s=s||(d(i)?u.predictNextWord:u.predictCompleteWord),c=c.concat(s(f(i),o))}}),c.sort(function(t,e){return t.fuzzyMatch!==e.fuzzyMatch?t.fuzzyMatch?1:-1:t.frequency!==e.frequency?t.frequency<e.frequency?1:-1:t.rank!==e.rank?t.rank&&void 0===e.rank?-1:e.rank&&void 0===t.rank?1:t.rank<e.rank?-1:1:0});for(var u=[],s=0;s<c.length&&u.length<o.maxPredictions;s++)-1===u.indexOf(c[s].word)&&(o.applyToInput?u.push(t.applyPrediction(i,c[s].word,{dontLearn:!0})):u.push(c[s].word));return u}this.loadDictionary=function(e,n){if(!e)throw"dictionaryJSON must be specified.";n=n||t.DEFAULT_DICTIONARY_KEY;var i=new o;i.load(e),r[n]=i},this.loadDictionaries=function(e){if(!e)throw"dictionariesJSON must be specified.";r={},JSON.parse(e).forEach(function(e){t.loadDictionary(e.json,e.key)})},this.dictionaryToJSON=function(e){e=e||t.DEFAULT_DICTIONARY_KEY;var n=r[e];return n?n.toJSON():null},this.dictionariesToJSON=function(){var t=[];return Object.keys(r).forEach(function(e){t.push({key:e,json:r[e].toJSON()})}),JSON.stringify(t)},this.useDictionary=function(t){if(!t)throw"dictionaryKey must be specified.";Object.keys(r).forEach(function(e){r[e].disabled=t!==e})},this.useDictionaries=function(e){if(!(e instanceof Array))throw"dictionaryKeys must be specified and of type Array.";Object.keys(r).forEach(function(n){r[n].disabled=-1===e.indexOf(n)&&n!==t.DEFAULT_DICTIONARY_KEY})},this.useAllDictionaries=function(){Object.keys(r).forEach(function(t){r[t].disabled=!1})},this.addDictionary=function(e,n){if(!e)throw"dictionaryKey must be specified.";if(r[e])throw"dictionary already existing.";r[e]=new o,n&&n instanceof Array&&n.forEach(function(n){t.addWord(n,e)})},this.addWord=function(e,n){if(n=n||t.DEFAULT_DICTIONARY_KEY,!e)throw"element to add not specified.";r[n]||t.addDictionary(n);var i=r[n];"string"==typeof e?i.addWord(l(e)):e.word&&"string"==typeof e.word&&i.addWord(l(e.word),e.rank)},this.addWords=function(e,n){if(!(e instanceof Array))throw"elements to add must be instance of array specified.";e.forEach(function(e){t.addWord(e,n)})},this.delete=function(e,n){var i=f(e);n=n||{},i&&(n.dictionaryKey?r[n.dictionaryKey]&&r[n.dictionaryKey].deleteWord(i,n.ignoreCase):t.getDictionaryKeys().forEach(function(t){r[t].deleteWord(i,n.ignoreCase)}))},this.parseWords=function(e,n){var r=(n=n||{}).elementSeparator||";",i=n.rankSeparator||" ",o=n.wordPosition||0,a=n.wordPosition2,c=n.rankPosition,u=n.addToDictionary||t.DEFAULT_DICTIONARY_KEY;e.split(r).forEach(function(e){var n=e.split(i),r=parseInt(n[c]);if(void 0!==o&&void 0!==a){var s=n[o],f=n[a];s&&f&&t.learn(f,s,u)}else if(n[o]){var d={word:n[o].trim()};isNaN(r)||(d.rank=r),t.addWord(d,u)}})},this.getWords=function(e){var n=[];return e?r[e]&&(n=r[e].getWords()):t.getDictionaryKeys().forEach(function(t){n=n.concat(r[t].getWords())}),n},this.hasWord=function(e,n,r){var i=" "+t.getWords(n).join(" ")+" ";return new RegExp(" "+e+" ",r?"":"i").test(i)},this.predict=function(t,e){return a(t,e)},this.predictCompleteWord=function(t,n){return a(t,n,e)},this.predictNextWord=function(t,e){return a(t,e,n)},this.applyPrediction=function(e,n,r){var i=(r=r||{}).addToDictionary||(t.isUsingOnlyDefaultDictionary()?t.DEFAULT_DICTIONARY_KEY:null),o=void 0!==r.shouldCompleteLastWord?r.shouldCompleteLastWord:!d(e),a=r.dontLearn,u=f(e),s=f(e,2),l=o?e.substring(0,e.lastIndexOf(u)):e;return l.length>0&&(!d(l)||new RegExp(c).test(l[l.length-1]))&&(l+=" "),a||t.learn(n,o?s:u,i),l+n+" "},this.learn=function(e,n,i){e=l(e),n=l(n);var o=t.getDictionaryKeys(!0);if(i=1===o.length?o[0]:i,o.length>0&&(!i||!r[i])){var a=0;o.forEach(function(r){var o=0;t.hasWord(e,r)&&(o+=2),t.hasWord(n,r)&&o++,o>0&&o>=a&&(a=o,i=r)})}i=i||t.DEFAULT_DICTIONARY_KEY,r[i]||t.addDictionary(i),Object.keys(r).forEach(function(t){var o=r[t];o.disabled||o.learn(e,n,i===t)})},this.learnFromInput=function(e,n){if(d(e)){var r=f(e,2),o=f(e,3);if(r&&r!==i)return i=r,t.learn(r,o,n),!0}return!1},this.learnFromText=function(t,e){var n=this;(t=t.replace(/\s\s/g," ")).split(new RegExp(u)).forEach(function(t){for(var r=t.split(" "),i=0;i<r.length-1;i++)n.learn(r[i+1],r[i],e)})},this.getDictionaryKeys=function(t){return t?Object.keys(r).filter(function(t){return!r[t].disabled}):Object.keys(r)},this.isUsingOnlyDefaultDictionary=function(){var e=t.getDictionaryKeys();return 0===e.length||1===e.length&&e[0]===t.DEFAULT_DICTIONARY_KEY}}function f(t,e){e=e||1;var n=t.trim().split(new RegExp(a)).filter(function(t){return!!t});return(n[n.length-e]||"").replace(new RegExp(a,"g"),"")}function d(t){return new RegExp(a).test(t[t.length-1])}function l(t){return(t=t||"").replace(/[^a-z0-9áéíóúñüäöß'`´’]/gim,"")}s.instance=function(){return new s};e.default=s;function p(){return new s}}]);