2016-11-11 4 views
7

मैंने उबंटू पर बीएलईयू स्कोर की गणना करने के लिए पायथन में nltk आयात किया है। मैं समझता हूं कि वाक्य-स्तर बीएलईयू स्कोर कैसे काम करता है, लेकिन मुझे समझ में नहीं आता कि कॉर्पस-स्तर बीएलईयू स्कोर कैसे काम करता है।एनएलटीके: कॉर्पस-लेवल ब्ली बनाम वाक्य-स्तर बीएलईयू स्कोर

नीचे कोष स्तरीय 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) 

किसी कारण से, ब्लू स्कोर उपरोक्त कोड के लिए 0 है। मैं कम से कम 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 है जो मैं उम्मीद, खाते में संक्षिप्तता-दंड और लापता शब्द "एक" ले रहा है। हालांकि, मुझे समझ में नहीं आता कि कॉर्पस-स्तर बीएलईयू स्कोर कैसे काम करता है।

किसी भी मदद की सराहना की जाएगी।

+0

उम्मीद है कि दस्तावेज़ीकरण और कार्य कोड में अनुमान/unittest आपको मदद करता है। लेकिन कृपया बीएलईयू के स्थाई संस्करण के लिए 'nltk' का नवीनतम संस्करण खींचें। असल में, आप फ़ंक्शन का उपयोग कैसे कर रहे हैं वास्तव में सही नहीं है, उत्तर में समझा जाएगा =) – alvas

+0

विकिपीडिया से, इस प्रश्नोत्तरी की पंक्ति में रुचि हो सकती है। 'बीएलईयू को मानव निर्णय को कॉर्पस स्तर पर अनुमानित करने के लिए डिज़ाइन किया गया है, और व्यक्तिगत वाक्य की गुणवत्ता का मूल्यांकन करने के लिए उपयोग किए जाने पर बुरी तरह प्रदर्शन करता है।' शायद पूछताछ की रेखा मीट्रिक के लिए प्रासंगिक नहीं है। –

उत्तर

11

टी एल; डॉ:

>>> 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 
+0

ऊपरी अक्षरों और कम अक्षरों के बारे में – tktktk0711

+0

देखें https://stackoverflow.com/questions/5541745/get-rid-of-stopwords-and-punctuation – alvas

+0

इसे अपने लिए आज़माएं; पी भी, ध्यान दें कि बीएलईयू था वाक्य स्तर गणना करने के लिए कभी नहीं था। यह एक कॉर्पस स्तर मीट्रिक है। – alvas

3

की एक नज़र डालते हैं:

>>> 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). 
    ... 

आप एक बेहतर स्थिति की तुलना में मेरे एल्गोरिथ्म के विवरण को समझने के लिए कर रहे हैं, इसलिए मैं आप के लिए यह "समझाने" करने की कोशिश नहीं करेंगे। यदि डॉकस्ट्रिंग पर्याप्त चीजों को साफ़ नहीं करता है, तो the source पर एक नज़र डालें। या यह स्थानीय स्तर पर लगता है:

>>> nltk.translate.bleu_score.__file__ 
'.../lib/python3.4/site-packages/nltk/translate/bleu_score.py' 
+0

बहुत अच्छा, स्रोत कोड को देखा जाना चाहिए था। मुझे बहुत मदद मिली –

संबंधित मुद्दे