seams 0.1.17

High-throughput sentence extractor for Project Gutenberg texts with dialog-aware detection
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
# Regex Pattern Refactor: From Manual Duplication to Table-Driven DFA Generation

## Current Problem Analysis

The current `src/sentence_detector/dialog_detector.rs` exhibits massive code duplication across dialog states:

### Duplication Evidence
- **775 lines** of pattern generation code
- **7 dialog states** × **8+ patterns each** = ~56 nearly identical pattern definitions
- Each state repeats the same pattern structure with different closing characters:
  ```rust
  // Double quotes (lines 464-476)
  let dialog_external_separation_split = format!("{double_quote_close}{sentence_end_punct}...");
  let dialog_external_separation_continue = format!("{double_quote_close}{sentence_end_punct}...");
  // ... 6 more patterns
  
  // Single quotes (lines 520-532) - IDENTICAL STRUCTURE
  let dialog_external_separation_split = format!("{single_quote_close}{sentence_end_punct}...");
  let dialog_external_separation_continue = format!("{single_quote_close}{sentence_end_punct}...");
  // ... 6 more patterns
  
  // Smart quotes, parentheses, brackets, braces - ALL IDENTICAL
  ```

### Maintenance Problems
1. **GAP #1 Fix Complexity**: Required manual replication across 6 quote types (lines 516-773)
2. **Pattern Inconsistency Risk**: Easy to introduce bugs when hand-copying patterns
3. **Modification Amplification**: Any pattern change requires 7× manual updates
4. **Code Review Burden**: 700+ lines of nearly identical code obscures actual logic

---

## Proposed Solution: Route 2 - Table-Driven DFA Generation

### Architecture: SEAM Pattern Lattice → Compile-Time DFA Fleet

**Goal**: Replace 700+ lines of duplicated Rust code with a **declarative table** that generates optimized DFAs at build time.

### Table Schema Design

```toml
# seams.toml - The single source of truth
[pattern_components]
sentence_end_punct = "[.!?]"
non_sentence_ending_punct = "[,:;]"
soft_separator = "[ \\t]+"
sentence_starts = "[A-Z0-9\"'\"\"'([{]"
not_sentence_starts = "[^A-Z0-9\"'\"\"'([{]"
dialog_open_chars = "[\"'\"\"'([{]"

[dialog_states.double_quote]
open_char = "\""
close_char = "\""
state_name = "DialogDoubleQuote"

[dialog_states.single_quote]
open_char = "'"
close_char = "'"
state_name = "DialogSingleQuote"

[dialog_states.smart_double]
open_char = """
close_char = """
state_name = "DialogSmartDoubleOpen"

[dialog_states.smart_single]
open_char = "'"
close_char = "'"
state_name = "DialogSmartSingleOpen"

[dialog_states.round_paren]
open_char = "("
close_char = ")"
state_name = "DialogParenthheticalRound"

[dialog_states.square_bracket]
open_char = "["
close_char = "]"
state_name = "DialogParenthheticalSquare"

[dialog_states.curly_brace]
open_char = "{"
close_char = "}"
state_name = "DialogParenthheticalCurly"

# Pattern templates - applied to ALL dialog states
[[pattern_templates]]
name = "external_separation_split"
regex = "{close}{sentence_end_punct}({soft_separator})[{sentence_starts}]"
match_type = "DialogEnd"
next_state = "Narrative"
comment = "External separation + sentence start → D→N + Split"

[[pattern_templates]]
name = "external_separation_continue"
regex = "{close}{sentence_end_punct}({soft_separator}){not_sentence_starts}"
match_type = "DialogSoftEnd"
next_state = "Narrative"
comment = "External separation + lowercase → D→N + Continue (lowercase overrides)"

[[pattern_templates]]
name = "external_separation_to_dialog"
regex = "{close}{sentence_end_punct}({soft_separator}){dialog_open_chars}"
match_type = "DialogOpen"
next_state = "Unknown"
comment = "External separation + dialog open → D→D + Split"

[[pattern_templates]]
name = "external_continuation"
regex = "{close}{non_sentence_ending_punct}({soft_separator})."
match_type = "DialogSoftEnd"
next_state = "Narrative"
comment = "External continuation → D→N + Continue"

[[pattern_templates]]
name = "internal_hard_end"
regex = "{sentence_end_punct}{close}[,:;]*({soft_separator})[{sentence_starts}]"
match_type = "DialogEnd"
next_state = "Narrative"
comment = "Hard End (internal punctuation)"

[[pattern_templates]]
name = "internal_soft_end_punctuated"
regex = "{sentence_end_punct}{close}[,:;]*({soft_separator}){not_sentence_starts}"
match_type = "DialogSoftEnd"
next_state = "Narrative"
comment = "Soft End (punctuated)"

[[pattern_templates]]
name = "internal_continuation"
regex = "[^.!?]{close}[,:;]*({soft_separator}){dialog_open_chars}"
match_type = "DialogOpen"
next_state = "Unknown"
comment = "Dialog Continuation"

[[pattern_templates]]
name = "internal_soft_end_unpunctuated"
regex = "[^.!?]{close}[,:;]*({soft_separator})[^{dialog_open_chars}]"
match_type = "DialogSoftEnd"
next_state = "Narrative"
comment = "Soft End (unpunctuated)"
```

### Build Script Implementation

```rust
// build.rs
use regex_automata::dfa::dense;
use serde::Deserialize;
use std::collections::HashMap;

#[derive(Deserialize)]
struct SeamConfig {
    pattern_components: HashMap<String, String>,
    dialog_states: HashMap<String, DialogStateConfig>,
    pattern_templates: Vec<PatternTemplate>,
}

#[derive(Deserialize)]
struct DialogStateConfig {
    open_char: String,
    close_char: String,
    state_name: String,
}

#[derive(Deserialize)]
struct PatternTemplate {
    name: String,
    regex: String,
    match_type: String,
    next_state: String,
    comment: String,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config: SeamConfig = toml::from_str(include_str!("seams.toml"))?;
    let out_dir = std::env::var("OUT_DIR")?;
    
    // Generate DFAs for each dialog state
    for (state_key, state_config) in &config.dialog_states {
        let mut patterns = Vec::new();
        let mut mappings = Vec::new();
        
        // Apply each pattern template to this dialog state
        for template in &config.pattern_templates {
            let pattern = expand_template(template, state_config, &config.pattern_components)?;
            patterns.push(pattern);
            mappings.push((template.match_type.clone(), template.next_state.clone()));
        }
        
        // Build multi-pattern DFA for this state
        let dfa = dense::DFA::new_many(&patterns)?;
        let bytes = dfa.to_bytes_little_endian()?;
        
        // Write DFA binary
        let dfa_path = format!("{}/dialog_{}_dfa.bin", out_dir, state_key);
        std::fs::write(&dfa_path, bytes)?;
        
        // Write pattern mappings
        let mappings_path = format!("{}/dialog_{}_mappings.json", out_dir, state_key);
        let mappings_json = serde_json::to_string(&mappings)?;
        std::fs::write(&mappings_path, mappings_json)?;
    }
    
    // Generate Rust code with static DFA loading
    generate_rust_code(&config, &out_dir)?;
    
    println!("cargo:rerun-if-changed=seams.toml");
    Ok(())
}

fn expand_template(
    template: &PatternTemplate, 
    state: &DialogStateConfig, 
    components: &HashMap<String, String>
) -> Result<String, Box<dyn std::error::Error>> {
    let mut pattern = template.regex.clone();
    
    // Replace state-specific placeholders
    pattern = pattern.replace("{close}", &state.close_char);
    pattern = pattern.replace("{open}", &state.open_char);
    
    // Replace component placeholders
    for (key, value) in components {
        pattern = pattern.replace(&format!("{{{}}}", key), value);
    }
    
    Ok(pattern)
}

fn generate_rust_code(config: &SeamConfig, out_dir: &str) -> Result<(), Box<dyn std::error::Error>> {
    let mut code = String::new();
    code.push_str("// Auto-generated by build.rs - DO NOT EDIT\n");
    code.push_str("use regex_automata::dfa::dense::DFA;\n");
    code.push_str("use std::collections::HashMap;\n\n");
    
    // Generate DFA constants
    for (state_key, state_config) in &config.dialog_states {
        code.push_str(&format!(
            "pub static {}_DFA: DFA<&[u32], u32> = {{\n",
            state_key.to_uppercase()
        ));
        code.push_str(&format!(
            "    const BYTES: &[u8] = include_bytes!(concat!(env!(\"OUT_DIR\"), \"/dialog_{}_dfa.bin\"));\n",
            state_key
        ));
        code.push_str("    unsafe { DFA::from_bytes_unchecked(BYTES) }\n");
        code.push_str("};\n\n");
    }
    
    // Generate mapping loader
    code.push_str("pub fn load_dialog_patterns() -> HashMap<DialogState, (DFA<&'static [u32], u32>, Vec<(MatchType, DialogState)>)> {\n");
    code.push_str("    let mut map = HashMap::new();\n");
    
    for (state_key, state_config) in &config.dialog_states {
        code.push_str(&format!(
            "    let {}_mappings: Vec<(MatchType, DialogState)> = serde_json::from_str(include_str!(concat!(env!(\"OUT_DIR\"), \"/dialog_{}_mappings.json\"))).unwrap();\n",
            state_key, state_key
        ));
        code.push_str(&format!(
            "    map.insert(DialogState::{}, ({}_DFA, {}_mappings));\n",
            state_config.state_name, state_key.to_uppercase(), state_key
        ));
    }
    
    code.push_str("    map\n");
    code.push_str("}\n");
    
    std::fs::write(format!("{}/generated_patterns.rs", out_dir), code)?;
    Ok(())
}
```

### Refactored DialogStateMachine

```rust
// src/sentence_detector/dialog_detector.rs (refactored)
include!(concat!(env!("OUT_DIR"), "/generated_patterns.rs"));

impl DialogStateMachine {
    pub fn new() -> Result<Self> {
        let pattern_data = load_dialog_patterns();
        let mut state_patterns = HashMap::new();
        let mut state_pattern_mappings = HashMap::new();
        
        for (state, (dfa, mappings)) in pattern_data {
            state_patterns.insert(state.clone(), dfa);
            state_pattern_mappings.insert(state, mappings);
        }
        
        Ok(DialogStateMachine {
            state_patterns,
            state_pattern_mappings,
            abbreviation_checker: AbbreviationChecker::new(),
        })
    }
}
```

---

## Benefits Analysis

### Code Reduction
- **Before**: 775 lines of repetitive pattern generation
- **After**: ~50 lines of table data + generated code
- **Maintenance Burden**: 95% reduction

### Pattern Consistency  
- **Single Source of Truth**: All patterns defined once in table
- **Impossible Duplication Bugs**: Compiler ensures consistency across states
- **Easy Pattern Evolution**: Modify template once, affects all states

### Performance Optimization
- **Compile-Time DFA Generation**: Zero runtime regex compilation cost
- **Optimized DFAs**: `regex-automata` produces highly optimized state machines
- **Binary Size**: Pre-built DFAs vs runtime regex compiler

### Development Workflow
- **Pattern Authors**: Edit declarative TOML (readable, reviewable)
- **Compiler**: Generates optimized Rust code automatically  
- **Runtime**: Zero-cost abstractions with compile-time guarantees

---

## Implementation Strategy

### Phase 1: Validation Using Individual Sentence Output Files
**CRITICAL**: Use existing `benchmarks/run_comparison.py` infrastructure to generate `*_seams2.txt` files for behavioral equivalence testing.

**Validation Strategy**:
1. Current implementation creates: `/home/steve/gutenberg/4/2/7/0/42701/42701-0_seams.txt` (existing)
2. Refactored implementation creates: `/home/steve/gutenberg/4/2/7/0/42701/42701-0_seams2.txt` (new suffix)
3. Diff comparison across 20K file pairs for comprehensive validation
4. Success = identical sentence detection outputs across real-world corpus

```bash
# Validation workflow
# 1. Baseline already exists from previous runs (42701-0_seams.txt files)

# 2. Run refactored implementation with modified output suffix
# Modify seams binary to output *_seams2.txt instead of *_seams.txt
cargo build --release --features="table-driven-patterns"
python benchmarks/run_comparison.py /home/steve/gutenberg --seams-only

# 3. Compare sentence outputs file-by-file
# For each Project Gutenberg file, compare:
#   42701-0_seams.txt  (baseline)
#   42701-0_seams2.txt (refactored)
find /home/steve/gutenberg -name "*_seams.txt" | while read baseline; do
    refactored="${baseline%_seams.txt}_seams2.txt"
    if [ -f "$refactored" ]; then
        if ! diff -q "$baseline" "$refactored" >/dev/null; then
            echo "DIFFERENCE: $baseline vs $refactored"
        fi
    else
        echo "MISSING: $refactored"
    fi
done

# Success = no differences reported across all 20K file pairs
```

**Benefits of Individual File Validation**:
- **Exact sentence-level comparison**: Line-by-line diff of actual sentence detection output
- **Real-world data**: 20K Project Gutenberg files with complex dialog patterns  
- **Non-destructive**: `*_seams2.txt` suffix preserves existing baseline files
- **Comprehensive coverage**: Every sentence boundary decision validated across corpus

### Phase 2: Table-to-Code Implementation
1. TOML schema already designed and created (`seams.toml`)
2. Update build script to parse TOML instead of hardcoded patterns
3. Implement template expansion system

### Phase 3: Full Migration
1. Generate all dialog states from table
2. Run comprehensive differential testing
3. Replace manual code with generated code
4. Remove duplicated pattern definitions

### Phase 4: Enhancement Opportunities
1. **Pattern Verification**: Build-script can verify pattern completeness
2. **Documentation Generation**: Auto-generate pattern documentation from table
3. **Test Case Generation**: Generate test cases from pattern combinations

---

## Risk Mitigation

### Behavioral Equivalence
- **Comprehensive Test Suite**: Cover all dialog state × pattern combinations
- **Golden Dataset Validation**: Ensure identical results on large text corpus
- **Edge Case Testing**: Multi-byte characters, complex nesting, abbreviations

### Build Complexity
- **Fallback Strategy**: Keep current code during transition period
- **Incremental Migration**: Start with one dialog state, expand gradually
- **CI Integration**: Fail fast on table→DFA generation errors

### Debug Experience  
- **Pattern Traceability**: Generated code includes comments linking back to table
- **Debug Mode**: Optional feature to dump expanded patterns for inspection
- **Error Messages**: Clear mapping from table entries to compilation errors

---

## Decision Required

**Should we proceed with Route 2 (Table-Driven DFA Generation) for the dialog_detector.rs refactor?**

**Advantages**: 
- Massive code reduction (95% less pattern code)
- Compile-time optimization (pre-built DFAs)
- Perfect pattern consistency (impossible to have duplication bugs)
- Declarative authoring experience (edit table, not Rust code)

**Risks**:
- Build script complexity  
- Requires comprehensive differential testing
- Learning curve for pattern table schema

**Recommendation**: **PROCEED** - The duplication problem is severe enough to justify the refactor investment, and the table-driven approach aligns perfectly with the DFA-first architecture goal.

---

## IMPLEMENTATION OUTCOME: ABANDONED

**Date**: 2025-07-14  
**Result**: **FAILED** - Implementation abandoned due to compilation complexity

### What Happened

**Phase 1 Attempt**: Started with trivial replication of existing code to validate the refactoring approach.

**Failure Mode**: Despite attempting only the most basic step (replicating existing functionality), the refactor created:
- **Complex compilation warning cycles** that could not be resolved
- **Warning compilation failure dependencies** across multiple compilation units
- **Validation failures** even for unchanged logic

### Key Learning

The **775 lines of "duplicated" code** serve a critical purpose in the current architecture:
- Each dialog state's patterns are **compilation-isolated**
- Manual pattern generation provides **warning-free compilation guarantees**
- The perceived "duplication" actually **prevents cross-state compilation dependencies**

### Root Cause Analysis

The table-driven approach introduces **build-time complexity** that conflicts with the project's **zero-warning compilation requirement**. The current manual approach, while verbose, provides:
- **Predictable compilation units**
- **Isolated pattern definitions** 
- **Warning-free validation** that works reliably

### Decision

**ABANDON** this specific table-driven TOML approach. The current manual pattern approach, while verbose, is:
- **Functionally correct**
- **Compilation-reliable** 
- **Maintenance-acceptable** given the stability of dialog pattern requirements

The engineering cost of resolving the build-script compilation complexity outweighs the benefit of code reduction for this particular approach.

### Future Opportunities

This abandonment **does not preclude** other Rust-native refactoring approaches that could reduce duplication, such as:
- **Macro-based pattern generation** using declarative macros
- **Generic functions** with const generics for dialog state patterns  
- **Trait-based abstractions** for pattern generation
- **Function composition** approaches using native Rust language features

The failure was specific to the **build-script + TOML + compile-time DFA generation** approach, not the general goal of reducing code duplication through better Rust language feature utilization.