Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

KWAL – Keyword And Line

Status: Current Last updated: 2026-06-15 12:55 EDT

Purpose

Searches for clusters containing specified keywords and displays the matching lines with context. The legacy manual gives KWAL a dedicated section and describes it as operating on “clusters”: the main tier plus the selected dependent tiers associated with that line.

In talkbank-clan, keywords are currently matched against countable words on the main tier, with the matched utterance shown in context.

Usage

chatter-clan kwal -k want file.cha
chatter-clan kwal -k want --speaker CHI file.cha
chatter-clan kwal -k want -k cookie file.cha

Options (chatter-native)

OptionCLAN flagDescription
--speaker <CODE>+t*CHI (or +tCHI)Include speaker
--exclude-speaker <CODE>-t*CHI (or -tCHI)Exclude speaker
-k <WORD> / --keyword <WORD>+s"WORD"Keyword to search for (repeatable)
--gem <LABEL>+g"label"Restrict to gem segment
--range <START-END>+z25-125Utterance range
--id-filter <PATTERN>+t@ID="..."Filter by @ID pattern
--include-retracings+r6Include retraced words in counting
--format <FMT>–Output format: clan (default), text, json, csv

CLAN +-flag coverage audit

Authoritative enumeration of every CLAN kwal flag, mapped against chatter’s coverage. Sources:

  • OSX-CLAN/src/clan/kwal.cpp: usage() and getflag().
  • OSX-CLAN/src/clan/cutt.cpp: mainusage() KWAL branches.
  • crates/talkbank-clan/src/clan_args.rs: chatter’s rewriter.
  • crates/talkbank-cli/src/cli/args/clan_commands.rs::Kwal plus clan_common.rs::CommonAnalysisArgs.

(Status legend: same as FREQ.)

KWAL-specific +-flags (from kwal.cpp::getflag)

CLAN flagMeaningChatterStatusNotes
+aSort output in alphabetical order of keywords (repeats tier per keyword),MissingKWAL’s keyword-alphabetised mode.
+bApply +s match only when the keyword is the only item on the tier--strict-matchDoneLanded 2026-05-23. Gate runs before keyword matching: utterances with words.len() != 1 are rejected outright. Pinned by kwal_strict_match_only_solo_word_matches (single-word ["want"] matches, ["I", "want", "cookie"] doesn’t) and kwal_default_matches_anywhere_on_tier (default matches both).
+nSInclude all utterances from speaker S when they follow a match for +s,MissingSliding-window-by-speaker, distinct from +wN general context window.
-nSExclude all utterances from speaker S when they follow a match,Missing
+d (no N)Output legal CHAT format--legal-chatDoneLanded 2026-05-23. Rewriter maps bare +d for KWAL; render_clan skips the --- separator and *** File … Keyword: X decoration so only the matching *Speaker: lines remain. The dependent tiers attached to the match (e.g. %mor) come through to_chat_string() as legal CHAT. Pinned by kwal_legal_chat_format_drops_location_decoration and the rewriter test kwal_legal_chat_format. +dN for N ≥ 1 still falls through to --display-mode N.
+d1..+d4, +d30, +d31, +d40, +d7, +d90, +d99Various output-format and tier-linking variants,Rewriter onlyAll rewrite to --display-mode N; no consuming clap field.

General +-flags KWAL inherits (from cutt.cpp::mainusage)

CLAN flagMeaningChatterStatusNotes
+t*X / -t*XInclude/exclude speaker--speaker / --exclude-speakerDone+tX accepted post-2026-05-21.
+t%X / -t%XInclude/exclude dependent tier--tier / --exclude-tier (rewriter target)Rewriter onlyKWAL stores fDepTierName / sDepTierName for cluster output; chatter does not expose this.
+t@ID="..."Filter by @ID pattern--id-filterDone
+t#ROLEFilter by role--roleDoneFixed 2026-05-22; see FREQ for the shared implementation.
+s"word" / -s"word"Keyword (KWAL’s primary input)-k / --keyword / --exclude-wordPartialThe search mapping (+s"word" to --keyword) is correct. Divergence: CLAN’s +s is optional, not required, verified by running OSX-CLAN/src/unix/bin/kwal with no +s (it echoes all clusters, the +t%/-t% tier-trim use the manual documents); chatter currently requires --keyword and has no no-search pass-through mode. Tighten when KWAL’s cluster/tier-echo behavior lands.
+s@F / -s@FKeywords / exclude words from file--include-word-file / --exclude-word-fileDoneLanded 2026-05-22. File format: one pattern per line; blank lines, # -comments, and ;%* -annotation lines skipped. Repeatable.
+gXGem filter--gemDoneKWAL uses the inherited semantic (gem segment), not an overloaded one.
+zN-MUtterance range--rangeDone
+rNRetrace / clitic / prosodic controls--include-retracings (+r6)Partial
+uCombine across files(default)DoneInverse default vs CLAN.
+reRecurse(default)Done
+pSWord delimiter,Missing
+kCase-sensitive--case-sensitiveDoneLanded 2026-05-23. Reads CommonAnalysisArgs::case_sensitive; KWAL process_utterance skips the NormalizedWord lowercasing on both keyword and word sides when set. The flag is wired across the search/frequency family, with polarity matching each command’s CLAN default; see the flag-translation guide. Pinned by kwal_case_sensitive_uppercase_keyword_misses_lowercase_word and kwal_case_sensitive_matches_when_case_aligned.
+wN / -wNContext window--context-after N / --context-before NDoneLanded 2026-05-23. +wN emits N utterances following each match; -wN emits N utterances preceding. KwalState carries a VecDeque ring buffer of recent utterance texts (for pre-context) and a Vec<(match_idx, remaining)> list of matches still collecting post-context. Per-utterance flow: feed any open awaiting-after entries, detect match, snapshot ring as pre_context, register new awaiting-after, then update ring. render_clan emits pre-context lines + match body + post-context lines per match block. Distinct from KWAL’s +nS speaker-context (separate Missing item). Pinned by kwal_context_after_captures_post_match_lines, kwal_context_before_captures_pre_match_lines, and kwal_default_no_context_window.
+f / +fEXTOutput to file--output-ext (rewriter target)Rewriter onlyPhase 1.1.

Audit summary

BucketCount
Done (byte-parity or in scope)11
Partial2
Rewriter only9
Missing6

KWAL has the largest “Rewriter only” bucket of any audited command so far (12). Almost all the variants come from +dN’s nine sub-modes (+d, +d1..+d4, +d30, +d31, +d40, +d7, +d90, +d99), each of which materially changes KWAL’s output shape. Until --display-mode N is wired in, a researcher pasting kwal +d1 +s"want" file.cha into chatter gets a parse error.

CLAN Equivalence

CLAN commandRust equivalent
kwal +s"want" file.chachatter-clan kwal file.cha -k want
kwal +s"want" +t*CHI file.chachatter-clan kwal file.cha -k want --speaker CHI

Display Modes (+dN / --display-mode N), partially landed

Status: +d (no number) implemented as --legal-chat; +dN for N ≥ 1 still drafted from CLAN manual. The generic rewriter at crates/talkbank-clan/src/clan_args.rs translates each +dN to --display-mode N, but no clap field consumes that token for N ≥ 1 today. This table is drafted from CLAN manual §7.17.5 (Unique Options, KWAL) verbatim for PI review. The generic +dN placeholder is tracked internally.

KWAL uses +d to switch the output shape (plain CHAT, with filenames, Excel form, etc.). Quoted from CLAN manual §7.17.5:

NCLAN behavior (verbatim from manual)
+d (no number)“Normally, kwal outputs the location of the tier where the match occurs. When the +d switch is turned on you can [output] in these formats: … outputs legal CHAT format.”
+d1“Outputs legal CHAT format plus file names and line numbers.”
+d2“Outputs file names once per file only.”
+d3“Outputs ONLY matched items.”
+d30“Outputs ONLY matched items without any defaults removed. The +d30 and the +d3 switches can be combined.”
+d99“Convert ‘word [x 2]’ to ‘word [/] word’ and so on.”
+d4“Outputs for Excel.”
+d40“Outputs for Excel, repeating the same tier for every keyword match.”
+d7“Compares items across dependent tiers.” Example: kwal +d7 +s@|-cop +sROOT +t%gra +t%mor t.cha

Open questions for PI review

  1. +d (no number): resolved 2026-05-23. Implemented as --legal-chat (boolean), chatter’s default render emits the *** File ... Keyword: X location decoration; +d switches to the matching utterance lines as a legal CHAT fragment. The --display-mode 0 numeric framing was rejected in favour of a named boolean since the rest of the +dN table is genuinely distinct shape/format options rather than a numeric severity axis.
  2. +d30 is “+d3 + don’t strip defaults”, combinable. Maps to --display-mode matched --no-strip-defaults or --display-mode 30?
  3. +d99 is conceptually orthogonal to the others (it’s a transformation, not an output shape). Worth splitting into a separate --expand-repetition flag rather than overloading --display-mode.
  4. +d4/+d40 for Excel: same Excel question as FREQ, overlap with --format csv.
  5. +d7 cross-tier comparison: deeply specific. In scope for the first --display-mode pass, or future work alongside freqpos / mortable?

Output

Each matching utterance with:

  • Speaker code
  • Full utterance text
  • File path (for multi-file searches)
  • Match count summary per keyword

Differences from CLAN

  • Manual intent: KWAL is a cluster-oriented search command, not just a main-tier keyword matcher.
  • Search: Operates on parsed AST word content rather than raw text lines.
  • Word identification: Uses AST-based is_countable_word() instead of CLAN’s string-prefix matching.
  • Scope reduction: The legacy manual describes richer tier-selection and output-shaping behavior, including cluster searches over selected dependent tiers and %mor/%gra combined searches with +d7. The current implementation is narrower.
  • Output formats: Supports text, JSON, and CSV formats (CLAN produces text only).
  • Golden test parity: Verified against CLAN C binary output.