CoNLL-U / Universal Dependencies toolchain: lossless parse and serialize, spec validation, tree queries, UAS/LAS evaluation, corpus statistics, document grouping, coverage analysis.
moon add PaperY0/conllu///|
test {
let text = "1\tI\tI\tPRON\tPRP\t_\t2\tnsubj\t_\t_\n2\tsaw\tsee\tVERB\tVBD\t_\t0\troot\t_\t_\n3\tthe\tthe\tDET\tDT\t_\t4\tdet\t_\t_\n4\tcat\tcat\tNOUN\tNN\t_\t2\tobj\tSpaceAfter=No\t_\n5\t.\t.\tPUNCT\t.\t_\t2\tpunct\t_\t_\n"
let sentences = @conllu.parse(text) catch { _ => [] }
assert_eq(sentences.length(), 1)
let sentence = sentences[0]
assert_eq(sentence.word_count(), 5)
assert_eq(sentence.words()[1].lemma, "see")
// HEAD 是解析过的整数,可直接用于树操作
assert_eq(sentence.words()[1].head(), Some(0))
// 序列化补回规范要求的句末空行
assert_eq(@conllu.to_string(sentences), text + "\n")
}///|
test {
let broken = "1\ta\ta\tNOUN\tNN\t_\t0\troot\t_\t_\n2\tb\tb\tNOUN\tNN\t_\t0\troot\t_\t_\n"
let sentences = @conllu.parse(broken) catch { _ => [] }
let diagnostics = @conllu.validate(sentences)
assert_eq(@conllu.count_errors(diagnostics), 1)
assert_eq(diagnostics[0].code, "ROOT_COUNT")
}| code | 含义 |
|---|---|
| ID_SEQUENCE | 词 ID 不是从 1 开始连续递增 |
| MULTIWORD_SPAN | 多词 token 的 5-6 越界 |
| HEAD_MISSING / HEAD_RANGE / HEAD_SELF | HEAD 非数字、越界、指向自己 |
| ROOT_COUNT | 根节点不是恰好 1 个 |
| CYCLE | 沿 HEAD 走不到根 |
| DEPS_FORMAT / DEPS_RANGE / DEPS_DEPREL / DEPS_MISSING | DEPS 语法、范围、关系名、空节点缺关系 |
| UPOS_UNKNOWN / DEPREL_UNKNOWN | 不在 UD v2 标签集内 |
| FORM_EMPTY / FORM_SPACE | FORM 为空或含空白 |
///|
test {
let text = "# newdoc id = d1\n# newpar id = p1\n# sent_id = 1\n1\ta\ta\tNOUN\tNN\t_\t0\troot\t_\t_\n\n# newpar id = p2\n# sent_id = 2\n1\tb\tb\tNOUN\tNN\t_\t0\troot\t_\t_\n"
let docs = @conllu.documents(@conllu.parse(text) catch { _ => [] })
assert_eq(docs.length(), 1)
assert_eq(docs[0].id(), "d1")
assert_eq(docs[0].paragraph_count(), 2)
assert_eq(docs[0].paragraphs()[0].id(), "p1")
assert_eq(docs[0].paragraphs()[1].id(), "p2")
assert_eq(docs[0].sentence_count(), 2)
}///|
test {
let text = "1\ta\ta\tNOUN\tNN\t_\t0\troot\t_\t_\n2\tb\tb\tUNKNOWN\tNN\t_\t1\tweird\t_\t_\n"
let cov = @conllu.coverage(@conllu.parse(text) catch { _ => [] })
assert_eq(cov.upos_used.length(), 1)
assert_eq(cov.unknown_upos[0], "UNKNOWN")
assert_eq(cov.unknown_deprel[0], "weird")
assert_false(cov.full_upos())
assert_eq(cov.projectivity(), 100.0)
}///|
test {
let gold = "1\ta\ta\tNOUN\tNN\t_\t2\tnsubj\t_\t_\n2\tb\tb\tVERB\tVB\t_\t0\troot\t_\t_\n"
let pred = "1\ta\ta\tNOUN\tNN\t_\t0\tnsubj\t_\t_\n2\tb\tb\tVERB\tVB\t_\t0\troot\t_\t_\n"
let g = @conllu.parse(gold) catch { _ => [] }
let p = @conllu.parse(pred) catch { _ => [] }
let result = @conllu.evaluate(g, p)
assert_eq(result.uas(), 50.0)
assert_eq(result.las(), 50.0)
}moon run cmd/main -- stats treebank.conllu # 句数/词数/多词/空节点/最长句/树库
moon run cmd/main -- validate treebank.conllu # 诊断列表,有 error 时退出码 1
moon run cmd/main -- eval gold.conllu pred.conllu
moon run cmd/main -- labels gold.conllu pred.conllu # 按关系拆开的 LAS
moon run cmd/main -- labels gold.conllu pred.conllu --format csv
moon run cmd/main -- docs treebank.conllu # 文档/段落结构与句数
moon run cmd/main -- analyze treebank.conllu # 覆盖报告,规范外标签时退出码 1
moon run cmd/main -- dump treebank.conllu # 归一化 dump,用于对拍
moon run cmd/main -- dump treebank.conllu --format json
moon run cmd/main -- roundtrip treebank.conllu # 解析再序列化,校验无损
moon run cmd/main -- compress treebank.conllu out.conllu.gz # 规范序列化 + gzip 写回D 2001
S 7 0 0 4
w 1 From ADP 3 casepython3 tools/fetch_corpus.py # 下载 UD 官方语料到 data/ud/(语料不随包分发)
python3 tools/diff_test.py --report| 文件 | 句数 | dump 行数 | 字段不一致 |
|---|---|---|---|
| en_ewt-ud-dev | 2001 | 27513 | 0 |
| en_ewt-ud-test | 2077 | 27528 | 0 |
| zh_gsd-ud-dev | 500 | 13166 | 0 |
| zh_gsd-ud-test | 500 | 12511 | 0 |
| ja_gsd-ud-dev | 507 | 12795 | 0 |
| ja_gsd-ud-test | 543 | 13578 | 0 |
| 函数 | 说明 |
|---|---|
| parse(text) -> Array[Sentence] raise ConlluError | 按空行分句,校验 10 列与 ID 形态,出错带行号 |
| to_string(sentences) -> String | 序列化整份文档,每句后补规范空行 |
| Sentence::to_string() | 单句序列化 |
| Sentence::words() / rows() / comments() | 词行 / 全部行 / 注释 |
| Sentence::word_count() | 词行数量(不含多词与空节点) |
| Sentence::word(i) -> Token? | 按 1-based 词序取词 |
| Sentence::comment(key) | 取 # key = value 的 value |
| Sentence::comment_value(key) | 同上的通用版本,Some(value) / None |
| Token::head() -> Int32? | HEAD 解析为整数,非数字返回 None |
| Token::is_word() / is_multiword() / is_empty_node() | 行类型判定 |
| Token::to_line() | 还原一行 10 列 |
| 函数 | 说明 |
|---|---|
| validate(sentences) -> Array[Diagnostic] | 全文档校验 |
| validate_sentence(sentence) | 单句校验 |
| count_errors(diagnostics) -> Int32 | 只数 Error 级别 |
| Diagnostic::to_string() | line 12: error [ROOT_COUNT] ... |
| 函数 | 说明 |
|---|---|
| children(s, head) / descendants(s, node) | 直接孩子 / 全部后代 |
| path(s, from, to) -> Array[Int32]? | 从 from 沿 HEAD 到祖先 to(to = 0 走到根) |
| is_ancestor(s, a, b) | a 是否为 b 的祖先 |
| depth(s, i) / max_depth(s) | 深度 / 最大深度 |
| is_projective(s) | 是否无交叉弧 |
| roots(s) / is_single_rooted(s) | 根节点 / 是否单根 |
| 函数 | 说明 |
|---|---|
| documents(sentences) -> Array[Document] | 按 # newdoc / # newpar 分组 |
| Document::id() / paragraph_count() / sentence_count() | 文档 id 与两级计数 |
| Document::paragraphs() / sentences() | 段落列表 / 展开成句子列表 |
| Paragraph::id() / sentences() / sentence_count() | 段落 id 与句数 |
| Sentence::has_newdoc() / has_newpar() | 是否开启新文档 / 新段落 |
| Sentence::newdoc_id() / newpar_id() | 对应注释的 id 值 |
| 函数 | 说明 |
|---|---|
| coverage(sentences) -> Coverage | 标签命中、规范外标签、树形与句长统计 |
| Coverage::upos_used() / deprel_used() | 实际出现的标签(字母序) |
| Coverage::upos_missing() / deprel_missing() | 规范里没出现的标签 |
| Coverage::projectivity() | 投射句占比(百分制) |
| Coverage::full_upos() / full_deprel() | 是否覆盖全部规范标签 |
| Coverage::to_string() | 多行报告,analyze 直接打印 |
| 函数 | 说明 |
|---|---|
| evaluate(gold, pred) -> EvalResult | 对齐后计算 UAS / UUAS / LAS |
| EvalResult::uas() / uuas() / las() | 百分制分数;uuas 不计方向 |
| evaluate_by_label(gold, pred) | 按 gold 关系拆开的 LAS,最差的排最前 |
| LabelScore::las() / to_string() | 单关系准确率 |
| stats(sentences) -> Stats | 句数、词数、多词、空节点、注释、最长句、树库 |
| Stats::average_length() / to_string() | 平均句长 / 一行摘要 |
pub(all) suberror ConlluError {
ConlluError(Int, String)
}pub struct Coverage {
sentences : Int
words : Int
upos_used : Array[String]
upos_missing : Array[String]
deprel_used : Array[String]
deprel_missing : Array[String]
unknown_upos : Array[String]
unknown_deprel : Array[String]
untagged : Int
projective : Int
non_projective : Int
single_rooted : Int
multi_rooted : Int
max_depth : Int
avg_depth : Double
long_sentences : Int
empty_nodes : Int
multiword_tokens : Int
documents : Int
paragraphs : Int
}pub struct EvalResult {
sentence_pairs : Int
skipped_sentences : Int
gold_tokens : Int
aligned_tokens : Int
uas_correct : Int
uuas_correct : Int
las_correct : Int
}pub struct LabelScore {
label : String
gold : Int
correct : Int
}pub(all) enum RowKind {
Word
Multiword
Empty
}pub struct Stats {
sentences : Int
words : Int
multiwords : Int
empty_nodes : Int
comments : Int
max_length : Int
treebanks : Array[String]
}pub struct Token {
id_raw : String
form : String
lemma : String
upos : String
xpos : String
feats : String
head_raw : String
deprel : String
deps : String
misc : String
kind : RowKind
start : Int
end : Int
line : Int
}Install
Download zipCoNLL-U / Universal Dependencies toolchain: lossless parse and serialize, spec validation, tree queries, UAS/LAS evaluation, corpus statistics, document grouping, coverage analysis.