UNPKG

wordpos

Version:

wordpos is a set of part-of-speech utilities for Node.js & browser using the WordNet database.

30 lines (28 loc) 19.2 kB
/*! Copyright (c) 2012-2020 mooster@42at.com https://github.com/moos/wordpos (The MIT License) Princeton University "About WordNet." WordNet (https://wordnet.princeton.edu/). Princeton University. 2010. wordpos v2.1.0 wordnet-db v3.1.14 WordNet DB version 3.1 */ parcelRequire=function(e,r,t,n){var i,o="function"==typeof parcelRequire&&parcelRequire,u="function"==typeof require&&require;function f(t,n){if(!r[t]){if(!e[t]){var i="function"==typeof parcelRequire&&parcelRequire;if(!n&&i)return i(t,!0);if(o)return o(t,!0);if(u&&"string"==typeof t)return u(t);var c=new Error("Cannot find module '"+t+"'");throw c.code="MODULE_NOT_FOUND",c}p.resolve=function(r){return e[t][1][r]||r},p.cache={};var l=r[t]=new f.Module(t);e[t][0].call(l.exports,p,l,l.exports,this)}return r[t].exports;function p(e){return f(p.resolve(e))}}f.isParcelRequire=!0,f.Module=function(e){this.id=e,this.bundle=f,this.exports={}},f.modules=e,f.cache=r,f.parent=o,f.register=function(r,t){e[r]=[function(e,r){r.exports=t},{}]};for(var c=0;c<t.length;c++)try{f(t[c])}catch(e){i||(i=e)}if(t.length){var l=f(t[t.length-1]);"object"==typeof exports&&"undefined"!=typeof module?module.exports=l:"function"==typeof define&&define.amd?define(function(){return l}):n&&(this[n]=l)}if(parcelRequire=f,i)throw i;return f}({"B54D":[function(require,module,exports) { var e=["about","after","all","also","am","an","and","another","any","are","as","at","be","because","been","before","being","between","both","but","by","came","can","come","could","did","do","each","for","from","get","got","has","had","he","have","her","here","him","himself","his","how","if","in","into","is","it","like","make","many","me","might","more","most","much","must","my","never","now","of","on","only","or","other","our","out","over","said","same","see","should","since","some","still","such","take","than","that","the","their","them","then","there","these","they","this","those","through","to","too","under","up","very","was","way","we","well","were","what","where","which","while","who","with","would","you","your","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","$","1","2","3","4","5","6","7","8","9","0","_"];exports.words=e; },{}],"UvVQ":[function(require,module,exports) { var n=require("../lib/natural/util/stopwords").words,t=r(n);function r(n){return" "+n.join(" ")+" "}function o(n,t){n&&n.apply(null,t)}function e(n){return String("00000000"+n).slice(-"00000000".length)}function i(n){return n.toLowerCase().replace(/\s+/g,"_")}function u(n,t){return n.indexOf(" "+t.toLowerCase()+" ")>=0}function c(n){return n.split(/\W+/)}function l(n){return n.filter(function(t,r){return n.indexOf(t)===r})}function a(n,t){return n.filter(function(n){return!t.includes(n)})}function f(n){return[].concat.apply([],n)}function s(n,t){for(var r=[],o=new Array(t),e=0;e<t;e++){var i=Math.floor(Math.random()*(n.length-e)+e),u=n[void 0===r[i]?i:r[i]];if(void 0===u){o.length=e;break}o[e]=u,r[i]=void 0===r[e]?e:r[e]}return o}function p(n){return"string"==typeof n}function d(n,t){return n.filter(function(n){return!t(n)})}function y(n){if(Array.isArray(n))return n;var r=l(c(n));return this.options.stopwords?d(r,u.bind(null,p(this.options.stopwords)?this.options.stopwords:t)):r}var w=Function.bind.call(Function.call,Array.prototype.reduce),b=Function.bind.call(Function.call,Object.prototype.propertyIsEnumerable),v=Function.bind.call(Function.call,Array.prototype.concat),g=Reflect.ownKeys;Object.values||(Object.values=function(n){return w(g(n),function(t,r){return v(t,"string"==typeof r&&b(n,r)?[n[r]]:[])},[])}),module.exports={isString:p,zeroPad:e,stopwords:n,nextTick:o,normalize:i,tokenizer:c,prepText:y,makeStopwordString:r,uniq:l,diff:a,flat:f,sample:s}; },{"../lib/natural/util/stopwords":"B54D"}],"uNn4":[function(require,module,exports) { var n=require("./util"),e=n.normalize,t=n.nextTick,o=n.isString,r=n.uniq,s=n.sample,u=n.diff,i=n.flat;function a(n,e){return o(n)&&(n=new RangeError(n)),e&&e(n,{}),Promise.reject(n)}function c(n){return function(o,r){var s=this.options.profile,u=s&&new Date,i=this.getFilesFor(n),a=[];return o=e(o),i.index.lookup(o).then(function(n){return n?i.data.lookup(n.synsetOffset).then(c):c([])}).catch(c);function c(n){return n instanceof Error?a.push([],o):a.push(n,o),s&&a.push(new Date-u),t(r,a),n}}}function l(n,e){var t=this;return new Promise(function(o,r){t.find(n,function(n){var t,r=null;if("hit"==n.status){var s=[],u=[],i=parseInt(n.tokens[3]);for(t=0;t<i;t++)s.push(n.tokens[t]);for(i=parseInt(n.tokens[2]),t=0;t<i;t++)u.push(n.tokens[s.length+6+t]);r={lemma:n.tokens[0],pos:n.tokens[1],ptrSymbol:s,senseCnt:parseInt(n.tokens[s.length+4],10),tagsenseCnt:parseInt(n.tokens[s.length+5],10),synsetOffset:u}}e&&e(r),o(r)})})}function p(n,e){var o=this,r=[],s=this.options.profile,u=s&&new Date;return Promise.all(["lookupAdverb","lookupAdjective","lookupVerb","lookupNoun"].map(function(e){return o[e].call(o,n).then(function(n){r=r.concat(n)})})).then(function(){var o=[r,n];return s&&o.push(new Date-u),t(e,o),r}).catch(function(o){throw t(e,[[],n]),o})}function f(n){return function(e,o,r){var s=this.options.profile&&!r,u=s&&new Date,i=this.parse(e),a=[],c=this;return l(i[0]).then(function(){return Promise.all(i.slice(1).map(l))}).then(function(){var n=[a];return s&&n.push(new Date-u),t(o,n),a}).catch(function(n){return Promise.reject(n)});function l(e){return c[n].call(c,e,null,!0).then(function(n){n&&a.push(e)})}}}function h(n,e){var o=this,s={nouns:[],verbs:[],adjectives:[],adverbs:[],rest:[]},a=this.options.profile,c=a&&new Date,l=this.parse(n);return Promise.all(["getAdverbs","getAdjectives","getVerbs","getNouns"].map(function(e){return o[e].call(o,n,null,!0).then(function(n){var t=e.replace("get","").toLowerCase();s[t]=n})})).then(function(){var n=[s],o=r(i(Object.values(s)));return s.rest=u(l,o),a&&n.push(new Date-c),t(e,n),s}).catch(function(n){throw console.log("Error >>> ",n),t(e,[]),n})}function v(n){return function(o,r,s){var u=this.options.profile&&!s,i=u&&new Date,a=[],c=this.getFilesFor(n).index;return o=e(o),c.lookup(o).then(function(n){var e=!!n;return a.push(e,o),u&&a.push(new Date-i),t(r,a),e})}}function m(n,e){var t,o=n.split("| "),r=o[0].split(/\s+/),s=[],u=parseInt(r[3],16),i=[];for(t=0;t<u;t++)i.push(r[4+2*t]);var a=2*(u-1)+6,c=parseInt(r[a],10);for(t=0;t<c;t++)s.push({pointerSymbol:r[a+1+4*t],synsetOffset:r[a+2+4*t],pos:r[a+3+4*t],sourceTarget:r[a+4+4*t]});for(var l=o[1].split("; "),p=l[0],f=l.slice(1),h=parseInt(r[1],10),v=0;v<f.length;v++)f[v]=f[v].replace(/\"/g,"").replace(/\s\s+/g,"");return{synsetOffset:r[0],lexFilenum:h,lexName:g[h],pos:r[2],wCnt:u,lemma:r[4],synonyms:i,lexId:r[5],ptrs:s,gloss:o[1],def:p,exp:f}}function b(n,e,t){var o=Number(n);if(isNaN(o)||o<=0)return a("Offset must be valid positive number: "+n,t);var r=this.getFilesFor(e).data;return r?r.lookup(n,t):a("Incorrect POS - 2nd argument must be a, r, n or v.",t)}var g=["adj.all","adj.pert","adv.all","noun.Tops","noun.act","noun.animal","noun.artifact","noun.attribute","noun.body","noun.cognition","noun.communication","noun.event","noun.feeling","noun.food","noun.group","noun.location","noun.motive","noun.object","noun.person","noun.phenomenon","noun.plant","noun.possession","noun.process","noun.quantity","noun.relation","noun.shape","noun.state","noun.substance","noun.time","verb.body","verb.change","verb.cognition","verb.communication","verb.competition","verb.consumption","verb.contact","verb.creation","verb.emotion","verb.motion","verb.perception","verb.possession","verb.social","verb.stative","verb.weather","adj.ppl"];module.exports={indexLookup:l,is:v,get:f,seek:b,getPOS:h,lineDataToJSON:m,LEX_NAMES:g,lookup:c,lookupPOS:p}; },{"./util":"UvVQ"}],"yHSB":[function(require,module,exports) { var t=require("./util"),n=t.uniq,e=t.sample;function i(t){return function(n,e,i){var r=this.options.profile&&!i,o=r&&new Date,u=[],a=this.getFilesFor(t).index,s=n&&n.startsWith||"",l=n&&n.count||1;return"function"==typeof n&&(e=n),a.rand(s,l,function(t){u.push(t,s),r&&u.push(new Date-o),e&&e.apply(null,u)})}}function r(t,i){"function"==typeof t?(i=t,t={}):t=Object.assign({startsWith:"",count:1},t);var r=this.options.profile,u=r&&new Date,a=[],s=t.count,l=[null,t.startsWith],c="Noun Verb Adjective Adverb".split(" "),p=this;return new Promise(function(f,h){var d=e(c,c.length);function v(){var n=d.pop(),e="rand"+n,i=o[n]/o.Total;t.count=Math.ceil(s*i*1.1),p[e](t,b)}function b(t){if(t&&(a=n(a.concat(t))),a.length<s&&d.length)return v();a=e(a,s),r&&l.push(new Date-u),l[0]=a,i&&i.apply(null,l),f(a)}v()})}var o={Noun:26,Verb:3,Adjective:5,Adverb:1,Total:37};module.exports={randX:i,rand:r}; },{"./util":"UvVQ"}],"wnIH":[function(require,module,exports) { function e(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function t(e,t){for(var o=0;o<t.length;o++){var n=t[o];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function o(e,o,n){return o&&t(e.prototype,o),n&&t(e,n),e}function n(e,t,o){return t in e?Object.defineProperty(e,t,{value:o,enumerable:!0,configurable:!0,writable:!0}):e[t]=o,e}var r=window.__mocha,i=function(){function t(o,r,i,a){e(this,t),n(this,"file",{}),this.type=o,this.filePath="".concat(r,"/").concat(o,".").concat(i,".js"),this.posName=i,this.loadError=null,this.options=Object.assign({},a)}return o(t,[{key:"load",value:function(){var e=this;if(this.loadError)return Promise.reject(this.loadError);if(this.loaded)return this.loaded;this.options.debug&&console.time("index load "+this.posName);var t=r?Promise.resolve(require(this.filePath)):import("".concat(this.filePath));return this.options.debug&&console.timeEnd("index load "+this.posName),this.loaded=t.then(function(t){return e.file=t.default,e}).catch(function(t){throw console.error('Error loading "'.concat(e.type,'" file ').concat(e.filePath,"."),t),e.loadError=t,t})}},{key:"ready",value:function(e,t){var o=this;return this.load().then(function(n){return e&&e.apply(o,t)||n})}}]),t}();module.exports=i; },{}],"bG4n":[function(require,module,exports) { function t(t){this.dictionary={},this.$=!1,void 0===t&&(t=!0),this.cs=t}t.prototype.addString=function(i){if(!1===this.cs&&(i=i.toLowerCase()),0===i.length){var n=this.$;return this.$=!0,n}var r=this.dictionary[i[0]];return r||(this.dictionary[i[0]]=new t(this.cs),r=this.dictionary[i[0]]),r.addString(i.substring(1))},t.prototype.addStrings=function(t){for(var i in t)this.addString(t[i])},t.prototype.keysWithPrefix=function(t){!1===this.caseSensitive&&(t=t.toLowerCase());var i=[];return function t(i,n,r){if(i&&(i.$&&r.push(n),!function(t){for(var i in t)if(t.hasOwnProperty(i))return!1;return!0}(i.dictionary)))for(var o in i.dictionary)t(i.dictionary[o],n+o,r)}(function t(i,n){return i?0==n.length?i:t(i.dictionary[n[0]],n.substring(1)):null}(this,t),t,i),i},t.prototype.contains=function(t){if(!1===this.cs&&(t=t.toLowerCase()),0===t.length)return this.$;var i=t[0],n=this.dictionary[i];return!!n&&n.contains(t.substring(1))},t.prototype.findMatchesOnPath=function(t){return!1===this.cs&&(t=t.toLowerCase()),function t(i,n,r,o){if(i.$&&o.push(r),0===n.length)return o;var s=i.dictionary[n[0]];return s?t(s,n.substring(1),r+n[0],o):o}(this,t,"",[])},t.prototype.findPrefix=function(t){return!1===this.cs&&(t=t.toLowerCase()),function t(i,n,r,o){if(i.$&&(o=r),0===n.length)return[o,n];var s=i.dictionary[n[0]];return s?t(s,n.substring(1),r+n[0],o):[o,n]}(this,t,"",null)},t.prototype.getSize=function(){var t=1;for(var i in this.dictionary)t+=this.dictionary[i].getSize();return t},module.exports=t; },{}],"sF2v":[function(require,module,exports) { function e(t){return(e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(t)}function t(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function n(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function r(e,t,r){return t&&n(e.prototype,t),r&&n(e,r),e}function i(t,n){return!n||"object"!==e(n)&&"function"!=typeof n?u(t):n}function o(e){return(o=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function u(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function s(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&f(e,t)}function f(e,t){return(f=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function c(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var a=require("../common"),l=a.indexLookup,y=require("../util"),p=y.sample,b=require("./baseFile"),h=require("../../lib/natural/trie/trie");function m(e,t){if(e in this.file){var n=this.file[e],r=n.split(/\s+/),i={status:"hit",key:e,line:n,tokens:r};i.tokens.unshift(e),t(i)}else t({status:"miss"})}function d(e,t,n){var r=this,i=function(t){return n(t,e||""),Promise.resolve(t)},o=function(e){var n=p(e,t);return i(n)},u=function(e){r.options.debug&&console.time(e+" "+r.posName)},s=function(e){r.options.debug&&console.timeEnd(e+" "+r.posName)};if(!e)return o(this.getKeys());this.trie||(u("Trie"),this.trie=new h,this.trie.addStrings(this.getKeys()),s("Trie"));var f;return u("trie-withprefix"),f=this.trie.keysWithPrefix(e),s("trie-withprefix"),f.length?o(f):i([])}var v=function(e){function n(e,r,s){var f;return t(this,n),c(u(f=i(this,o(n).call(this,"index",e,r,s))),"keys",null),f.options=Object.assign({},s),f.posName=r,f}return s(n,b),r(n,[{key:"getKeys",value:function(){return this.keys||(this.keys=Object.keys(this.file))}},{key:"lookup",value:function(){return this.ready(l,arguments)}},{key:"find",value:function(){return this.ready(m,arguments)}},{key:"rand",value:function(){return this.ready(d,arguments)}}]),n}();module.exports=v; },{"../common":"uNn4","../util":"UvVQ","./baseFile":"wnIH","../../lib/natural/trie/trie":"bG4n"}],"yi5j":[function(require,module,exports) { function t(e){return(t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(e)}function e(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function n(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function r(t,e,r){return e&&n(t.prototype,e),r&&n(t,r),t}function o(e,n){return!n||"object"!==t(n)&&"function"!=typeof n?i(e):n}function i(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function u(t){return(u=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function f(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&c(t,e)}function c(t,e){return(c=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}var a=require("../common"),l=a.lineDataToJSON,s=a.LEX_NAMES,p=require("../util"),y=p.zeroPad,b=require("./baseFile");function h(t){var e=this.file[t];return e?l(t+" "+e):{}}function m(t,e){var n=[],r=this,o=h.bind(this),i=function(t){return t.pos},u=!Array.isArray(t);return u&&(t=[t]),new Promise(function(f,c){if((n=t.map(y).map(o).filter(i)).length)u&&(n=n[0]),e&&e(null,n),f(n);else{var a=new RangeError("No data at offsets ".concat(t.join()," in ").concat(r.filePath,"."));e&&e(a,u?{}:[]),c(a)}})}var v=function(t){function n(t,r){return e(this,n),o(this,u(n).call(this,"data",t,r))}return f(n,b),r(n,[{key:"lookup",value:function(){return this.ready(m,arguments)}}]),n}();v.LEX_NAMES=s,module.exports=v; },{"../common":"uNn4","../util":"UvVQ","./baseFile":"wnIH"}],"Focm":[function(require,module,exports) { function e(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function t(e,t){for(var i=0;i<t.length;i++){var r=t[i];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function i(e,i,r){return i&&t(e.prototype,i),r&&t(e,r),e}function r(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}var n=require("../util"),o=n.stopwords,s=n.prepText,a=n.makeStopwordString,u=n.flat,l=require("../common"),d=l.is,h=l.get,p=l.getPOS,c=l.lookup,f=l.seek,v=l.lookupPOS,b=require("../rand"),k=b.randX,y=b.rand,g=require("./indexFile"),m=require("./dataFile"),w={n:"noun",v:"verb",a:"adj",r:"adv"},F=function(){function t(i){e(this,t),r(this,"options",{}),r(this,"parse",s),r(this,"seek",f),r(this,"isAdjective",d("a")),r(this,"isAdverb",d("r")),r(this,"isNoun",d("n")),r(this,"isVerb",d("v")),r(this,"getPOS",p),r(this,"getAdjectives",h("isAdjective")),r(this,"getAdverbs",h("isAdverb")),r(this,"getNouns",h("isNoun")),r(this,"getVerbs",h("isVerb")),r(this,"lookup",v),r(this,"lookupAdjective",c("a")),r(this,"lookupAdverb",c("r")),r(this,"lookupNoun",c("n")),r(this,"lookupVerb",c("v")),r(this,"rand",y),r(this,"randAdjective",k("a")),r(this,"randAdverb",k("r")),r(this,"randNoun",k("n")),r(this,"randVerb",k("v")),this.options=Object.assign({},t.defaults,i),this.initFiles(),Array.isArray(this.options.stopwords)&&(this.options.stopwords=a(this.options.stopwords))}return i(t,[{key:"ready",value:function(){return this.loaded||Promise.resolve()}},{key:"initFiles",value:function(){var e=this,t=Object.keys(w),i=function(i){return t.map(function(t,i){return new t(e.options.dictPath,w[i],e.options)}.bind(null,i))},r=function(e){return e.reduce(function(e,i,r){return e[t[r]]=i,e},{})};this.indexFiles=r(i(g)),this.dataFiles=r(i(m)),this.options.preload&&(this.loaded=this.preloadFiles(this.options.preload))}},{key:"getFilesFor",value:function(e){return{index:this.indexFiles[e],data:this.dataFiles[e]}}},{key:"preloadFiles",value:function(e){var t=this._preload(this.indexFiles,e);return this.options.includeData&&(t=Promise.all([].concat(t,this._preload(this.dataFiles,e))).then(function(e){return u(e)})),t}},{key:"_preload",value:function(e,t){var i,r=function(t){return e[t].load()};return t&&!0!==t?"string"==typeof t&&e[t]?i=r(t):t instanceof Array&&(i=Promise.all(t.map(r))):i=Promise.all(Object.keys(w).map(r)),i||Promise.reject(new RangeError('Unknown POS "'.concat(t,'" for preload.')))}}]),t}();F.defaults={dictPath:"",profile:!1,stopwords:!0,preload:!1,includeData:!1,debug:!1},F.stopwords=o,F.POS=w,module.exports=F; },{"../util":"UvVQ","../common":"uNn4","../rand":"yHSB","./indexFile":"sF2v","./dataFile":"yi5j"}]},{},["Focm"], "WordPOS") //# sourceMappingURL=/wordpos.min.js.map