pyroid.metrics¶
ROUGE as a drop-in for rouge-score 0.1.2's modules, and BLEU and chrF as a drop-in for sacrebleu
2.6.0. Guide: pyroid.metrics.
pyroid.metrics.rouge_scorer¶
A drop-in, Rust-backed replacement for rouge_score.rouge_scorer (rouge-score 0.1.2).
from pyroid.metrics import rouge_scorer, scoring replaces
from rouge_score import rouge_scorer, scoring. RougeScorer takes
rouge-score's arguments and returns the same dict[str, Score]: the same
floats bit for bit, int zeros where rouge-score has them, the same key
order, the same ValueError messages. ROUGE-N (rouge1..rouge9), ROUGE-L
and ROUGE-Lsum are supported, with or without nltk 3.10.4's Porter stemmer
(ported to Rust: nltk is not needed).
Tokenizing, stemming and scoring run in Rust. What needs the interpreter runs in Python, exactly where rouge-score runs it:
- a custom
tokenizeris called from Python (for the target, the prediction, and each rougeLsum sentence); its tokens are scored in Rust. Tokens that are not lists ofstrare scored by the real rouge-score, with a one-timepyroid.FallbackWarning(pyroid.PyroidErrorif rouge-score is not installed); split_summaries=Truesplits rougeLsum sentences withpyroid.nltk.sent_tokenize, the Rust port ofnltk.sent_tokenize(the same sentences; it reads NLTK'spunkt_tabmodel, and a missing model raisesLookupErroras in rouge-score).
RougeScorer.score_batch (a pyroid extension) scores many pairs at once
on pyroid's thread pool.
Example
from pyroid.metrics import rouge_scorer
scorer = rouge_scorer.RougeScorer(["rouge1", "rougeL"], use_stemmer=True)
scorer.score("The quick brown fox jumps over the lazy dog",
"The quick brown dog jumps on the log.")
# {'rouge1': Score(precision=0.75, recall=0.666..., fmeasure=0.705...),
# 'rougeL': Score(precision=0.625, recall=0.555..., fmeasure=0.588...)}
scorer.score_batch(targets, predictions, threads=8) # list of such dicts
Differences from rouge-score¶
- Lone surrogates (U+D800..U+DFFF) in a
strtarget or prediction raiseUnicodeEncodeError(with a note) when the default tokenizer is used. rouge-score treats them as separators. Remove them first, e.g.s.encode("utf-8", "replace").decode("utf-8"). A custom tokenizer's tokens may hold them (those are scored by rouge-score). RougeScoreris pyroid's class, apyroid.metrics.scoring.BaseScorer; its scores arepyroid.metrics.scoring.Scorenamedtuples, equal to rouge-score's but not instances ofrouge_score.scoring.Score.- rouge-score's private helpers (
_score_lcs,_lcs_table,_summary_level_lcs, ...) andlcs_indare not provided. score_multipicks the first maximal f-measure asnp.argmaxdoes, without numpy (its error for no targets is numpy'sValueError("attempt to get argmax of an empty sequence")).- "Using default tokenizer." is logged at INFO level with
logging(loggerpyroid.metrics.rouge_scorer), not with absl. - nltk is not needed. With
split_summaries=Trueit is imported, if installed, to find thepunkt_tabmodel; rouge-score always imports nltk, absl, numpy and six.
RougeScorer ¶
RougeScorer(
rouge_types: Sequence[str],
use_stemmer: bool = False,
split_summaries: bool = False,
tokenizer: _Tokenizer | None = None,
)
Bases: BaseScorer
Calculate rouge scores between two blobs of text (rouge-score's RougeScorer).
Sample usage
scorer = RougeScorer(["rouge1", "rougeL"], use_stemmer=True)
scores = scorer.score("The quick brown fox jumps over the lazy dog",
"The quick brown dog jumps on the log.")
Initializes a new RougeScorer.
Valid rouge types that can be computed are:
rouge1..rouge9: n-gram based scoring.rougeL: longest common subsequence based scoring.rougeLsum: summary-level LCS over sentences (lines, or Punkt sentences withsplit_summaries).
Types are checked when scoring, as in rouge-score.
Parameters:
-
rouge_types(Sequence[str]) –A list of rouge types to calculate.
-
use_stemmer(bool, default:False) –Bool indicating whether Porter stemmer should be used to strip word suffixes to improve matching. This arg is used in the DefaultTokenizer, but other tokenizers might or might not choose to use this.
-
split_summaries(bool, default:False) –whether to add newlines between sentences for rougeLsum (sentences are split with
pyroid.nltk.sent_tokenize). -
tokenizer(_Tokenizer | None, default:None) –Tokenizer object which has a tokenize() method. A falsy value means the default tokenizer.
rouge_types
instance-attribute
¶
The rouge types to calculate, read at every call (as in rouge-score).
score_multi ¶
Calculates rouge scores between targets and prediction.
The target with the maximum f-measure is used for the final score for each score type (the first one on ties).
Parameters:
-
targets(Iterable[str]) –list of texts containing the targets
-
prediction(str) –Text containing the predicted text.
Returns:
Raises:
-
ValueError–If an invalid rouge type is encountered, or
targetsis empty.
score ¶
Calculates rouge scores between the target and prediction.
Parameters:
-
target(str) –Text containing the target (ground truth) text.
-
prediction(str) –Text containing the predicted text.
Returns:
Raises:
-
ValueError–If an invalid rouge type is encountered (
"Invalid rouge type: ...", or"rougen requires positive n: ..."forrouge0). -
UnicodeEncodeError–targetorpredictionholds a lone surrogate (default tokenizer).
score_batch ¶
Score many pairs at once, on pyroid's thread pool (not part of rouge-score).
Element i equals self.score(targets[i], predictions[i]). With the
default tokenizer and a list/tuple of rouge type strs, every pair is
tokenized and scored in Rust in parallel (with split_summaries=True,
sentences are split in parallel too). Otherwise (a custom tokenizer,
list items that are not str) the pairs are scored one by one with
score.
Parameters:
-
targets(Sequence[str] | Sequence[str] | ArrowStrings) –The targets: a list or tuple of str, or an Arrow string array/column (
string,large_stringorstring_view; pyarrow, polars,pyroid.Column, ...). Not a single str: usescorefor one pair. -
predictions(Sequence[str] | Sequence[str] | ArrowStrings) –The predictions, as for
targets, of the same length. -
threads(int | None, default:None) –Noneuses pyroid's shared thread pool;nruns at mostntasks at once;1runs on the calling thread.
Returns:
-
list[dict[str, scoring.Score]] | list[dict[str, scoring.Score] | None]–A list (for any input kind) with one
dict[str, Score]per pair, in order, andNonefor a pair with an Arrow null on either side.
Raises:
-
ValueError–targetsandpredictionsdiffer in length, or a rouge type is invalid (raised before any pair is scored, even for an empty batch). -
InputError–targetsorpredictionsis not a list, tuple or Arrow string array (a single str is rejected), a list or tuple holdsNone(the message gives its index), orthreadsis not an int. -
UnicodeEncodeError–A str holds a lone surrogate.
-
UnicodeDecodeError–An Arrow value is not valid UTF-8.
pyroid.metrics.scoring¶
rouge-score's scoring classes (rouge_score.scoring), as a drop-in.
from pyroid.metrics import scoring replaces from rouge_score import
scoring. The classes are ports of rouge-score 0.1.2's, line for line:
ScoreandAggregateScoreare namedtuples with the same fields and reprs (Score(precision=1.0, recall=0.5, fmeasure=0.666...)); they compare equal to rouge-score's.BootstrapAggregatorresamples with numpy's global random state, exactly as rouge-score does: after the samenp.random.seed(...)it returns identical intervals.
Differences from rouge-score¶
- numpy is imported when
BootstrapAggregator.aggregateis first called (it is not a pyroid dependency). Without it,aggregateraisesImportErrorwith an install hint. rouge-score imports numpy at import. - The classes are pyroid's own:
isinstance(score, rouge_score.scoring.Score)is false for a pyroidScore(and vice versa), though they compare equal.
Score ¶
Bases: NamedTuple
Tuple containing precision, recall, and f-measure values.
rougeL and rougeLsum return Score(precision=0, recall=0, fmeasure=0)
with int zeros when the target or prediction has no tokens, as
rouge-score does; every other value is a float.
AggregateScore ¶
BootstrapAggregator ¶
Aggregates scores to provide confidence intervals (bootstrap resampling).
Example
from pyroid.metrics import rouge_scorer, scoring
scorer = rouge_scorer.RougeScorer(["rouge1", "rougeL"])
aggregator = scoring.BootstrapAggregator()
aggregator.add_scores(scorer.score("one two three", "one two"))
aggregator.add_scores(scorer.score("one two five six", "seven eight"))
result = aggregator.aggregate()
# {'rouge1': AggregateScore(low=..., mid=..., high=...), 'rougeL': ...}
Initializes a BootstrapAggregator object.
Parameters:
-
confidence_interval(float, default:0.95) –Confidence interval to compute on the mean as a decimal.
-
n_samples(int, default:1000) –Number of samples to use for bootstrap resampling.
Raises:
-
ValueError–If invalid argument is given.
add_scores ¶
aggregate ¶
aggregate() -> dict[str, AggregateScore]
Aggregates scores previously added using add_scores.
The low and high bounds are the percentiles of n_samples bootstrap
means given by confidence_interval; mid is the median of those means.
Resampling uses numpy.random (the global state).
Returns:
-
dict[str, AggregateScore]–A dict mapping score_type to AggregateScore objects.
Raises:
-
ImportError–numpy is not installed.
pyroid.metrics.tokenizers¶
rouge-score's tokenizer classes (rouge_score.tokenizers), Rust-backed.
from pyroid.metrics import tokenizers replaces from rouge_score import
tokenizers. DefaultTokenizer tokenizes exactly as rouge-score 0.1.2's:
lowercase, keep runs of a-z0-9, and with use_stemmer=True stem tokens
longer than 3 characters with nltk 3.10.4's Porter stemmer (ported to Rust;
nltk is not needed).
Pass a Tokenizer subclass (or any object with a tokenize method)
as RougeScorer(tokenizer=...) to tokenize differently; it is called from
Python where rouge-score calls it.
Differences from rouge-score¶
DefaultTokenizerhas no_stemmerattribute (rouge-score keeps an nltkPorterStemmerthere). Stemming runs in Rust.- A
strwith lone surrogates (U+D800..U+DFFF) raisesUnicodeEncodeError(rouge-score drops them like any other non-alphanumeric character).
Tokenizer ¶
DefaultTokenizer ¶
DefaultTokenizer(use_stemmer: bool = False)
Bases: Tokenizer
Default tokenizer which tokenizes on whitespace (rouge-score's).
Example
from pyroid.metrics import tokenizers
tokenizers.DefaultTokenizer(use_stemmer=True).tokenize("the friends had a meeting")
# ['the', 'friend', 'had', 'a', 'meet']
Constructor for DefaultTokenizer.
Parameters:
-
use_stemmer(bool, default:False) –boolean, indicating whether Porter stemmer should be used to strip word suffixes to improve matching.
tokenize ¶
rouge-score's tokens of text.
text.lower(), then every run of characters other than a-z0-9
separates tokens (Unicode lowercasing included: "İ" gives "i",
the Kelvin sign gives "k"). With use_stemmer, tokens longer
than 3 are Porter-stemmed. bytes and other objects with a
lower() method are handled as rouge-score handles them.
Parameters:
-
text(str) –The text to tokenize.
Returns:
Raises:
-
UnicodeEncodeError–textholds a lone surrogate.
pyroid.metrics.tokenize¶
rouge-score's tokenizing function (rouge_score.tokenize), Rust-backed.
from pyroid.metrics import tokenize replaces from rouge_score import
tokenize: tokenize and the module's regex constants.
Differences from rouge-score¶
- A
strwith lone surrogates (U+D800..U+DFFF) raisesUnicodeEncodeError. - A
stemmerpassed here is called from Python for every token longer than 3, as in rouge-score.pyroid.metrics.tokenizers.DefaultTokenizerwithuse_stemmer=Truestems in Rust instead.
tokenize ¶
Tokenize input text into a list of tokens, as rouge-score does.
This approach aims to replicate the approach taken by Chin-Yew Lin in the original ROUGE implementation.
Parameters:
-
text(str) –A text blob to tokenize.
-
stemmer(_Stemmer | None) –An optional stemmer (e.g.
nltk.stem.porter.PorterStemmer()), applied to tokens longer than 3 characters.
Returns:
pyroid.metrics.sacrebleu¶
A drop-in, Rust-backed replacement for sacrebleu 2.6.0's BLEU and chrF API.
import pyroid.metrics.sacrebleu as sacrebleu
bleu = sacrebleu.corpus_bleu(hyps, [refs]) # BLEUScore, exactly sacrebleu's
print(bleu) # the same line sacrebleu prints: "BLEU = ... (BP = ... ratio = ...)"
chrf = sacrebleu.corpus_chrf(hyps, [refs], word_order=2) # chrF2++
sacrebleu.sentence_bleu_batch(hyps, [refs], threads=8) # one BLEUScore per pair
Every score, statistic, str()/repr(), format() output, signature string, exception
and sacrebleu log warning is sacrebleu 2.6.0's. __version__, like the version:
field of signatures, is the sacrebleu API version reproduced. The integer n-gram statistics
are computed in Rust, in parallel on pyroid's thread pool. The score formulas
(BLEU.compute_bleu, CHRF._compute_f_score), result classes and argument checks are
sacrebleu's own Python code, so scores are bit-for-bit sacrebleu's on the same interpreter.
To pick the best of several references, chrF uses a step-for-step Rust port of the f-score,
with the same operations in the same order.
pyroid handles these natively:
- the tokenizers
13a(the default),intl,zh(also viatrg_lang="zh"),charandnone; - every smoothing method and value,
lowercase(Python'sstr.lower()),force,effective_orderandmax_ngram_order; - several references per segment, including
Noneholes (nrefs:var); - chrF and chrF++ with any
char_order,word_order,beta,whitespaceandeps_smoothing; - pre-computed references (
BLEU(references=...),CHRF(references=...)).
Degenerate settings (e.g. max_ngram_order=0, a non-int order,
char_order + word_order == 0) run sacrebleu's own code in pure Python, with sacrebleu's
results and errors.
These are delegated to the real sacrebleu, with one pyroid.FallbackWarning per
function and reason: the ja-mecab, ko-mecab, spm, flores101, flores200
and spBLEU-1K tokenizers (and trg_lang "ja"/"ko"),
corpus_score(..., n_bootstrap > 1), TER, corpus_ter,
sentence_ter, DATASETS and the test set helpers (DELEGATED). They need
sacrebleu installed; without it they raise pyroid.PyroidError with an install hint.
Importing this module never imports sacrebleu.
pyroid extensions (keyword-only): threads= on corpus_bleu,
raw_corpus_bleu, corpus_chrf and corpus_score (None: the whole pool;
n: at most n tasks; 1: the calling thread), and sentence_bleu_batch /
sentence_chrf_batch.
Differences from sacrebleu:
- Lone surrogates (
"\ud800") in a hypothesis or reference raiseUnicodeEncodeError(with a note naming the item), as everywhere in pyroid. sacrebleu treats them as code points. Remove them first, e.g.s.encode("utf-8", "replace").decode("utf-8"). - Wrong item types that sacrebleu's own argument checks let through (a non-str hypothesis
after the first, a non-str, non-
Nonereference) raisepyroid.InputError(aTypeError) naming the item. sacrebleu raises anAttributeErrorfrom deep inside. sacrebleu's checks themselves (_check_*_args) raise the sameTypeErrors with the same messages. BLEU.tokenizeris a pyroid tokenizer object for the native tokenizers (signature()and__call__(line), the same output as sacrebleu's), and sacrebleu's own tokenizer object for the delegated ones.- Pre-computed references are held in a native object. Changing
max_ngram_order,char_orderorword_orderof a metric after constructing it withreferences=raisesValueError(sacrebleu would mix the old reference n-grams with new hypothesis ones). sentence_bleu_batchlogs sacrebleu'seffective_orderrecommendation once per call, not once per sentence.
__version__
module-attribute
¶
The sacrebleu release whose API and results this module reproduces: "2.6.0".
DELEGATED
module-attribute
¶
Names that are forwarded to the real sacrebleu (it must be installed).
SACREBLEU_DIR
module-attribute
¶
SACREBLEU_DIR: str
Where sacrebleu stores test sets: $SACREBLEU, else ~/.sacrebleu (as sacrebleu).
DATASETS
module-attribute
¶
sacrebleu's test set registry (read-only; delegated to sacrebleu on first use).
Score ¶
The result of a metric (sacrebleu's Score).
str(score) and repr(score) are score.format().
Parameters:
-
name(str) –The name of the underlying metric.
-
score(float) –A floating point number for the final metric.
format ¶
format(
width: int = 2,
score_only: bool = False,
signature: str = "",
is_json: bool = False,
) -> str
A pretty representation of the score, as sacrebleu prints it.
Parameters:
-
width(int, default:2) –Floating point decimal precision width.
-
score_only(bool, default:False) –If
True, and the format is notjson, returns a single score string. -
signature(str, default:'') –A string representation of the metric's signature (
str(metric.get_signature())). -
is_json(bool, default:False) –If
True, will output the score in JSON string.
Returns:
-
str–A plain or JSON-formatted string, e.g.
"BLEU|nrefs:1|case:mixed|eff:no|tok:13a|smooth:exp|version:2.6.0 = ...".
Signature ¶
A sacreBLEU reproducibility signature; str() is format().
Parameters:
Raises:
-
ValueError–The metric has not been evaluated yet.
format ¶
BLEUSignature ¶
CHRFSignature ¶
BLEUScore ¶
BLEUScore(
score: float,
counts: list[int],
totals: list[int],
precisions: list[float],
bp: float,
sys_len: int,
ref_len: int,
)
Bases: Score
A BLEU score with its statistics.
Parameters:
-
score(float) –The BLEU score.
-
counts(list[int]) –Counts of correct n-grams, 1 <= n <= max_ngram_order (with add-k smoothing, including the added k, as in sacrebleu).
-
totals(list[int]) –Counts of total n-grams, 1 <= n <= max_ngram_order.
-
precisions(list[float]) –n-gram precisions (percentages).
-
bp(float) –The brevity penalty.
-
sys_len(int) –The cumulative system length.
-
ref_len(int) –The cumulative reference length.
CHRFScore ¶
Metric ¶
Metric()
The base class of BLEU and CHRF (sacrebleu's Metric).
num_refs
instance-attribute
¶
num_refs: int
References per segment of the last evaluation (or the cache); -1 if they vary.
sentence_score ¶
corpus_score ¶
corpus_score(
hypotheses: Sequence[str],
references: Sequence[Sequence[str | None]] | None,
n_bootstrap: int = 1,
*,
threads: int | None = None,
) -> Score
Score a corpus against one or more reference streams.
Hypotheses and reference segments are zipped, as in sacrebleu: extra items of the longer one are ignored.
Parameters:
-
hypotheses(Sequence[str]) –A sequence of hypothesis strings.
-
references(Sequence[Sequence[str | None]] | None) –Reference streams:
references[k][i]is the k-th reference of hypothesis i (Nonefor a missing one).None(or empty) uses the references the metric was constructed with. -
n_bootstrap(int, default:1) –If > 1, also estimate a 95% confidence interval by bootstrap resampling with that many samples (delegated to sacrebleu).
-
threads(int | None, default:None) –pyroid extension:
Noneuses the whole thread pool,nat mostntasks at once,1the calling thread. Results are the same.
Returns:
-
Score–The corpus score.
Raises:
-
TypeError–sacrebleu's argument checks.
-
RuntimeError–No references given and none cached.
get_signature ¶
get_signature() -> Signature
The reproducibility signature (available once the metric has been evaluated).
Raises:
-
ValueError–The metric has not been evaluated yet.
BLEU ¶
BLEU(
lowercase: bool = False,
force: bool = False,
tokenize: str | None = None,
smooth_method: str = "exp",
smooth_value: float | None = None,
max_ngram_order: int = 4,
effective_order: bool = False,
trg_lang: str = "",
references: Sequence[Sequence[str | None]]
| None = None,
)
Bases: Metric
The BLEU metric (sacrebleu.BLEU).
Parameters:
-
lowercase(bool, default:False) –If True, lowercased BLEU is computed.
-
force(bool, default:False) –Ignore data that looks already tokenized (otherwise 100 hypotheses ending in
" ."log a warning on thesacrebleulogger). -
tokenize(str | None, default:None) –The tokenizer (one of
TOKENIZERS). If None,"13a", or the language's tokenizer fortrg_lang"zh","ja","ko". -
smooth_method(str, default:'exp') –"floor","add-k","exp"or"none". -
smooth_value(float | None, default:None) –The value for
floorandadd-k(None: 0.1 and 1). -
max_ngram_order(int, default:4) –The maximum n-gram order (default 4).
-
effective_order(bool, default:False) –Stop including n-gram orders for which precision is 0 (use it for sentence-level BLEU).
-
trg_lang(str, default:'') –An optional language code to pick (or warn about) the tokenizer.
-
references(Sequence[Sequence[str | None]] | None, default:None) –Reference streams to pre-compute for scoring many systems.
Raises:
-
AssertionError–Unknown
smooth_method(as sacrebleu). -
KeyError–Unknown
tokenize.
compute_bleu
staticmethod
¶
compute_bleu(
correct: list[int],
total: list[int],
sys_len: int,
ref_len: int,
smooth_method: str = "none",
smooth_value: float | None = None,
effective_order: bool = False,
max_ngram_order: int = 4,
) -> BLEUScore
BLEU from its sufficient statistics, with smoothing (sacrebleu's own code).
Parameters:
-
correct(list[int]) –Counts of correct n-grams, 1 <= n <= max_ngram_order (modified in place by
add-k). -
total(list[int]) –Counts of total n-grams, 1 <= n <= max_ngram_order.
-
sys_len(int) –The cumulative system length.
-
ref_len(int) –The cumulative reference length.
-
smooth_method(str, default:'none') –"floor","add-k","exp"or"none". -
smooth_value(float | None, default:None) –The value for
floorandadd-k(None: the default). -
effective_order(bool, default:False) –Stop at the first order without n-grams.
-
max_ngram_order(int, default:4) –The maximum n-gram order.
Returns:
-
BLEUScore–The score.
sentence_score ¶
corpus_score ¶
corpus_score(
hypotheses: Sequence[str],
references: Sequence[Sequence[str | None]] | None,
n_bootstrap: int = 1,
*,
threads: int | None = None,
) -> BLEUScore
Corpus-level BLEU; see Metric.corpus_score.
Parameters:
-
hypotheses(Sequence[str]) –A sequence of hypothesis strings.
-
references(Sequence[Sequence[str | None]] | None) –Reference streams (
references[k][i]), or None for the cached ones. -
n_bootstrap(int, default:1) –Bootstrap resamples for a confidence interval (delegated if > 1).
-
threads(int | None, default:None) –pyroid's thread pool setting.
Returns:
-
BLEUScore–The score.
get_signature ¶
get_signature() -> BLEUSignature
The signature, e.g. nrefs:1|case:mixed|eff:no|tok:13a|smooth:exp|version:2.6.0.
CHRF ¶
CHRF(
char_order: int = 6,
word_order: int = 0,
beta: int = 2,
lowercase: bool = False,
whitespace: bool = False,
eps_smoothing: bool = False,
references: Sequence[Sequence[str | None]]
| None = None,
)
Bases: Metric
The chrF and chrF++ metrics (sacrebleu.CHRF).
Parameters:
-
char_order(int, default:6) –Character n-gram order.
-
word_order(int, default:0) –Word n-gram order. If equals to 2, the metric is referred to as chrF++.
-
beta(int, default:2) –Determine the importance of recall w.r.t precision.
-
lowercase(bool, default:False) –Enable case-insensitivity.
-
whitespace(bool, default:False) –If
True, include whitespaces when extracting character n-grams. -
eps_smoothing(bool, default:False) –If
True, applies epsilon smoothing similar to reference chrF++.py, NLTK and Moses implementations. Otherwise, it takes into account effective match order similar to sacreBLEU < 2.0.0. -
references(Sequence[Sequence[str | None]] | None, default:None) –Reference streams to pre-compute for scoring many systems.
sentence_score ¶
corpus_score ¶
corpus_score(
hypotheses: Sequence[str],
references: Sequence[Sequence[str | None]] | None,
n_bootstrap: int = 1,
*,
threads: int | None = None,
) -> CHRFScore
Corpus-level chrF; see Metric.corpus_score.
Parameters:
-
hypotheses(Sequence[str]) –A sequence of hypothesis strings.
-
references(Sequence[Sequence[str | None]] | None) –Reference streams (
references[k][i]), or None for the cached ones. -
n_bootstrap(int, default:1) –Bootstrap resamples for a confidence interval (delegated if > 1).
-
threads(int | None, default:None) –pyroid's thread pool setting.
Returns:
-
CHRFScore–The score.
get_signature ¶
get_signature() -> CHRFSignature
The signature, e.g. nrefs:1|case:mixed|eff:yes|nc:6|nw:0|space:no|version:2.6.0.
TER ¶
TER(
normalized: bool = False,
no_punct: bool = False,
asian_support: bool = False,
case_sensitive: bool = False,
references: Sequence[Sequence[str]] | None = None,
)
sacrebleu's TER metric (delegated: every call runs a real sacrebleu.TER).
Parameters:
-
normalized(bool, default:False) –Enable character normalization.
-
no_punct(bool, default:False) –Remove punctuation.
-
asian_support(bool, default:False) –Enable special treatment of Asian characters.
-
case_sensitive(bool, default:False) –Enables case-sensitivity.
-
references(Sequence[Sequence[str]] | None, default:None) –Reference streams to pre-compute.
Raises:
-
PyroidError–sacrebleu is not installed.
corpus_bleu ¶
corpus_bleu(
hypotheses: Sequence[str],
references: Sequence[Sequence[str | None]],
smooth_method: str = "exp",
smooth_value: float | None = None,
force: bool = False,
lowercase: bool = False,
tokenize: str = "13a",
use_effective_order: bool = False,
*,
threads: int | None = None,
) -> BLEUScore
BLEU of a corpus against one or more reference streams, like sacrebleu.corpus_bleu.
>>> corpus_bleu(["The cat sat on the mat."], [["The cat sat on a mat."]]).score
48.892302243490086
Parameters:
-
hypotheses(Sequence[str]) –A sequence of hypothesis strings.
-
references(Sequence[Sequence[str | None]]) –Reference streams:
references[k][i]is the k-th reference of hypothesis i (Nonefor a missing one). -
smooth_method(str, default:'exp') –"floor","add-k","exp"or"none". -
smooth_value(float | None, default:None) –The value for
floorandadd-k(None: the default). -
force(bool, default:False) –Ignore data that looks already tokenized.
-
lowercase(bool, default:False) –Lowercase the data.
-
tokenize(str, default:'13a') –The tokenizer to use.
-
use_effective_order(bool, default:False) –Don't take into account n-gram orders without any match.
-
threads(int | None, default:None) –pyroid extension:
Noneuses the whole thread pool,nat mostntasks at once,1the calling thread.
Returns:
-
BLEUScore–The score.
raw_corpus_bleu ¶
raw_corpus_bleu(
hypotheses: Sequence[str],
references: Sequence[Sequence[str | None]],
smooth_value: float | None = 0.1,
*,
threads: int | None = None,
) -> BLEUScore
BLEU of the raw corpus: no tokenization, floor smoothing, effective order.
Parameters:
-
hypotheses(Sequence[str]) –A sequence of hypothesis strings.
-
references(Sequence[Sequence[str | None]]) –Reference streams (
references[k][i]). -
smooth_value(float | None, default:0.1) –The
floorsmoothing value. -
threads(int | None, default:None) –pyroid's thread pool setting (see
corpus_bleu).
Returns:
-
BLEUScore–The score.
sentence_bleu ¶
sentence_bleu(
hypothesis: str,
references: Sequence[str | None],
smooth_method: str = "exp",
smooth_value: float | None = None,
lowercase: bool = False,
tokenize: str = "13a",
use_effective_order: bool = True,
) -> BLEUScore
BLEU of one hypothesis against its reference(s), like sacrebleu.sentence_bleu.
Parameters:
-
hypothesis(str) –A single hypothesis string.
-
references(Sequence[str | None]) –Its reference strings (
Noneentries are skipped). -
smooth_method(str, default:'exp') –"floor","add-k","exp"or"none". -
smooth_value(float | None, default:None) –The value for
floorandadd-k(None: the default). -
lowercase(bool, default:False) –Lowercase the data.
-
tokenize(str, default:'13a') –The tokenizer to use.
-
use_effective_order(bool, default:True) –Don't take into account n-gram orders without any match.
Returns:
-
BLEUScore–The score.
sentence_bleu_batch ¶
sentence_bleu_batch(
hypotheses: Sequence[str],
references: Sequence[
Sequence[str | None] | ArrowStrings
],
smooth_method: str = "exp",
smooth_value: float | None = None,
lowercase: bool = False,
tokenize: str = "13a",
use_effective_order: bool = True,
*,
threads: int | None = None,
) -> list[BLEUScore]
sentence_bleu_batch(
hypotheses: ArrowStrings,
references: Sequence[
Sequence[str | None] | ArrowStrings
],
smooth_method: str = "exp",
smooth_value: float | None = None,
lowercase: bool = False,
tokenize: str = "13a",
use_effective_order: bool = True,
*,
threads: int | None = None,
) -> list[BLEUScore | None]
sentence_bleu of every hypothesis at once, on pyroid's thread pool.
Element i equals sentence_bleu(hypotheses[i], [r[i] for r in references], ...).
Parameters:
-
hypotheses(Sequence[str] | ArrowStrings) –The hypothesis strings: a list or tuple of str, or an Arrow string array/column (pyarrow, polars,
pyroid.Column, ...). Not a single str: usesentence_bleufor one hypothesis. -
references(Sequence[Sequence[str | None] | ArrowStrings]) –Reference streams, as for
corpus_bleu:references[k][i]is the k-th reference of hypothesis i (Nonefor a missing one). Every stream must have one entry per hypothesis. A stream may be an Arrow string column, whose nulls are missing references. -
smooth_method(str, default:'exp') –"floor","add-k","exp"or"none". -
smooth_value(float | None, default:None) –The value for
floorandadd-k(None: the default). -
lowercase(bool, default:False) –Lowercase the data.
-
tokenize(str, default:'13a') –The tokenizer to use.
-
use_effective_order(bool, default:True) –Don't take into account n-gram orders without any match.
-
threads(int | None, default:None) –Noneuses the whole thread pool,nat mostntasks at once,1the calling thread.
Returns:
-
list[BLEUScore] | list[BLEUScore | None]–A list (for any input kind) with one score per hypothesis, and
Nonefor a null in an Arrowhypothesescolumn.
Raises:
-
InputError–hypothesesis not a sequence of str or an Arrow string column, or a list or tuple of hypotheses holdsNone(the message gives its index), orreferencesis not a sequence of streams (e.g. a flat list of str: pass[refs]). -
ValueError–No streams, or a stream's length differs from the hypotheses'.
corpus_chrf ¶
corpus_chrf(
hypotheses: Sequence[str],
references: Sequence[Sequence[str | None]],
char_order: int = 6,
word_order: int = 0,
beta: int = 2,
remove_whitespace: bool = True,
eps_smoothing: bool = False,
*,
threads: int | None = None,
) -> CHRFScore
Corpus-level chrF (word_order=2: chrF++), like sacrebleu.corpus_chrf.
Parameters:
-
hypotheses(Sequence[str]) –A sequence of hypothesis strings.
-
references(Sequence[Sequence[str | None]]) –Reference streams (
references[k][i];Nonefor a missing one). -
char_order(int, default:6) –Character n-gram order.
-
word_order(int, default:0) –Word n-gram order. If equals to 2, the metric is referred to as chrF++.
-
beta(int, default:2) –Determine the importance of recall w.r.t precision.
-
remove_whitespace(bool, default:True) –If
True, removes whitespaces prior to character n-gram extraction. -
eps_smoothing(bool, default:False) –If
True, applies epsilon smoothing similar to reference chrF++.py, NLTK and Moses implementations. Otherwise, it takes into account effective match order similar to sacreBLEU < 2.0.0. -
threads(int | None, default:None) –pyroid's thread pool setting (see
corpus_bleu).
Returns:
-
CHRFScore–The score.
sentence_chrf ¶
sentence_chrf(
hypothesis: str,
references: Sequence[str | None],
char_order: int = 6,
word_order: int = 0,
beta: int = 2,
remove_whitespace: bool = True,
eps_smoothing: bool = False,
) -> CHRFScore
Sentence-level chrF against the best reference, like sacrebleu.sentence_chrf.
Parameters:
-
hypothesis(str) –A single hypothesis string.
-
references(Sequence[str | None]) –Its reference strings (
Noneentries are skipped). -
char_order(int, default:6) –Character n-gram order.
-
word_order(int, default:0) –Word n-gram order. If equals to 2, the metric is referred to as chrF++.
-
beta(int, default:2) –Determine the importance of recall w.r.t precision.
-
remove_whitespace(bool, default:True) –If
True, removes whitespaces prior to character n-gram extraction. -
eps_smoothing(bool, default:False) –Epsilon smoothing instead of effective-order smoothing.
Returns:
-
CHRFScore–The score.
sentence_chrf_batch ¶
sentence_chrf of every hypothesis at once, on pyroid's thread pool.
Element i equals sentence_chrf(hypotheses[i], [r[i] for r in references], ...).
Parameters:
-
hypotheses(Sequence[str] | ArrowStrings) –The hypothesis strings, as for
sentence_bleu_batch. -
references(Sequence[Sequence[str | None] | ArrowStrings]) –Reference streams (see
sentence_bleu_batch). -
char_order(int, default:6) –Character n-gram order.
-
word_order(int, default:0) –Word n-gram order. If equals to 2, the metric is referred to as chrF++.
-
beta(int, default:2) –Determine the importance of recall w.r.t precision.
-
remove_whitespace(bool, default:True) –If
True, removes whitespaces prior to character n-gram extraction. -
eps_smoothing(bool, default:False) –Epsilon smoothing instead of effective-order smoothing.
-
threads(int | None, default:None) –Noneuses the whole thread pool,nat mostntasks at once,1the calling thread.
Returns:
-
list[CHRFScore] | list[CHRFScore | None]–A list (for any input kind) with one score per hypothesis, and
Nonefor a null in an Arrowhypothesescolumn.
Raises:
-
InputError–As for
sentence_bleu_batch. -
ValueError–No streams, or a stream's length differs from the hypotheses'.
extract_word_ngrams ¶
extract_char_ngrams ¶
smart_open ¶
corpus_ter ¶
corpus_ter(
hypotheses: Sequence[str],
references: Sequence[Sequence[str]],
normalized: bool = False,
no_punct: bool = False,
asian_support: bool = False,
case_sensitive: bool = False,
) -> Any
sacrebleu.corpus_ter (delegated; returns sacrebleu's TERScore).
Parameters:
-
hypotheses(Sequence[str]) –A sequence of hypothesis strings.
-
references(Sequence[Sequence[str]]) –Reference streams.
-
normalized(bool, default:False) –Enable character normalization.
-
no_punct(bool, default:False) –Remove punctuation.
-
asian_support(bool, default:False) –Enable special treatment of Asian characters.
-
case_sensitive(bool, default:False) –Enables case-sensitivity.
sentence_ter ¶
sentence_ter(
hypothesis: str,
references: Sequence[str],
normalized: bool = False,
no_punct: bool = False,
asian_support: bool = False,
case_sensitive: bool = False,
) -> Any
sacrebleu.sentence_ter (delegated; returns sacrebleu's TERScore).
Parameters:
-
hypothesis(str) –A single hypothesis string.
-
references(Sequence[str]) –Its reference strings.
-
normalized(bool, default:False) –Enable character normalization.
-
no_punct(bool, default:False) –Remove punctuation.
-
asian_support(bool, default:False) –Enable special treatment of Asian characters.
-
case_sensitive(bool, default:False) –Enables case-sensitivity.
download_test_set ¶
get_source_file ¶
get_reference_files ¶
get_available_testsets ¶
sacrebleu.get_available_testsets (delegated).