# makcedward/nlpaug architecture

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

- Interactive diagram: https://gitdiagram.com/makcedward/nlpaug
- Repository: https://github.com/makcedward/nlpaug

## Overview

#### Purpose and Entry Points
`nlpaug` is a Python 3.12+ library for synthetic-data augmentation across text, audio, and spectrogram inputs. Consumers import domain-specific augmenters from `nlpaug`, instantiate an augmenter, and call its augmentation API; there is no server or CLI runtime. Package exports begin at `nlpaug/__init__.py`; shared augmentation behavior is rooted in `nlpaug/base_augmenter.py`.

#### Core Augmenter Contract
The base layer defines the common lifecycle: validate input, select augmentation positions/candidates, execute an action (insert, delete, substitute, swap, split), and return one or more augmented outputs with change-history support. Domain-specific base classes specialize that contract and input handling:
- `nlpaug/augmenter/augment.py`
- `nlpaug/augmenter/word/word_augmenter.py`
- `nlpaug/augmenter/audio/audio_augmenter.py`

This is the main boundary between user-facing augmentation policy and model/data-source implementations.

#### Text Augmentation Families
Text processing is split by granularity, with concrete augmenters implementing transformations while inheriting shared selection/tokenization behavior:
- Character augmentation: `nlpaug/augmenter/char/char_augmenter.py`, `nlpaug/augmenter/char/keyboard.py`, `nlpaug/augmenter/char/ocr.py`
- Word augmentation: `nlpaug/augmenter/word/word_augmenter.py`, `nlpaug/augmenter/word/synonym.py`, `nlpaug/augmenter/word/context_word_embs.py`
- Sentence augmentation: `nlpaug/augmenter/sentence/sentence_augmenter.py`, `nlpaug/augmenter/sentence/abst_summ.py`, `nlpaug/augmenter/sentence/lambada.py`

Static keyboard/OCR/spelling resources are packaged under `nlpaug/res/`, separating deterministic local dictionaries from optional learned or downloaded assets.

#### Audio and Spectrogram Transforms
Signal augmenters operate on audio arrays or spectrogram tensors rather than text. Audio operations include crop, noise, time/frequency-related transformations, normalization, and inversion; spectrogram operations include masking and warping:
- `nlpaug/augmenter/audio/audio_augmenter.py`, `nlpaug/augmenter/audio/noise.py`, `nlpaug/augmenter/audio/vtlp.py`
- `nlpaug/augmenter/spectrogram/spectrogram_augmenter.py`, `nlpaug/augmenter/spectrogram/frequency_masking.py`, `nlpaug/augmenter/spectrogram/time_warping.py`

Implementation logic is delegated to matching model-layer transforms, keeping public augmentation semantics distinct from numerical signal operations.

#### Model and Provider Adapters
`nlpaug/model/` abstracts the backing source used by augmenters: local dictionaries/rules, embeddings/statistics, numerical transforms, and language-model providers. Key adapter families are:
- `nlpaug/model/word_dict/word_dictionary.py`, `nlpaug/model/word_embs/word_embeddings.py`, `nlpaug/model/word_stats/tfidf.py`
- `nlpaug/model/lang_models/language_models.py`, `nlpaug/model/lang_models/fill_mask_transformers.py`, `nlpaug/model/lang_models/machine_translation_transformers.py`
- `nlpaug/model/audio/audio.py`, `nlpaug/model/spectrogram/spectrogram.py`

Optional integrations include Transformers models, NLTK/WordNet, gensim embeddings, librosa/audio dependencies, and OpenAI chat generation via `nlpaug/model/lang_models/openai_chat.py`. These are dependency and network/model-availability boundaries; core usage can remain offline.

#### Flow Orchestration
Flows compose augmenters into reusable pipelines rather than embedding orchestration in individual augmenters:
- `nlpaug/flow/pipeline.py`
- `nlpaug/flow/sequential.py`
- `nlpaug/flow/sometimes.py`

`Sequential` passes each output through ordered stages. `Sometimes` probabilistically applies contained stages. This allows mixing compatible augmenters while preserving each stage’s own action and model boundary.

#### Assets, Model Acquisition, and Runtime Support
Utilities provide tokenization, randomness, filtering, model caching, audio I/O, and asset download behavior:
- `nlpaug/util/model_cache.py`
- `nlpaug/util/file/download.py`
- `nlpaug/util/audio/loader.py`

Pretrained embeddings and some language resources are external/downloaded; package-local mappings remain in `nlpaug/res/`. Packaging is defined by `pyproject.toml` and `setup.py`; CI targets Python 3.12 in `.github/workflows/python312.yml`.

Main entry points: [`nlpaug/base_augmenter.py`](https://github.com/makcedward/nlpaug/blob/master/nlpaug/base_augmenter.py) (Base augmenter), [`nlpaug/augmenter/augment.py`](https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/augment.py) (Augment base), [`nlpaug/augmenter/char/char_augmenter.py`](https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/char/char_augmenter.py) (Character augmenters), [`nlpaug/augmenter/word/word_augmenter.py`](https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/word/word_augmenter.py) (Word augmenter)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_public["Public API"]
  node_exports["Package exports<br/>Python package<br/>[__init__.py]"]
  node_base["Base augmenter<br/>augmentation contract<br/>[base_augmenter.py]"]
  node_augment["Augment base<br/>domain base<br/>[augment.py]"]
end

subgraph group_text["Text Augmenters"]
  node_char["Character augmenters<br/>text family<br/>[char_augmenter.py]"]
  node_keyboard["Keyboard augmenter<br/>character transform<br/>[keyboard.py]"]
  node_ocr["OCR augmenter<br/>character transform<br/>[ocr.py]"]
  node_word["Word augmenter<br/>text family base<br/>[word_augmenter.py]"]
  node_synonym["Synonym augmenter<br/>word transform<br/>[synonym.py]"]
  node_context_embs["Context embedding augmenter<br/>word transform"]
  node_sentence["Sentence augmenter<br/>text family base"]
  node_generation["Summarization &amp; generation<br/>sentence transforms<br/>[abst_summ.py]"]
end

subgraph group_signal["Signal Augmenters"]
  node_audio["Audio augmenter<br/>signal family base<br/>[audio_augmenter.py]"]
  node_audio_ops["Noise &amp; VTLP<br/>audio transforms<br/>[noise.py]"]
  node_spectrogram["Spectrogram augmenter<br/>signal family base"]
  node_spectrogram_ops["Masking &amp; warping<br/>spectrogram transforms"]
end

subgraph group_models["Model Adapters"]
  node_word_sources[("Dictionary, embeddings &amp; TF-IDF<br/>text model adapters<br/>[word_dictionary.py]")]
  node_language_models{{"Language-model providers<br/>provider adapters<br/>[language_models.py]"}}
  node_signal_models["Audio &amp; spectrogram models<br/>numerical transforms<br/>[audio.py]"]
end

subgraph group_runtime["Flows &amp; Runtime Support"]
  node_resources[("Packaged mappings<br/>local assets<br/>[en.json]")]
  node_flows["Augmentation flows<br/>pipeline orchestration<br/>[pipeline.py]"]
  node_model_cache["Model cache<br/>runtime support<br/>[model_cache.py]"]
  node_download["Asset download<br/>runtime support<br/>[download.py]"]
  node_audio_loader["Audio loader<br/>runtime support<br/>[loader.py]"]
end

node_exports -->|"exposes"| node_base
node_base -->|"defines lifecycle for"| node_augment
node_augment -->|"specializes into"| node_char
node_augment -->|"specializes into"| node_word
node_augment -->|"specializes into"| node_sentence
node_base -->|"specializes into"| node_audio
node_base -->|"specializes into"| node_spectrogram
node_char -->|"base for"| node_keyboard
node_char -->|"base for"| node_ocr
node_keyboard -->|"uses mappings"| node_resources
node_ocr -->|"uses mappings"| node_resources
node_word -->|"base for"| node_synonym
node_word -->|"base for"| node_context_embs
node_synonym -->|"queries"| node_word_sources
node_context_embs -->|"queries"| node_word_sources
node_sentence -->|"base for"| node_generation
node_generation -->|"uses"| node_language_models
node_audio -->|"base for"| node_audio_ops
node_audio_ops -->|"delegates transforms"| node_signal_models
node_spectrogram -->|"base for"| node_spectrogram_ops
node_spectrogram_ops -->|"delegates transforms"| node_signal_models
node_flows -->|"composes augmenters"| node_base
node_language_models -->|"caches models"| node_model_cache
node_download -->|"supplies external assets"| node_model_cache
node_audio -->|"loads audio inputs"| node_audio_loader

click node_exports "https://github.com/makcedward/nlpaug/blob/master/nlpaug/__init__.py"
click node_base "https://github.com/makcedward/nlpaug/blob/master/nlpaug/base_augmenter.py"
click node_augment "https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/augment.py"
click node_char "https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/char/char_augmenter.py"
click node_keyboard "https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/char/keyboard.py"
click node_ocr "https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/char/ocr.py"
click node_word "https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/word/word_augmenter.py"
click node_synonym "https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/word/synonym.py"
click node_context_embs "https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/word/context_word_embs.py"
click node_sentence "https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/sentence/sentence_augmenter.py"
click node_generation "https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/sentence/abst_summ.py"
click node_audio "https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/audio/audio_augmenter.py"
click node_audio_ops "https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/audio/noise.py"
click node_spectrogram "https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/spectrogram/spectrogram_augmenter.py"
click node_spectrogram_ops "https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/spectrogram/frequency_masking.py"
click node_word_sources "https://github.com/makcedward/nlpaug/blob/master/nlpaug/model/word_dict/word_dictionary.py"
click node_language_models "https://github.com/makcedward/nlpaug/blob/master/nlpaug/model/lang_models/language_models.py"
click node_signal_models "https://github.com/makcedward/nlpaug/blob/master/nlpaug/model/audio/audio.py"
click node_resources "https://github.com/makcedward/nlpaug/blob/master/nlpaug/res/char/keyboard/en.json"
click node_flows "https://github.com/makcedward/nlpaug/blob/master/nlpaug/flow/pipeline.py"
click node_model_cache "https://github.com/makcedward/nlpaug/blob/master/nlpaug/util/model_cache.py"
click node_download "https://github.com/makcedward/nlpaug/blob/master/nlpaug/util/file/download.py"
click node_audio_loader "https://github.com/makcedward/nlpaug/blob/master/nlpaug/util/audio/loader.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_exports,node_base,node_augment toneBlue
class node_char,node_keyboard,node_ocr,node_word,node_synonym,node_context_embs,node_sentence,node_generation toneAmber
class node_audio,node_audio_ops,node_spectrogram,node_spectrogram_ops toneMint
class node_word_sources,node_language_models,node_signal_models toneRose
class node_resources,node_flows,node_model_cache,node_download,node_audio_loader toneIndigo
```

## Components

### Public API

- [Package exports](https://github.com/makcedward/nlpaug/blob/master/nlpaug/__init__.py): Python package · Consumer import entry point. · `nlpaug/__init__.py`
- [Base augmenter](https://github.com/makcedward/nlpaug/blob/master/nlpaug/base_augmenter.py): augmentation contract · Validation, action lifecycle, selection, and change history. · `nlpaug/base_augmenter.py`
- [Augment base](https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/augment.py): domain base · `nlpaug/augmenter/augment.py`

### Text Augmenters

- [Character augmenters](https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/char/char_augmenter.py): text family · Shared character selection and tokenization behavior. · `nlpaug/augmenter/char/char_augmenter.py`
- [Keyboard augmenter](https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/char/keyboard.py): character transform · `nlpaug/augmenter/char/keyboard.py`
- [OCR augmenter](https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/char/ocr.py): character transform · `nlpaug/augmenter/char/ocr.py`
- [Word augmenter](https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/word/word_augmenter.py): text family base · Shared word-level selection and tokenization. · `nlpaug/augmenter/word/word_augmenter.py`
- [Synonym augmenter](https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/word/synonym.py): word transform · `nlpaug/augmenter/word/synonym.py`
- [Context embedding augmenter](https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/word/context_word_embs.py): word transform · `nlpaug/augmenter/word/context_word_embs.py`
- [Sentence augmenter](https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/sentence/sentence_augmenter.py): text family base · `nlpaug/augmenter/sentence/sentence_augmenter.py`
- [Summarization & generation](https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/sentence/abst_summ.py): sentence transforms · Abstractive summarization and LAMBADA generation augmenters. · `nlpaug/augmenter/sentence/abst_summ.py`

### Signal Augmenters

- [Audio augmenter](https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/audio/audio_augmenter.py): signal family base · Public semantics for audio-array transformations. · `nlpaug/augmenter/audio/audio_augmenter.py`
- [Noise & VTLP](https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/audio/noise.py): audio transforms · `nlpaug/augmenter/audio/noise.py`
- [Spectrogram augmenter](https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/spectrogram/spectrogram_augmenter.py): signal family base · `nlpaug/augmenter/spectrogram/spectrogram_augmenter.py`
- [Masking & warping](https://github.com/makcedward/nlpaug/blob/master/nlpaug/augmenter/spectrogram/frequency_masking.py): spectrogram transforms · `nlpaug/augmenter/spectrogram/frequency_masking.py`

### Model Adapters

- [Dictionary, embeddings & TF-IDF](https://github.com/makcedward/nlpaug/blob/master/nlpaug/model/word_dict/word_dictionary.py): text model adapters · Local rules and statistical or embedding-backed sources. · `nlpaug/model/word_dict/word_dictionary.py`
- [Language-model providers](https://github.com/makcedward/nlpaug/blob/master/nlpaug/model/lang_models/language_models.py): provider adapters · Transformers, translation, and optional OpenAI-backed generation. · `nlpaug/model/lang_models/language_models.py`
- [Audio & spectrogram models](https://github.com/makcedward/nlpaug/blob/master/nlpaug/model/audio/audio.py): numerical transforms · Numerical signal-operation implementations. · `nlpaug/model/audio/audio.py`

### Flows & Runtime Support

- [Packaged mappings](https://github.com/makcedward/nlpaug/blob/master/nlpaug/res/char/keyboard/en.json): local assets · Static keyboard, OCR, and spelling resources. · `nlpaug/res/char/keyboard/en.json`
- [Augmentation flows](https://github.com/makcedward/nlpaug/blob/master/nlpaug/flow/pipeline.py): pipeline orchestration · Sequential and probabilistic composition of compatible stages. · `nlpaug/flow/pipeline.py`
- [Model cache](https://github.com/makcedward/nlpaug/blob/master/nlpaug/util/model_cache.py): runtime support · `nlpaug/util/model_cache.py`
- [Asset download](https://github.com/makcedward/nlpaug/blob/master/nlpaug/util/file/download.py): runtime support · Acquires external pretrained resources. · `nlpaug/util/file/download.py`
- [Audio loader](https://github.com/makcedward/nlpaug/blob/master/nlpaug/util/audio/loader.py): runtime support · `nlpaug/util/audio/loader.py`

## Connections

- Package exports → Base augmenter: exposes
- Base augmenter → Augment base: defines lifecycle for
- Augment base → Character augmenters: specializes into
- Augment base → Word augmenter: specializes into
- Augment base → Sentence augmenter: specializes into
- Base augmenter → Audio augmenter: specializes into
- Base augmenter → Spectrogram augmenter: specializes into
- Character augmenters → Keyboard augmenter: base for
- Character augmenters → OCR augmenter: base for
- Keyboard augmenter → Packaged mappings: uses mappings
- OCR augmenter → Packaged mappings: uses mappings
- Word augmenter → Synonym augmenter: base for
- Word augmenter → Context embedding augmenter: base for
- Synonym augmenter → Dictionary, embeddings & TF-IDF: queries
- Context embedding augmenter → Dictionary, embeddings & TF-IDF: queries
- Sentence augmenter → Summarization & generation: base for
- Summarization & generation → Language-model providers: uses
- Audio augmenter → Noise & VTLP: base for
- Noise & VTLP → Audio & spectrogram models: delegates transforms
- Spectrogram augmenter → Masking & warping: base for
- Masking & warping → Audio & spectrogram models: delegates transforms
- Augmentation flows → Base augmenter: composes augmenters
- Language-model providers → Model cache: caches models
- Asset download → Model cache: supplies external assets
- Audio augmenter → Audio loader: loads audio inputs
