From 219f140a9d5b5a9649e172042e7c28a7c5ee5a8f Mon Sep 17 00:00:00 2001 From: upstream Date: Wed, 14 Oct 2026 19:36:06 +0200 Subject: [PATCH] Upstream spencermountain/compromise state of the touched files before a9042f8c5f --- src/1-one/output/api/wrap.js | 40 ++++++++++++++++++++++++++++++ tests/02-two/output/out.test.js | 44 +++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/1-one/output/api/wrap.js create mode 100644 tests/02-two/output/out.test.js diff --git a/src/1-one/output/api/wrap.js b/src/1-one/output/api/wrap.js new file mode 100644 index 0000000..f4a8e45 --- /dev/null +++ b/src/1-one/output/api/wrap.js @@ -0,0 +1,40 @@ +const toText = function (term) { + const pre = term.pre || '' + const post = term.post || '' + return pre + term.text + post +} + +const findStarts = function (doc, obj) { + const starts = {} + Object.keys(obj).forEach(reg => { + const m = doc.match(reg) + m.fullPointer.forEach(a => { + starts[a[3]] = { fn: obj[reg], end: a[2] } + }) + }) + return starts +} + +const wrap = function (doc, obj) { + // index ids to highlight + const starts = findStarts(doc, obj) + let text = '' + doc.docs.forEach((terms, n) => { + for (let i = 0; i < terms.length; i += 1) { + const t = terms[i] + // do a span tag + if (Object.hasOwn(starts, t.id)) { + const { fn, end } = starts[t.id] + const m = doc.update([[n, i, end]]) + text += terms[i].pre || '' + text += fn(m) + i = end - 1 + text += terms[i].post || '' + } else { + text += toText(t) + } + } + }) + return text +} +export default wrap \ No newline at end of file diff --git a/tests/02-two/output/out.test.js b/tests/02-two/output/out.test.js new file mode 100644 index 0000000..c8f3d85 --- /dev/null +++ b/tests/02-two/output/out.test.js @@ -0,0 +1,44 @@ +import test from 'tape' +import nlp from '../../lib/two.js' +const here = '[two/output/out] ' + +test('out-tags', function (t) { + const out = nlp(`he's cool.`).out('tags') + t.equal(out.length, 1, 'one sentence') + t.equal(out[0].he.indexOf('Pronoun') !== -1, true, here + 'has he:Pronoun') + t.equal(out[0].is.indexOf('Copula') !== -1, true, here + 'has is:Copula') + t.equal(out[0].cool.indexOf('Adjective') !== -1, true, here + 'has cool:Adjective') + t.end() +}) + +test('out-topk', function (t) { + const doc = nlp(`What'd I say? Monorail. and it called? Monorail.`) + const out = doc.out('freq') + // t.equal(out.length, 3, here + 'three results') + t.equal(out[0].normal, 'monorail.', here + 'top-result is most-frequent') + t.equal(out[0].count, 2, here + 'got count') + t.end() +}) + +test('out-array', function (t) { + const arr = nlp('1-2').terms().out('array') + t.equal(arr.length, 1, here + 'one result') + t.equal(arr[0], '1-2', here + 'got contraction text') + t.end() +}) + +test('out-wrap', function (t) { + let doc = nlp("soft and yielding like a nerf ball") + let out = doc.out({ + '#Adjective': (m) => `[${m.text()}]` + }) + t.equal(out, `[soft] and [yielding] like a nerf ball`, here + 'two matches') + + // pre-post + doc = nlp("before (match) after") + out = doc.wrap({ + 'match': () => `few more words`, + }) + t.equal(out, `before (few more words) after`, here + 'pre+post') + t.end() +})