mla-titlecase-cli 0.3.0

CLI for building and inspecting mla-titlecase lexicons
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use std::io::Read;

use flate2::read::GzDecoder;
use mla_titlecase::{
    util::normalize::{lookup_key, normalized_unique_sorted},
    MapEntry, PluginPayload, RankedEntry,
};
use rmpv::Value;
use serde_json::Value as JsonValue;

use crate::{
    error::{CliError, Result},
    manifest::NormalizationReport,
};

pub(crate) struct NormalizedPayload {
    pub(crate) payload: PluginPayload,
    pub(crate) report: NormalizationReport,
}

pub(crate) fn parse_stopwords_json(raw: &[u8]) -> Result<NormalizedPayload> {
    let value: JsonValue = serde_json::from_slice(raw)?;
    let words: Vec<String> = match value {
        JsonValue::Array(items) => {
            items.into_iter().map(string_value).collect::<std::result::Result<Vec<_>, _>>()?
        }
        JsonValue::Object(map) => map
            .get("en")
            .ok_or_else(|| CliError::UnsupportedInput {
                path: std::path::PathBuf::from("<memory>"),
                message: "expected an `en` array in stopwords JSON".to_string(),
            })?
            .as_array()
            .ok_or_else(|| CliError::UnsupportedInput {
                path: std::path::PathBuf::from("<memory>"),
                message: "expected `en` to be an array".to_string(),
            })?
            .iter()
            .cloned()
            .map(string_value)
            .collect::<std::result::Result<Vec<_>, _>>()?,
        _ => {
            return Err(CliError::UnsupportedInput {
                path: std::path::PathBuf::from("<memory>"),
                message: "expected a JSON array or object".to_string(),
            })
        }
    };
    Ok(word_set_payload(words, 0))
}

pub(crate) fn parse_scowl_word_list(raw: &[u8]) -> Result<NormalizedPayload> {
    let raw = std::str::from_utf8(raw).map_err(|error| CliError::UnsupportedInput {
        path: std::path::PathBuf::from("<memory>"),
        message: format!("SCOWL input must be UTF-8: {error}"),
    })?;

    let mut extracted = Vec::new();
    let mut ignored_records = 0;
    let mut current_headword: Option<String> = None;

    for line in raw.lines() {
        let trimmed = line.trim();
        if trimmed.is_empty() || trimmed.starts_with('#') {
            continue;
        }

        let without_comment = trimmed.split('#').next().unwrap_or(trimmed).trim();
        let Some((_metadata, body)) = without_comment.split_once(':') else {
            ignored_records += 1;
            continue;
        };

        let mut sections = body.split(':');
        let Some(main_section) = sections.next() else {
            ignored_records += 1;
            continue;
        };

        let headword = resolve_scowl_headword(main_section, current_headword.as_deref());
        let Some(headword) = headword else {
            ignored_records += 1;
            continue;
        };
        current_headword = Some(headword.clone());
        extracted.extend(extract_scowl_words(&headword, Some(&headword)));

        for variant_section in sections {
            extracted.extend(extract_scowl_words(variant_section, Some(&headword)));
        }
    }

    let input_records = extracted.len();
    Ok(word_set_payload_with_counts(extracted, input_records, ignored_records))
}

pub(crate) fn parse_wordfreq_msgpack(raw: &[u8]) -> Result<NormalizedPayload> {
    let mut decoder = GzDecoder::new(raw);
    let mut bytes = Vec::new();
    decoder.read_to_end(&mut bytes)?;
    let value = rmpv::decode::read_value(&mut std::io::Cursor::new(bytes)).map_err(|error| {
        CliError::UnsupportedInput {
            path: std::path::PathBuf::from("<memory>"),
            message: format!("invalid wordfreq MessagePack payload: {error}"),
        }
    })?;

    let buckets = parse_cbpack(value)?;
    let mut next_rank = 1_u64;
    let mut input_records = 0_usize;
    let mut deduped = std::collections::BTreeMap::new();

    for bucket in buckets {
        for word in bucket {
            input_records += 1;
            deduped.entry(word).or_insert_with(|| {
                let rank = next_rank;
                next_rank += 1;
                rank
            });
        }
    }

    let entries =
        deduped.into_iter().map(|(word, rank)| RankedEntry { word, rank }).collect::<Vec<_>>();

    Ok(NormalizedPayload {
        report: NormalizationReport {
            input_records,
            output_entries: entries.len(),
            duplicates_removed: input_records.saturating_sub(entries.len()),
            ignored_records: 0,
        },
        payload: PluginPayload::RankedWords { entries },
    })
}

pub(crate) fn canonical_map_payload(entries: Vec<(String, String)>) -> NormalizedPayload {
    map_payload(entries, normalize_single_word_key, |entries| PluginPayload::CanonicalMap {
        entries,
    })
}

pub(crate) fn multiword_map_payload(entries: Vec<(String, String)>) -> NormalizedPayload {
    map_payload(entries, normalize_phrase_key, |entries| PluginPayload::MultiwordMap { entries })
}

pub(crate) fn protected_spellings_payload(entries: Vec<(String, String)>) -> NormalizedPayload {
    map_payload(entries, normalize_single_word_key, |entries| PluginPayload::ProtectedSpellings {
        entries,
    })
}

fn word_set_payload(words: Vec<String>, ignored_records: usize) -> NormalizedPayload {
    word_set_payload_with_counts(words.clone(), words.len(), ignored_records)
}

fn word_set_payload_with_counts(
    words: Vec<String>,
    input_records: usize,
    ignored_records: usize,
) -> NormalizedPayload {
    let normalized = normalized_unique_sorted(words);
    let output_entries = normalized.len();
    NormalizedPayload {
        report: NormalizationReport {
            input_records,
            output_entries,
            duplicates_removed: input_records.saturating_sub(output_entries),
            ignored_records,
        },
        payload: PluginPayload::WordSet { words: normalized },
    }
}

fn map_payload<F>(
    entries: Vec<(String, String)>,
    normalize_key: F,
    build_payload: impl FnOnce(Vec<MapEntry>) -> PluginPayload,
) -> NormalizedPayload
where
    F: Fn(&str) -> String,
{
    let input_records = entries.len();
    let mut deduped = std::collections::BTreeMap::new();
    let mut valid_records = 0_usize;
    let mut ignored_records = 0_usize;

    for (key, value) in entries {
        let normalized_key = normalize_key(&key);
        let normalized_value = value.trim();
        if normalized_key.is_empty() || normalized_value.is_empty() {
            ignored_records += 1;
            continue;
        }
        valid_records += 1;
        deduped.entry(normalized_key).or_insert_with(|| normalized_value.to_string());
    }

    let entries =
        deduped.into_iter().map(|(key, value)| MapEntry { key, value }).collect::<Vec<_>>();

    NormalizedPayload {
        report: NormalizationReport {
            input_records,
            output_entries: entries.len(),
            duplicates_removed: valid_records.saturating_sub(entries.len()),
            ignored_records,
        },
        payload: build_payload(entries),
    }
}

fn normalize_single_word_key(value: &str) -> String {
    lookup_key(value)
}

fn normalize_phrase_key(value: &str) -> String {
    value.split_whitespace().map(lookup_key).collect::<Vec<_>>().join(" ")
}

fn resolve_scowl_headword(fragment: &str, current_headword: Option<&str>) -> Option<String> {
    let head = fragment.split('<').next().unwrap_or(fragment).trim();
    let candidate = head.rsplit(':').next().unwrap_or(head).trim();
    match candidate {
        "" => None,
        "-" | "~" => current_headword.map(ToOwned::to_owned),
        other => Some(other.to_string()),
    }
}

fn extract_scowl_words(fragment: &str, current_headword: Option<&str>) -> Vec<String> {
    let base = fragment.split('<').next().unwrap_or(fragment).trim();
    let candidate = base.rsplit(':').next().unwrap_or(base).trim();
    let replaced = match candidate {
        "-" | "~" => current_headword.unwrap_or(candidate).to_string(),
        other => other.replace('~', current_headword.unwrap_or("")),
    };

    let mut words = Vec::new();
    let mut current = String::new();
    for ch in replaced.chars() {
        if ch.is_ascii_alphanumeric() || matches!(ch, '\'' | '.' | '-') {
            current.push(ch);
        } else if !current.is_empty() {
            push_candidate(&mut words, &mut current);
        }
    }
    if !current.is_empty() {
        push_candidate(&mut words, &mut current);
    }
    words
}

fn push_candidate(words: &mut Vec<String>, current: &mut String) {
    let candidate = current.trim_matches(|ch: char| ch == '\'' || ch == '-');
    if candidate.chars().any(|ch| ch.is_ascii_alphanumeric()) {
        words.push(candidate.to_string());
    }
    current.clear();
}

fn parse_cbpack(value: Value) -> Result<Vec<Vec<String>>> {
    let array = value.as_array().ok_or_else(|| CliError::UnsupportedInput {
        path: std::path::PathBuf::from("<memory>"),
        message: "wordfreq data must decode to a top-level array".to_string(),
    })?;
    let Some(header) = array.first() else {
        return Err(CliError::UnsupportedInput {
            path: std::path::PathBuf::from("<memory>"),
            message: "wordfreq data is missing the cBpack header".to_string(),
        });
    };
    validate_cbpack_header(header)?;

    array
        .iter()
        .skip(1)
        .map(|bucket| {
            let words = bucket.as_array().ok_or_else(|| CliError::UnsupportedInput {
                path: std::path::PathBuf::from("<memory>"),
                message: "wordfreq bucket must be an array of strings".to_string(),
            })?;
            words
                .iter()
                .map(|word| {
                    word.as_str().map(ToOwned::to_owned).ok_or_else(|| CliError::UnsupportedInput {
                        path: std::path::PathBuf::from("<memory>"),
                        message: "wordfreq bucket entry must be a string".to_string(),
                    })
                })
                .collect::<Result<Vec<_>>>()
        })
        .collect()
}

fn validate_cbpack_header(header: &Value) -> Result<()> {
    let map = header.as_map().ok_or_else(|| CliError::UnsupportedInput {
        path: std::path::PathBuf::from("<memory>"),
        message: "wordfreq header must be a map".to_string(),
    })?;

    let format =
        map.iter().find_map(|(key, value)| (key.as_str() == Some("format")).then_some(value));
    let version =
        map.iter().find_map(|(key, value)| (key.as_str() == Some("version")).then_some(value));

    if format.and_then(Value::as_str) != Some("cB") || version.and_then(Value::as_i64) != Some(1) {
        return Err(CliError::UnsupportedInput {
            path: std::path::PathBuf::from("<memory>"),
            message: "unexpected wordfreq cBpack header".to_string(),
        });
    }

    Ok(())
}

fn string_value(value: JsonValue) -> Result<String> {
    value.as_str().map(ToOwned::to_owned).ok_or_else(|| CliError::UnsupportedInput {
        path: std::path::PathBuf::from("<memory>"),
        message: "expected a string value".to_string(),
    })
}

#[cfg(test)]
mod tests {
    use flate2::{write::GzEncoder, Compression};
    use mla_titlecase::{PluginPayload, RankedEntry};
    use rmpv::Value;

    use super::{parse_scowl_word_list, parse_stopwords_json, parse_wordfreq_msgpack};

    #[test]
    fn parses_stopword_json() {
        let payload = parse_stopwords_json(br#"["and", "the"]"#).unwrap();
        assert_eq!(
            payload.payload,
            PluginPayload::WordSet { words: vec!["and".to_string(), "the".to_string()] }
        );
        assert_eq!(payload.report.input_records, 2);
    }

    #[test]
    fn parses_scowl_pre_lines() {
        let payload = parse_scowl_word_list(
            br#"
35: that <d>: those
35: who <pn>: whom, -, whose, whose
50 [12dicts]: - <n>: who's
"#,
        )
        .unwrap();

        assert_eq!(
            payload.payload,
            PluginPayload::WordSet {
                words: vec![
                    "that".to_string(),
                    "those".to_string(),
                    "who".to_string(),
                    "who's".to_string(),
                    "whom".to_string(),
                    "whose".to_string(),
                ],
            }
        );
        assert_eq!(payload.report.input_records, 8);
    }

    #[test]
    fn parses_wordfreq_msgpack_payload() {
        let payload = parse_wordfreq_msgpack(&encode_cbpack(vec![
            vec![],
            vec!["the".to_string()],
            vec!["and".to_string(), "of".to_string()],
        ]))
        .unwrap();

        assert_eq!(
            payload.payload,
            PluginPayload::RankedWords {
                entries: vec![
                    RankedEntry { word: "and".to_string(), rank: 2 },
                    RankedEntry { word: "of".to_string(), rank: 3 },
                    RankedEntry { word: "the".to_string(), rank: 1 },
                ],
            }
        );
        assert_eq!(payload.report.input_records, 3);
    }

    fn encode_cbpack(buckets: Vec<Vec<String>>) -> Vec<u8> {
        let mut top_level = Vec::with_capacity(1 + buckets.len());
        top_level.push(Value::Map(vec![
            (Value::from("format"), Value::from("cB")),
            (Value::from("version"), Value::from(1)),
        ]));
        top_level.extend(
            buckets.into_iter().map(|bucket| {
                Value::Array(bucket.into_iter().map(Value::from).collect::<Vec<_>>())
            }),
        );

        let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
        rmpv::encode::write_value(&mut encoder, &Value::Array(top_level)).unwrap();
        encoder.finish().unwrap()
    }
}