File size: 3,272 Bytes
4ed10db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import json, itertools, pyter
from nltk.translate.bleu_score import SmoothingFunction, corpus_bleu


class NLPFactGenerator:
    def __init__(self):
        self.gen_fact_list = [] 
        self.evidence_list = []

    def _split_into_words(self, sentences):
        return list(itertools.chain(*[_.split(" ") for _ in sentences]))
    
    def _get_word_ngrams(self, n, sentences):
        assert len(sentences) > 0
        assert n > 0
        words = self._split_into_words(sentences)
        return self._get_ngrams(n, words)

    def _get_ngrams(self, n, text):
        ngram_set = set()
        text_length = len(text)
        max_index_ngram_start = text_length - n
        for i in range(max_index_ngram_start + 1):
            ngram_set.add(tuple(text[i:i + n]))
        return ngram_set
    
    def load_data(self, filename):
        with open(filename, "r") as infile:
            self.data = json.load(infile)
    
    def get_title_evidence_generated_facts(self):
        titles = []
        evidences = []
        generated_facts = []

        for entry in self.data:
            titles.append(entry["title"])
            evidences.append(entry["evidence"])
            generated_facts.append(entry["generated_fact"])

        return evidences, generated_facts
    
    def ter(self):
        ref, gen = self.get_title_evidence_generated_facts()
        if len(ref) == 1:
            total_score =  pyter.ter(gen[0].split(), ref[0].split())
        else:
            total_score = 0
            for i in range(len(gen)):
                total_score = total_score + pyter.ter(gen[i].split(), ref[i].split())
            total_score = total_score/len(gen)
        return total_score  

    def bleu(self):
        evidence_list, gen_fact_list = self.get_title_evidence_generated_facts()
        ref_bleu = []
        gen_bleu = []
        for l in evidence_list:
            gen_bleu.append(l.split())
        for i,l in enumerate(gen_fact_list):
            ref_bleu.append([l.split()])
        cc = SmoothingFunction()
        score_bleu = corpus_bleu(ref_bleu, gen_bleu, weights=(0, 1, 0, 0), smoothing_function=cc.method4)
        return score_bleu
    
    def rouge_one(self,n=3):
        evidence_list, gen_fact_list = self.get_title_evidence_generated_facts()
        evaluated_ngrams = self._get_word_ngrams(n, evidence_list)
        reference_ngrams = self._get_word_ngrams(n, gen_fact_list)
        reference_count = len(reference_ngrams)
        evaluated_count = len(evaluated_ngrams)
        overlapping_ngrams = evaluated_ngrams.intersection(reference_ngrams)
        overlapping_count = len(overlapping_ngrams)
        if evaluated_count == 0:
            precision = 0.0
        else:
            precision = overlapping_count / evaluated_count

        if reference_count == 0:
            recall = 0.0
        else:
            recall = overlapping_count / reference_count

        f1_score = 2.0 * ((precision * recall) / (precision + recall + 1e-8))
        return recall


if __name__ == "__main__":
    fact_generator = NLPFactGenerator()
    fact_generator.load_data("generated_facts_xlsum.json")
    rouge_one_score = fact_generator.rouge_one()
    blue_score = fact_generator.bleu()
    print(blue_score)
    print(rouge_one_score)