spencermountain / compromise

modest natural-language processing
http://compromise.cool
MIT License
11.49k stars 655 forks source link

`replaceWith()`s `keep.keepTags` not keeping tags when enabled #1120

Closed Fdawgs closed 3 months ago

Fdawgs commented 4 months ago

Node version: 22.2.0 Compromise version: 14.13.0

Using replaceWith, with the keep.keepTags param property set to true, I would expect it to retain tags as documented.

Example:

'use strict'

/** @type {import('compromise').default} */
const nlp = require('compromise')

const text = 'Worst-case Ontario, you get caught.'

const doc = nlp(text)
console.log(doc.out('tags'))
/*
Prints:
[
  {
    worst: [ 'Adjective', 'Superlative', 'Hyphenated' ],
    case: [ 'Noun', 'Singular', 'Hyphenated' ],
    ontario: [ 'Noun', 'Singular', 'Place', 'ProperNoun', 'Region' ],
    you: [ 'Noun', 'Pronoun' ],
    get: [ 'Verb', 'PresentTense', 'Infinitive' ],
    caught: [ 'Verb', 'PastTense' ]
  }
]
*/

doc.match('Ontario').replaceWith('scenario', { keepTags: true })
console.log(doc.out('tags'))
/*
Prints:
[
  {
    worst: [ 'Adjective', 'Superlative', 'Hyphenated' ],
    case: [ 'Noun', 'Singular', 'Hyphenated' ],
    scenario: [ 'Noun', 'Singular' ],
    you: [ 'Noun', 'Pronoun' ],
    get: [ 'Verb', 'PresentTense', 'Infinitive' ],
    caught: [ 'Verb', 'PastTense' ]
  }
]

Expected:
[
  {
    worst: [ 'Adjective', 'Superlative', 'Hyphenated' ],
    case: [ 'Noun', 'Singular', 'Hyphenated' ],
    scenario: [ 'Noun', 'Singular', 'Place', 'ProperNoun', 'Region' ],
    you: [ 'Noun', 'Pronoun' ],
    get: [ 'Verb', 'PresentTense', 'Infinitive' ],
    caught: [ 'Verb', 'PastTense' ]
  }
]
  */
spencermountain commented 4 months ago

hey - you're right, this seems like a bug, to me. Will put it on the pile! cheers

spencermountain commented 3 months ago

hey Frazer, i don't remember when the API changed for this, but the usage now is { tags: true }:

const doc = nlp('Worst-case Ontario, you get caught.')
doc.match('Ontario').replaceWith('scenario', { tags: true })
doc.debug()

sorry for the delay, cheers