# davidberenstein1957/concise-concepts architecture

> Architecture diagram of the GitHub repository davidberenstein1957/concise-concepts, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-03-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/davidberenstein1957/concise-concepts
- Repository: https://github.com/davidberenstein1957/concise-concepts

## Overview

**Concise Concepts** is a **Python library** (published to PyPI) that provides **few-shot Named Entity Recognition (NER)** by:
- taking a small **concept dictionary** (labels → example terms),
- expanding terms via **embedding similarity** (Gensim / Sense2Vec / keyed vectors),
- generating **rule-based matching patterns**,
- injecting them into a **spaCy `EntityRuler`** (and optionally fuzzy matching),
- optionally computing **entity scores** (similarity of matched entity to concept group).

So your “system” is not a web service—it's a **library embedded inside a user’s Python process**, typically inside a spaCy pipeline.

From the tree:

- `concise_concepts/`
- `__init__.py`
- public API surface (exports `Conceptualizer`, registers spaCy component entrypoint)
- `conceptualizer/Conceptualizer.py`
- the core engine (pattern generation, similarity expansion, scoring, integration into spaCy)
- `examples/`
- usage patterns: spaCy pipeline mode, standalone mode, custom embedding models/paths
- `tests/test_model_import.py`
- import + integration sanity check (packaging correctness)
- `pyproject.toml`, `setup.cfg`
- packaging/build metadata
- `.github/workflows/*`
- CI for tests and publish

The README describes **two primary usage modes**:

##### spaCy Pipeline Component mode (most important)
User does:
- `nlp = spacy.load(...)`
- `nlp.add_pipe("concise_concepts", config={...})`
- `doc = nlp(text)` → `doc.ents` enriched

Key runtime fact: Concise Concepts is a **spaCy pipeline component** that configures and populates an **EntityRuler** with patterns derived from embeddings + seed terms.

##### Standalone mode
User does:
- `conceptualizer = Conceptualizer(nlp, data, model)`
- `conceptualizer.nlp(text).ents`

Key runtime fact: the library can be used without repeatedly reloading large models; user passes an embedding model object directly.

##### What to put

- “User Application / Notebook / Script”
- “spaCy”
- “Concise Concepts (this library)”
- “Embedding Model Provider”
- examples: `gensim.downloader`, local keyed vectors, `sense2vec`
- Optional: “Filesystem” (for `json_path` patterns export)

- User → spaCy: constructs pipeline, calls `nlp(text)`
- User → Concise Concepts: provides `data` config, options, optionally `model_path`/`model`
- Concise Concepts → Embedding Models: similarity queries / nearest neighbors
- Concise Concepts → Filesystem: write generated patterns JSON

**Notes to annotate**
- Runs **in-process** (no network boundary unless embedding download is used)
- Output is `doc.ents` + optional `ent._.ent_score`

##### User Code
- supplies: `data`, config, text
##### spaCy Pipeline
- base pipeline (tokenizer, tagger, parser, etc. depending on loaded model)
##### Concise Concepts spaCy Component
- installed via `nlp.add_pipe("concise_concepts", config=...)`
##### Conceptualizer Core
- pattern builder + scoring logic
##### EntityRuler
- pattern matching + entity injection into `Doc`
##### Embedding Model
- Gensim `KeyedVectors`/FastText/Word2Vec, Sense2Vec, etc.
##### Optional Fuzzy Matcher
- README mentions fuzzy matching via `spaczz` (treat as conditional subcomponent)
##### Patterns JSON Output
- `json_path` export for interpretability/debugging

- `data (label -> seed terms)` → Conceptualizer Core
- Conceptualizer Core → Embedding Model: “get similar terms / similarity scores”
- Conceptualizer Core → Pattern Generator: “build token/phrase patterns”
- Pattern Generator → EntityRuler: “load patterns”
- spaCy pipeline executes:
- Text → Tokenization → (other components) → Concise Concepts Component → EntityRuler match → `Doc.ents`
- If `ent_score=True`:
- Matched entity spans → Embedding Model similarity → attach `ent._.ent_score`

**Key config knobs to label on the edges**
- `exclude_pos`, `exclude_dep`, `include_compound_words`, `case_sensitive` affect pattern construction/matching
- `topn` affects expansion breadth per concept group
- `fuzzy=True` routes matching through fuzzy mechanism
- `model_path` or `model` determines embedding backend

`concise_concepts/conceptualizer/Conceptualizer.py` and how it connects outward.

- `Conceptualizer` (central class)
- “Similarity/Expansion Engine”
- “Scoring Engine” (conditional if `ent_score=True`)
- “spaCy Component Adapter” (registration + `nlp.add_pipe` integration)
- “EntityRuler Adapter” (create/populate patterns)
- “I/O: JSON Pattern Exporter”

- `Conceptualizer` uses spaCy `Language`/`Doc` APIs
- `Conceptualizer` uses embedding model API
- `Conceptualizer` produces patterns → EntityRuler
- `Conceptualizer` optionally writes JSON

**Why this level matters here:** this library’s “architecture” is mainly the transformation pipeline:
seed terms → expanded vocabulary → matcher patterns → entity spans (+ scores)

##### Important architectural patterns/design principles to call out
- **Adapter pattern**: the library adapts external embedding model interfaces into a consistent “similarity lookup” role.
- **Pipeline component architecture (spaCy)**: works as a composable pipeline stage, not as a model trainer.
- **Rule generation + interpretability**: explicit pattern export (`json_path`) is part of the design.
- **Configuration-driven behavior**: most behavior is driven by config passed to `nlp.add_pipe(...)`.

- **Python library** (packaged via `pyproject.toml` / `setup.cfg`)
- **spaCy** (`EntityRuler`, pipeline component)
- **Gensim / Sense2Vec** (embedding backends)
- **spaczz** (conditional fuzzy matching)

## Diagram (Mermaid)

```mermaid
flowchart TD

%% =========================
%% Level 1: Context (in-process library)
%% =========================
subgraph "Level 1 — Context (runs in user's Python process)"
    U["User Application / Notebook / Script"]:::user
    SP["spaCy (Language pipeline)"]:::external
    CC["Concise Concepts (Python library)"]:::internal
    EM["Embedding Model Provider (Gensim KeyedVectors / Sense2Vec / local vectors)"]:::external
    FS[("Filesystem (optional JSON export)")]:::external
    OUT["Output: doc.ents (+ optional ent._.ent_score)"]:::artifact
end

U -->|"construct_pipeline_and_call_nlp(text)"| SP
U -->|"provide_data_config_and_options"| CC
CC -->|"similarity_queries_topn_neighbors"| EM
CC -->|"export_patterns_json(json_path)"| FS
SP -->|"produces_enriched_entities"| OUT

N1["Note: No service boundary (in-process). Network only if embeddings are downloaded."]:::note
N2["Design: Adapter pattern for embeddings + configuration-driven rule generation + interpretability via JSON export."]:::note
CC -.->|"architecture_notes"| N1
CC -.->|"architecture_notes"| N2

%% =========================
%% Level 2: Container / Runtime inside the process
%% =========================
subgraph "Level 2 — Runtime Containers (inside Python process)"
    UCODE["User Code\n(data, config, text)"]:::user

    subgraph "spaCy Pipeline"
        TXT["Input Text"]:::artifact
        TOK["Tokenizer / base pipeline components"]:::external
        CCCOMP["Concise Concepts spaCy Component\n(nlp.add_pipe('concise_concepts', config=...))"]:::internal
        ER["spaCy EntityRuler"]:::external
        DOC["Doc (doc.ents)"]:::artifact
    end

    subgraph "Concise Concepts — Core Runtime"
        CORE["Conceptualizer Core"]:::internal
        EXP["Similarity / Expansion Engine\n(topn)"]:::internal
        PB["Pattern Builder\n(exclude_pos, exclude_dep,\ninclude_compound_words, case_sensitive)"]:::internal
        SCE["Scoring Engine\n(ent_score=True)"]:::optional
        FUZ["Optional Fuzzy Matcher (spaczz)\n(fuzzy=True)"]:::optional
        IO["I/O: JSON Pattern Exporter\n(json_path)"]:::internal
        PJSON["Patterns JSON (artifact)"]:::artifact
    end

    EM2["Embedding Model\n(Gensim / Sense2Vec / vectors)"]:::external
    FS2[("Filesystem")]:::external
end

UCODE -->|"data(label→seed_terms)_and_config"| CCCOMP
UCODE -->|"standalone_instantiation"| CORE

TXT -->|"nlp(text)"| TOK --> CCCOMP
CCCOMP -->|"build_and_load_patterns"| ER
ER -->|"match_spans_and_inject_entities"| DOC

CCCOMP -->|"delegates_to"| CORE
CORE -->|"expand_topN_similar_terms"| EXP -->|"similarity_lookup"| EM2
CORE -->|"generate_matcher_patterns"| PB -->|"add_patterns"| ER

DOC -.->|"if_ent_score=True_compute_similarity"| SCE
SCE -.->|"similarity_against_concept_group"| EM2
SCE -.->|"attach_extension_ent._.ent_score"| DOC

PB -.->|"if_fuzzy=True_route_matching"| FUZ
FUZ -.->|"fuzzy_match_candidates"| DOC

CORE -->|"optional_export_patterns"| IO -->|"write_json"| PJSON --> FS2

%% =========================
%% Level 3: Internal Components (package-level view)
%% =========================
subgraph "Level 3 — Library Components (package structure)"
    API["Public API + spaCy entrypoint\n(exports Conceptualizer)"]:::internal
    PKG["conceptualizer package init"]:::internal
    CLS["Conceptualizer class (core engine)"]:::internal
    EXS["Examples (usage patterns)"]:::support
    TST["Tests (import/integration sanity)"]:::support
    DOCS["README (architecture + usage)"]:::support
end

API -->|"imports_and_exports"| CLS
PKG -->|"exposes"| CLS
EXS -->|"demonstrates_pipeline_and_standalone"| API
TST -->|"verifies_import_and_entrypoint"| API
DOCS -->|"documents_config_and_modes"| API

%% =========================
%% Usage Modes (branching entrypoints)
%% =========================
subgraph "Primary Usage Modes"
    MODEA["Mode A: spaCy Pipeline Component"]:::mode
    MODEB["Mode B: Standalone Conceptualizer"]:::mode
end

MODEA -->|"nlp.add_pipe('concise_concepts')"| CCCOMP
MODEB -->|"Conceptualizer(nlp, data, model)"| CORE

%% =========================
%% Optional: Delivery / Release lifecycle
%% =========================
subgraph "Optional — Delivery / Release (PyPI library)"
    GH["GitHub Repository"]:::support
    CI1["GitHub Actions: Tests\n(python-package.yml)"]:::support
    CI2["GitHub Actions: Publish\n(python-publish.yml)"]:::support
    PKGMD["Packaging Metadata\n(pyproject.toml + setup.cfg)"]:::support
    PYPI["PyPI"]:::external
    PIP["User installs via pip"]:::user
end

GH -->|"CI_trigger"| CI1 -->|"build_and_test"| PKGMD
CI1 -->|"on_release"| CI2 -->|"publish"| PYPI -->|"pip_install"| PIP -->|"import_and_use"| API

%% =========================
%% Click Events (from component_mapping)
%% =========================
click API "https://github.com/davidberenstein1957/concise-concepts/blob/main/concise_concepts/__init__.py"
click CLS "https://github.com/davidberenstein1957/concise-concepts/blob/main/concise_concepts/conceptualizer/Conceptualizer.py"
click PKG "https://github.com/davidberenstein1957/concise-concepts/blob/main/concise_concepts/conceptualizer/__init__.py"
click EXS "https://github.com/davidberenstein1957/concise-concepts/tree/main/concise_concepts/examples/"
click MODEA "https://github.com/davidberenstein1957/concise-concepts/blob/main/concise_concepts/examples/example_spacy.py"
click MODEB "https://github.com/davidberenstein1957/concise-concepts/blob/main/concise_concepts/examples/example_gensim_default.py"
click EM2 "https://github.com/davidberenstein1957/concise-concepts/blob/main/concise_concepts/examples/example_gensim_custom_model.py"
click EXP "https://github.com/davidberenstein1957/concise-concepts/blob/main/concise_concepts/examples/example_gensim_custom_path.py"
click UCODE "https://github.com/davidberenstein1957/concise-concepts/blob/main/concise_concepts/examples/data.py"
click TST "https://github.com/davidberenstein1957/concise-concepts/blob/main/tests/test_model_import.py"
click CI1 "https://github.com/davidberenstein1957/concise-concepts/blob/main/.github/workflows/python-package.yml"
click CI2 "https://github.com/davidberenstein1957/concise-concepts/blob/main/.github/workflows/python-publish.yml"
click PKGMD "https://github.com/davidberenstein1957/concise-concepts/blob/main/pyproject.toml"
click GH "https://github.com/davidberenstein1957/concise-concepts/blob/main/setup.cfg"
click DOCS "https://github.com/davidberenstein1957/concise-concepts/blob/main/README.md"
click PIP "https://github.com/davidberenstein1957/concise-concepts/blob/main/tests/__init__.py"

%% =========================
%% Styles
%% =========================
classDef user fill:#E8F3FF,stroke:#2B6CB0,stroke-width:1.5px,color:#0B1F3A;
classDef external fill:#F3F4F6,stroke:#374151,stroke-width:1.2px,color:#111827;
classDef internal fill:#E9FCEB,stroke:#166534,stroke-width:1.5px,color:#052E16;
classDef support fill:#FFF7ED,stroke:#9A3412,stroke-width:1.2px,color:#431407;
classDef artifact fill:#FDF2F8,stroke:#9D174D,stroke-width:1.2px,color:#500724;
classDef optional fill:#ECFEFF,stroke:#155E75,stroke-width:1.2px,color:#083344,stroke-dasharray: 6 4;
classDef note fill:#FFFBEB,stroke:#92400E,stroke-width:1px,color:#451A03,stroke-dasharray: 3 3;
classDef mode fill:#EEF2FF,stroke:#3730A3,stroke-width:1.4px,color:#1E1B4B;
```
