1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//! Multilingual operation vocabulary loaded from
//! `data/seed/operation-vocabulary.lino`.
use std::collections::BTreeMap;
use super::parser::parse_lino;
use super::OPERATION_VOCABULARY_LINO;
/// Localized surface forms for one operation in one supported language.
#[derive(Debug, Clone, Default)]
pub struct OperationLanguageForms {
pub phrases: Vec<String>,
pub combos: Vec<Vec<String>>,
}
impl OperationLanguageForms {
fn matches(&self, normalized: &str) -> bool {
self.phrases
.iter()
.any(|phrase| normalized.contains(phrase.as_str()))
|| self.combos.iter().any(|combo| {
!combo.is_empty()
&& combo
.iter()
.all(|token| normalized.contains(token.as_str()))
})
}
}
/// One canonical operation token plus localized trigger phrases.
#[derive(Debug, Clone, Default)]
pub struct OperationTrigger {
pub canonical: String,
pub languages: BTreeMap<String, OperationLanguageForms>,
/// The canonical operation this one undoes, when declared via an `inverse`
/// child in `operation-vocabulary.lino`. Subtractive program-plan rules are
/// *derived* from this declaration (issue #386), so adding a "cancel X"
/// operation stays pure seed data rather than new control flow.
pub inverse_of: Option<String>,
}
impl OperationTrigger {
/// Does any phrase or combo for this operation appear in `normalized`?
#[must_use]
pub fn matches(&self, normalized: &str) -> bool {
self.languages
.values()
.any(|forms| forms.matches(normalized))
}
}
/// The full multilingual operation vocabulary.
#[derive(Debug, Clone, Default)]
pub struct OperationVocabulary {
pub operations: Vec<OperationTrigger>,
}
impl OperationVocabulary {
/// Returns `true` when the operation with this canonical token is requested
/// by the normalized prompt in any supported language.
#[must_use]
pub fn matches(&self, canonical: &str, normalized: &str) -> bool {
self.operations
.iter()
.any(|op| op.canonical == canonical && op.matches(normalized))
}
/// Every canonical operation token whose phrasing appears in the normalized
/// prompt, in declaration order.
#[must_use]
pub fn detect(&self, normalized: &str) -> Vec<String> {
self.operations
.iter()
.filter(|op| op.matches(normalized))
.map(|op| op.canonical.clone())
.collect()
}
/// Append canonical English operation tokens to a normalized prompt.
///
/// Handlers can keep their canonical matching logic while accepting native
/// verbs from `operation-vocabulary.lino`.
#[must_use]
pub fn canonicalized_prompt(&self, normalized: &str) -> String {
let detected = self.detect(normalized);
if detected.is_empty() {
return normalized.to_owned();
}
let mut out = String::from(normalized);
for canonical in detected {
out.push(' ');
out.push_str(&canonical);
let phrase = canonical.replace('_', " ");
if phrase != canonical {
out.push(' ');
out.push_str(&phrase);
}
}
out
}
/// Every declared `(canonical, base)` inverse relationship, where `canonical`
/// is the operation that undoes `base` (e.g. `("cancel_reverse_sort",
/// "reverse_sort")`).
///
/// The program-plan engine derives subtractive substitution rules from these
/// pairs (issue #386), so a new "cancel X" stays pure seed data instead of
/// requiring new branching logic.
#[must_use]
pub fn inverse_pairs(&self) -> Vec<(String, String)> {
self.operations
.iter()
.filter_map(|op| {
op.inverse_of
.as_ref()
.map(|base| (op.canonical.clone(), base.clone()))
})
.collect()
}
}
#[must_use]
pub fn operation_vocabulary() -> OperationVocabulary {
let tree = parse_lino(OPERATION_VOCABULARY_LINO);
let mut vocabulary = OperationVocabulary::default();
if let Some(root) = tree.children.first() {
for operation_node in root.children.iter().filter(|c| c.name == "operation") {
let mut languages = BTreeMap::new();
for language_node in operation_node
.children
.iter()
.filter(|c| c.name == "language")
{
let mut forms = OperationLanguageForms::default();
for entry in &language_node.children {
match entry.name.as_str() {
"phrase" => forms.phrases.push(entry.id.clone()),
"combo" => forms.combos.push(split_combo(&entry.id)),
_ => {}
}
}
languages.insert(language_node.id.clone(), forms);
}
let inverse_of = match operation_node.find_child_value("inverse") {
"" => None,
base => Some(base.to_owned()),
};
vocabulary.operations.push(OperationTrigger {
canonical: operation_node.id.clone(),
languages,
inverse_of,
});
}
}
vocabulary
}
fn split_combo(raw: &str) -> Vec<String> {
raw.split('+')
.map(str::trim)
.filter(|s| !s.is_empty())
.map(ToOwned::to_owned)
.collect()
}