NLTK: блеф на уровне корпуса против баллов BLEU на уровне предложений
Я импортировал nltk в python для расчета баллов BLEU в Ubuntu. Я понимаю, как работает оценка BLEU на уровне предложений, но я не понимаю, как работает оценка BLEU на уровне корпуса.
Ниже мой код для оценки BLEU на уровне корпуса:
import nltk
hypothesis = ['This', 'is', 'cat']
reference = ['This', 'is', 'a', 'cat']
BLEUscore = nltk.translate.bleu_score.corpus_bleu([reference], [hypothesis], weights = [1])
print(BLEUscore)
По какой-то причине показатель bleu равен 0 для приведенного выше кода. Я ожидал, что показатель BLEU на уровне корпуса составляет как минимум 0,5.
Вот мой код для оценки BLEU на уровне предложений
import nltk
hypothesis = ['This', 'is', 'cat']
reference = ['This', 'is', 'a', 'cat']
BLEUscore = nltk.translate.bleu_score.sentence_bleu([reference], hypothesis, weights = [1])
print(BLEUscore)
Здесь оценка BLEU на уровне предложения составляет 0,71, что я ожидаю, принимая во внимание штраф за краткость и пропущенное слово "а". Однако я не понимаю, как работает оценка BLEU на уровне корпуса.
Любая помощь будет оценена.
2 ответа
TL; DR:
>>> import nltk
>>> hypothesis = ['This', 'is', 'cat']
>>> reference = ['This', 'is', 'a', 'cat']
>>> references = [reference] # list of references for 1 sentence.
>>> list_of_references = [references] # list of references for all sentences in corpus.
>>> list_of_hypotheses = [hypothesis] # list of hypotheses that corresponds to list of references.
>>> nltk.translate.bleu_score.corpus_bleu(list_of_references, list_of_hypotheses)
0.6025286104785453
>>> nltk.translate.bleu_score.sentence_bleu(references, hypothesis)
0.6025286104785453
(Примечание: вы должны вытащить последнюю версию NLTK на develop
ветка для получения стабильной версии реализации оценки BLEU)
В длинных:
На самом деле, если в вашем корпусе только одна ссылка и одна гипотеза, оба corpus_bleu()
а также sentence_bleu()
должен вернуть то же значение, как показано в примере выше.
В коде мы видим, что sentence_bleu
на самом деле тип утки corpus_bleu
:
def sentence_bleu(references, hypothesis, weights=(0.25, 0.25, 0.25, 0.25),
smoothing_function=None):
return corpus_bleu([references], [hypothesis], weights, smoothing_function)
И если мы посмотрим на параметры для sentence_bleu
:
def sentence_bleu(references, hypothesis, weights=(0.25, 0.25, 0.25, 0.25),
smoothing_function=None):
""""
:param references: reference sentences
:type references: list(list(str))
:param hypothesis: a hypothesis sentence
:type hypothesis: list(str)
:param weights: weights for unigrams, bigrams, trigrams and so on
:type weights: list(float)
:return: The sentence-level BLEU score.
:rtype: float
"""
Вход для sentence_bleu
Ссылка list(list(str))
,
Так что если у вас есть строка предложения, например, "This is a cat"
, вы должны токенизировать его, чтобы получить список строк, ["This", "is", "a", "cat"]
и так как он допускает множественные ссылки, он должен быть списком списка строк, например, если у вас есть вторая ссылка "Это кошачий", ваш ввод sentence_bleu()
было бы:
references = [ ["This", "is", "a", "cat"], ["This", "is", "a", "feline"] ]
hypothesis = ["This", "is", "cat"]
sentence_bleu(references, hypothesis)
Когда дело доходит до corpus_bleu()
Параметр list_of_references, это в основном список того, что sentence_bleu()
принимает в качестве ссылок:
def corpus_bleu(list_of_references, hypotheses, weights=(0.25, 0.25, 0.25, 0.25),
smoothing_function=None):
"""
:param references: a corpus of lists of reference sentences, w.r.t. hypotheses
:type references: list(list(list(str)))
:param hypotheses: a list of hypothesis sentences
:type hypotheses: list(list(str))
:param weights: weights for unigrams, bigrams, trigrams and so on
:type weights: list(float)
:return: The corpus-level BLEU score.
:rtype: float
"""
Кроме взгляда на документ в рамках nltk/translate/bleu_score.py
Вы также можете посмотреть на unittest на nltk/test/unit/translate/test_bleu_score.py
чтобы увидеть, как использовать каждый компонент внутри bleu_score.py
,
Кстати, так как sentence_bleu
импортируется как bleu
в (nltk.translate.__init__.py
] ( https://github.com/nltk/nltk/blob/develop/nltk/translate/__init__.py), используя
from nltk.translate import bleu
будет так же, как:
from nltk.translate.bleu_score import sentence_bleu
и в коде:
>>> from nltk.translate import bleu
>>> from nltk.translate.bleu_score import sentence_bleu
>>> from nltk.translate.bleu_score import corpus_bleu
>>> bleu == sentence_bleu
True
>>> bleu == corpus_bleu
False
Давайте взглянем:
>>> help(nltk.translate.bleu_score.corpus_bleu)
Help on function corpus_bleu in module nltk.translate.bleu_score:
corpus_bleu(list_of_references, hypotheses, weights=(0.25, 0.25, 0.25, 0.25), smoothing_function=None)
Calculate a single corpus-level BLEU score (aka. system-level BLEU) for all
the hypotheses and their respective references.
Instead of averaging the sentence level BLEU scores (i.e. marco-average
precision), the original BLEU metric (Papineni et al. 2002) accounts for
the micro-average precision (i.e. summing the numerators and denominators
for each hypothesis-reference(s) pairs before the division).
...
Вы лучше меня понимаете описание алгоритма, поэтому я не буду пытаться "объяснить" его вам. Если строка документации не проясняет ситуацию достаточно, взгляните на сам источник. Или найти его локально:
>>> nltk.translate.bleu_score.__file__
'.../lib/python3.4/site-packages/nltk/translate/bleu_score.py'