Skip to main content

AfroCorpus

A Python package for managing and querying African text corpora with flexible metadata-based filtering.

Features

  • Load and manage text corpora with metadata
  • Flexible filtering by country, language, URL, date, and content
  • Easy extraction of text content
  • Dynamic corpus expansion with the add() method
  • Content matching with match and match_all options
  • Automatic corpus data download with possibility to iupdate data with more data being added periodically without upgrading the package itself

Installation

pip install afrocorpus

Or from source:

git clone https://github.com/avokan-air/afrocorpus.git
cd afrocorpus
pip install -e .

Downloading Corpus Data

After installation, download the corpus data files:

from afrocorpus import AfroCorpus

# Download corpus data (tries primary URL, falls back to backups if needed)
AfroCorpus.download_data()

The download function automatically:

  • Tries the primary Google Drive URL first
  • Falls back to backup URLs if primary fails
  • Extracts .txt files to the data directory
  • Shows progress and confirmation messages

To re-download and overwrite existing data:

AfroCorpus.download_data(force=True)

Quick Start

from afrocorpus import AfroCorpus

# First time: download the data
AfroCorpus.download_data()

# Load all documents
corpus = AfroCorpus()
print(f"Loaded {len(corpus)} documents")

# Load with filters
corpus = AfroCorpus({
    "country": "bj",
    "language": "fr",
    "url": "wikipedia",
    "date": ">2026-01-01"
})

# Get contents
texts = corpus.contents()
full_text = " ".join(texts)

# Add more documents with additional filters
corpus.add({"url": "nouvelletribune", "match_all": ["talon", "yayi"]})

Using Your Own Data

If you have your own corpus files locally:

corpus = AfroCorpus(
    filters={"language": "fr"},
    data_dir="/path/to/your/data"
)

File Format

Text files should follow this format:

URL: https://example.com/article
Filename: article.txt
Language: fr
Last Updated: 2026-01-15T10:00:00.000000
Country: bj
================================================================================
Your article content goes here...

Lines 1-5 contain metadata, line 6 is an optional separator, and line 7+ contains the actual content.

Filter Options

Filter Description Example
country Country code (defaults to "bj") {"country": "bj"}
language Language code {"language": "fr"}
url URL substring match {"url": "wikipedia"}
date Date with operators (>, <, >=, <=, =) {"date": ">2026-01-01"}
match At least one term must appear {"match": ["term1", "term2"]}
match_all All terms must appear {"match_all": ["term1", "term2"]}

Advanced Usage

Chaining Filters

# Start with French documents from Benin
corpus = AfroCorpus({"country": "bj", "language": "fr"})

# Add English documents from Nigeria
corpus.add({"country": "ng", "language": "en"})

# Add any documents containing specific keywords
corpus.add({"match": ["economy", "politique"]})

Accessing Individual Documents

corpus = AfroCorpus()

# Get total number of documents
print(len(corpus))

# Access first document
first_doc = corpus[0]
print(first_doc['url'])
print(first_doc['language'])
print(first_doc['content'][:100])

# Iterate through all documents
for doc in corpus.documents:
    print(f"{doc['filename']}: {doc['language']}")

Development

Running Tests

python -m pytest tests/
# or
python -m unittest discover tests

Building the Package

python -m build

Publishing to PyPI

python -m twine upload dist/*

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Troubleshooting

Download Issues

If download_data() fails:

  • Check your internet connection
  • The function will automatically try backup URLs
  • Use force=True to re-download: AfroCorpus.download_data(force=True)

No Documents Found

If your corpus loads but has 0 documents:

  • Check that your filters aren't too restrictive
  • Verify the metadata format in your .txt files matches the expected format
  • Try loading without filters: AfroCorpus()

License

MIT License - see LICENSE file for details.

Author

Ola ABOUBAKAR (ola@avokan.com)

Citation

If you use AfroCorpus in your research, please cite:

@software{afrocorpus2026,
  author = {Ola ABOUBAKAR},
  title = {AfroCorpus: A Python Package for African Text Corpora},
  year = {2026},
  url = {https://github.com/avokan-air/afrocorpus}
}

Metadata

Release files for afrocorpus 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for afrocorpus 0.1.3
File Size Uploaded Provenance
afrocorpus-0.1.3.tar.gz 9.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for afrocorpus 0.1.3
File Interpreter ABI Platform
afrocorpus-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 18.6 kB

Release files / afrocorpus-0.1.3.tar.gz

Download URL afrocorpus-0.1.3.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
5c4ce6aebae69fa5a2dc406eda267ea7d500656deb3ee4d1f380336509b9b4ce
BLAKE2b-256 checksum
How to use checksums
a6b893c3c4c8c37cd171e088b1ed044b9bd26e6d19f9495bf8d1df407448c827
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.20

Release files / afrocorpus-0.1.3-py3-none-any.whl

Download URL afrocorpus-0.1.3-py3-none-any.whl
Size 9.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7c369251e87172aaca199f6481def35a9117a417ed5fae5701035242214d71f1
BLAKE2b-256 checksum
How to use checksums
6619e026f145def400eec53bf9101cd59aca25ee9f17be5d7938095865caeb81
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.20

Security for afrocorpus 0.1.3

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.

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page