1use std::collections::HashMap;
5use std::path::Path;
6
7use gcn_ir::normalize_label;
8
9use crate::KnowledgeError;
10use crate::loader::load_all_taxonomies;
11use crate::taxonomy::{LexicalEntry, Taxonomy, TaxonomyClass};
12
13#[derive(Debug, Default, Clone)]
29pub struct AliasTable {
30 aliases: HashMap<String, String>,
31}
32
33impl AliasTable {
34 pub fn new() -> Self {
35 Self::default()
36 }
37
38 pub fn insert(&mut self, variant: &str, canonical: &str) {
40 self.aliases
41 .insert(normalize_label(variant), normalize_label(canonical));
42 }
43
44 pub fn resolve(&self, label: &str) -> String {
46 let norm = normalize_label(label);
47 self.aliases.get(&norm).cloned().unwrap_or(norm)
48 }
49
50 pub fn is_alias(&self, label: &str) -> bool {
52 self.aliases.contains_key(&normalize_label(label))
53 }
54
55 pub fn len(&self) -> usize {
56 self.aliases.len()
57 }
58
59 pub fn is_empty(&self) -> bool {
60 self.aliases.is_empty()
61 }
62}
63
64pub struct Lexicon {
65 taxonomies: HashMap<String, Taxonomy>,
66}
67
68impl Lexicon {
69 pub fn load_from_dir(path: &Path) -> Result<Self, KnowledgeError> {
70 let taxonomies = load_all_taxonomies(path)?;
71 Ok(Self { taxonomies })
72 }
73
74 pub fn lookup_verb(&self, lemma: &str) -> Option<&TaxonomyClass> {
75 let taxonomy = self.taxonomies.get("verbes")?;
76 taxonomy.classes.values().find(|class| {
77 let in_fr = class
78 .examples_fr
79 .as_ref()
80 .is_some_and(|ex| ex.iter().any(|e| e.lemma == lemma));
81 let in_generic = class
82 .examples
83 .as_ref()
84 .is_some_and(|ex| ex.iter().any(|e| e.lemma == lemma));
85 in_fr || in_generic
86 })
87 }
88
89 pub fn lookup_by_pos(&self, pos: &str, lemma: &str) -> Option<(&str, &LexicalEntry)> {
90 let taxonomy_name = pos_to_taxonomy(pos)?;
91 let taxonomy = self.taxonomies.get(taxonomy_name)?;
92 for (class_name, class) in &taxonomy.classes {
93 for examples in [&class.examples_fr, &class.examples].into_iter().flatten() {
94 for entry in examples {
95 if entry.lemma == lemma {
96 return Some((class_name.as_str(), entry));
97 }
98 }
99 }
100 }
101 None
102 }
103
104 pub fn taxonomy(&self, name: &str) -> Option<&Taxonomy> {
105 self.taxonomies.get(name)
106 }
107
108 pub fn taxonomies(&self) -> &HashMap<String, Taxonomy> {
109 &self.taxonomies
110 }
111
112 #[cfg(test)]
113 pub fn from_taxonomies_for_test(taxonomies: HashMap<String, Taxonomy>) -> Self {
114 Self { taxonomies }
115 }
116}
117
118#[cfg(test)]
119mod alias_tests {
120 use super::*;
121
122 #[test]
123 fn alias_auth_login_authentification() {
124 let mut t = AliasTable::new();
125 t.insert("auth", "authentification");
126 t.insert("login", "authentification");
127 assert_eq!(t.resolve("auth"), "authentification");
129 assert_eq!(t.resolve("Auth"), "authentification");
130 assert_eq!(t.resolve("LOGIN"), "authentification");
131 assert_eq!(t.resolve("login"), "authentification");
132 assert_eq!(t.resolve("authentification"), "authentification");
134 assert_eq!(t.resolve("Inconnu"), "inconnu");
135 }
136
137 #[test]
138 fn alias_accented_variant() {
139 let mut t = AliasTable::new();
140 t.insert("économie", "economie");
141 assert_eq!(t.resolve("Économie"), "economie");
142 assert_eq!(t.resolve("economie"), "economie");
143 }
144
145 #[test]
146 fn alias_empty_table() {
147 let t = AliasTable::new();
148 assert!(t.is_empty());
149 assert_eq!(t.resolve("auth"), "auth");
150 }
151}
152
153fn pos_to_taxonomy(pos: &str) -> Option<&'static str> {
154 match pos {
155 "VERB" => Some("verbes"),
156 "ADV" => Some("adverbes"),
157 "ADJ" => Some("adjectifs"),
158 "SCONJ" | "CCONJ" => Some("conjonctions"),
159 "ADP" => Some("prepositions"),
160 "NOUN" => Some("noms"),
161 "PRON" => Some("pronoms"),
162 "DET" => Some("determinants"),
163 _ => None,
164 }
165}