quarb 0.37.0

Query engine for arbors (arboreal graphs - tree-spanned or tree-dominated graphs)
Documentation
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
//! The command registry: every name a pipeline may call, with the
//! positions it stands in, the family that evaluates it, and a line
//! of documentation — one table behind the parser's validation, the
//! executor's dispatch, completion and highlighting.
//!
//! A name is *scalar* when it works per capsa on the plain pipe
//! (`| upper`), and an *aggregate* when it reads the whole context
//! on `@|`. The aggregates divide by what they do with the capsae:
//! a [`Agg::Reducing`] aggregate folds them to a value (and so also
//! works per capsa on a group's list members: `@| group(::k) |
//! mean`); a [`Agg::Context`] command reads the whole context only and
//! never takes the plain pipe; a [`Agg::Keyed`] aggregate reorders or
//! filters the capsae themselves by per-capsa keys; an
//! [`Agg::Association`] reduces two per-capsa expressions to one
//! number. A few names are both scalar and aggregate (`json` renders
//! each topic, or the whole context as one document).

use crate::ast::FnCall;

/// What an aggregate does with the capsae.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Agg {
    /// Folds the capsae to a value; per capsa on a list topic too.
    Reducing,
    /// Reads the whole context only: `@|` and never `|`.
    Context,
    /// Reorders or filters the capsae by per-capsa keys.
    Keyed,
    /// Reduces two per-capsa expressions to one number; an
    /// *agreement* measure reads them as labels, not numbers.
    Association { agreement: bool },
}

/// The argument check the parser runs on a call to this name.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Check {
    /// No shape beyond the generic argument rules.
    None,
    /// The record convention (`key = value` fields, auto-named
    /// projections).
    Record,
    /// A keyed aggregate: expression keys, the count of `top` /
    /// `bottom`, the collation words of `sort`, `group`'s keys.
    Keyed,
    /// A whole-context command's own shape: `window` / `shift` (an
    /// offset range or distance, then a key), the sizes of
    /// `combinations` / `pairs` / `triples`, the columns of `csv` /
    /// `table`, `number`, `timeline`, `outline`, `zscore`.
    Command,
    /// `decode` / `dec`: one reversible scheme name.
    Decode,
    /// The statistics round: `sample` for the dispersion measures,
    /// two expressions for an association.
    Stat,
}

/// One registered command.
#[derive(Debug, Clone, Copy)]
pub struct FnSpec {
    pub name: &'static str,
    /// Works per capsa on the plain pipe.
    pub scalar: bool,
    /// Works on `@|`, and how.
    pub agg: Option<Agg>,
    /// The parser's argument check.
    pub check: Check,
    /// One line for people: completion, the manual.
    pub doc: &'static str,
    /// The options the command declares, as a utility's synopsis
    /// lists them (`-i`, `--locale`); any other option in its
    /// segment is refused by name. No built-in declares one yet:
    /// the syntax stands empty for the shell-function round.
    pub options: &'static [&'static str],
}

const fn scalar(name: &'static str, doc: &'static str) -> FnSpec {
    FnSpec {
        name,
        scalar: true,
        agg: None,
        check: Check::None,
        doc,
        options: &[],
    }
}

const fn reducing(name: &'static str, doc: &'static str) -> FnSpec {
    FnSpec {
        name,
        scalar: false,
        agg: Some(Agg::Reducing),
        check: Check::None,
        doc,
        options: &[],
    }
}

const fn context(name: &'static str, doc: &'static str) -> FnSpec {
    FnSpec {
        name,
        scalar: false,
        agg: Some(Agg::Context),
        check: Check::None,
        doc,
        options: &[],
    }
}

/// A closing renderer: an aggregate the tool takes off and renders
/// (`@| csv`, `@| pretty`), and a scalar command after a value
/// (`| pretty` after a table).
const fn rendering(name: &'static str, doc: &'static str) -> FnSpec {
    let mut s = context(name, doc);
    s.scalar = true;
    s
}

const fn keyed(name: &'static str, doc: &'static str) -> FnSpec {
    FnSpec {
        name,
        scalar: false,
        agg: Some(Agg::Keyed),
        check: Check::Keyed,
        doc,
        options: &[],
    }
}

const fn assoc(name: &'static str, agreement: bool, doc: &'static str) -> FnSpec {
    FnSpec {
        name,
        scalar: false,
        agg: Some(Agg::Association { agreement }),
        check: Check::Stat,
        doc,
        options: &[],
    }
}

const fn both(name: &'static str, doc: &'static str) -> FnSpec {
    FnSpec {
        name,
        scalar: true,
        agg: Some(Agg::Reducing),
        check: Check::None,
        doc,
        options: &[],
    }
}

const fn with_check(spec: FnSpec, check: Check) -> FnSpec {
    FnSpec { check, ..spec }
}

/// Every command, in the order the manual lists them.
pub const SPECS: &[FnSpec] = &[
    // Text.
    scalar("upper", "the text in upper case"),
    scalar("lower", "the text in lower case"),
    scalar("title", "the text with each word capitalized"),
    scalar("trim", "the text without leading and trailing whitespace"),
    scalar("ltrim", "the text without leading whitespace"),
    scalar("rtrim", "the text without trailing whitespace"),
    scalar("alpha", "the letters of the text"),
    scalar("digit", "the digits of the text"),
    scalar("alnum", "the letters and digits of the text"),
    scalar("cc", "the count of codepoints"),
    scalar("bc", "the count of UTF-8 bytes"),
    scalar("wc", "the count of words"),
    scalar("lc", "the count of lines"),
    scalar("sc", "the count of sentences"),
    scalar("lines", "the lines of the text, one capsa each"),
    scalar("words", "the words of the text, one capsa each"),
    with_check(
        both(
            "ngrams",
            "the word n-grams of the text: ngrams(N); as an aggregate, the \
             adjacent N-windows of the stream, full windows only",
        ),
        Check::Command,
    ),
    scalar("levenshtein", "the edit distance to the argument"),
    scalar(
        "jaro_winkler",
        "the Jaro–Winkler similarity to the argument, 0 to 1",
    ),
    scalar(
        "tokens",
        "the corpus tier's tokens of the text as a list; tokens(word) the \
         word class alone",
    ),
    scalar("casefold", "Unicode case folding (ß to ss, final sigma)"),
    scalar(
        "deaccent",
        "the text without combining marks (NFD, marks stripped); deaccent(ru) \
         folds ё too",
    ),
    scalar(
        "stem",
        "the Snowball stem under a language: stem(en), stem(ru), …",
    ),
    scalar("s", "regex substitution: s/pattern/replacement/mods"),
    scalar(
        "split",
        "the pieces between a separator (whitespace unless given)",
    ),
    scalar("substr", "the codepoints of a range: substr(a..b)"),
    scalar("pad", "the text padded on the right to a width"),
    scalar("lpad", "the text padded on the left to a width"),
    scalar("rpad", "the text padded on the right to a width"),
    scalar("repeat", "the text repeated N times"),
    scalar("indent", "every line indented by N spaces"),
    scalar(
        "sentences",
        "the sentences of the text, under the session's bonds",
    ),
    scalar("default", "the argument where the topic is null"),
    // Unicode, stress, transliteration, spelling.
    scalar("nfc", "the text in Unicode NFC"),
    scalar("nfd", "the text in Unicode NFD"),
    scalar("vowels", "the vowel count of the word"),
    scalar("stress", "the word with the stress mark on vowel N"),
    scalar("stress_at", "the index of the stressed vowel"),
    scalar("unstress", "the word without stress marks"),
    scalar("accent", "the stress mark rendered in a style"),
    scalar("translit", "the text transliterated under a scheme"),
    scalar(
        "modernize",
        "historical orthography modernized under a table",
    ),
    scalar("gc", "the count of grapheme clusters"),
    // Numbers.
    FnSpec {
        name: "number",
        scalar: true,
        agg: Some(Agg::Context),
        check: Check::Command,
        doc: "the numeric reading of text (per capsa), or the running number of each capsa (@|)",
        options: &[],
    },
    scalar("round", "the number rounded to N digits"),
    scalar("floor", "the number rounded down"),
    scalar("ceil", "the number rounded up"),
    scalar("abs", "the absolute value"),
    scalar("log", "the natural logarithm"),
    scalar("log2", "the base-2 logarithm"),
    scalar("log10", "the base-10 logarithm"),
    scalar("exp", "e to the power of the number"),
    scalar("sqrt", "the square root"),
    scalar("pow", "the number to the power of the argument"),
    scalar(
        "loglik",
        "the log-likelihood keyness of a count against a reference",
    ),
    scalar(
        "chi2",
        "the chi-squared keyness of a count against a reference",
    ),
    scalar("mi", "the mutual-information collocation measure"),
    scalar("t_score", "the t-score collocation measure"),
    scalar("log_dice", "the logDice collocation measure"),
    scalar("logdice", "log_dice, alias"),
    scalar("tscore", "t_score, alias"),
    scalar("dice", "the Dice coefficient of a collocation"),
    scalar("haversine", "the great-circle distance to a point"),
    scalar("quantity", "the text read as a quantity with a unit"),
    scalar("convert", "the quantity converted to a unit"),
    // The two readings of a table (spec: Command Libraries, "A
    // library value") and the reshapes that need no algorithm.
    both(
        "@",
        "the row reading: a library value as its rows, a record of lists as a list of records",
    ),
    both(
        "%",
        "the column reading: a library value as its columns, a list of records as a record of lists",
    ),
    both(
        "transpose",
        "a list of lists with its rows and columns exchanged",
    ),
    both(
        "#",
        "the table reading: a list of records as a frame, a list of lists as an array, a list of values as a sample",
    ),
    both(
        "rows",
        "the row reading of a table or a library value, as '| @'",
    ),
    both(
        "columns",
        "the column reading of a table or a library value, as '| %'",
    ),
    both(
        "header",
        "a table's header, the list of column names (null on the array face)",
    ),
    both(
        "dim",
        "a table's shape, @(rows; columns)",
    ),
    both(
        "[]",
        "a row of a table by position, or a slice of its rows (written '| [n]', '| [a..b]')",
    ),
    both(
        "cell",
        "one cell of a table: cell(i; j), the row by position, the column by position or name",
    ),
    both(
        "row-to-header",
        "the first row lifted into the header",
    ),
    both(
        "header-to-row",
        "the header lowered into the first row (as text), the table then an array",
    ),
    both(
        "pivot",
        "long to wide: pivot(row; column; value) makes one record per row key with a field per column key",
    ),
    FnSpec {
        name: "melt",
        scalar: true,
        agg: Some(Agg::Reducing),
        check: Check::None,
        doc: "wide to long: melt(id; …) makes one record per other field, as name and value",
        options: &["--name", "--value"],
    },
    // Serializations.
    both("json", "the topic (per capsa) or the context (@|) as JSON"),
    both(
        "jsonl",
        "the topic (per capsa) or the context (@|) as JSON Lines",
    ),
    both("kaiv", "the topic (per capsa) or the context (@|) as kaiv"),
    scalar("link", "the node's link address"),
    scalar("xml", "the node as XML"),
    scalar("markdown", "the node as Markdown"),
    scalar("html", "the node as HTML"),
    scalar("atrep", "the node as atrep text"),
    scalar("conllu", "the node as CoNLL-U"),
    scalar("bibref", "the reference parsed into its fields"),
    with_check(
        scalar("record", "retired: the record is %(…)"),
        Check::Record,
    ),
    with_check(scalar("rec", "retired: the record is %(…)"), Check::Record),
    // The temporal fragment.
    scalar("datetime", "the instant read from text"),
    scalar("epoch", "the instant as seconds since the epoch"),
    scalar("isoformat", "the instant in ISO 8601"),
    scalar("year", "the instant's year"),
    scalar("month", "the instant's month"),
    scalar("day", "the instant's day of the month"),
    scalar("hour", "the instant's hour"),
    scalar("minute", "the instant's minute"),
    scalar("second", "the instant's second"),
    scalar("weekday", "the instant's day of the week"),
    scalar("date", "the instant's calendar date"),
    scalar("isodate", "the instant's date in ISO 8601"),
    scalar("isomonth", "the instant's year and month in ISO 8601"),
    scalar("isoweek", "the instant's ISO week"),
    scalar("seconds", "the duration in seconds"),
    scalar("minutes", "the duration in minutes"),
    scalar("hours", "the duration in hours"),
    scalar("days", "the duration in days"),
    scalar("duration", "the duration read from text"),
    scalar("td", "duration, short"),
    scalar("strptime", "the instant read under a format and a locale"),
    scalar("tp", "strptime, short"),
    scalar(
        "strftime",
        "the instant written under a format and a locale",
    ),
    scalar("tfmt", "strftime, short"),
    scalar("timex", "the time expressions of the text"),
    scalar(
        "time_anchor",
        "the time expression resolved against an anchor",
    ),
    scalar("period", "the period a time expression denotes"),
    scalar("grain", "the granularity of a time expression"),
    // Encodings and the shell.
    scalar("sha256", "the SHA-256 digest, hex"),
    scalar("base64", "the text in base64"),
    scalar("base64url", "the text in URL-safe base64"),
    scalar("base32", "the text in base32"),
    scalar("crockford32", "the text in Crockford base32"),
    scalar("hex", "the text in hexadecimal"),
    with_check(
        scalar("decode", "the text decoded from a scheme"),
        Check::Decode,
    ),
    with_check(scalar("dec", "decode, short"), Check::Decode),
    scalar(
        "sh",
        "the topic through a shell command (needs --allow-shell)",
    ),
    // Reductions.
    reducing("count", "how many"),
    reducing("sum", "the sum"),
    reducing("product", "the product"),
    reducing("min", "the least"),
    reducing("max", "the greatest"),
    reducing("mean", "the arithmetic mean"),
    reducing("avg", "mean, alias"),
    reducing("median", "the median"),
    reducing("percentile", "the Nth percentile"),
    with_check(
        reducing(
            "std",
            "the standard deviation (sample with the word sample)",
        ),
        Check::Stat,
    ),
    with_check(
        reducing("var", "the variance (sample with the word sample)"),
        Check::Stat,
    ),
    with_check(reducing("stddev", "std, alias"), Check::Stat),
    with_check(reducing("variance", "var, alias"), Check::Stat),
    reducing(
        "entropy",
        "the Shannon entropy, bits: of a stream of counts, or of the \
         distribution of a stream of values",
    ),
    reducing("ttr", "the type-token ratio: distinct values over all"),
    reducing(
        "hapax",
        "the values that occur once, in first-occurrence order",
    ),
    with_check(
        reducing(
            "skipgrams",
            "the n-grams with up to k skipped positions: skipgrams(n; k)",
        ),
        Check::Command,
    ),
    reducing(
        "jaccard",
        "the Jaccard similarity of the stream's distinct values to a list",
    ),
    reducing("yule_k", "Yule's K over a stream of counts"),
    reducing(
        "mtld",
        "the measure of textual lexical diversity over tokens",
    ),
    with_check(
        reducing("bigrams", "the adjacent pairs of the stream (ngrams(2))"),
        Check::Command,
    ),
    with_check(
        reducing("trigrams", "the adjacent triples of the stream (ngrams(3))"),
        Check::Command,
    ),
    with_check(
        reducing(
            "frequencies",
            "each distinct value with its count, most frequent first, as \
             records (value, n); frequencies(K) keeps the top K",
        ),
        Check::Command,
    ),
    with_check(
        reducing("combinations", "every combination of N capsae"),
        Check::Command,
    ),
    with_check(reducing("pairs", "every pair of capsae"), Check::Command),
    with_check(
        reducing("triples", "every triple of capsae"),
        Check::Command,
    ),
    with_check(
        reducing(
            "sort",
            "the capsae sorted by value (sort(LOCALE), sort(atergo))",
        ),
        Check::Keyed,
    ),
    reducing("unique", "the distinct values"),
    reducing("reverse", "the capsae in reverse order"),
    reducing("first", "the first N (one unless given)"),
    reducing("last", "the last N (one unless given)"),
    reducing("join", "the values joined with a separator"),
    // Associations.
    assoc("corr", false, "Pearson correlation of two expressions"),
    assoc(
        "spearman",
        false,
        "Spearman rank correlation of two expressions",
    ),
    assoc("cosine", false, "cosine similarity of two expressions"),
    assoc("delta", false, "Burrows's Delta between two expressions"),
    assoc("dp", false, "Gries's deviation of proportions"),
    assoc(
        "kappa",
        true,
        "Cohen's kappa agreement of two label expressions",
    ),
    assoc(
        "precision",
        true,
        "precision of a prediction against a truth",
    ),
    assoc("recall", true, "recall of a prediction against a truth"),
    assoc("f1", true, "the F1 score of a prediction against a truth"),
    // Whole-context commands.
    context("ungroup", "the groups' members as the context again"),
    with_check(
        context(
            "window",
            "each capsa with its neighbours: window(-2..0), window(3)",
        ),
        Check::Command,
    ),
    with_check(
        context("shift", "each capsa's value from N capsae back"),
        Check::Command,
    ),
    with_check(
        context("zscore", "each value standardized over the context"),
        Check::Command,
    ),
    with_check(
        context("outline", "the context as a nested outline"),
        Check::Command,
    ),
    with_check(
        context("timeline", "the context ordered on its time anchors"),
        Check::Command,
    ),
    with_check(
        rendering("csv", "the records as CSV (closing: the tool renders it); after a table value, the table as CSV"),
        Check::Command,
    ),
    with_check(
        rendering(
            "pretty",
            "the records as an aligned table (closing: the tool renders it); after a value, the value pretty-printed: a table the grid, a record one field per line, a list one item per line",
        ),
        Check::Command,
    ),
    with_check(
        rendering(
            "table",
            "the aligned table, as 'pretty' (accepted through 0.37; retired after)",
        ),
        Check::Command,
    ),
    context(
        "encoding",
        "the output encoding (closing: the tool applies it)",
    ),
    // Keyed aggregates.
    keyed("sort_by", "the capsae sorted by keys"),
    keyed("unique_by", "the first capsa per distinct key"),
    keyed("min_by", "the capsa with the least key"),
    keyed("max_by", "the capsa with the greatest key"),
    keyed("top", "the N capsae with the greatest key"),
    keyed("bottom", "the N capsae with the least key"),
    keyed("group", "the capsae grouped by keys"),
];

/// The registered command called `name`.
pub fn spec(name: &str) -> Option<&'static FnSpec> {
    SPECS.iter().find(|s| s.name == name)
}

/// The commands that may stand in a position, for enumeration
/// (completion, the manual).
pub fn names(scalar: bool, agg: Option<Agg>) -> impl Iterator<Item = &'static str> {
    SPECS
        .iter()
        .filter(move |s| (scalar && s.scalar) || (agg.is_some() && s.agg == agg))
        .map(|s| s.name)
}

/// The parser's argument check for a call, by name.
pub fn check(call: &FnCall) -> Check {
    spec(&call.name).map_or(Check::None, |s| s.check)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn names_are_unique() {
        let mut seen = std::collections::HashSet::new();
        for s in SPECS {
            assert!(seen.insert(s.name), "{} registered twice", s.name);
            assert!(s.scalar || s.agg.is_some(), "{} stands nowhere", s.name);
            assert!(!s.doc.is_empty(), "{} has no doc", s.name);
        }
    }
}