本文整理汇总了Python中nltk.translate.bleu_score.SmoothingFunction方法的典型用法代码示例。如果您正苦于以下问题:Python bleu_score.SmoothingFunction方法的具体用法?Python bleu_score.SmoothingFunction怎么用?Python bleu_score.SmoothingFunction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nltk.translate.bleu_score
的用法示例。
在下文中一共展示了bleu_score.SmoothingFunction方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compute
# 需要导入模块: from nltk.translate import bleu_score [as 别名]
# 或者: from nltk.translate.bleu_score import SmoothingFunction [as 别名]
def compute(guess: str, answers: List[str], k: int = 4) -> Optional['BleuMetric']:
"""
Compute approximate BLEU score between guess and a set of answers.
"""
if nltkbleu is None:
# bleu library not installed, just return a default value
return None
# Warning: BLEU calculation *should* include proper tokenization and
# punctuation etc. We're using the normalize_answer for everything though,
# so we're over-estimating our BLEU scores. Also note that NLTK's bleu is
# going to be slower than fairseq's (which is written in C), but fairseq's
# requires that everything be in arrays of ints (i.e. as tensors). NLTK's
# works with strings, which is better suited for this module.
weights = [1 / k for _ in range(k)]
score = nltkbleu.sentence_bleu(
[normalize_answer(a).split(" ") for a in answers],
normalize_answer(guess).split(" "),
smoothing_function=nltkbleu.SmoothingFunction(epsilon=1e-12).method1,
weights=weights,
)
return BleuMetric(score)
示例2: get_report
# 需要导入模块: from nltk.translate import bleu_score [as 别名]
# 或者: from nltk.translate.bleu_score import SmoothingFunction [as 别名]
def get_report(self):
tokenize = get_tokenize()
print('Generate report for {} samples'.format(len(self.hyps)))
refs, hyps = [], []
for label, hyp in zip(self.labels, self.hyps):
# label = label.replace(EOS, '')
# hyp = hyp.replace(EOS, '')
# ref_tokens = tokenize(label)[1:]
# hyp_tokens = tokenize(hyp)[1:]
ref_tokens = tokenize(label)
hyp_tokens = tokenize(hyp)
refs.append([ref_tokens])
hyps.append(hyp_tokens)
bleu = corpus_bleu(refs, hyps, smoothing_function=SmoothingFunction().method1)
report = '\n===== BLEU = %f =====\n' % (bleu,)
return '\n===== REPORT FOR DATASET {} ====={}'.format(self.data_name, report)
示例3: bleu
# 需要导入模块: from nltk.translate import bleu_score [as 别名]
# 或者: from nltk.translate.bleu_score import SmoothingFunction [as 别名]
def bleu(answer_file, standard_answer_file):
rf_answer = open(answer_file, 'r', "utf-8")
rf_standard_answer = open(standard_answer_file, 'r', "utf-8")
answer_lines = rf_answer.readlines()
standard_answer_lines = rf_standard_answer.readlines()
# compute score
scores = []
for i in range(len(answer_lines)):
candidate = list(answer_lines[i].strip())
each_score = 0
for j in range(10):
references = []
standard_answer_line = standard_answer_lines[i * 11 + j].strip().split('\t')
references.append(list(standard_answer_line[0].strip()))
standard_score = standard_answer_line[1]
bleu_score = sentence_bleu(references, candidate, weights=(0.35, 0.45, 0.1, 0.1),
smoothing_function=SmoothingFunction().method1)
each_score = bleu_score * float(standard_score) + each_score
scores.append(each_score / 10)
rf_answer.close()
rf_standard_answer.close()
score_final = sum(scores) / float(len(answer_lines))
precision_score = round(score_final, 6)
return precision_score
示例4: __call__
# 需要导入模块: from nltk.translate import bleu_score [as 别名]
# 或者: from nltk.translate.bleu_score import SmoothingFunction [as 别名]
def __call__(self, trainer):
device = self.device
with chainer.no_backprop_mode():
references = []
hypotheses = []
for i in range(0, len(self.test_data), self.batch):
sources, targets = zip(*self.test_data[i:i + self.batch])
references.extend([[t.tolist()] for t in targets])
sources = [device.send(x) for x in sources]
ys = [y.tolist()
for y in self.model.translate(sources, self.max_length)]
hypotheses.extend(ys)
bleu = bleu_score.corpus_bleu(
references, hypotheses,
smoothing_function=bleu_score.SmoothingFunction().method1)
chainer.report({self.key: bleu})
示例5: __call__
# 需要导入模块: from nltk.translate import bleu_score [as 别名]
# 或者: from nltk.translate.bleu_score import SmoothingFunction [as 别名]
def __call__(self, trainer):
with chainer.no_backprop_mode():
references = []
hypotheses = []
for i in range(0, len(self.test_data), self.batch):
sources, targets = zip(*self.test_data[i:i + self.batch])
references.extend([[t.tolist()] for t in targets])
sources = [
chainer.dataset.to_device(self.device, x) for x in sources]
ys = [y.tolist()
for y in self.model.translate(sources, self.max_length)]
hypotheses.extend(ys)
bleu = bleu_score.corpus_bleu(
references, hypotheses,
smoothing_function=bleu_score.SmoothingFunction().method1)
reporter.report({self.key: bleu})
示例6: __call__
# 需要导入模块: from nltk.translate import bleu_score [as 别名]
# 或者: from nltk.translate.bleu_score import SmoothingFunction [as 别名]
def __call__(self, trainer):
print('## Calculate BLEU')
with chainer.no_backprop_mode():
with chainer.using_config('train', False):
references = []
hypotheses = []
for i in range(0, len(self.test_data), self.batch):
sources, targets = zip(*self.test_data[i:i + self.batch])
references.extend([[t.tolist()] for t in targets])
sources = [
chainer.dataset.to_device(self.device, x) for x in sources]
ys = [y.tolist()
for y in self.model.translate(sources, self.max_length)]
hypotheses.extend(ys)
bleu = bleu_score.corpus_bleu(
references, hypotheses,
smoothing_function=bleu_score.SmoothingFunction().method1) * 100
print('BLEU:', bleu)
reporter.report({self.key: bleu})
示例7: forward
# 需要导入模块: from nltk.translate import bleu_score [as 别名]
# 或者: from nltk.translate.bleu_score import SmoothingFunction [as 别名]
def forward(self, trainer):
with chainer.no_backprop_mode():
references = []
hypotheses = []
for i in range(0, len(self.test_data), self.batch):
sources, targets = zip(*self.test_data[i:i + self.batch])
references.extend([[t.tolist()] for t in targets])
sources = [
chainer.dataset.to_device(self.device, x) for x in sources]
ys = [y.tolist()
for y in self.model.translate(sources, self.max_length)]
hypotheses.extend(ys)
bleu = bleu_score.corpus_bleu(
references, hypotheses,
smoothing_function=bleu_score.SmoothingFunction().method1)
chainer.report({self.key: bleu})
示例8: _bleu
# 需要导入模块: from nltk.translate import bleu_score [as 别名]
# 或者: from nltk.translate.bleu_score import SmoothingFunction [as 别名]
def _bleu(guess, answers):
"""Compute approximate BLEU score between guess and a set of answers."""
if nltkbleu is None:
# bleu library not installed, just return a default value
return None
# Warning: BLEU calculation *should* include proper tokenization and
# punctuation etc. We're using the normalize_answer for everything though,
# so we're over-estimating our BLEU scores. Also note that NLTK's bleu is
# going to be slower than fairseq's (which is written in C), but fairseq's
# requires that everything be in arrays of ints (i.e. as tensors). NLTK's
# works with strings, which is better suited for this module.
return nltkbleu.sentence_bleu(
[normalize_answer(a).split(" ") for a in answers],
normalize_answer(guess).split(" "),
smoothing_function=nltkbleu.SmoothingFunction(epsilon=1e-12).method1,
)
示例9: __init__
# 需要导入模块: from nltk.translate import bleu_score [as 别名]
# 或者: from nltk.translate.bleu_score import SmoothingFunction [as 别名]
def __init__(self, smoothing):
'''
Params:
:smoothing: Smoothing method for bleu.
'''
self.metrics = {'bleu-1': [], 'bleu-2': [], 'bleu-3': [], 'bleu-4': []}
self.smoothing = [bleu_score.SmoothingFunction().method0,
bleu_score.SmoothingFunction().method1,
bleu_score.SmoothingFunction().method2,
bleu_score.SmoothingFunction().method3,
bleu_score.SmoothingFunction().method4,
bleu_score.SmoothingFunction().method5,
bleu_score.SmoothingFunction().method6,
bleu_score.SmoothingFunction().method7]
self.smoothing = self.smoothing[smoothing]
# Calculate metrics for one example.
示例10: cal_BLEU_nltk
# 需要导入模块: from nltk.translate import bleu_score [as 别名]
# 或者: from nltk.translate.bleu_score import SmoothingFunction [as 别名]
def cal_BLEU_nltk(refer, candidate, ngram=1):
'''
SmoothingFunction refer to https://github.com/PaddlePaddle/models/blob/a72760dff8574fe2cb8b803e01b44624db3f3eff/PaddleNLP/Research/IJCAI2019-MMPMS/mmpms/utils/metrics.py
'''
smoothie = SmoothingFunction().method7
if ngram == 1:
weight = (1, 0, 0, 0)
elif ngram == 2:
weight = (0.5, 0.5, 0, 0)
elif ngram == 3:
weight = (0.33, 0.33, 0.33, 0)
elif ngram == 4:
weight = (0.25, 0.25, 0.25, 0.25)
return sentence_bleu(refer, candidate,
weights=weight,
smoothing_function=smoothie)
# BLEU of nlg-eval
示例11: get_bleu
# 需要导入模块: from nltk.translate import bleu_score [as 别名]
# 或者: from nltk.translate.bleu_score import SmoothingFunction [as 别名]
def get_bleu(self, dataloader, input, reference_key, gen_key):
refs = []
gens = []
for gen_sen, resp_sen in zip(input[gen_key], input[reference_key]):
gen_sen_processed = dataloader.trim_in_ids(gen_sen)
resp_sen_processed = dataloader.trim_in_ids(resp_sen[1:])
refs.append(resp_sen_processed)
gens.append(gen_sen_processed)
gens = replace_unk(gens)
bleu_irl_bw, bleu_irl_fw = [], []
for i in range(len(gens)):
bleu_irl_fw.append(sentence_bleu(refs, gens[i], smoothing_function=SmoothingFunction().method1))
for i in range(len(refs)):
bleu_irl_bw.append(sentence_bleu(gens, refs[i], smoothing_function=SmoothingFunction().method1))
fw_bleu = (1.0 * sum(bleu_irl_fw) / len(bleu_irl_fw))
bw_bleu = (1.0 * sum(bleu_irl_bw) / len(bleu_irl_bw))
return 2.0 * bw_bleu * fw_bleu / (fw_bleu + bw_bleu)
示例12: _score
# 需要导入模块: from nltk.translate import bleu_score [as 别名]
# 或者: from nltk.translate.bleu_score import SmoothingFunction [as 别名]
def _score(self, gen: List[int], reference: List[int]) -> float:
'''Return a BLEU score \in [0, 1] to calculate BLEU-ngram precision and recall.
Arguments:
gen (list): list of generated word ids.
reference (list): list of word ids of a reference.
Here is an Example:
>>> gen = [4,5]
>>> reference = [5,6]
>>> self._score(gen, reference)
0.150 # assume self.weights = [0.25,0.25,0.25,0.25]
'''
gen = self._replace_unk(gen)
return sentence_bleu([reference], gen, self.weights, SmoothingFunction().method1)
示例13: print_batch
# 需要导入模块: from nltk.translate import bleu_score [as 别名]
# 或者: from nltk.translate.bleu_score import SmoothingFunction [as 别名]
def print_batch(learner: Learner, modeldata: ModelData, input_field, output_field, num_batches=1, num_sentences=-1,
is_test=False, num_beams=1, weights=None, smoothing_function=None):
predictions, targets, inputs = learner.predict_with_targs_and_inputs(is_test=is_test, num_beams=num_beams)
weights = (1 / 3., 1 / 3., 1 / 3.) if weights is None else weights
smoothing_function = SmoothingFunction().method1 if smoothing_function is None else smoothing_function
blue_scores = []
for batch_num, (input, target, prediction) in enumerate(zip(inputs, targets, predictions)):
inputs_str: BatchBeamTokens = modeldata.itos(input, input_field)
predictions_str: BatchBeamTokens = modeldata.itos(prediction, output_field)
targets_str: BatchBeamTokens = modeldata.itos(target, output_field)
for index, (inp, targ, pred) in enumerate(zip(inputs_str, targets_str, predictions_str)):
blue_score = sentence_bleu([targ], pred, smoothing_function=smoothing_function, weights=weights)
print(
f'batch: {batch_num} sample : {index}\ninput: {" ".join(inp)}\ntarget: { " ".join(targ)}\nprediction: {" ".join(pred)}\nbleu: {blue_score}\n\n')
blue_scores.append(blue_score)
if 0 < num_sentences <= index - 1:
break
if 0 < num_batches <= batch_num - 1:
break
print(f'mean bleu score: {np.mean(blue_scores)}')
示例14: sim_bleu
# 需要导入模块: from nltk.translate import bleu_score [as 别名]
# 或者: from nltk.translate.bleu_score import SmoothingFunction [as 别名]
def sim_bleu(self, hyps, ref):
"""
:param ref - a list of tokens of the reference
:param hyps - a list of tokens of the hypothesis
:return maxbleu - recall bleu
:return avgbleu - precision bleu
"""
scores = []
for hyp in hyps:
try:
# scores.append(sentence_bleu([ref], hyp, smoothing_function=SmoothingFunction().method7,
# weights=[1./4, 1./4, 1./4, 1./4]))
scores.append(smoothed_bleu(list(bleu_stats(hyp, ref))))
except:
scores.append(0.0)
return np.max(scores), np.mean(scores)
示例15: bleu_score
# 需要导入模块: from nltk.translate import bleu_score [as 别名]
# 或者: from nltk.translate.bleu_score import SmoothingFunction [as 别名]
def bleu_score(candidate, reference):
score = sentence_bleu(
[list(reference)], list(candidate),
weights=(0.25, 0.25, 0.25, 0.25),
smoothing_function=SmoothingFunction().method1)
return score