compromise-nlp
它能做什么
它教 AI 使用 compromise 库匹配词语、标注词性并转换句子。适用于处理英文文本的 JavaScript 开发者。
适用场景
- ✓当你编写 JavaScript,需要在英文文本中找出名词、动词或人名时。
- ✓当你想用 compromise 把句子改为过去时,或规范化文本时。
- ✓当你调试 compromise 代码,且返回的结果与预期不符时。
技能链接
https://github.com/spencermountain/compromise/blob/master/docs/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请帮我编写 JavaScript,从一段英文文本中提取人名。”
每周 5 个新技能,1 分钟读完。
工作原理
AI 会把文本转换为文档,为每个词做标注,然后使用选择和转换方法查找或修改其中的部分。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Using compromise
compromise is a rule-based English NLP library for JavaScript (no network, no model, no deps).
You tokenize text into a document, it tags each word's part-of-speech, and you find and
transform parts of the text with a jQuery-like chained API.
import nlp from 'compromise'
let doc = nlp('she sells seashells by the seashore.')
doc.verbs().toPastTense() // transform
doc.text() // 'she sold seashells by the seashore.'
The five rules that prevent almost every mistake
These are the things that are easy to get wrong from memory. Internalize them before writing code.
1. Transforms mutate the document in place — read the result from the original variable
Every transform method (.toPastTense(), .replace(), .tag(), .normalize(), case/whitespace
methods…) changes the underlying document. The View it returns is the selection it acted on, not
the whole document. So calling .text() on the chain gives you only the selected fragment:
let doc = nlp('I walk to work')
doc.verbs().toPastTense()
doc.text() // ✅ 'I walked to work' (read from doc)
nlp('I walk to work').verbs().toPastTense().text() // ❌ 'walked work' (just the selection)
To transform a copy and leave the original untouched, call .clone() first:
let past = doc.clone().verbs().toPastTense().text()
Read-only methods (.match, .has, .if, .found, .text, .json, accessors) never mutate.
2. Only real tags work — an invalid #Tag matches nothing, silently
There are ~88 valid part-of-speech tags, and they're a hierarchy (#FirstName ⊂ #Person ⊂
#Noun). A #Tag that isn't real does not error — it just matches nothing, which looks like a
logic bug. Common inventions that are NOT tags: #Name, #Location, #Subject, #Object,
#Adj, #Entity. (Valid ones include #Person, #Place, #Organization, #Noun, #Verb,
#Value, #Date.) When unsure, check node_modules/compromise/docs/tag-definitions.md.
3. Match-syntax is term-level, not regex
.match() matches whole words/terms, not characters. + * ? . ^ $ operate on terms. For
character-level patterns, use a /regex/ token. Cheat sheet (see references for the rest):
| Token | Means | Example |
|---|---|---|
#Tag |
a part-of-speech tag | #Person |
. |
any one term | the . sat |
* |
any run of terms | the * sat |
(a|b) |
one of these | (cat|dog) |
word? |
optional term | the big? cat |
#Tag+ |
one or more | #Adjective+ |
! |
negate a term | the !#Verb |
[ ] / [<name> ] |
capture group | [<who>#Person+] |
^ / $ |
sentence start / end | ^the / sat$ |
~word~ |
fuzzy / typo-tolerant | ~organization~ |
{root} |
match all conjugations | {walk} matches walked/walking |
/regex/ |
character-level regex | /colou?r/ |
nlp('John Smith left').match('[<who>#Person+]').groups('who').text() // 'john smith'
4. Sentences are the ceiling — matches don't cross sentence boundaries
nlp("that's it. Back to Winnipeg!").has('it back') is false. For multi-sentence matching, use
the compromise-paragraphs plugin.
5. compromise is the full build — import it unless size is critical
import nlp from 'compromise'— full library:.people(),.verbs(),.numbers(), all selections. Default.import nlp from 'compromise/two'— POS tags +.match(), but no named selections.import nlp from 'compromise/tokenize'(/one) — tokenize only, no tags (#Tagpatterns won't work).
There's no useful tree-shaking below these tiers; run the full build.
Common tasks (verified patterns)
// entities
nlp(text).people().out('array') // ['Mary', 'Dr. John Smith']
nlp(text).topics().out('array') // people + places + organizations
// NOTE: out('array') keeps trailing punctuation ('Paris.'); use .text('normal') for clean tokens
// transform (remember rule #1 — read from doc)
let d = nlp('I walk'); d.verbs().toPastTense(); d.text() // 'I walked'
let n = nlp('one dog'); n.nouns().toPlural(); n.text() // 'one dogs'
// numbers
nlp('five hundred').numbers().toNumber().text() // '500'
nlp('it cost twelve dollars').numbers().get() // [12]
// find / route (boolean, never mutates)
nlp('the deal is closed').has('#Determiner #Noun') // true
// teach it words
nlp(text, { kermit: 'FirstName' })
常见问题
它需要 AI 服务或网络连接吗?
不需要。compromise 库在本地运行,不需要网络、模型或额外依赖。
为什么我的输出只显示句子的一部分?
转换会原地修改文档,并且只返回被选中的部分,因此请从原始文档变量中读取完整文本。
它能理解含义吗?
不能完全理解。它使用基于规则的标注,不提供深入的语义理解。
详情
作者的原始描述
Write correct code with the `compromise` JavaScript NLP library (the npm package `compromise`, imported as `nlp`). Use this whenever you are writing or editing JS/TS that imports compromise, calls `nlp(...)`, or chains methods like `.match()`, `.tag()`, `.people()`, `.verbs()`, `.nouns()`, `.numbers()`, `.normalize()`, or `.replace()` — and also whenever doing rule-based natural-language tasks in JavaScript (extracting entities/dates/numbers, matching text patterns, changing verb tense, pluralizing, redacting/anonymizing text, building a simple chatbot intent matcher) where compromise is or co
热度
- GitHub 星标
- 12k
- 排名
- #11471
- 许可证
- MIT
- 更新时间
- 2026/10/30
评论
- …