Upload files to "/"
This commit is contained in:
+98
@@ -0,0 +1,98 @@
|
||||
// Offline thesaurus built on the Princeton WordNet 3.1 database files in ./wordnet.
|
||||
// WordNet is free to use under its own license (see wordnet/LICENSE and the file headers).
|
||||
'use strict';
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const DIR = path.join(__dirname, 'wordnet');
|
||||
const POS = { noun: 'n', verb: 'v', adj: 'a', adv: 'r' };
|
||||
const POS_NAME = { noun: 'Noun', verb: 'Verb', adj: 'Adjective', adv: 'Adverb' };
|
||||
|
||||
const index = {}; // pos -> Map(lemma -> [offsets])
|
||||
const data = {}; // pos -> Buffer
|
||||
|
||||
function load(pos) {
|
||||
if (index[pos]) return;
|
||||
const m = new Map();
|
||||
const txt = fs.readFileSync(path.join(DIR, 'index.' + pos), 'latin1');
|
||||
txt.split('\n').forEach((line) => {
|
||||
if (!line || line.charAt(0) === ' ') return;
|
||||
const f = line.split(' ');
|
||||
const ptrCnt = parseInt(f[3], 10);
|
||||
const senseCnt = parseInt(f[4 + ptrCnt], 10);
|
||||
const offs = f.slice(6 + ptrCnt, 6 + ptrCnt + senseCnt);
|
||||
m.set(f[0], offs);
|
||||
});
|
||||
index[pos] = m;
|
||||
data[pos] = fs.readFileSync(path.join(DIR, 'data.' + pos));
|
||||
}
|
||||
|
||||
function synset(pos, off) {
|
||||
const buf = data[pos];
|
||||
const start = parseInt(off, 10);
|
||||
let end = buf.indexOf(10, start);
|
||||
if (end < 0) end = buf.length;
|
||||
const line = buf.toString('latin1', start, end);
|
||||
const bar = line.indexOf('|');
|
||||
const head = line.slice(0, bar).split(' ');
|
||||
const gloss = line.slice(bar + 1).trim();
|
||||
const wc = parseInt(head[3], 16);
|
||||
const words = [];
|
||||
for (let i = 0; i < wc; i++) {
|
||||
words.push(head[4 + i * 2].replace(/\(.*\)$/, '').replace(/_/g, ' '));
|
||||
}
|
||||
return { words, gloss: gloss.split(';')[0].replace(/^\s+|\s+$/g, '') };
|
||||
}
|
||||
|
||||
const IRREGULAR = {
|
||||
was: 'be', were: 'be', is: 'be', are: 'be', been: 'be', am: 'be', had: 'have', has: 'have', did: 'do', does: 'do', done: 'do',
|
||||
went: 'go', gone: 'go', goes: 'go', said: 'say', made: 'make', took: 'take', taken: 'take', came: 'come', saw: 'see', seen: 'see',
|
||||
got: 'get', gave: 'give', given: 'give', knew: 'know', known: 'know', thought: 'think', told: 'tell', found: 'find', began: 'begin',
|
||||
begun: 'begin', ran: 'run', wrote: 'write', written: 'write', men: 'man', women: 'woman', children: 'child', people: 'person',
|
||||
feet: 'foot', teeth: 'tooth', mice: 'mouse', better: 'good', best: 'good', worse: 'bad', worst: 'bad', bigger: 'big', biggest: 'big'
|
||||
};
|
||||
|
||||
function candidates(w) {
|
||||
const out = [w];
|
||||
if (IRREGULAR[w]) out.push(IRREGULAR[w]);
|
||||
const add = (x) => { if (x && x.length > 1 && out.indexOf(x) < 0) out.push(x); };
|
||||
if (/ies$/.test(w)) { add(w.slice(0, -3) + 'y'); }
|
||||
if (/(s|x|z|ch|sh)es$/.test(w)) add(w.slice(0, -2));
|
||||
if (/s$/.test(w) && !/ss$/.test(w)) add(w.slice(0, -1));
|
||||
if (/ied$/.test(w)) add(w.slice(0, -3) + 'y');
|
||||
if (/ed$/.test(w)) { add(w.slice(0, -2)); add(w.slice(0, -1)); if (/(.)\1ed$/.test(w)) add(w.slice(0, -3)); }
|
||||
if (/ing$/.test(w)) { add(w.slice(0, -3)); add(w.slice(0, -3) + 'e'); if (/(.)\1ing$/.test(w)) add(w.slice(0, -4)); }
|
||||
if (/ier$|iest$/.test(w)) add(w.replace(/ier$|iest$/, 'y'));
|
||||
if (/er$/.test(w)) { add(w.slice(0, -2)); add(w.slice(0, -1)); if (/(.)\1er$/.test(w)) add(w.slice(0, -3)); }
|
||||
if (/est$/.test(w)) { add(w.slice(0, -3)); add(w.slice(0, -2)); }
|
||||
if (/ly$/.test(w)) add(w.slice(0, -2));
|
||||
return out;
|
||||
}
|
||||
|
||||
// Returns [{pos: 'Noun', senses: [{gloss, words: [..]}]}] for a word or phrase, with the word itself left out.
|
||||
function lookup(raw) {
|
||||
const word = String(raw || '').trim().toLowerCase().replace(/\s+/g, '_');
|
||||
if (!word) return { word: '', lemma: '', groups: [] };
|
||||
let lemma = word;
|
||||
let groups = [];
|
||||
const tried = candidates(word);
|
||||
for (const cand of tried) {
|
||||
const g = [];
|
||||
Object.keys(POS).forEach((pos) => {
|
||||
load(pos);
|
||||
const offs = index[pos].get(cand);
|
||||
if (!offs) return;
|
||||
const senses = [];
|
||||
offs.forEach((o) => {
|
||||
const s = synset(pos, o);
|
||||
const words = s.words.filter((x) => x.toLowerCase() !== cand.replace(/_/g, ' '));
|
||||
if (words.length) senses.push({ gloss: s.gloss, words });
|
||||
});
|
||||
if (senses.length) g.push({ pos: POS_NAME[pos], senses });
|
||||
});
|
||||
if (g.length) { groups = g; lemma = cand; break; }
|
||||
}
|
||||
return { word: String(raw).trim(), lemma: lemma.replace(/_/g, ' '), groups };
|
||||
}
|
||||
|
||||
module.exports = { lookup };
|
||||
Reference in New Issue
Block a user