# cliang1453/bond architecture

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

- Interactive diagram: https://gitdiagram.com/cliang1453/bond
- Repository: https://github.com/cliang1453/bond

## Overview

##### Project Type and Purpose
- BOND is a research-oriented NLP pipeline/tool for open-domain Named Entity Recognition (NER) leveraging distant/weak supervision and BERT-based models.
- It provides pre-processed datasets, training/evaluation scripts, and model definitions to reproduce the KDD2020 results.

##### High-Level Components
From the file tree and README, we group the code into these major modules:
a. Data Layer
- dataset/ (pre-processed JSON files for BC5CDR-chem, CoNLL03, OntoNotes, Twitter, Webpage, Wikigold)
- data_utils.py (data loading, tokenization, label mapping)
b. Model Layer
- modeling_bert.py & modeling_roberta.py (model definitions, BERT/Roberta wrappers, classification heads)
- model_utils.py (helper functions: loss computation, model saving/loading)
c. Training & Self-Training Pipeline
- run_ner.py (Stage I: baseline fine-tuning on distant data)
- run_self_training_ner.py (Stage II: iterative distillation/self-training loop – the core BOND algorithm)
- scripts/*.sh & semi_script/*.sh (shell scripts orchestrating dataset choice, hyperparameters, and two‐stage runs)
d. Evaluation
- eval.py (entity-level F1 calculation, logging results)
e. External Dependencies
- Python 3.7, PyTorch 1.3, Hugging Face Transformers v2.3.0

##### Relationships & Data Flow
- Raw JSON files → data_utils.py → token IDs & labels
- Tokenized data → run_ner.py (Stage I) → model_utils.py → saved Stage I checkpoints
- Stage I checkpoints & unlabeled/distant data → run_self_training_ner.py (Stage II self-training loop) → model_utils.py → final checkpoints
- Any checkpoint → eval.py → F1 scores → output logs
- scripts/*.sh drive run_ner.py and run_self_training_ner.py with dataset-specific parameters

##### Architectural Patterns & Principles
- Pipeline pattern: clear separation of data ingestion, model definition, training logic, and evaluation.
- Modularization: reusable utilities in data_utils.py and model_utils.py.
- Two-stage training workflow (baseline then self-training) reflects a staged pipeline.

##### Step-by-Step Construction
a. At the left, place your Data Layer:
[ dataset/*.json ] → [ data_utils.py ]
b. In the center, stack two processing stages:
Stage I: [ run_ner.py ] → uses → [ modeling_bert.py / modeling_roberta.py ] → [ model_utils.py ]
Stage II: [ run_self_training_ner.py ] → iterative loop using the same model definitions
e. At the bottom, note external dependencies: Python, PyTorch, Transformers

##### Don’t Overthink—Just Separate Everything
- To include both baseline and self-training flows to reflect the BOND framework.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Orchestration Layer"
        SCR[scripts/*.sh]:::orchestration
        SEM[semi_script/*.sh]:::orchestration
    end

    subgraph "Data Layer"
        DATA[dataset/*.json]:::dataLayer
        DU[data_utils.py]:::dataLayer
    end

    subgraph "Training Pipeline"
        subgraph "Stage I: Baseline Fine-tuning"
            RN[run_ner.py]:::training
            MB[modeling_bert.py]:::modelLayer
            MR[modeling_roberta.py]:::modelLayer
            MU[model_utils.py]:::modelLayer
        end
        subgraph "Stage II: Self-training Loop"
            RST[run_self_training_ner.py]:::training
        end
    end

    subgraph "Evaluation"
        EV[eval.py]:::evaluation
    end

    EXT[(Python 3.7, PyTorch 1.3, Transformers v2.3.0)]

    %% Data flow
    DATA -->|"load_data()"| DU
    DU -->|"token IDs & labels"| RN
    RN -->|"fine-tune()"| MB
    RN -->|"fine-tune()"| MR
    MB -->|"compute loss"| MU
    MR -->|"compute loss"| MU
    MU -->|"save Stage I checkpoint"| RST
    DU -->|"token IDs & labels"| RST
    RST -->|"iterative self-training"| MB
    RST -->|"iterative self-training"| MR
    RST -->|"loss & save"| MU
    MU -->|"checkpoint"| EV

    %% Control flow
    SCR -.->|"invoke"| RN
    SCR -.->|"invoke"| RST
    SEM -.->|"invoke"| RST

    %% External dependencies note
    EXT

    %% Click Events
    click DU "https://github.com/cliang1453/bond/blob/master/data_utils.py"
    click DATA "https://github.com/cliang1453/bond/tree/master/dataset/"
    click MB "https://github.com/cliang1453/bond/blob/master/modeling_bert.py"
    click MR "https://github.com/cliang1453/bond/blob/master/modeling_roberta.py"
    click MU "https://github.com/cliang1453/bond/blob/master/model_utils.py"
    click RN "https://github.com/cliang1453/bond/blob/master/run_ner.py"
    click RST "https://github.com/cliang1453/bond/blob/master/run_self_training_ner.py"
    click SCR "https://github.com/cliang1453/bond/tree/master/scripts/"
    click SEM "https://github.com/cliang1453/bond/tree/master/semi_script/"
    click EV "https://github.com/cliang1453/bond/blob/master/eval.py"

    %% Styles
    classDef dataLayer fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef modelLayer fill:#90EE90,stroke:#000,stroke-width:1px
    classDef training fill:#FFA500,stroke:#000,stroke-width:1px
    classDef evaluation fill:#DDA0DD,stroke:#000,stroke-width:1px
    classDef orchestration fill:#E0E0E0,stroke:#000,stroke-width:1px
```
