本文整理匯總了Python中sacrebleu.compute_bleu方法的典型用法代碼示例。如果您正苦於以下問題:Python sacrebleu.compute_bleu方法的具體用法?Python sacrebleu.compute_bleu怎麽用?Python sacrebleu.compute_bleu使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sacrebleu
的用法示例。
在下文中一共展示了sacrebleu.compute_bleu方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: calc_bleu_from_stats
# 需要導入模塊: import sacrebleu [as 別名]
# 或者: from sacrebleu import compute_bleu [as 別名]
def calc_bleu_from_stats(sentence_stats: pd.DataFrame) -> sacrebleu.BLEU:
corpus_stats = sentence_stats.sum(axis=0)
corpus_bleu = sacrebleu.compute_bleu(
correct=[
corpus_stats.correct_1_grams,
corpus_stats.correct_2_grams,
corpus_stats.correct_3_grams,
corpus_stats.correct_4_grams,
],
total=[
corpus_stats.total_1_grams,
corpus_stats.total_2_grams,
corpus_stats.total_3_grams,
corpus_stats.total_4_grams,
],
sys_len=corpus_stats.translation_length,
ref_len=corpus_stats.reference_length,
)
return corpus_bleu
示例2: score_cached_corpus
# 需要導入模塊: import sacrebleu [as 別名]
# 或者: from sacrebleu import compute_bleu [as 別名]
def score_cached_corpus(self, sent_ids, cached_stats):
"""
Score a corpus using SacreBLEU score with cache
Args:
sent_ids: The sentence ids for reference and output corpora
cached_stats: A list of cached statistics
Returns:
A tuple containing a single value for the SacreBLEU score and a string summarizing auxiliary information
"""
if len(cached_stats) == 0:
return 0.0, None
counts, totals, sys_len, ref_len = zip(*cached_stats)
counts, totals, sys_len, ref_len = [np.sum(np.array(x)[sent_ids], 0) for x in [counts, totals, sys_len, ref_len]]
return sacrebleu.compute_bleu(counts, totals, sys_len, ref_len, smooth_method=self.smooth_method, smooth_value=self.smooth_value, use_effective_order=self.use_effective_order).score, None
示例3: sentence_bleu
# 需要導入模塊: import sacrebleu [as 別名]
# 或者: from sacrebleu import compute_bleu [as 別名]
def sentence_bleu(hypothesis, reference):
bleu = _corpus_bleu(hypothesis, reference)
for i in range(1, 4):
bleu.counts[i] += 1
bleu.totals[i] += 1
bleu = compute_bleu(
bleu.counts, bleu.totals,
bleu.sys_len, bleu.ref_len,
smooth='exp', smooth_floor=0.0,
)
return bleu.score
示例4: reduce_metrics
# 需要導入模塊: import sacrebleu [as 別名]
# 或者: from sacrebleu import compute_bleu [as 別名]
def reduce_metrics(self, logging_outputs, criterion):
super().reduce_metrics(logging_outputs, criterion)
if self.args.eval_bleu:
def sum_logs(key):
return sum(log.get(key, 0) for log in logging_outputs)
counts, totals = [], []
for i in range(EVAL_BLEU_ORDER):
counts.append(sum_logs('_bleu_counts_' + str(i)))
totals.append(sum_logs('_bleu_totals_' + str(i)))
if max(totals) > 0:
# log counts as numpy arrays -- log_scalar will sum them correctly
metrics.log_scalar('_bleu_counts', np.array(counts))
metrics.log_scalar('_bleu_totals', np.array(totals))
metrics.log_scalar('_bleu_sys_len', sum_logs('_bleu_sys_len'))
metrics.log_scalar('_bleu_ref_len', sum_logs('_bleu_ref_len'))
def compute_bleu(meters):
import inspect
import sacrebleu
fn_sig = inspect.getfullargspec(sacrebleu.compute_bleu)[0]
if 'smooth_method' in fn_sig:
smooth = {'smooth_method': 'exp'}
else:
smooth = {'smooth': 'exp'}
bleu = sacrebleu.compute_bleu(
correct=meters['_bleu_counts'].sum,
total=meters['_bleu_totals'].sum,
sys_len=meters['_bleu_sys_len'].sum,
ref_len=meters['_bleu_ref_len'].sum,
**smooth
)
return round(bleu.score, 2)
metrics.log_derived('bleu', compute_bleu)
示例5: test_scoring
# 需要導入模塊: import sacrebleu [as 別名]
# 或者: from sacrebleu import compute_bleu [as 別名]
def test_scoring(statistics, expected_score):
score = sacrebleu.compute_bleu(statistics[0].common, statistics[0].total, statistics[1], statistics[2]).score / 100
assert abs(score - expected_score) < EPSILON
示例6: test_degenerate_statistics
# 需要導入模塊: import sacrebleu [as 別名]
# 或者: from sacrebleu import compute_bleu [as 別名]
def test_degenerate_statistics(statistics, offset, expected_score):
score = sacrebleu.compute_bleu(statistics[0].common, statistics[0].total, statistics[1], statistics[2], smooth_method='floor', smooth_value=offset).score / 100
assert score == expected_score
示例7: test_degenerate_statistics
# 需要導入模塊: import sacrebleu [as 別名]
# 或者: from sacrebleu import compute_bleu [as 別名]
def test_degenerate_statistics(statistics, offset, expected_score):
score = sacrebleu.compute_bleu(statistics[0].common, statistics[0].total, statistics[1], statistics[2],
smooth_method='floor', smooth_value=offset).score / 100
assert score == expected_score