Skip to main content

An enhanced language detector for Kinyarwanda

Project description

kgt_rwalang

An enhanced language detector for Kinyarwanda, designed to accurately identify Kinyarwanda text, including code-mixed content involving English, French, and Swahili.

Note: It is not perfect yet, but it is the best there is, and it works.

Overview

kgt_rwalang combines traditional methods like character n-grams and TF-IDF with Kinyarwanda-specific linguistic features to achieve robust language detection. It includes specialized handling and confidence scoring for text that mixes Kinyarwanda with common foreign loan words and grammatical structures.

Features

  • Character N-grams & TF-IDF: Standard text feature extraction.
  • Kinyarwanda Linguistic Features: Incorporates analysis of ibihekane, ibyungo, accented vowels, grammatical markers, and common affixes.
  • Code-Mixing Detection: Includes logic to identify and better handle text containing a mix of Kinyarwanda and foreign language elements (loan words, grammatical patterns).
  • Ensemble Model: Uses a combination of machine learning classifiers for improved accuracy.
  • Model Persistence: Ability to save and load trained models using joblib.
  • Configurable Thresholds: Adjust detection sensitivity.

Installation

You can install the package using pip:

pip install kgt_rwalang

Usage

Here's a basic example of how to use the KinyaLangDetector class:

import pandas as pd
from kgt_rwalang.detector import KinyaLangDetector

# Instantiate the Detector
detector = KinyaLangDetector()

# Load or Train the Model ---

try:
    # Attempt to load the model.
    # Call load_model as is or pass a path to your own model.
    detector.load_model()
except FileNotFoundError:
    print(f"Model file not found. Training new model...")
    try:
        training_df = pd.read_csv('path/to/your_training_data.csv', encoding='utf-8')
    except FileNotFoundError:
         print("Error: Training data CSV not found. Cannot train model.")
         # Handle this error - maybe exit or get data another way
         training_df = None

    if training_df is not None:
        # Train the model using your DataFrame
        detector.train(training_df)
        print("Training complete.")

        # Save the newly trained model for future use
        # User might save their trained model to a specific path they control
        detector.save_model(model_filepath)
        print(f"Model saved to {model_filepath}")
except Exception as e:
    # Catch any other unexpected errors during loading
    print(f"An unexpected error occurred during model handling: {e}")
    # The detector.model might be None here

# Use the Detector ---

if detector.model: # Check if a model is successfully loaded or trained
    test_texts = [
        "Muraho, amakuru yanyu?",      # Kinyarwanda
        "Ese ubu yamaze gu testing?",  # Code-mixed Kinyarwanda/English
        "Hello, how are you?",         # Pure English
        "Je parle français",           # Pure French
        "Habari zenu?",                # Pure Swahili
        "Mfite message kuri whatsapp.", # Code-mixed Kinyarwanda/English
        "Ibyo ni sawa kabisa.",        # Code-mixed Kinyarwanda/Swahili
        "Ni mwicare se banyabusa!" # Pure Kinyarwanda
    ]

    for text in test_texts:
        result = detector.detect(text)
        print(f"Input: '{text}'")
        print(f"Detection: {result}")
        # Result will be a dictionary like:
        # {
        #   'language': 'kinyarwanda' or 'english' etc.,
        #   'confidence': 0.0 to 1.0,
        #   'is_kinyarwanda': True or False,
        #   'code_mixed': True or False,
        #   'mix_info': {... details if code_mixed},
        #   'analysis_scores': {... additional scores}
        # }
        print("-" * 20)
else:
    print("Detector model is not available. Please train or load a model first.")

Training Data

To train the model, you will need a dataset of text samples labelled with their language. The detector.train() method is designed to accept a dictionary with at least a 'text' key (containing the text samples) and a 'language' key (containing the corresponding language labels).

A recommended format for your training data CSV is:

text,language,source,original_id,timestamp,is_code_mixed,mixed_languages,annotator,quality_score
"Muraho, amakuru?",kinyarwanda,manual,k_001,2024-01-01,False,None,UserA,5
"Ese ubu urakora project?",kinyarwanda,social,tw_123,2024-03-15,True,english,AutoCollect,4
"Hello, how are you?",english,manual,e_001,2024-01-01,False,None,UserA,5
...

Licence

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

We welcome contributions to kgt_rwalang! If you have suggestions for improvements, bug fixes, or want to add more linguistic features or data, please follow these steps:

  1. Fork the repository on GitHub
  2. Create a new branch for your feature or bugfix.
  3. Make your changes, ensuring your code follows the project's style and includes appropriate tests (if applicable).
  4. Write clear commit messages.
  5. Push your branch to your fork.
  6. Submit a pull request to the main repository, describing your changes.

Please feel free to open an issue first to discuss larger changes.

Author

izamha

giliza@kigalithm.com

Sponsor

Kigalithm

foss@kigalithm.com

intern@kigalithm.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kgt_rwalang-1.0.0.tar.gz (24.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kgt_rwalang-1.0.0-py3-none-any.whl (26.1 kB view details)

Uploaded Python 3

File details

Details for the file kgt_rwalang-1.0.0.tar.gz.

File metadata

  • Download URL: kgt_rwalang-1.0.0.tar.gz
  • Upload date:
  • Size: 24.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for kgt_rwalang-1.0.0.tar.gz
Algorithm Hash digest
SHA256 907f0d4ca34cb2e4fb782ad15dd1e89443a9701386ecba0ebd016bf0f91287a6
MD5 b13e46005a5c8db9bcf7468bb02f9fa5
BLAKE2b-256 cff570fcb7cd1bdf89704d441e193fe9ef59cc68ed4322a591c7a6c8e1063da2

See more details on using hashes here.

File details

Details for the file kgt_rwalang-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: kgt_rwalang-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 26.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for kgt_rwalang-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0c3c65416bc61cc78a3224101b0911441e88b7a4c288031ef42c8482bed3780d
MD5 b40adf66d13674c83ee87ee34e4d35d3
BLAKE2b-256 32ebc2e39bd8d9fadd7a1adbe462a795e023d7776eed6bcce52a83b1a6e10ef3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page