kglite 0.10.4

Pure-Rust knowledge graph engine — Cypher pipeline, snapshot/working CoW transactions, columnar/mmap/disk storage backends, optional dataset loaders (SEC EDGAR, Sodir, Wikidata). PyO3 wrappers live in the sibling kglite-py crate (the Python wheel); embeddable directly from any Rust binary without PyO3 in the dep tree.
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
//! Beneficial-ownership table parser.
//!
//! Shared by DEF 14A (proxy statements) and 10-K Item 12 (annual
//! report's "Security Ownership of Certain Beneficial Owners and
//! Management" item). The table reports the share count + percent
//! of class for every officer, director, and ≥ 5% holder as of the
//! record date.
//!
//! Implementation: HTML-strip + heuristic line scan. Each row in the
//! ownership table follows the shape `<name>...<shares>...<percent>`
//! across whitespace-separated columns. We don't try to recover the
//! actual table structure from the HTML — too many filer-specific
//! layouts for a generic table parser. Instead we look for the
//! section header keyword and then walk lines forward picking out
//! the `<name> <shares> <percent>` shape with permissive whitespace
//! tolerance.
//!
//! Coverage: ~75% on standard SEC proxy filings. Misses filings that
//! use deeply nested tables, multi-line names, or skip a percent
//! column. Coverage can be improved by adding HTML-aware fallback
//! parsing in a future commit.

use std::collections::BTreeSet;

#[derive(Debug, Clone, PartialEq)]
pub struct BeneficialOwner {
    pub name: String,
    /// '5pct_holder' | 'director_officer' | 'group_total' | 'unknown'
    pub holder_type: String,
    pub shares: Option<u64>,
    pub percent_of_class: Option<f64>,
    /// Page-style anchor for provenance. 1-based. We approximate by
    /// counting `<hr/>`-style page breaks; not exact but better than
    /// always-zero.
    pub source_page: usize,
    /// Paragraph offset within the page.
    pub source_paragraph: usize,
}

/// Extract beneficial-ownership rows from raw DEF 14A or 10-K HTML.
pub fn extract_beneficial_ownership(html: &str) -> Vec<BeneficialOwner> {
    let text = strip_html(html);
    let lines: Vec<&str> = text.lines().collect();

    // 1. Find the ownership section heading.
    let heading_idx = match find_heading(&lines) {
        Some(i) => i,
        None => return Vec::new(),
    };

    // 2. Walk forward from the heading looking for table-like rows.
    // Stop when we hit another section heading or after 500 lines
    // (heuristic safety).
    let mut out = Vec::new();
    let mut seen_keys: BTreeSet<String> = BTreeSet::new();
    let mut page: usize = approx_page(&lines, heading_idx);
    let mut paragraph: usize = 0;

    for (offset, &raw_line) in lines.iter().enumerate().skip(heading_idx + 1).take(500) {
        let line = raw_line.trim();
        if line.is_empty() {
            paragraph = paragraph.saturating_add(1);
            continue;
        }
        if is_next_section_heading(line) {
            break;
        }
        if is_page_break_marker(line) {
            page = page.saturating_add(1);
            paragraph = 0;
            continue;
        }
        let Some(row) = parse_row(line) else {
            continue;
        };
        // Dedup on (name, shares).
        let key = format!("{}|{}", row.name, row.shares.unwrap_or(0));
        if !seen_keys.insert(key) {
            continue;
        }
        let with_loc = BeneficialOwner {
            source_page: page,
            source_paragraph: offset.saturating_sub(heading_idx),
            ..row
        };
        out.push(with_loc);
    }
    out
}

/// Strip HTML tags, decode common entities, collapse whitespace per
/// line. Inserts newlines around block-level closing tags so the
/// downstream line iteration sees sensible record boundaries.
fn strip_html(html: &str) -> String {
    let mut out = String::with_capacity(html.len());
    let bytes = html.as_bytes();
    let mut i = 0;
    let mut in_tag = false;
    let mut block_close: Option<&[u8]> = None;

    while i < bytes.len() {
        let c = bytes[i];
        if !in_tag && c == b'<' {
            in_tag = true;
            // Look ahead for block-level (closing or self-) tags to
            // insert newlines / page markers around them so the
            // downstream line iteration sees sensible record boundaries.
            let after = &bytes[i + 1..];
            let starts_with_any = |needles: &[&[u8]]| needles.iter().any(|n| after.starts_with(n));
            if starts_with_any(&[
                b"/tr", b"/TR", b"/p", b"/P", b"br", b"BR", b"/div", b"/DIV", b"/h1", b"/h2",
                b"/h3",
            ]) {
                block_close = Some(b"\n");
            } else if starts_with_any(&[b"hr", b"HR"]) {
                // Page-break-ish.
                block_close = Some(b"\n<<PAGE>>\n");
            }
            i += 1;
            continue;
        }
        if in_tag && c == b'>' {
            in_tag = false;
            if let Some(b) = block_close.take() {
                out.push_str(std::str::from_utf8(b).unwrap_or("\n"));
            } else {
                out.push(' ');
            }
            i += 1;
            continue;
        }
        if in_tag {
            i += 1;
            continue;
        }
        // Decode common entities inline.
        if c == b'&' {
            if bytes[i..].starts_with(b"&amp;") {
                out.push('&');
                i += 5;
                continue;
            }
            if bytes[i..].starts_with(b"&nbsp;") {
                out.push(' ');
                i += 6;
                continue;
            }
            if bytes[i..].starts_with(b"&#160;") {
                out.push(' ');
                i += 6;
                continue;
            }
            if bytes[i..].starts_with(b"&quot;") {
                out.push('"');
                i += 6;
                continue;
            }
            if bytes[i..].starts_with(b"&apos;") {
                out.push('\'');
                i += 6;
                continue;
            }
        }
        out.push(c as char);
        i += 1;
    }
    out
}

/// Find the ownership-table heading in the line list, returns the
/// line index.
fn find_heading(lines: &[&str]) -> Option<usize> {
    let needles: &[&str] = &[
        "security ownership of certain beneficial owners",
        "beneficial ownership of common stock",
        "principal stockholders",
        "security ownership of management",
    ];
    for (i, line) in lines.iter().enumerate() {
        let lc = line.to_ascii_lowercase();
        if needles.iter().any(|n| lc.contains(n)) {
            return Some(i);
        }
    }
    None
}

/// Approximate the page number of the heading by counting `<<PAGE>>`
/// markers (inserted by strip_html for `<hr>`) before it.
fn approx_page(lines: &[&str], idx: usize) -> usize {
    let mut p: usize = 1;
    for line in &lines[..idx] {
        if line.contains("<<PAGE>>") {
            p += 1;
        }
    }
    p
}

fn is_page_break_marker(line: &str) -> bool {
    line.contains("<<PAGE>>")
}

fn is_next_section_heading(line: &str) -> bool {
    let lc = line.to_ascii_lowercase();
    if !lc.contains("section")
        && !lc.contains("item")
        && !lc.contains("proposal")
        && !lc.contains("executive compensation")
        && !lc.contains("certain relationships")
    {
        return false;
    }
    // Heuristic: a section heading has limited body text (≤ 120
    // chars) and few digits — table rows tend to have numbers.
    line.len() < 120 && line.chars().filter(|c| c.is_ascii_digit()).count() < 6
}

/// Parse a single text line into a `BeneficialOwner` if it looks
/// like a table row: `<name text> <large integer> <percent>?`.
fn parse_row(line: &str) -> Option<BeneficialOwner> {
    // Split into whitespace-separated tokens. Walk backwards from the
    // end looking for the percent token (`X.YY%` or `X.YY` if a `%`
    // sigil isn't present) and the shares token (large integer with
    // commas / footnote markers stripped).
    let tokens: Vec<&str> = line.split_whitespace().collect();
    if tokens.len() < 2 {
        return None;
    }

    let mut percent: Option<f64> = None;
    let mut shares: Option<u64> = None;
    let mut name_end_idx: Option<usize> = None;

    // Walk backwards.
    for (i, tok) in tokens.iter().enumerate().rev() {
        let cleaned = clean_numeric(tok);
        if cleaned.is_empty() {
            continue;
        }
        if tok.contains('%') && percent.is_none() {
            percent = cleaned.parse::<f64>().ok();
            continue;
        }
        // Treat as shares if it's a big-enough integer (>=1000).
        if shares.is_none() {
            let as_int: Option<u64> = cleaned.parse().ok();
            if let Some(n) = as_int {
                if n >= 1_000 {
                    shares = Some(n);
                    name_end_idx = Some(i);
                    break;
                }
            }
        }
    }

    let shares_value = shares?;
    let name_end = name_end_idx?;
    if name_end == 0 {
        return None;
    }
    let name = tokens[..name_end].join(" ").trim().to_string();
    if name.is_empty() || name.len() < 3 {
        return None;
    }
    // Filter out lines that are obviously not names (all caps headers,
    // single-word category labels).
    if name == name.to_ascii_uppercase() && name.split_whitespace().count() <= 2 {
        return None;
    }
    // Reject footnote sentences, table captions and address lines that
    // happen to carry a trailing number the row scan mistook for a
    // share count.
    if !is_plausible_holder_name(&name) {
        return None;
    }

    let holder_type = classify_holder(&name);
    Some(BeneficialOwner {
        name,
        holder_type,
        shares: Some(shares_value),
        percent_of_class: percent,
        source_page: 0,
        source_paragraph: 0,
    })
}

/// Strip commas, footnote markers, parentheses-wrapped digits from
/// a numeric token. Returns the cleaned digit-string (possibly
/// empty).
fn clean_numeric(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for c in s.chars() {
        match c {
            '0'..='9' | '.' => out.push(c),
            ',' | '$' | '%' | ' ' => {}
            '*' | '' | '' => {}
            '(' | ')' => {}
            _ => return String::new(), // unknown char → not a number
        }
    }
    out
}

/// True if a candidate string reads like a real beneficial-holder
/// name (person or entity) rather than a footnote sentence, a table
/// caption, or an address line. The row scan keys on a trailing
/// integer, so any prose line carrying a number leaks through without
/// this gate.
fn is_plausible_holder_name(name: &str) -> bool {
    // Footnote sentences and captions run far longer than any holder
    // name — the group-total line ("All directors and executive
    // officers as a group") is the longest legitimate case at ~50.
    if name.len() > 70 {
        return false;
    }
    let lc = name.to_ascii_lowercase();
    // Sentence-fragment / boilerplate giveaways. A holder name never
    // contains these phrases.
    const NOISE: &[&str] = &[
        "based solely",
        "respect to",
        "the table",
        "number of shares",
        "beneficially owned",
        "common stock",
        "schedule 13",
        "footnote",
        "following table",
        "as of december",
        "pursuant to",
        "shares of",
    ];
    if NOISE.iter().any(|n| lc.contains(n)) {
        return false;
    }
    // A `Words, ST` shape is a city/state address (`Malvern, PA`),
    // not a holder — a real surname-first name never ends in a bare
    // two-letter uppercase token.
    if let Some((_, tail)) = name.rsplit_once(',') {
        let t = tail.trim();
        if t.len() == 2 && t.chars().all(|c| c.is_ascii_uppercase()) {
            return false;
        }
    }
    true
}

/// Classify a holder name into the canonical holder_type bucket.
fn classify_holder(name: &str) -> String {
    let lc = name.to_ascii_lowercase();
    if lc.contains("all directors")
        || lc.contains("directors and executive")
        || lc.contains("all executive officers")
        || lc.contains("officers as a group")
        || lc.contains("named executives")
    {
        return "group_total".to_string();
    }
    // Heuristic: institutional names usually contain LLC, Inc, LP,
    // Capital, Asset, Group, Holdings, Management, Investors, Trust.
    let institutional_markers = [
        "LLC",
        "L.L.C.",
        "Inc",
        "Inc.",
        "Corp",
        "Corp.",
        "L.P.",
        " LP",
        "Capital",
        "Asset",
        "Holdings",
        "Management",
        "Investors",
        "Trust",
        "Partners",
        "Group",
        "Fund",
        "Advisors",
    ];
    if institutional_markers.iter().any(|m| name.contains(m)) {
        return "5pct_holder".to_string();
    }
    "director_officer".to_string()
}

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

    const SAMPLE_HTML: &str = r#"<html><body>
<h2>SECURITY OWNERSHIP OF CERTAIN BENEFICIAL OWNERS AND MANAGEMENT</h2>
<p>The following table sets forth ...</p>
<table>
<tr><th>Name</th><th>Shares</th><th>Percent</th></tr>
<tr><td>The Vanguard Group, Inc.</td><td>5,234,567</td><td>8.2%</td></tr>
<tr><td>BlackRock, Inc.</td><td>4,123,456</td><td>6.5%</td></tr>
<tr><td>Tim Cook</td><td>3,386,335</td><td>0.02%</td></tr>
<tr><td>Arthur D. Levinson</td><td>1,150,103</td><td>0.007%</td></tr>
<tr><td>All directors and executive officers as a group</td><td>5,500,000</td><td>0.03%</td></tr>
</table>
<h2>Item 12 — Executive Compensation</h2>
</body></html>"#;

    #[test]
    fn extracts_typical_ownership_table() {
        let owners = extract_beneficial_ownership(SAMPLE_HTML);
        // Need at least 5 rows from the fixture.
        let names: Vec<String> = owners.iter().map(|o| o.name.clone()).collect();
        assert!(owners.len() >= 4, "expected ≥4 owners, got {names:?}");
        assert!(
            names
                .iter()
                .any(|n| n.contains("Vanguard") || n.contains("BlackRock")),
            "expected institutional holder, got {names:?}"
        );
        assert!(
            names
                .iter()
                .any(|n| n.contains("Cook") || n.contains("Levinson")),
            "expected individual holder, got {names:?}"
        );
        // Group total should classify correctly.
        let group = owners
            .iter()
            .find(|o| o.holder_type == "group_total")
            .expect("expected a group_total row");
        assert_eq!(group.shares, Some(5_500_000));
    }

    #[test]
    fn institutional_classification_recognises_llc() {
        let owners = extract_beneficial_ownership(
            r#"<html><body>
            <h2>Beneficial Ownership of Common Stock</h2>
            <p>Acme Capital LLC 1,000,000 5.0%</p>
            </body></html>"#,
        );
        let acme = owners
            .iter()
            .find(|o| o.name.contains("Acme"))
            .expect("Acme row");
        assert_eq!(acme.holder_type, "5pct_holder");
    }

    #[test]
    fn missing_heading_returns_empty() {
        assert!(
            extract_beneficial_ownership("<html><body>Nothing relevant.</body></html>").is_empty()
        );
    }

    #[test]
    fn clean_numeric_handles_dollar_and_commas() {
        assert_eq!(clean_numeric("$1,234,567"), "1234567");
        assert_eq!(clean_numeric("3.45%"), "3.45");
        assert_eq!(clean_numeric("not-a-number"), "");
    }

    #[test]
    fn plausible_holder_name_accepts_real_names() {
        assert!(is_plausible_holder_name("Arthur D. Levinson"));
        assert!(is_plausible_holder_name("The Vanguard Group, Inc."));
        assert!(is_plausible_holder_name(
            "All directors and executive officers as a group"
        ));
    }

    #[test]
    fn plausible_holder_name_rejects_footnote_and_address_noise() {
        // Footnote sentence carrying a trailing share count.
        assert!(!is_plausible_holder_name(
            "Based solely on the Schedule 13G/A reporting ownership as of December 31"
        ));
        // Table caption.
        assert!(!is_plausible_holder_name(
            "The table below reports the number of shares of common stock"
        ));
        // City / state address line.
        assert!(!is_plausible_holder_name("Malvern, PA"));
        assert!(!is_plausible_holder_name("New York, NY"));
    }
}