Note
本文档由 WeHub 基于上游 README 翻译整理,属于社区翻译,非官方中文文档。
English · 原始项目 · 上游 README
原作者、版权与许可证归属以原始项目及本仓库 LICENSE 文件为准。
import nlp from 'compromise'
let doc = nlp('she sells seashells by the seashore.')
doc.verbs().toPastTense()
doc.text()
// 'she sold seashells by the seashore.'
if (doc.has('simon says #Verb')) {
return true
}
let doc = nlp(entireNovel)
doc.match('the #Adjective of times').text()
// "the blurst of times?"
并获取数据:
import plg from 'compromise-speech'
nlp.extend(plg)
let doc = nlp('Milwaukee has certainly had its share of visitors..')
doc.compute('syllables')
doc.places().json()
/*
[{
"text": "Milwaukee",
"terms": [{
"normal": "milwaukee",
"syllables": ["mil", "wau", "kee"]
}]
}]
*/
避免脆弱解析器带来的问题:
let doc = nlp("we're not gonna take it..")
doc.has('gonna') // true
doc.has('going to') // true (implicit)
// transform
doc.contractions().expand()
doc.text()
// 'we are not going to take it..'
并像操作数据一样随意摆弄内容:
let doc = nlp('ninety five thousand and fifty two')
doc.numbers().add(20)
doc.text()
// 'ninety five thousand and seventy two'
——因为它实际上就是数据——
let doc = nlp('the purple dinosaur')
doc.nouns().toPlural()
doc.text()
// 'the purple dinosaurs'
在客户端使用:
<script src="https://unpkg.com/compromise"></script>
<script>
var doc = nlp('two bottles of beer')
doc.numbers().minus(1)
document.body.innerHTML = doc.text()
// 'one bottle of beer'
</script>
或者同样地:
import nlp from 'compromise'
var doc = nlp('London is calling')
doc.verbs().toNegative()
// 'London is not calling'
compromise 约为 ~250kb(minified):
它相当快,可在按键时运行:
它主要通过为基础词表做全部词形变化(conjugating all forms)来实现。
最终词库约为 ~14,000 words:
你可以在这里. 了解更多工作原理——挺奇怪的。
好吧 —
compromise/one
对单词、句子和标点进行 tokenizer(分词器)。
import nlp from 'compromise/one'
let doc = nlp("Wayne's World, party time")
let data = doc.json()
/* [{
normal:"wayne's world party time",
terms:[{ text: "Wayne's", normal: "wayne" },
...
]
}]
*/
compromise/one 会把文本拆分,并封装为便捷的 API,
/one 很快——大多数句子只需十分之一毫秒。
它每秒可处理 ~1mb 文本——相当于 10 个 Wikipedia 页面。
Infinite jest 需要 3 秒。
compromise/two
part-of-speech(词性)标注器与语法解释器。
import nlp from 'compromise/two'
let doc = nlp("Wayne's World, party time")
let str = doc.match('#Possessive #Noun').text()
// "Wayne's World"
这有时比人们意识到的更有用。
轻量语法有助于你编写更清晰的模板,并更接近信息本身。
compromise 拥有 83 tags,排列在 一张美观的图谱中。
#FirstName → #Person → #ProperNoun → #Noun
运行 doc.debug() 即可查看每个词的语法
使用 nlp.verbose('tagger') 可查看每个标签的推理过程。
如果你更喜欢 Penn tags,可以这样派生:
let doc = nlp('welcome thrillho')
doc.compute('penn')
doc.json()
compromise/three
Phrase 与句子工具集。
import nlp from 'compromise/three'
let doc = nlp("Wayne's World, party time")
let str = doc.people().normalize().text()
// "wayne"
compromise/three 是一套用于聚焦文本片段并进行操作的工具。
例如,.numbers() 会抓取文档中的所有数字,并通过 .subtract() 等方法进行扩展。
当你有一个短语或词组时,可用 .json() 查看其附加元数据
let doc = nlp('four out of five dentists')
console.log(doc.fractions().json())
/*[{
text: 'four out of five',
terms: [ [Object], [Object], [Object], [Object] ],
fraction: { numerator: 4, denominator: 5, decimal: 0.8 }
}
]*/
let doc = nlp('$4.09CAD')
doc.money().json()
/*[{
text: '$4.09CAD',
terms: [ [Object] ],
number: { prefix: '$', num: 4.09, suffix: 'cad'}
}
]*/
(匹配方法使用 match-syntax.)
(这些方法位于主 nlp 对象上)
nlp.tokenize(str) - 解析文本,但不进行词性标注(POS-tagging)
nlp.lazy(str, match) - 以最小分析量扫描文本
nlp.plugin({}) - 混入 compromise-plugin
nlp.parseMatch(str) - 预先将匹配语句解析为 json
nlp.world() - 获取或修改库内部结构
nlp.model() - 获取所有当前语言数据
nlp.methods() - 获取或修改内部方法
nlp.hooks() - 查看哪些计算方法会自动运行
nlp.verbose(mode) - 记录决策过程以便调试
nlp.version - 库的当前 semver 版本
nlp.addWords(obj, isFrozen?) - 向词表添加新词
nlp.addTags(obj) - 向 tagSet 添加新标签
nlp.typeahead(arr) - 向自动填充词典添加词语
nlp.buildTrie(arr) - 将词列表编译为快速查找形式
nlp.buildNet(arr) - 将匹配列表编译为快速匹配形式
'football captain' → 'football captains''turnovers' → 'turnover''will go' → 'went''walked' → 'walks''walked' → 'will walk''walks' → 'walk''walks' → 'walking''drive' → 'had driven''went' → 'did not go'"didn't study" → 'studied'5fivefifth 或 5thfive 或 5'$2.50' 这类金额
he walks -> he walkedhe walked -> he walkshe walks -> he will walkhe walks -> he walkhe walks -> he didn't walk? 的疑问句! 的感叹句? 或 ! 的陈述句'quick' 之类的词
'wash-out''(939) 555-0113' 之类的内容'#nlp' 之类的内容'hi@compromise.cool' 之类的内容:) 之类的内容💋 之类的内容'@nlp_compromise' 之类的内容'compromise.cool' 之类的内容'he' 之类的内容'but' 之类的内容'of' 之类的内容'Mrs.' 之类的内容people() + places() + organizations()'quickly' 之类的词
'FBI' 之类的内容
"Spencer's" 之类的内容
本库内置了一套体贴且符合常识的英语语法基线。
你可以随意修改,甚至彻底推翻任何设置——这其实也是乐趣所在。
最简单的做法是为任意给定词语建议标签:
let myWords = {
kermit: 'FirstName',
fozzie: 'FirstName',
}
let doc = nlp(muppetText, myWords)
或者通过 compromise-plugin. 进行更大幅度的修改
import nlp from 'compromise'
nlp.extend({
// add new tags
tags: {
Character: {
isA: 'Person',
notA: 'Adjective',
},
},
// add or change words in the lexicon
words: {
kermit: 'Character',
gonzo: 'Character',
},
// change inflections
irregulars: {
get: {
pastTense: 'gotten',
gerund: 'gettin',
},
},
// add new methods to compromise
api: View => {
View.prototype.kermitVoice = function () {
this.sentences().prepend('well,')
this.match('i [(am|was)]').prepend('um,')
return this
}
},
})
以下是一些实用的扩展:
npm install compromise-dates
June 8th 或 03/03/18 的日期
2 weeks 或 5mins
4:30pm 或 half past five
npm install compromise-stats
.tfidf({}) - 按词频与独特性对词语排序
.ngrams({}) - 按词数列出所有重复的子短语
.unigrams() - 一词 n-gram
.bigrams() - 两词 n-gram
.trigrams() - 三词 n-gram
.startgrams() - 包含短语首词的 n-gram
.endgrams() - 包含短语末词的 n-gram
.edgegrams() - 包含短语首词或末词的 n-gram
npm install compromise-syllables
npm install compromise-wikipedia
我们致力于支持 TypeScript/Deno,主库与官方插件均提供支持:
import nlp from 'compromise'
import stats from 'compromise-stats'
const nlpEx = nlp.extend(stats)
nlpEx('This is type safe!').ngrams({ min: 1 })
slash-support:
我们目前会像处理连字符一样,将斜杠拆成不同的词,因此像下面这样的情况无法正常工作:
nlp('the koala eats/shoots/leaves').has('koala leaves') //false
inter-sentence match:
默认情况下,句子是顶层抽象单元。
若无 插件,则不支持跨句或多句匹配:
nlp("that's it. Back to Winnipeg!").has('it back')//false
nested match syntax:
正则表达式的危险之美在于可以无限递归。
我们的匹配语法要弱得多。像下面这样的情况尚(尚未)支持:
doc.match('(modern (major|minor))? general')
复杂匹配必须通过连续的 .match() 调用来实现。
dependency parsing: 正确的句子变换需要理解句子的 syntax tree,我们目前尚未实现。 我们应当支持!欢迎在此方面提供帮助。
en-pos - 非常精巧的 JavaScript POS 标注器 by Alex Corvi
naturalNode - JavaScript 中更精致的统计 NLP
winkJS - JavaScript 中的 POS 标注器、分词器与机器学习
dariusk/pos-js - JavaScript 版 fastTag 分支
compendium-js - JavaScript 中的 POS 与情感分析
nodeBox linguistics - JavaScript 中的词形变化与屈折
reText - JavaScript 中令人印象深刻的 text utilities
superScript - JavaScript 对话引擎
jsPos - 久经考验的 Brill 标注器的 JavaScript 构建版
spaCy - C/Python 实现的快速多语言标注器(tagger)
Prose - Joseph Kato 用 Go 编写的快速标注器
TextBlob - Python 标注器
MIT