Elffuss BraidHub

kikocis/utopia public

dev · 19.3 KiBRawPermalink
1//! 把模型说出的谓词落到本体里**已经有**的关系上。
2//!
3//! 抽取原本只做精确 key 比对(`rel_ids.get(f.predicate)`),对不上就降级成
4//! `related_to`,原词留在 `fact_evidence.proposed_predicate` 等人来采纳。
5//!
6//! 实测这样漏得很凶。ai-timeline 语料(维基百科的 AI 公司条目)跑到一半时,
7//! 49.8% 的事实是 `related_to`,`ontology_misses` 里 398 个不同的关系名、895 次使用。
8//! 其中一类根本不是缺词汇,是**词汇就在本体里,模型只是说反了或换了时态**:
9//! `produced_by | ChatGPT → OpenAI` 想说的就是已有的 `produces`,主宾对调而已。
10//!
11//! 那个 49.8% 是**测量台把自动扩本体关掉之后**的数字(`run.mjs` 建库即置
12//! `auto_extend_ontology=FALSE`,而列默认值是 true)。产品的实际冷启动路径是
13//! `bootstrap_ontology` 事后补本体,所以别拿这个占比去说产品有多糟——
14//! 它量的是"缺词汇时降级有多频繁",而这正是本模块要减少的那一部分。
15//!
16//! 这里补三段,**顺序即优先级**,宽的永远排在窄的后面:
17//!
18//! 1. 精确 key —— 原有行为,一字不改
19//! 2. 写法对齐 —— `acquiredFrom` / `acquired_from` / `Acquired From` 是同一个
20//! 3. 屈折归一 —— `produced` 与 `produces` 折到同一串(只削时态与单复数,见 `inflect_base`)
21//!
22//! 2、3 各再试一次「去掉结尾的 by」,命中就**把主宾对调**:英语里 `_by` 是被动的
23//! 明确标记,`X produced_by Y` 与 `Y produces X` 是同一条边。
24//!
25//! **撞车就不匹配。** 本体里若同时有 `produces` 和 `produced`,两者折到同一串,
26//! 这时选谁都是猜——宁可降级,让人去采纳。只有精确 key 不受此限,它本来就唯一。
27//!
28//! **量到了什么**(同一份 895 次未匹配,两种词表):种子本体 10 个关系捞回 49 次;
29//! schema.org 629 个关系捞回 59 次。捞回的 9 个说法里 6 个正确、3 个方向可疑
30//!(`addresses→address`、`funds→funding`、`sponsors→sponsor`),各 1 次,
31//! **全部来自光削一个 `s` 那条路**——英语里 `sponsors` 既是动词第三人称也是名词复数,
32//! 靠后缀分不出来。没为这三条再加规则:样本太小,加了就是过拟合。
33//! 它们的反事实也不是「正确」,而是 `related_to`——原词仍在 proposed_predicate 里。
34//!
35//! 不做同义判断(`partners_with` 与 `collaborates_with`):那是检索与模型的活。
36//! 摆在这里会把「写法对齐」悄悄变成「意思大概差不多」,而后者错了没人看得见。
37
38use std::collections::HashMap;
39use utopia_core::models::RelationType;
40use uuid::Uuid;
41
42/// 切词:非字母数字处切,驼峰的大小写交界处也切。
43///
44/// 驼峰那一半不是可选的:OWL 导入的 key 就是 `acquiredFrom`,不切的话它跟手写的
45/// `acquired_from` 折不到一起,而「导入的本体能不能被用上」正是这条路要保的。
46fn words(s: &str) -> Vec<String> {
47 let mut out = Vec::new();
48 let mut cur = String::new();
49 let mut prev_is_lower_or_digit = false;
50 for c in s.chars() {
51 if !c.is_alphanumeric() {
52 if !cur.is_empty() {
53 out.push(std::mem::take(&mut cur));
54 }
55 prev_is_lower_or_digit = false;
56 continue;
57 }
58 if c.is_uppercase() && prev_is_lower_or_digit && !cur.is_empty() {
59 out.push(std::mem::take(&mut cur));
60 }
61 prev_is_lower_or_digit = c.is_lowercase() || c.is_numeric();
62 cur.extend(c.to_lowercase());
63 }
64 if !cur.is_empty() {
65 out.push(cur);
66 }
67 out
68}
69
70/// 写法对齐形:切完的词直接拼起来,分隔符不留。与
71/// `api::ontology_routes::normalize_name` 同规则。
72fn joined(words: &[String]) -> String {
73 words.concat()
74}
75
76/// 屈折归一:削掉时态与单复数,**不碰派生后缀**。
77///
78/// 这里一度用的是 Snowball(`rust-stemmers`),实测反了:词表 10 个词时捞回 49 次,
79/// 换成 schema.org 的 629 个词只剩 18 次。因为 Snowball 连派生后缀一起削,
80/// `producer` 与 `produces` 都成了 `produc`,撞车规则于是拒绝匹配——**词表越大越不敢动**。
81/// 而 `producer`(一个人)和 `produces`(一个动作)本来就该是两个关系,
82/// 折到一起是词干器的错,不是撞车规则的错。
83///
84/// 所以只做屈折:`-ies→y`、`-ing`、`-ed`、`-s`(`-ss` 除外),削完再去掉结尾的 `e`。
85/// 最后那一步是为了让 `-s` 和 `-ed` 两条路汇合——`produces→produce→produc`、
86/// `produced→produc`,否则同一个动词的两种时态永远对不上。
87///
88/// 长度守卫看的是**削完之后**的结果,不是中间结果。按中间结果卡会让两条路
89/// 走岔:`uses` 削 `-s` 剩三个字母过关、再掉个 `e` 变两个,而 `used` 削 `-ed`
90/// 当场就是两个、被拦下——同一个动词的两种时态于是永远对不上。
91/// 剩不到两个字母就整个不削(`is` 不该变成 `i`,`led` 不该变成 `l`)。
92///
93/// 中文没有屈折后缀,走这里是恒等变换,所以不必按 `ontology_lang` 分叉。
94fn inflect_base(w: &str) -> String {
95 let n = w.len();
96 let mut s = if w.ends_with("ies") && n > 3 {
97 format!("{}y", &w[..n - 3])
98 } else if w.ends_with("ing") && n > 3 {
99 w[..n - 3].to_string()
100 } else if w.ends_with("ed") && n > 2 {
101 w[..n - 2].to_string()
102 } else if w.ends_with('s') && !w.ends_with("ss") && n > 1 {
103 w[..n - 1].to_string()
104 } else {
105 w.to_string()
106 };
107 // 结尾的 e 一律去掉:`produces→produce` 与 `produced→produc` 靠这一步汇合,
108 // 顺带让 `note` 与 `notes` 落到一起
109 if s.ends_with('e') {
110 s.pop();
111 }
112 if s.chars().count() < 2 {
113 return w.to_string();
114 }
115 s
116}
117
118/// 领头的轻动词:`has_funding` 与 `funding` 是同一个关系,前缀是命名习惯不是意思。
119///
120/// 实测(ai-timeline-ends × schema.org):空谓词事实的原文说法里
121/// `has_funding` ×4 落空,而本体里就有 `funding`;`product` ×2 落空,而本体里有
122/// `has_product`——**差的只是这一个前缀**。
123///
124/// 只在还剩词的时候剥:`has` 单独一个词是它自己,不能剥成空。
125/// 过度归并不会造成错配——`insert` 遇到一个键落到两个关系上就作废(撞车即作废),
126/// 所以最坏情况是退回"匹配不上",而不是匹配到错的那个。
127const LEADING_AUX: &[&str] = &[
128 "has", "have", "had", "is", "are", "was", "were", "be", "been",
129];
130
131fn stems(words: &[String]) -> Vec<String> {
132 let words = match words.split_first() {
133 Some((head, rest)) if !rest.is_empty() && LEADING_AUX.contains(&head.as_str()) => rest,
134 _ => words,
135 };
136 words.iter().map(|w| inflect_base(w)).collect()
137}
138
139/// 一个说法的**归并键**:同一个关系的不同时态落到同一个键上。
140///
141/// 给本体采纳那条路用的。抽取时说法要跟**已有**关系比对(上面那套三段匹配),
142/// 采纳时要做的是另一件事——把彼此之间是同一个意思的说法先并起来再算票数。
143///
144/// 不并就要吃亏,而且吃得见:ai-timeline 跑完后还压在兜底谓词上的说法里,
145/// `sued` 与 `sues` 是两条(各 11 和 5)、`integrated_with` 与 `integrates_with`
146/// 是两条(各 5)、`announced`/`announces`、`supported`/`supports`、
147/// `developed`/`develops` 都是。各自算票,各自够不着「出现在 ≥2 篇」的门槛;
148/// 并起来之后够格的候选从 70 组涨到 85 组、281 条涨到 355 条。
149///
150/// **介词不并**:`integrated_with` 与 `integrated_into` 保持两组。`works_at` 与
151/// `works_in` 确实可能是两回事,这里宁可漏。
152///
153/// **`_by` 也不并**(待做):`founded_by` 与 `founded` 是同一条边的两个方向。
154///
155/// 当初不并的理由是「采纳路径从旧行原样复制主语,对调不了」——**那个理由已经
156/// 不成立**(#109 让 `adopt` 显式绑定主语并支持交换)。现在缺口只剩一处:
157/// 两边都还不在本体里时(`founded_by` 42 条、`founded` 4 条,都够票),
158/// 采纳前的 `PredicateIndex` 查询谁也匹配不上,于是各建一个、方向相反。
159/// 补法是把 `_by` 折进同一组并把整组标成需对调,不再有阻碍。
160pub fn merge_key(form: &str) -> Vec<String> {
161 stems(&words(form))
162}
163
164/// 撞车即作废:同一个形式落到两个不同的关系上,选谁都是猜。
165fn insert<K: std::hash::Hash + Eq>(map: &mut HashMap<K, Option<Uuid>>, key: K, id: Uuid) {
166 map.entry(key)
167 .and_modify(|slot| {
168 if *slot != Some(id) {
169 *slot = None;
170 }
171 })
172 .or_insert(Some(id));
173}
174
175pub struct PredicateIndex {
176 exact: HashMap<String, Uuid>,
177 by_joined: HashMap<String, Option<Uuid>>,
178 by_stems: HashMap<Vec<String>, Option<Uuid>>,
179}
180
181impl PredicateIndex {
182 /// **只收 `kind == "relation"`。** 属性走字面值通道,模糊匹配跨过去,
183 /// `founding_date` 就会变成一条指向实体「2015」的边——那正是本体采纳那条路
184 /// 已经专门挡掉的东西,这里不能从后门放回来。
185 pub fn build(rtypes: &[RelationType]) -> Self {
186 let mut exact = HashMap::new();
187 let mut by_joined = HashMap::new();
188 let mut by_stems = HashMap::new();
189 for r in rtypes.iter().filter(|r| r.kind == "relation") {
190 exact.insert(r.key.clone(), r.id);
191 let w = words(&r.key);
192 if w.is_empty() {
193 continue;
194 }
195 insert(&mut by_joined, joined(&w), r.id);
196 insert(&mut by_stems, stems(&w), r.id);
197 }
198 Self {
199 exact,
200 by_joined,
201 by_stems,
202 }
203 }
204
205 fn widened(&self, w: &[String]) -> Option<Uuid> {
206 if w.is_empty() {
207 return None;
208 }
209 if let Some(hit) = self.by_joined.get(&joined(w)) {
210 return *hit;
211 }
212 *self.by_stems.get(&stems(w))?
213 }
214
215 /// 返回 `(关系 id, 主宾是否要对调)`。`None` = 本体里确实没有,该降级。
216 pub fn lookup(&self, proposed: &str) -> Option<(Uuid, bool)> {
217 if let Some(id) = self.exact.get(proposed) {
218 return Some((*id, false));
219 }
220 let w = words(proposed);
221 if let Some(id) = self.widened(&w) {
222 return Some((id, false));
223 }
224 // 被动形:`produced_by` 去掉 by 之后才对得上 `produces`,且主宾要反过来。
225 // 要求至少两个词——光一个 `by` 削完是空的。
226 if w.len() >= 2 && w[w.len() - 1] == "by" {
227 if let Some(id) = self.widened(&w[..w.len() - 1]) {
228 return Some((id, true));
229 }
230 }
231 None
232 }
233}
234
235#[cfg(test)]
236mod tests {
237 use super::*;
238
239 /// `has_funding` 与 `funding` 是同一个关系,前缀是命名习惯不是意思。
240 ///
241 /// 实测里这两组各自落空:本体有 `funding`、模型写 `has_funding`(×4);
242 /// 本体有 `has_product`、模型写 `product`(×2)。差的只是这一个前缀。
243 #[test]
244 fn a_leading_auxiliary_does_not_make_a_different_relation() {
245 let rels = vec![rel("funding"), rel("has_product")];
246 let idx = PredicateIndex::build(&rels);
247 assert!(
248 idx.lookup("has_funding").is_some(),
249 "has_funding 该落到 funding 上"
250 );
251 assert!(
252 idx.lookup("product").is_some(),
253 "product 该落到 has_product 上"
254 );
255 // 两个方向都要通
256 assert!(idx.lookup("funding").is_some());
257 assert!(idx.lookup("has_product").is_some());
258 }
259
260 /// **只在还剩词的时候剥。** `has` 单独一个词是它自己,剥成空就什么都匹配了。
261 #[test]
262 fn a_bare_auxiliary_is_still_a_word() {
263 let rels = vec![rel("has")];
264 let idx = PredicateIndex::build(&rels);
265 assert!(idx.lookup("has").is_some(), "has 自己该匹配得上");
266 assert!(idx.lookup("owns").is_none(), "剥成空会让不相干的词也匹配上");
267 }
268
269 /// 撞车仍然作废:本体同时有 `funding` 与 `has_funding` 时,选谁都是猜。
270 /// 过度归并的最坏结果是「匹配不上」,不是「匹配到错的那个」。
271 #[test]
272 fn folding_the_prefix_never_produces_a_wrong_match() {
273 let rels = vec![rel("funding"), rel("has_funding")];
274 let idx = PredicateIndex::build(&rels);
275 // 精确 key 仍然直达
276 assert!(idx.lookup("funding").is_some());
277 assert!(idx.lookup("has_funding").is_some());
278 // 撞车在**词干**那一层:`funding` 与 `has_funding` 剥掉前缀后都是 ["funding"]。
279 // 要测它就得给一个走不到写法对齐、只能落到词干的形式——
280 // `has_fundings` 拼起来是 hasfundings,本体里没有,于是往下走到词干,撞车作废
281 assert!(
282 idx.lookup("has_fundings").is_none(),
283 "词干层撞车了却还是选了一个"
284 );
285 }
286
287 fn rel(key: &str) -> RelationType {
288 RelationType {
289 id: Uuid::new_v4(),
290 kb_id: Uuid::nil(),
291 key: key.to_string(),
292 label: key.to_string(),
293 temporal: "state".into(),
294 functional: false,
295 inverse_functional: false,
296 builtin: false,
297 description: String::new(),
298 iri: None,
299 kind: "relation".into(),
300 domains: Vec::new(),
301 ranges: Vec::new(),
302 datatype: None,
303 unit: None,
304 }
305 }
306
307 fn attr(key: &str) -> RelationType {
308 RelationType {
309 kind: "attribute".into(),
310 ..rel(key)
311 }
312 }
313
314 #[test]
315 fn splits_camel_case_and_separators_the_same_way() {
316 assert_eq!(words("acquiredFrom"), ["acquired", "from"]);
317 assert_eq!(words("acquired_from"), ["acquired", "from"]);
318 assert_eq!(words("Acquired From"), ["acquired", "from"]);
319 // 全大写不是驼峰边界,别把 IRI 切成 i/r/i
320 assert_eq!(words("IRI"), ["iri"]);
321 assert_eq!(words("gpt4Model"), ["gpt4", "model"]);
322 }
323
324 #[test]
325 fn exact_key_still_wins_unchanged() {
326 let types = [rel("produces")];
327 let idx = PredicateIndex::build(&types);
328 assert_eq!(idx.lookup("produces"), Some((types[0].id, false)));
329 }
330
331 #[test]
332 fn separator_and_case_differences_align() {
333 let types = [rel("acquired_from")];
334 let idx = PredicateIndex::build(&types);
335 assert_eq!(idx.lookup("acquiredFrom"), Some((types[0].id, false)));
336 assert_eq!(idx.lookup("Acquired From"), Some((types[0].id, false)));
337 }
338
339 #[test]
340 fn tense_folds_without_swapping() {
341 let types = [rel("produces")];
342 let idx = PredicateIndex::build(&types);
343 // 模型写过去式,说的还是同一条边,方向也没变
344 assert_eq!(idx.lookup("produced"), Some((types[0].id, false)));
345 }
346
347 /// 这条是这个模块存在的理由:`ChatGPT produced_by OpenAI` 与
348 /// `OpenAI produces ChatGPT` 是同一条边,只差主宾方向。
349 #[test]
350 fn passive_form_matches_and_asks_for_a_swap() {
351 let types = [rel("produces")];
352 let idx = PredicateIndex::build(&types);
353 assert_eq!(idx.lookup("produced_by"), Some((types[0].id, true)));
354 assert_eq!(idx.lookup("producedBy"), Some((types[0].id, true)));
355 }
356
357 #[test]
358 fn multi_word_passive_matches() {
359 let types = [rel("invests_in")];
360 let idx = PredicateIndex::build(&types);
361 assert_eq!(idx.lookup("invested_in"), Some((types[0].id, false)));
362 }
363
364 /// **派生不是屈折。** 这条是换掉 Snowball 的理由:schema.org 里 `producer`
365 /// 与 `produces` 并存,词干器把两个都折成 `produc`,于是撞车规则把
366 /// `produced` 也一并拒了——词表越大捞得越少(实测 49 次掉到 18 次)。
367 /// 只削屈折后缀就不会有这个撞车:`producer` 保持原样。
368 #[test]
369 fn derivational_forms_stay_separate_from_inflected_ones() {
370 let types = [rel("produces"), rel("producer"), rel("production_company")];
371 let idx = PredicateIndex::build(&types);
372 assert_eq!(idx.lookup("produced"), Some((types[0].id, false)));
373 assert_eq!(idx.lookup("produced_by"), Some((types[0].id, true)));
374 assert_eq!(idx.lookup("producer"), Some((types[1].id, false)));
375 // 派生词之间也别互相串
376 assert_eq!(idx.lookup("producers"), Some((types[1].id, false)));
377 }
378
379 /// 单复数与时态两条路要汇合到同一串,否则同一个动词的两种写法永远对不上。
380 #[test]
381 fn plural_and_past_forms_meet_at_the_same_base() {
382 assert_eq!(inflect_base("produces"), inflect_base("produced"));
383 assert_eq!(inflect_base("uses"), inflect_base("used"));
384 assert_eq!(inflect_base("notes"), inflect_base("note"));
385 assert_eq!(inflect_base("studies"), inflect_base("study"));
386 // 短词不削:is 不该变成 i
387 assert_eq!(inflect_base("is"), "is");
388 // -ss 不是复数
389 assert_eq!(inflect_base("address"), inflect_base("addresses"));
390 }
391
392 /// 撞车宁可不匹配:本体里同时有 `produces` 和 `produced` 时,
393 /// `producing` 折到两者共同的词干上,选谁都是猜。
394 #[test]
395 fn ambiguous_stem_declines_rather_than_guesses() {
396 let types = [rel("produces"), rel("produced")];
397 let idx = PredicateIndex::build(&types);
398 assert_eq!(idx.lookup("producing"), None);
399 // 但精确 key 不受影响,它本来就唯一
400 assert_eq!(idx.lookup("produces"), Some((types[0].id, false)));
401 assert_eq!(idx.lookup("produced"), Some((types[1].id, false)));
402 }
403
404 /// 属性不能从这条路被匹配上:宾语是字面值,配不出一条边。
405 #[test]
406 fn attributes_are_not_reachable() {
407 let types = [attr("founding_date")];
408 let idx = PredicateIndex::build(&types);
409 assert_eq!(idx.lookup("founding_date"), None);
410 assert_eq!(idx.lookup("foundingDate"), None);
411 }
412
413 /// 本体里真没有的,照旧降级——这里不做同义判断。
414 #[test]
415 fn genuinely_missing_vocabulary_still_declines() {
416 let types = [rel("produces"), rel("works_at")];
417 let idx = PredicateIndex::build(&types);
418 assert_eq!(idx.lookup("partners_with"), None);
419 assert_eq!(idx.lookup("acquired"), None);
420 // 介词不同就是不同的关系,别替模型改口
421 assert_eq!(idx.lookup("works_in"), None);
422 }
423
424 #[test]
425 fn lone_by_does_not_strip_to_nothing() {
426 let types = [rel("produces")];
427 let idx = PredicateIndex::build(&types);
428 assert_eq!(idx.lookup("by"), None);
429 assert_eq!(idx.lookup("_by_"), None);
430 }
431
432 /// 中文 key 走词干器是恒等变换,不该被削也不该错配。
433 #[test]
434 fn chinese_keys_pass_through() {
435 let types = [rel("隶属于"), rel("生产")];
436 let idx = PredicateIndex::build(&types);
437 assert_eq!(idx.lookup("隶属于"), Some((types[0].id, false)));
438 assert_eq!(idx.lookup("生产"), Some((types[1].id, false)));
439 assert_eq!(idx.lookup("收购"), None);
440 }
441}