Skip to content

About

Tuneable fuzzy matching for language identification evaluation

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

36 Commits

Folders and files

Repository files navigation

langid-eval

Tuneable fuzzy matching for language identification evaluation.

Why langid-eval?

Evaluating language identification (LID) entails deciding whether a predicted language label matches the gold. However, this task is not as straightforward as it first seems: models and test sets may use different labelling schemes and cover different sets of languages, and which language varieties are matches depends on the application.

langid-eval is an open-source Python package for simplifying LID evaluation. It reconciles language labels automatically, allowing the user to define the granularity of match that they need. It also calculates key statistics over whole test sets or over a user-defined subset of languages (for example, those covered by the model). This makes evaluation much more reproducible and less prone to human error.

Installation

langid-eval can be installed from this repository, directly or using tools like pip or uv. It will be added to PyPI after publication!

Requires Python ≥3.14.

Usage

Python

from langid_eval import LangIdEval

# similar labels match without preprocessing
golds = ["en", "en", "es", "ar", "zh", "fr"]
preds = ["en-GB", "fr", "es-MX", "arb", "cmn", "fr"]

lideval = LangIdEval(golds, preds)
lideval.precision()        # macro average over all classes by default
lideval.recall(average="weighted")
lideval.f1(by_class=True)  # {'en': 0.667, 'es': 1.0, ...}

lideval = LangIdEval(golds, preds, supported_languages=["en", "fr"])
lideval.fpr(supported_languages_only=True)
print(lideval.classification_report(digits=2))

Command line

Given two equal-length files of gold and predicted labels (one per line), print an sklearn-style classification report.

langid-eval [--supported_langs_file SUPPORTED_LANGS_FILE] [--max_tag_distance MAX_TAG_DISTANCE] [--match_script] golds_file preds_file

API reference

Full parameter details are in the docstrings, e.g. help(LangIdEval.f1).

Name Description
LangIdEval(golds, preds, ignore_script=True, max_tag_distance=5, supported_languages=()) Evaluate predicted labels against gold labels. See How matching works.
.precision(), .recall(), .f1(), .fpr() Overall metric (average="macro", "micro" or "weighted") or per language (by_class=True). Use supported_languages_only=True to evaluate on supported languages only.
.classification_report(supported_languages_only=False, output_dict=False, digits=4) sklearn-style per-language report, as a string or a dict (output_dict=True).

How matching works

Arguments

Three constructor arguments to LangIdEval affect how matches are defined:

  • ignore_script (default: True). Ignore script when matching. If ignore_script is False, both tags are filled in with their most likely script if not already specified. The scripts must be the same, or there is no match.
  • max_tag_distance (default: 5). The maximum tag distance (as defined by langcodes) at which two language tags will count as a match.
  • supported_languages (default: ()). The subset of languages to evaluate on, intended to be the languages supported by the model under testing. Setting the method argument supported_languages_only=True will filter the test set only to the gold labels which are within max_tag_distance of one of the languages in this sequence prior to calculating statistics. The intended use is to evaluate models only on the languages they claim to cover.

max_tag_distance

langid-eval uses the tag_distance function from the langcodes package to adjust the strictness of the match. The original function returns a number from 0 to 134 indicating the distance between the language the user desires and a supported language. This distance is intended as a measure of likely language acceptability; that is, given a user understands one language, how likely is it that the other will be acceptable to them? The distance data comes from CLDR v38.1 and involves a lot of judgment calls made by the Unicode Consortium. For this reason, it is not a true metric, but rather should be taken as indicative. For more explanation and examples, see the docstring of tag_distance.

langid-eval makes some alterations to the original tag_distance function to make it suitable for fuzzy language identification evaluation. The library enforces max_tag_distance<20, because language pairs at distances above this are unlikely to be considered "the same" language. In addition, langid-eval calculates matches based on the minimum tag distance between pairs, since the distances calculated by langcodes are not always symmetric.

As a rule of thumb, a value of 0 matches codes that langcodes treats as the same language after normalisation. This includes some macrolanguages and their main individual language, for example zh and cmn, or ar and arb. 5 (the default) allows for regional differences, and 10-15 matches other individual languages to their macrolanguage, for example arz and ar. A full list of distance interpretations is available in the langcodes repository. In case of unexpected behaviour, please raise an issue in this repository.

Handling und

The tag und (undetermined) marks examples where the predicting model (or the gold labeller) could not determine what language the example is in. und only matches und and if ignore_script=False, the script must also match. Usually, und will not be in the gold labels, so a prediction of und will not change the false positive count for any class. If und is in the gold labels, it behaves like other gold labels and increments its true positive, true negative, false positive and false negative counts as usual.

Counting false positives

Assuming a tag pair does not match, langid-eval compares the prediction against the other available gold labels to find the closest match within max_tag_distance. In case of a tie, an exact string match wins, then the gold label that comes first alphabetically. If no gold label is close enough, no false positive is counted and unattributed_count for the gold label in the tag pair is incremented.

Citation

After publication! Please see the paper for further details and analysis of the package.

License

langid-eval is released under the MIT license.

About

Tuneable fuzzy matching for language identification evaluation

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages