Skip to content

About

Pure-Go port of the compromise NLP subset Fair Copy's rules use (tokenizer, contractions, tagger, match syntax, values().toNumber()), with parity tests against JS compromise

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

compromise-go

A pure-Go port of the parts of compromise (v14.16.0, MIT, Spencer Kelly) that Fair Copy's NLP rules use: tokenising with offsets, contractions, the lexicon and the full two tagger (pre-tagger and the 570 post-tagger rules), the match syntax, and values().toNumber().

It exists so the Ditto backend can run Fair Copy's no-llm-speak and no-n-things rules in-process (the MCP faircopy_review_text tool and connected-chat review), without the goja-embedded compromise bundle that DITTO-2120 measured at ~0.5 s per 1 KB review and timeouts at 16 KB.

No cgo, no dependencies outside the standard library.

import (
	compromise "github.com/omniaura/compromise-go"
	"github.com/omniaura/compromise-go/faircopy"
)

doc := compromise.New("It's important to note that we found three things.")
for _, s := range doc.Match("#Value+ (thing|things)").Spans() {
	fmt.Println(s.Start, s.End) // UTF-16 offsets, as compromise reports them; ByteStart/ByteEnd too
}
diags := faircopy.Review(text) // both rules on one parse, static messages

Why a port (and why this name)

A survey of existing Go packages found nothing that covers what the rules need.

Package Status (2026-11) Gap for these rules
github.com/jdkato/prose/v3 Active again (v3.2.3, 2026-10-31), MIT, pure Go Byte offsets, Penn tagger and sentences, but no contraction expansion, no phrase matching, no number parsing
neurosnap/sentences MIT, pure Go Sentence splitting only
clipperhouse/uax29 MIT, pure Go Word/sentence segmentation only
aaaton/golem, kljensen/snowball MIT Lemmatiser / stemmer only (golem's English data is ODbL)
dharmab/numwords and similar MIT Number words to int, no offsets, no tagging
spago / cybertron (transformer taggers) BSD-2 Large models, too slow for a 2 s deadline
Go port or binding of compromise none exists —

Matching compromise's output is the point: the browser runs the JS rules, and the server should agree with it span for span. So this is a port, not a re-implementation. The name says exactly that: compromise in Go, under omniaura/.

How it is built

The algorithms are ported line by line, quirks included. The data is not retyped: tools/dump-model/dump.mjs serialises compromise's compiled model (lexicon, tag set, suffix/regex tables, switch clues, and the post-tagger rules already parsed by compromise's own parser) to model/model.json.gz (167 KB), which is embedded and decoded once per process (~30–40 ms, 17 MiB allocated).

Go file compromise source
tokenize.go src/1-one/tokenize
contractions.go src/1-one/contraction-one, src/2-two/contraction-two
lexicon.go, tag.go src/1-one/lexicon, freeze, tag
pretagger.go src/2-two/preTagger/compute/tagger
sweep.go src/1-one/sweep, src/2-two/postTagger
match_syntax.go, match_run.go src/1-one/match
doc.go views, split*, not, text output, offsets
numbers.go, fractions.go, tonumber.go src/3-three/numbers
faircopy/ @faircopy/rules-nlp 1.26.0 no-llm-speak, no-n-things

Not ported: the rest of three (verbs, nouns, sentences API, chunker, etc.), {root} match tokens (inflection), and anything the two rules do not touch.

Parity

go test ./... compares Go against JS compromise outputs stored in testdata/golden/ (generated by tools/dump-model/goldens.mjs):

Corpus Docs Terms Differences
drafts.txt (hand-written emails, legal, LLM-speak, numbers) 40 1,057 0
compromise-tests.txt (string literals from compromise's MIT test suite) 8,278 37,382 0
adversarial.txt (generated rule stress cases, written after parity was reached) 1,124 9,640 0
Penn/UD tagger sample (held out, never tuned on, not committed) 1,455 8,802 0

Per term it checks text, pre, post, normal, implicit, machine, UTF-16 offset and the tag set (also tag order, which matches); per document it checks values() and values().toNumber(). faircopy.TestRuleParity checks no-llm-speak against the JS rule and no-n-things against the corrected JS rule in goldens.mjs (see below): 82 + 2 + 899 + 4 diagnostics, all identical. The match engine has its own fixture tests (match_test.go, fixtures from tools/dump-model/match-fixtures.mjs).

The tests fail when the port is wrong: the first runs reported 258 tag differences (a lexicon fall-through bug), then 38 (number ranges and fractions), then 1 (beside['constructor'] resolving through Object.prototype), each fixed in turn.

Run the held-out Penn check locally with PARITY_EXTRA=<golden.jsonl.gz>.

Known places where Go cannot be byte-identical (none occur in the corpora): /^[\p{Emoji_Presentation}\p{Extended_Pictographic}]/ is approximated by block ranges (RE2 has no such property); Go's regexp case folding differs from JS non-unicode /i for a few non-ASCII letters; lookaround inside a user /regex/ match token never matches.

Benchmarks

go test -bench . -benchmem ./..., medians of 10, on a dedicated Boat sandbox (bx_2pmgrb6a, 4 vCPU AMD Ryzen 9 9950X, go1.23.4, load average ~1; raw output in benchmarks/). Drafts are cut from testdata/corpus/drafts.txt.

1 KB 8 KB 16 KB
faircopy.Review (one parse, both rules) 1.8 ms / 0.38 MiB 15 ms / 3.4 MiB 31 ms / 6.6 MiB
compromise.New (parse + tag) 1.4 ms / 0.25 MiB 11 ms / 2.1 MiB 22 ms / 4.2 MiB

The embedded model is decoded once per process: 27 ms, 17 MiB allocated. Run-to-run spread was about 3%.

For comparison, DITTO-2120 measured the same two rules through the goja bundle (omniaura/faircopy#46, ditto-assistant/backend#4156, 4 vCPU Boat sandbox): 533 ms / 334 MiB at 1 KB, 1.15 s / 998 MiB at 8 KB, and 5 of 14 timeouts at the 2 s deadline / 1,740 MiB at 16 KB, plus ~300 ms and 190 MiB to load the bundle on every call.

Fair Copy rule notes

  • faircopy messages are static. The JS messages quote the draft; a server review must never echo draft text, so only the spans match JS.
  • no-n-things fixes two bugs in the JS rule. The JS rule re-finds each match by its text and calls values().toNumber() on the whole document, which rewrites numbers in place, so a repeated "three things" is reported once; and it reads the first value of a match, so "one, two, three things" counts as 1. The Go rule counts each match on a copy of its own sentence, from the value right before the noun, ignoring brackets and quotes around it ("(5) things"). The corrected JS rule (noNThingsFixed in goldens.mjs) gives the same answers. Against the original JS rule it adds 7 hits on the drafts and 166 on the adversarial set, and drops none.
  • Both rules still flag legitimate formal writing with their default lists ("Furthermore", "robust", "crucial", "Two things are required before closing"). Server use still needs allowlists, as DITTO-2120 found.
  • toNumber() output goes through Number(), so "5,000 things", "first things" and "a few things" are never flagged.

Regenerating

cd tools/dump-model && npm ci
node dump.mjs | gzip -9 -n > ../../model/model.json.gz
for c in drafts adversarial; do node goldens.mjs ../../testdata/corpus/$c.txt | gzip -9 -n > ../../testdata/golden/$c.jsonl.gz; done
node goldens.mjs --lines ../../testdata/corpus/compromise-tests.txt | gzip -9 -n > ../../testdata/golden/compromise-tests.jsonl.gz

After bumping compromise, regenerate everything and run go test ./...; any algorithm change upstream shows up as golden differences.

Licence

MIT (LICENSE), keeping compromise's copyright notice. The embedded model (model/model.json.gz) is generated from compromise's data, and testdata/corpus/compromise-tests.txt is harvested from compromise's test suite.

About

Pure-Go port of the compromise NLP subset Fair Copy's rules use (tokenizer, contractions, tagger, match syntax, values().toNumber()), with parity tests against JS compromise

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages