|
| 1 | +# structflo.ner.fast — Dictionary-Based NER for TB Drug Discovery |
| 2 | + |
| 3 | +Fast, deterministic entity extraction using curated YAML gazetteers. No LLM, no API key, no network — runs in milliseconds. |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +```bash |
| 8 | +uv add "structflo-ner[fast]" |
| 9 | + |
| 10 | +# with DataFrame support |
| 11 | +uv add "structflo-ner[fast,dataframe]" |
| 12 | +``` |
| 13 | + |
| 14 | +## Quick Start |
| 15 | + |
| 16 | +```python |
| 17 | +from structflo.ner.fast import FastNERExtractor |
| 18 | + |
| 19 | +fast = FastNERExtractor() |
| 20 | +result = fast.extract("Bedaquiline inhibits AtpE (Rv1305) in MDR-TB.") |
| 21 | + |
| 22 | +print(result.compounds) # [ChemicalEntity(text='Bedaquiline', ...)] |
| 23 | +print(result.targets) # [TargetEntity(text='AtpE', ...)] |
| 24 | +print(result.accessions) # [AccessionEntity(text='Rv1305', ...)] |
| 25 | +print(result.diseases) # [DiseaseEntity(text='MDR-TB', ...)] |
| 26 | + |
| 27 | +df = result.to_dataframe() |
| 28 | +result.display() # interactive HTML in Jupyter |
| 29 | +``` |
| 30 | + |
| 31 | +## How It Works |
| 32 | + |
| 33 | +Three-phase matching, all without an LLM: |
| 34 | + |
| 35 | +### Phase 1 — Exact Dictionary Match |
| 36 | +Looks up every text span against a normalized dictionary built from the YAML gazetteers. Auto-derived variants include: |
| 37 | +- **Case variants**: InhA, inha, INHA |
| 38 | +- **Hyphen-optional**: DprE-1 ↔ DprE1, MDR-TB ↔ MDRTB |
| 39 | +- **Period-optional**: M. tuberculosis ↔ M tuberculosis |
| 40 | +- **Greek letters**: β-lactam ↔ beta-lactam |
| 41 | + |
| 42 | +Word boundaries are enforced — "Rho" won't match inside "Rhodamine". |
| 43 | + |
| 44 | +### Phase 1b — Regex Patterns (Accession Numbers) |
| 45 | +Seed entries in `accession_number.yml` auto-derive regex patterns for entire ID families: |
| 46 | + |
| 47 | +| Seed | Auto-derived Pattern | Matches | |
| 48 | +|---|---|---| |
| 49 | +| `Rv0005` | `Rv\d{4}[c]?` | All Rv locus tags | |
| 50 | +| `MT0005` | `MT\w+` | Mycobrowser IDs | |
| 51 | +| `P9WGR1` | `[OPQ][0-9][A-Z0-9]{3}[0-9]` | UniProt accessions | |
| 52 | +| `4TZK` | `[0-9][A-Z0-9]{3}` | PDB codes | |
| 53 | +| `WP_003407354` | `WP_\d+` | NCBI RefSeq proteins | |
| 54 | + |
| 55 | +### Phase 2 — Fuzzy Match |
| 56 | +Unmatched "entity-like" tokens (capitalized, contain digits, length ≥ 4) are compared against the dictionary using rapidfuzz. Catches typos and minor variants. |
| 57 | + |
| 58 | +```python |
| 59 | +# Configurable threshold (0–100, default 85) |
| 60 | +strict = FastNERExtractor(fuzzy_threshold=0) # disable fuzzy |
| 61 | +lenient = FastNERExtractor(fuzzy_threshold=75) # more permissive |
| 62 | +``` |
| 63 | + |
| 64 | +## Gazetteers |
| 65 | + |
| 66 | +YAML files live in `structflo/ner/fast/gazetteers/`. Each file is a simple list of names — **nothing else**: |
| 67 | + |
| 68 | +```yaml |
| 69 | +# target.yml |
| 70 | +- InhA |
| 71 | +- DprE1 |
| 72 | +- MmpL3 |
| 73 | +- AtpE |
| 74 | +``` |
| 75 | +
|
| 76 | +The filename (without `.yml`) becomes the `entity_type`. Built-in gazetteers: |
| 77 | + |
| 78 | +| File | Entity Type | Coverage | |
| 79 | +|---|---|---| |
| 80 | +| `target.yml` | target → `TargetEntity` | ~80 TB drug targets | |
| 81 | +| `gene_name.yml` | gene_name → `TargetEntity` | ~75 Mtb gene names | |
| 82 | +| `compound_name.yml` | compound_name → `ChemicalEntity` | ~50 TB compounds & abbreviations | |
| 83 | +| `disease.yml` | disease → `DiseaseEntity` | TB disease variants | |
| 84 | +| `accession_number.yml` | accession_number → `AccessionEntity` | Seed entries → regex patterns | |
| 85 | +| `screening_method.yml` | screening_method → `ScreeningMethodEntity` | ~35 screening approaches | |
| 86 | +| `functional_category.yml` | functional_category → `FunctionalCategoryEntity` | ~25 Mtb functional categories | |
| 87 | +| `product.yml` | product → `ProductEntity` | ~35 gene product descriptions | |
| 88 | + |
| 89 | +## Adding New Gazetteers |
| 90 | + |
| 91 | +### Option 1: Add to existing files |
| 92 | +Edit a YAML file and add names: |
| 93 | + |
| 94 | +```yaml |
| 95 | +# target.yml |
| 96 | +- InhA |
| 97 | +- DprE1 |
| 98 | +- MyNewTarget # just add it |
| 99 | +``` |
| 100 | + |
| 101 | +### Option 2: Create a new YAML file |
| 102 | +Drop a new `.yml` file into any directory: |
| 103 | + |
| 104 | +```yaml |
| 105 | +# my_gazetteers/assay.yml |
| 106 | +- resazurin assay |
| 107 | +- luciferase reporter assay |
| 108 | +- disk diffusion assay |
| 109 | +``` |
| 110 | + |
| 111 | +```python |
| 112 | +fast = FastNERExtractor(gazetteer_dir="my_gazetteers/") |
| 113 | +``` |
| 114 | + |
| 115 | +### Option 3: Add terms programmatically |
| 116 | + |
| 117 | +```python |
| 118 | +fast = FastNERExtractor( |
| 119 | + extra_gazetteers={ |
| 120 | + "target": ["NovelTarget1", "NovelTarget2"], |
| 121 | + "compound_name": ["CompoundXYZ"], |
| 122 | + } |
| 123 | +) |
| 124 | +``` |
| 125 | + |
| 126 | +## Output Compatibility |
| 127 | + |
| 128 | +`FastNERExtractor` produces identical `NERResult` objects as the LLM-based `NERExtractor`. Everything downstream works the same: |
| 129 | + |
| 130 | +```python |
| 131 | +result.all_entities() # flat list |
| 132 | +result.to_dict() # serializable dict |
| 133 | +result.to_dataframe() # pandas DataFrame |
| 134 | +result.display() # interactive HTML |
| 135 | +``` |
| 136 | + |
| 137 | +Each entity includes `match_method` ("exact", "regex", or "fuzzy") and `canonical` (the gazetteer term it matched) in its `attributes` dict. |
| 138 | + |
| 139 | +## Fast vs LLM |
| 140 | + |
| 141 | +| | `FastNERExtractor` | `NERExtractor` | |
| 142 | +|---|---|---| |
| 143 | +| Speed | ~1–5 ms per abstract | ~2–5 s per abstract | |
| 144 | +| Novel entities | Only known terms | Discovers new entities | |
| 145 | +| Context | String matching | Full contextual understanding | |
| 146 | +| Cost | Free | API calls or GPU | |
| 147 | +| Setup | Zero config | API key or Ollama | |
| 148 | + |
| 149 | +**Recommended workflow**: Fast extractor as first pass (bulk screening), LLM extractor as second pass (deep analysis on interesting papers). |
0 commit comments