DECAF: A Dynamically Extensible Corpus Analysis Framework
DECAF is an open-source Python framework that enables fine-grained linguistic analyses and filtering of existing datasets for generating targeted training interventions for LM generalization research.
Getting Started
For basic analyses and filtering, DECAF can be installed without any external dependencies:
pip install decaffinate
For importing datasets, and for more advanced analyses, please install the package with external dependencies:
pip install decaffinate[full]
For getting a quick overview of DECAF's core functionalities, we recommend taking a look at the demo notebook.
Building an Index
Rather than creating new resources for each experiment, DECAF builds indices over datasets with existing linguistic annotations, and leverages them to analyze, filter, and generate highly controlled and reproducible experimental settings targeting specific research questions. It maintains extensibility by constructing separate indices over raw text (literals) and annotations (structures).
Indexing is specific to each dataset format, so please refer to the import documentation for details. In general, the import scripts follow the simple structure:
script/import/format.py \
--input /path/to/data.txt \
--output /path/to/index
After having built the index, you can query it using the DecafIndex class:
from decaf import DecafIndex
di = DecafIndex('/path/to/index')
literals = di.get_literal_counts()
structures = di.get_structure_counts()
Building a Filter
DECAF treats indices and filters as independent entities from the original corpus. This means, that indices can be continually extended with new annotation layers, and that filters can be transferred across datasets.
Filters are constructed using the Filter class, which contains Constraint objects, which in turn contain a Condition sequence.
Filter([
Criterion([
Condition(
stype='type1',
values=['label1'],
literals=['form1']
),
Condition(
stype='type2',
values=['label2.1', 'label2.2'],
literals=['form2']
)],
operation='AND'
)],
sequential=True,
hierarchy=['sentence', 'token']
)
A Condition specified what to match at the structure level, i.e., the structure type, its value (if any), and specific surface forms (if any). Within a Criterion, multiple conditions, or nested criteria, can be combined using boolean operations. Finally, the top-level criteria or wrapped in a Filter, which can enforce whether the criteria can occur anywhere, or in a direct sequence. If the index contains hierarchical information, we can further enforce that the criteria must apply within a certain hierarchical level, e.g., token annotations occurring within a self-contained sentence.
Analyzing an Index
Once an index is built, we can analyze its statistics using general, as well as filter-specific queries, e.g.:
di = DecafIndex('/path/to/index')
# get the database sizes
num_literals, num_structures, num_hierarchies = di.get_size()
# get the frequency of each literal
literals = di.get_literal_counts()
# get the frequency of each structure
structures = di.get_structure_counts()
total_of_type = di.get_structure_counts(types=['type'])
type_value_counts = di.get_structure_counts(types=['token'], values=True)
type_value_form_counts = di.get_structure_counts(types=['token'], values=True, literals=True)
# get the co-occurence across two filters
cooccurrence = di.get_cooccurrence(
source_filter=df,
target_filter=df
)
Exporting Data
DECAF supports exporting filtered versions of the original data, either by keeping only the matched structures, or alternatively, by masking them out.
# exporting filter results
outputs = di.filter(constraint=df)
# masking filter results
outputs = di.mask(constraint=df)
By default, this will return/mask any structure that is matched. However, sometimes, we want to be more precise and remove structures, that are matched within a hierarchical constraint (e.g., relative clauses within their main clause). In these cases, we specify an output_level, which differs from the matched structure itself:
# exporting filter results
outputs = di.filter(
constraint=df,
output_level='substructure'
)
# masking filter results
outputs = di.mask(
constraint=df,
output_level='substructure'
)
Sharing
DECAF indices can be easily shared, as they are self-contained within their respective directories. Simply zip them up, and publish.
Similarly, filters are transferable across datasets, since they query the underlying, unified index, instead of the original corpus itself.
We provide some example experiments/, and highly encourage everyone to share their DECAF experiments, as well! ☕️
Metadata
Release files for decaffinate 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| decaffinate-0.2.1.tar.gz | 26.6 kB | Without provenance | Details for decaffinate-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| decaffinate-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for decaffinate-0.2.1-py3-none-any.whl |
Total release size: 56.7 kB
Release files / decaffinate-0.2.1.tar.gz
| Download URL | decaffinate-0.2.1.tar.gz |
|---|---|
| Size | 26.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
94dc482c8d5dada26d19a1f1ac42288d31ed7ab7ebc2992f895927248c2a7d2f
|
|
BLAKE2b-256 checksum How to use checksums |
b8a077ab14ba5ae66adb572a33f0279f309626905fb79bb0c3927c72903002e4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.5
|
Release files / decaffinate-0.2.1-py3-none-any.whl
| Download URL | decaffinate-0.2.1-py3-none-any.whl |
|---|---|
| Size | 30.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
abf3a93b83f3e661f89768ac478fc4f7bb87bb398bed069145a4e15aa144be81
|
|
BLAKE2b-256 checksum How to use checksums |
f5b24136c0eae1cd7e17f1803d32aaf8a5cd873a5e44e05bb66991b8c6f5a301
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.5
|
Security for decaffinate 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.