टी एल; डॉ:
>>> 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
(ध्यान दें: आप आदेश की एक स्थिर संस्करण प्राप्त करने के लिए develop
शाखा पर NLTK के नवीनतम संस्करण को निकालने के लिए BLEU स्कोर कार्यान्वयन)
लांग में:
दरअसल, यदि आपके पूरे कॉर्पस में केवल एक संदर्भ और एक परिकल्पना है, तो corpus_bleu()
और sentence_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
के लिए मानकों को देखो:
कोड में, हम है कि sentence_bleu
is actually a duck-type of corpus_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 पैरामीटर की बात आती है, यह मूल रूप से a list of whatever the sentence_bleu()
takes as references है:
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
के भीतर सबसे व्यस्त समय पर, के भीतर प्रत्येक घटक का उपयोग करने के तरीके को देखने के लिए आप nltk/test/unit/translate/test_bleu_score.py
पर सबसे अचूक रूप से देख सकते हैं।
वैसे, sentence_bleu
के बाद से, (nltk.translate.__init__.py
में bleu
] (https://github.com/nltk/nltk/blob/develop/nltk/translate/init.py#L21) के रूप में आयात किया जाता है
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
उम्मीद है कि दस्तावेज़ीकरण और कार्य कोड में अनुमान/unittest आपको मदद करता है। लेकिन कृपया बीएलईयू के स्थाई संस्करण के लिए 'nltk' का नवीनतम संस्करण खींचें। असल में, आप फ़ंक्शन का उपयोग कैसे कर रहे हैं वास्तव में सही नहीं है, उत्तर में समझा जाएगा =) – alvas
विकिपीडिया से, इस प्रश्नोत्तरी की पंक्ति में रुचि हो सकती है। 'बीएलईयू को मानव निर्णय को कॉर्पस स्तर पर अनुमानित करने के लिए डिज़ाइन किया गया है, और व्यक्तिगत वाक्य की गुणवत्ता का मूल्यांकन करने के लिए उपयोग किए जाने पर बुरी तरह प्रदर्शन करता है।' शायद पूछताछ की रेखा मीट्रिक के लिए प्रासंगिक नहीं है। –