All contributions

spencermountain/compromise

compromise

A natural language processing library in JavaScript: it splits text into sentences and words, tags parts of speech, conjugates verbs and inflects nouns, and reads dates through a plugin. It runs in the browser and in Node.js.

Language
JavaScript
GitHub stars
12,176
Merged changes
23
Project site
compromise.cool

What we changed

We fixed how the main clause verb is chosen, how contractions are built and expanded (can't, he's not, wanna), verb and noun conversions, dates (fortnight, a month's time) and the match pattern parser. We also fixed the payload and speech plugins and a test that crashed the test run against the built library.

What this work shows

  • Natural language processing (NLP)
  • Syntax parsing: subject, verb, subordinate clauses
  • Tokenisation and match pattern parsing
  • Regression tests with tape
What was broken

The verb came from a secondary clause

mainClause dropped every part of a sentence containing that or who, so in a sentence with a list after "that included" the verb came out as "state engineering" instead of "led". Sentences opening with "To win", "Had I known" or "In case", and a relative clause right after the subject ("The boy who you saw..."), also took the verb from the wrong part.

How we fixed it

The verb comes from the main clause

A part of a sentence is now dropped only when the subordinator or relative pronoun comes before its own verb, and "since 2010" or "once upon a time" no longer count as subordinators. We recognise more openers of a secondary clause and skip the verb of a relative clause after the subject: on a probe of 20 sentences with such openers, 18 now come out right instead of 14.

#1225 #1224 #1222

What was broken

Contractions came out mangled

contract() turned "can not" into "cann't" and "he is not" into "he'sn't", and treated two matches in one sentence as one: "he is here and she is there" became "he'shere and there". Expanding "I wanna pickle" added a stray "to", and "Leonard's was Randall" became "Leonard has was".

How we fixed it

Each contraction is built on its own

Each match is contracted on its own, "can not" gives "can't", "he is not" gives "he's not", and the n't list now covers "did not" and "were not". "wanna" before a noun expands to "want" without "to", and 's before is, are, was, were or am is read as possessive.

#1244 #1243 #1240 #1237 #1230

What was broken

Conversions lost meaning and letter case

toPositive() only turned "He does not like camels" into "He does like camels", and going to the infinitive turned "I will never do that" into "I do not never do that". toSingular made "Great Danes" into "great dane", and a capitalised "Higher" before "than" was tagged as a proper noun.

How we fixed it

Correct forms and original letter case

toPositive() drops does and did and inflects the verb ("He likes camels", "she went home"), and toNegative() still restores the original. never stays without an extra "do not", toSingular gives each word back its original case, all caps included, and a known comparative right before "than" stays an adjective.

#1242 #1239 #1229 #1228

What was broken

The dates plugin missed some phrases

fortnight was not in the duration lexicon, so "in a fortnight" did not work, and "in a month's time" matched no unit because parseUnit only stripped a trailing s. In "2025-05-11 - They have taken the bridge", .dates().text() returned the dash as well.

How we fixed it

Two weeks, a month and a clean date

fortnight is in the duration lexicon and counts as two weeks, and parseUnit also strips 's, so "in a month's time" lands a month out. A spaced dash after the last word of a match is dropped like other end punctuation, while the range "june 5 - june 7" stays whole.

#1247 #1236 #1231

What was broken

Matching ignored some characters

The tokeniser cut the closing bracket off "foo(bar)", and lexicon words starting with # or @, such as #GoJetsGo, never matched. The match parser split hyphens differently from the tokeniser, so has("x-ray") looked for "x ray", and + or / escaped with a backslash still acted as an operator and a regex.

How we fixed it

The parser reads characters like the tokeniser

A closing bracket that closes one inside the word stays in the word, for [] and {} too. The lexicon lookup also tries the normal form with # or @, the match parser splits hyphens with the tokeniser's own hasHyphen, and escaped + and / are read as plain characters.

#1253 #1249 #1248 #1241

What was broken

The payload and speech plugins went wrong

The payload plugin looked data up by sentence number, so after .remove() of an earlier sentence later payloads landed on the wrong words or went missing, and a new document could see an old one's payloads. The speech plugin merged the -ent syllable into the first one in silent, talent, parent and agent, and pnpm testb crashed with doc.normalize is not a function.

How we fixed it

Payloads survive .remove(), syllables split

Payloads are found by the ids of their first and last terms, so they survive .remove(), and the limitations section is gone from the README. silent, talent, parent and agent now have two syllables, and the regex-boundaries test uses the full build, which includes normalize.

#1246 #1233 #1232

What was broken

An empty string replaced the whole document

replace('dog', '') on the document "the dog sat on the mat" turned the whole text into "dog". The empty string was read as a missing second argument, so the call went to replaceWith('dog').

How we fixed it

An empty string removes the match

replace now hands over to replaceWith only when the second argument is undefined. replace('dog', '') gives "the sat on the mat", just like match('dog').replaceWith(''), and a new test also checks a document with two sentences.

#1278

Merged changes 23

  1. #1278 let replace() remove a match when the replacement is an empty string Merged 21 November 2026
  2. #1253 fix match escaping for + and / Merged 28 October 2026
  3. #1246 find payloads by term id so they survive a .remove() Merged 6 October 2026
  4. #1242 keep a titlecase comparative before 'than' as an adjective Merged 6 October 2026
  5. #1249 keep a closing bracket in the word when it closes one inside it Merged 5 October 2026
  6. #1248 match lexicon words that start with # or @ Merged 5 October 2026
  7. #1247 leave a spaced dash out of a match's text Merged 5 October 2026
  8. #1244 contract each match on its own Merged 5 October 2026
  9. #1243 don't add an implicit 'to' after 'wanna' before a noun Merged 5 October 2026
  10. #1241 split hyphens in a match the same way the tokenizer does Merged 5 October 2026
  11. #1240 contract 'he is not' to he's not instead of he'sn't Merged 5 October 2026
  12. #1239 drop does/did when turning a negative verb positive Merged 5 October 2026
  13. #1237 contract 'can not' to can't Merged 5 October 2026
  14. #1236 dates: read singular possessive durations Merged 5 October 2026
  15. #1233 don't merge an -ent syllable back into an open first syllable Merged 5 October 2026
  16. #1232 run the regex-boundaries tests on the full build Merged 5 October 2026
  17. #1231 parse 'fortnight' as two weeks Merged 5 October 2026
  18. #1230 Treat 's before a copula as possessive Merged 29 September 2026
  19. #1229 Keep the case in toSingular Merged 29 September 2026
  20. #1228 Keep "never" when going to the infinitive Merged 29 September 2026
  21. #1225 Skip the verb of a relative clause following the subject Merged 19 September 2026
  22. #1224 Recognize more openers of a secondary clause in mainClause Merged 19 September 2026
  23. #1222 Only treat subordinators as secondary-clause signals before the verb Merged 14 September 2026

What it taught us

In a large rule-based parser changing one rule easily breaks another, so every fix ships with its own test cases. For the mainClause change we also measured the effect on performance.

More about this technology: TypeScript, React and Node

Other projects we write code for

Describe your project.

A free quote comes back within an hour during working hours.