# lasigebiotm/bent architecture

> Architecture diagram of the GitHub repository lasigebiotm/bent, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lasigebiotm/bent
- Repository: https://github.com/lasigebiotm/bent

## Overview

##### Project type and purpose
- BENT is an open-source Python library and CLI tool that implements a biomedical entity extraction pipeline combining Named Entity Recognition (NER) and Named Entity Linking (NEL).
- Its primary use cases are: a) entity recognition, b) entity linking, or c) both in batch (files) or on strings.

##### Key components from the file tree & README
- CLI & orchestrator:
- bent/annotate.py – entry point for `bt.annotate(...)`
- bent/get_kbs.py, bent/get_data.sh, bent/get_kb_dicts.sh – scripts to download external KBs
- bent/setup_package.sh & wrapper – installer for non-Python dependencies and data
- Core modules (in bent/src):
- NER: bent/src/ner.py
- NEL: two interchangeable back-ends under bent/src/NILINKER and bent/src/REEL
- Abbreviation detection: bent/src/abbreviation_detector/run.py
- Utility modules: bent/src/utils.py, bent/src/cfg.py, bent/src/classes.py
- Dictionaries: bent/src/dicts – code to build/load knowledge-base dicts
- External dependencies: TensorFlow/PyTorch models (hidden), Java for REEL’s ppr_for_ned_all.java, plus large KB data (MEDIC, CHEBI, etc.)

1. CLI / API Layer (“bent.annotate”)
2. Configuration Loader (cfg.py)
3. Pre-processing & Abbrev Detector
4. NER Module
5. NEL Module (plugin interface)
– NILINKER sub-component
– REEL sub-component
6. Dictionary Manager (dicts/ generate_dicts.py & KB store)
7. Output Writer (annotation files)
8. External KB Data Repositories (ontologies, databases)
9. Setup / Data Download Scripts
- CLI → Config Loader → (Pre-processing → Abbrev Detector)
- Pre-processing → NER → NEL
- NEL → Dictionary Manager → External KB Data
- NEL → Output Writer → Annotation Files
- Setup Scripts → External KB Data → Dictionary Manager
c. Architectural patterns & principles
- Modular pipeline: clear stage separation (pre-process, NER, NEL, post-process)
- Plugin-style NEL: interchangeable NILINKER vs REEL back-ends
- Single responsibility: each script/module has a focused task
d. Key technologies
- Python packaging (setup.py, entry_points)
- Shell scripts for data management
- Java runtime for REEL’s graph-based linking
- Sphinx for documentation (docs/)

- Blue for core Python modules
- Green for data/KB components
- Orange for external scripts/runtimes
- Rectangles for code modules

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% CLI Layer
    CLIAPI["CLI / API Layer\n(bent/annotate.py)"]:::python
    click CLIAPI "https://github.com/lasigebiotm/bent/blob/main/bent/annotate.py"

    %% Configuration
    Config["Configuration Loader\n(cfg.py)"]:::python
    click Config "https://github.com/lasigebiotm/bent/blob/main/bent/src/cfg.py"

    %% Pre-processing
    Preproc["Pre-processing & Abbrev Detector\n(run.py)"]:::python
    click Preproc "https://github.com/lasigebiotm/bent/blob/main/bent/src/abbreviation_detector/run.py"

    %% Core NER
    NER["NER Module\n(ner.py)"]:::python
    click NER "https://github.com/lasigebiotm/bent/blob/main/bent/src/ner.py"

    %% NEL Plugin Interface
    subgraph "NEL Module (Plugin Interface)" 
        direction TB
        NELInterface["NEL Interface\n(nel.py)"]:::python
        click NELInterface "https://github.com/lasigebiotm/bent/blob/main/bent/src/nel.py"
        subgraph "NILINKER Backend"
            direction TB
            NILinkerComp["nilinker.py"]:::python
            click NILinkerComp "https://github.com/lasigebiotm/bent/blob/main/bent/src/NILINKER/nilinker.py"
            NILPrd["predict_nilinker.py"]:::python
            click NILPrd "https://github.com/lasigebiotm/bent/blob/main/bent/src/NILINKER/predict_nilinker.py"
            NILUtils["utils.py"]:::python
            click NILUtils "https://github.com/lasigebiotm/bent/blob/main/bent/src/NILINKER/utils.py"
        end
        subgraph "REEL Backend"
            direction TB
            REELRun["run.py"]:::python
            click REELRun "https://github.com/lasigebiotm/bent/blob/main/bent/src/REEL/run.py"
            REELPre["pre_process.py"]:::python
            click REELPre "https://github.com/lasigebiotm/bent/blob/main/bent/src/REEL/pre_process.py"
            REELCand["candidates.py"]:::python
            click REELCand "https://github.com/lasigebiotm/bent/blob/main/bent/src/REEL/candidates.py"
            REELInfo["information_content.py"]:::python
            click REELInfo "https://github.com/lasigebiotm/bent/blob/main/bent/src/REEL/information_content.py"
            REELPost["post_process.py"]:::python
            click REELPost "https://github.com/lasigebiotm/bent/blob/main/bent/src/REEL/post_process.py"
            REELJava["ppr_for_ned_all.java"]:::script
            click REELJava "https://github.com/lasigebiotm/bent/blob/main/bent/src/REEL/ppr_for_ned_all.java"
            REELUtils["utils.py"]:::python
            click REELUtils "https://github.com/lasigebiotm/bent/blob/main/bent/src/REEL/utils.py"
        end
    end

    %% Dictionary Manager
    subgraph "Dictionary Manager\n(bent/src/dicts/)" 
        direction TB
        DictBuilder["generate_dicts.py"]:::python
        click DictBuilder "https://github.com/lasigebiotm/bent/blob/main/bent/src/dicts/kb/generate_dicts.py"
        KBInterface["kb.py"]:::python
        click KBInterface "https://github.com/lasigebiotm/bent/blob/main/bent/src/dicts/kb/kb.py"
        DatasetEntities["dataset_entities.py"]:::python
        click DatasetEntities "https://github.com/lasigebiotm/bent/blob/main/bent/src/dicts/annotations/dataset_entities.py"
        RelDicts["dicts.py"]:::python
        click RelDicts "https://github.com/lasigebiotm/bent/blob/main/bent/src/dicts/relation_extraction/dicts.py"
        KBStore["KB Store"]:::data
    end

    %% Output Writer
    OutputWriter["Output Writer\n(annotate.py)"]:::python
    click OutputWriter "https://github.com/lasigebiotm/bent/blob/main/bent/annotate.py"
    AnnoFiles["Annotation Files"]:::data

    %% External KB Data Repos
    ExtKB["External KB Data Repositories"]:::data

    %% Setup & Download Scripts
    subgraph "Setup & Download Scripts" 
        direction TB
        GetKBs["get_kbs.py"]:::script
        click GetKBs "https://github.com/lasigebiotm/bent/blob/main/bent/get_kbs.py"
        GetKBDicts["get_kb_dicts.sh"]:::script
        click GetKBDicts "https://github.com/lasigebiotm/bent/blob/main/bent/get_kb_dicts.sh"
        GetData["get_data.sh"]:::script
        click GetData "https://github.com/lasigebiotm/bent/blob/main/bent/get_data.sh"
        SetupPkg["setup_package.sh"]:::script
        click SetupPkg "https://github.com/lasigebiotm/bent/blob/main/bent/setup_package.sh"
        SetupWrapper["setup_package_wrapper.py"]:::script
        click SetupWrapper "https://github.com/lasigebiotm/bent/blob/main/bent/setup_package_wrapper.py"
    end

    %% Workflow connections
    CLIAPI -->|"load config"| Config
    Config -->|"preprocess"| Preproc
    Preproc -->|"NER input"| NER
    NER -->|"to NEL"| NELInterface
    NELInterface -->|"query dicts"| DictBuilder
    DictBuilder --> KBStore
    KBStore --> KBInterface
    KBInterface --> NELInterface
    NELInterface --> OutputWriter
    OutputWriter --> AnnoFiles

    %% Data download flow
    SetupPkg --> ExtKB
    SetupWrapper --> ExtKB
    GetKBs --> ExtKB
    GetKBDicts --> ExtKB
    GetData --> ExtKB
    ExtKB --> DictBuilder

    %% Styles
    classDef python fill:#D6EFFF,stroke:#036,stroke-width:1px
    classDef data fill:#E0FFD6,stroke:#060,stroke-width:1px
    classDef script fill:#FFE8B3,stroke:#A60,stroke-width:1px
```
