From 6a37de09a791faddc790ed40117e8cbb1b31d79c Mon Sep 17 00:00:00 2001 From: upstream Date: Sun, 4 Oct 2026 17:09:02 +0200 Subject: [PATCH] Upstream spencermountain/compromise state of the touched files before f65ca4648e --- plugins/payload/README.md | 100 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 plugins/payload/README.md diff --git a/plugins/payload/README.md b/plugins/payload/README.md new file mode 100644 index 0000000..3375451 --- /dev/null +++ b/plugins/payload/README.md @@ -0,0 +1,100 @@ +
+ + +
An ad-hoc datastore for compromise
+ + + + + + + + + + + +
+ npm install compromise-payload +
+
+ + + + +This plugin provides a facilty for storing a retreiving more complex data than tags, for compromise documents and matches. + +### Payload + +```js +import plg from 'compromise-payload' +nlp.extend(plg) + +let doc = nlp('i saw John Lennon in Manchester, and Bob Dylan in Southhampton') + +// markup metadata +doc.match('(john lennon|bob dylan)').addPayload({ instrument: 'guitar' }) + +// add more +doc.people().forEach(m => { + if (m.has('lennon')) { + m.addPayload({ height: `5'11` }) + } + if (m.has('ringo')) { + m.addPayload({ height: `5'8` }) + } +}) + +// retrieve specific payloads +doc.match('john lennon').getPayloads() +doc.match('bob dylabn').getPayloads() + +// retrieve them all +doc.getPayloads() + +// inspect given payloads: +doc.debug('payload') +``` + +You can also pass a callback into `.addPayload()`: + +```js +let doc = nlp('i saw John Lennon, and john smith and bob dylan') +doc.people().addPayload(m => { + return { lastName: m.terms().last().text() } +}) +``` + +You can remove all, or selected payloads with `.clearPayload()`: + +```js +doc.match('bob .').clearPayloads() +doc.getPayloads().length // now 2 + +doc.clearPayloads() +doc.getPayloads().length // now 0 +``` + +### Limitations + +Payloads are saved by sentence number and term position, in one store that every document from the same `nlp` shares. That means: + +- **Removing a sentence breaks the payloads that come after it.** They move to the wrong sentence, and the last one can go missing. Add payloads after you remove sentences, or add them again. Changing words inside a sentence is fine. + +```js +let doc = nlp('One is red. Two is blue. Three is green.') +doc.match('(red|blue|green)').forEach(m => m.addPayload({ color: m.text() })) +doc.remove('two is blue') +doc.getPayloads().map(p => [p.match.text(), p.val.color]) +// [['red', 'red'], ['Three is green', 'blue']] +``` + +- **A new document can see payloads from an older one** at the same sentence and term positions. Call `.clearPayloads()` on a document when you are done with it. + +```js +nlp('the red car').match('car').addPayload({ kind: 'vehicle' }) +nlp('a blue bike').getPayloads().length // 1, from the first document +``` + +See [#1188](https://github.com/spencermountain/compromise/issues/1188). + +MIT