Skip to main content

CharacTER

CharacTER: Translation Edit Rate on Character Level

CharacTer (cer) is a novel character level metric inspired by the commonly applied translation edit rate (TER). It is defined as the minimum number of character edits required to adjust a hypothesis, until it completely matches the reference, normalized by the length of the hypothesis sentence. CharacTer calculates the character level edit distance while performing the shift edit on word level. Unlike the strict matching criterion in TER, a hypothesis word is considered to match a reference word and could be shifted, if the edit distance between them is below a threshold value. The Levenshtein distance between the reference and the shifted hypothesis sequence is computed on the character level. In addition, the lengths of hypothesis sequences instead of reference sequences are used for normalizing the edit distance, which effectively counters the issue that shorter translations normally achieve lower TER.

Modifications by Bram Vanroy

Bram Vanroy made some changes to this package that do not affect the result of the metric but that should improve usability. Code has been re-written to avoid the need for custom C++ code (instead the C implementation of Levenshtein alongside an LRU cache is used), to make functions more accessible and readable, and typing info has been included. Packaging has also improved to make uploading to PyPi a breeze. This means that the package can now be installed via pip:

pip install cer

The main functions are calculate_cer and calculate_cer_corpus, which both expect tokenized input. The first argument contains the hypotheses and the second the references.

from cer import calculate_cer

cer_score = calculate_cer(["i", "like", "your", "bag"], ["i", "like", "their", "bags"])
cer_score
0.3333333333333333

calculate_cer_corpus is similar but instead it expects a sequence of sequence of words, basically a corpus of sentences of words. It will report some statistics of the sentence-level CER scores that were calculated.

from cer import calculate_cer_corpus

hyps = ["this week the saudis denied information published in the new york times",
        "this is in fact an estimate"]
refs = ["saudi arabia denied this week information published in the american new york times",
        "this is actually an estimate"]

hyps = [sent.split() for sent in hyps]
refs = [sent.split() for sent in refs]

cer_corpus_score = calculate_cer_corpus(hyps, refs)
cer_corpus_score
{
    'count': 2,
    'mean': 0.3127282211789254,
    'median': 0.3127282211789254,
    'std': 0.07561653111280243,
    'min': 0.25925925925925924,
    'max': 0.36619718309859156
}

In addition to the Python interface, a command-line entry-point is also installed, which you can use as calculate-cer. Its idea is to calculate aggregate scores on the corpus-level (similar to calculate_cer_corpus) based on two input files. One with hypotheses and one with references (one on each line). Results are written to stdout.

usage: calculate-cer [-h] [-r] fhyp fref

CharacTER: Character Level Translation Edit Rate

positional arguments:
  fhyp                Path to file containing hypothesis sentences. One per line.
  fref                Path to file containing reference sentences. One per line.

optional arguments:
  -h, --help          show this help message and exit
  -r, --per_sentence  Whether to output CER scores per ref/hyp pair in addition to corpus-level statistics

License

GPLv3

Metadata

Release files for cer 1.2.0

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

Source distribution (sdist)

Source distribution for cer 1.2.0
File Size Uploaded
cer-1.2.0.tar.gz 18.2 kB Details

Built distribution (wheel)

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

Total release size: 35.8 kB

Release files / cer-1.2.0.tar.gz

Download URL cer-1.2.0.tar.gz
Size 18.2 kB
Tags Source
SHA-256 checksum
How to use checksums
485cb7ea2e6cbafcaed2147905bd06c3a5453cba8e598fe5b5976c625ba5904e
BLAKE2b-256 checksum
How to use checksums
bc8a9799debef342e3819a198f135ba2de5c278c13fefffbfb4ce7dd131b7920
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.13

Release files / cer-1.2.0-py3-none-any.whl

Download URL cer-1.2.0-py3-none-any.whl
Size 17.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a978a7a178bc369b0d32084ef5768b9636fd4e400fa7bbad1452f7f013a5107f
BLAKE2b-256 checksum
How to use checksums
06722993280000b13dd8b3b6be348807f01fe84997d5d42333fe0516685507c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.13

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

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