UNPKG

1.75 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _lodash = require('lodash');
8
9var _lodash2 = _interopRequireDefault(_lodash);
10
11var _async = require('async');
12
13var _async2 = _interopRequireDefault(_async);
14
15var _wordpos = require('wordpos');
16
17var _wordpos2 = _interopRequireDefault(_wordpos);
18
19function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
21const wordpos = new _wordpos2.default();
22
23// Unhandled promises should throw top-level errors, not just silently fail
24// This is a shim for wordnet lookup.
25// http://wordnet.princeton.edu/wordnet/man/wninput.5WN.html
26
27process.on('unhandledRejection', err => {
28 throw err;
29});
30
31// Does a word lookup
32// @word can be a word or a word/pos to filter out unwanted types
33const lookup = function lookup(word, pointerSymbol = '~', cb) {
34 let pos = null;
35
36 const match = word.match(/~(\w)$/);
37 if (match) {
38 pos = match[1];
39 word = word.replace(match[0], '');
40 }
41
42 return wordpos.lookup(word).then(results => {
43 const synets = [];
44
45 results.forEach(result => {
46 result.ptrs.forEach(part => {
47 if (pos !== null && part.pos === pos && part.pointerSymbol === pointerSymbol) {
48 synets.push(part);
49 } else if (pos === null && part.pointerSymbol === pointerSymbol) {
50 synets.push(part);
51 }
52 });
53 });
54
55 const itor = (word1, next) => {
56 wordpos.seek(word1.synsetOffset, word1.pos).then(sub => {
57 next(null, sub.lemma);
58 });
59 };
60
61 _async2.default.map(synets, itor, (err, items) => {
62 items = _lodash2.default.uniq(items);
63 items = items.map(x => x.replace(/_/g, ' '));
64 cb(err, items);
65 });
66 });
67};
68
69exports.default = {
70 lookup
71};
\No newline at end of file