How makcedward/nlpaug works
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: base_augmenter.py Base augmenter · augment.py Augment base · char_augmenter.py Character augmenters · word_augmenter.py Word augmenter
Components 23
Public API
- Package exports · Consumer import entry point.
nlpaug/__init__.py - Base augmenter · Validation, action lifecycle, selection, and change history.
nlpaug/base_augmenter.py - Augment base
nlpaug/augmenter/augment.py
Text Augmenters
- Character augmenters · Shared character selection and tokenization behavior.
nlpaug/augmenter/char/char_augmenter.py - Keyboard augmenter
nlpaug/augmenter/char/keyboard.py - OCR augmenter
nlpaug/augmenter/char/ocr.py - Word augmenter · Shared word-level selection and tokenization.
nlpaug/augmenter/word/word_augmenter.py - Synonym augmenter
nlpaug/augmenter/word/synonym.py - Context embedding augmenter
nlpaug/augmenter/word/context_word_embs.py - Sentence augmenter
nlpaug/augmenter/sentence/sentence_augmenter.py - Summarization & generation · Abstractive summarization and LAMBADA generation augmenters.
nlpaug/augmenter/sentence/abst_summ.py
Signal Augmenters
- Audio augmenter · Public semantics for audio-array transformations.
nlpaug/augmenter/audio/audio_augmenter.py - Noise & VTLP
nlpaug/augmenter/audio/noise.py - Spectrogram augmenter
nlpaug/augmenter/spectrogram/spectrogram_augmenter.py - Masking & warping
nlpaug/augmenter/spectrogram/frequency_masking.py
Model Adapters
- Dictionary, embeddings & TF-IDF · Local rules and statistical or embedding-backed sources.
nlpaug/model/word_dict/word_dictionary.py - Language-model providers · Transformers, translation, and optional OpenAI-backed generation.
nlpaug/model/lang_models/language_models.py - Audio & spectrogram models · Numerical signal-operation implementations.
nlpaug/model/audio/audio.py
Flows & Runtime Support
- Packaged mappings · Static keyboard, OCR, and spelling resources.
nlpaug/res/char/keyboard/en.json - Augmentation flows · Sequential and probabilistic composition of compatible stages.
nlpaug/flow/pipeline.py - Model cache
nlpaug/util/model_cache.py - Asset download · Acquires external pretrained resources.
nlpaug/util/file/download.py - Audio loader
nlpaug/util/audio/loader.py
Connections 25
- 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