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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
use hashbrown::HashMap;
use serde::{Deserialize, Serialize};
use smallvec::ToSmallVec;
use super::super::word_map::{WordMap, WordMapEntry};
use super::Error;
use super::affix_replacement::AffixReplacement;
use super::expansion::Property;
use super::expansion::{
AffixEntryKind,
AffixEntryKind::{Prefix, Suffix},
Expansion, HumanReadableExpansion,
};
use super::word_list::AnnotatedWord;
use crate::dict_word_metadata_orthography::OrthFlags;
use crate::spell::WordId;
use crate::{CharString, DictWordMetadata, Span};
#[derive(Debug, Clone)]
pub struct AttributeList {
/// Key = Affix Flag
affixes: HashMap<char, Expansion>,
properties: HashMap<char, Property>,
}
impl AttributeList {
fn into_human_readable(self) -> HumanReadableAttributeList {
HumanReadableAttributeList {
affixes: self
.affixes
.into_iter()
.map(|(affix, exp)| (affix, exp.into_human_readable()))
.collect(),
properties: self.properties,
}
}
pub fn parse(source: &str) -> Result<Self, Error> {
let human_readable: Result<HumanReadableAttributeList, _> = serde_json::from_str(source);
human_readable
.map_err(Error::from)
.and_then(|parsed| parsed.into_normal())
}
/// Expand an [`AnnotatedWord`] into a list of full words, including itself.
///
/// This function processes a word and its attributes to:
/// 1. Apply properties to the base word
/// 2. Generate derived words using affix rules
/// 3. Handle conditional expansions
/// 4. Manage cross-product expansions
///
/// # Arguments
/// * `word` - The word to expand, along with its attributes
/// * `dest` - The WordMap to store the expanded words and their metadata
pub fn expand_annotated_word(&self, annotated_word: AnnotatedWord, word_map: &mut WordMap) {
// Pre-allocate space in the destination map for better performance
word_map.reserve(annotated_word.annotations.len() + 1);
// Initialize base metadata that will be applied to all derived forms
let mut base_metadata = DictWordMetadata::default();
// Store metadata that should only be applied if certain conditions are met
let orth_flags = OrthFlags::from_letters(&annotated_word.letters);
base_metadata.orth_info = orth_flags;
let mut conditional_expansion_metadata = Vec::new();
// First pass: Process all properties to build the base metadata
// Properties directly modify the word's metadata (e.g., part of speech, usage)
for attr in &annotated_word.annotations {
let Some(property) = self.properties.get(attr) else {
continue;
};
base_metadata.merge(&property.metadata);
}
// Second pass: Process all affix rules to generate derived forms
for attr in &annotated_word.annotations {
// Skip if this attribute isn't an affix rule
let Some(expansion) = self.affixes.get(attr) else {
continue;
};
// Add any base metadata from this affix rule
base_metadata.merge(&expansion.base_metadata);
// Track new words generated by this affix rule
let mut new_words: HashMap<CharString, DictWordMetadata> = HashMap::new();
// Apply each replacement rule in this affix
for replacement in &expansion.replacements {
if let Some(replaced) =
Self::apply_replacement(replacement, &annotated_word.letters, expansion.kind)
{
// Get or create metadata for this new word form
let metadata = new_words.entry(replaced.clone()).or_default();
// Process each target for this replacement
for target in &expansion.target {
if let Some(condition) = &target.if_base {
// Store conditional metadata to be applied later
conditional_expansion_metadata.push((
replaced.clone(),
target.metadata.clone(),
condition.clone(),
));
} else {
// Apply target metadata immediately
metadata.merge(&target.metadata);
}
}
}
}
// Handle cross-product expansions (e.g., both prefix and suffix)
if expansion.cross_product {
// Collect attributes that should be applied to the opposite affix type
let mut opposite_attributes = Vec::new();
// Add properties that should propagate to derived forms
for attr in &annotated_word.annotations {
let Some(property) = self.properties.get(attr) else {
continue;
};
if expansion.kind == Prefix || property.propagate {
opposite_attributes.push(*attr);
}
}
// Add affix attributes of the opposite type
for attr in &annotated_word.annotations {
let Some(attr_def) = self.affixes.get(attr) else {
continue;
};
// This checks if the current affix is of the opposite type
if (attr_def.kind != Prefix) != (expansion.kind != Prefix) {
opposite_attributes.push(*attr);
}
}
// Recursively process each new word form
for (new_word, metadata) in new_words {
self.expand_annotated_word(
AnnotatedWord {
letters: new_word.clone(),
annotations: opposite_attributes.clone(),
},
word_map,
);
// Update the metadata of the expanded word
let target_metadata = word_map.get_metadata_mut_chars(&new_word).unwrap();
target_metadata.merge(&metadata);
target_metadata.derived_from =
Some(WordId::from_word_chars(&annotated_word.letters));
}
} else {
// Simple case: no cross-product expansion needed
for (key, mut value) in new_words.into_iter() {
value.derived_from = Some(WordId::from_word_chars(&annotated_word.letters));
if let Some(existing_metadata) = word_map.get_metadata_mut_chars(&key) {
// Merge with existing metadata
existing_metadata.merge(&value);
} else {
// Add new entry
word_map.insert(WordMapEntry {
canonical_spelling: key,
metadata: value,
});
}
}
}
}
// Finalize the metadata for the base word
let mut full_metadata = base_metadata;
// Merge with any existing metadata for this word
if let Some(existing_metadata) = word_map.get_with_chars(&annotated_word.letters) {
full_metadata.merge(&existing_metadata.metadata);
}
// Store the final metadata for the base word
word_map.insert(WordMapEntry {
metadata: full_metadata.clone(),
canonical_spelling: annotated_word.letters,
});
// Process any conditional expansions
for (letters, metadata, condition) in conditional_expansion_metadata {
// Check if the condition is satisfied by the base word's metadata
let condition_satisfied = full_metadata.or(&condition) == full_metadata;
if !condition_satisfied {
continue;
}
// Apply the conditional metadata
word_map
.get_metadata_mut_chars(&letters)
.unwrap()
.merge(&metadata);
}
}
/// Expand an iterator of annotated words into strings.
/// Note that this does __not__ guarantee that produced words will be
/// unique.
pub fn expand_annotated_words(
&self,
words: impl IntoIterator<Item = AnnotatedWord>,
dest: &mut WordMap,
) {
for word in words {
self.expand_annotated_word(word, dest);
}
}
fn apply_replacement(
replacement: &AffixReplacement,
letters: &[char],
kind: AffixEntryKind,
) -> Option<CharString> {
if replacement.condition.len() > letters.len() {
return None;
}
let target_span = if kind == Suffix {
Span::new(letters.len() - replacement.condition.len(), letters.len())
} else {
Span::new(0, replacement.condition.len())
};
let target_segment = target_span.get_content(letters);
if replacement.condition.matches(target_segment) {
let mut replaced_segment = letters.to_smallvec();
let mut remove: CharString = replacement.remove.to_smallvec();
if kind != Suffix {
replaced_segment.reverse();
} else {
remove.reverse();
}
for c in &remove {
let last = replaced_segment.last()?;
if last == c {
replaced_segment.pop();
} else {
return None;
}
}
let mut to_add = replacement.add.to_vec();
if kind != Suffix {
to_add.reverse()
}
replaced_segment.extend(to_add);
if kind != Suffix {
replaced_segment.reverse();
}
return Some(replaced_segment);
}
None
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HumanReadableAttributeList {
affixes: HashMap<char, HumanReadableExpansion>,
properties: HashMap<char, Property>,
}
impl HumanReadableAttributeList {
pub fn into_normal(self) -> Result<AttributeList, Error> {
let mut affixes = HashMap::with_capacity(self.affixes.len());
for (affix, expansion) in self.affixes.into_iter() {
affixes.insert(affix, expansion.into_normal()?);
}
Ok(AttributeList {
affixes,
properties: self.properties,
})
}
}
#[cfg(test)]
mod tests {
use crate::spell::{Dictionary, FstDictionary};
#[test]
fn proper_noun_property_propagates_to_plurals() {
let fst_dict = FstDictionary::curated();
if let Some(vw_plural) = fst_dict.get_word_metadata_str("Volkswagens") {
assert!(vw_plural.is_proper_noun());
}
}
#[test]
fn proper_noun_propagates_to_possessives_2327() {
if let Some(vw_possessive) = FstDictionary::curated().get_word_metadata_str("Volkswagen's")
{
assert!(vw_possessive.is_possessive_noun());
}
}
}