11from dataclasses import dataclass
22from bs4 import BeautifulSoup
33from pathlib import Path
4- from typing import Dict , List
4+ from typing import Dict , List , Callable
55from collections import OrderedDict
66from sacrebleu import corpus_bleu
77
1010
1111
1212class TranslationMetrics :
13- def __init__ (self , source_dataset_path : Path , target_dataset_path ):
13+ def __init__ (self ,
14+ source_dataset_path : Path ,
15+ target_dataset_path : Path ,
16+ tokenization : Callable [[str ], str ] = None ):
1417 self ._src_dataset_path = source_dataset_path
1518 self ._dst_dataset_path = target_dataset_path
1619 self .answers = {}
1720 self .source_documents , self .source_segments = self ._load_dataset (self ._src_dataset_path )
1821 self ._target_documents , self ._target_segments = self ._load_dataset (self ._dst_dataset_path )
22+ self ._tokenization = tokenization
1923 self ._results = None
2024
2125 def _load_dataset (self , dataset_path ):
@@ -41,12 +45,16 @@ def evaluate(self, ignore_missing=False):
4145 target_segments = {sid : text for sid , text in self ._target_segments .items () if sid in keep }
4246 else :
4347 target_segments = self ._target_segments
44- references = [[target for target in target_segments .values ()]]
4548 answers = [self .answers .get (sid , "" ) for sid in target_segments ]
46- bleu = corpus_bleu (answers , references )
47- self ._results = {
48- 'BLEU score' : bleu .score
49- }
49+ references = [target for target in target_segments .values ()]
50+ bleu = corpus_bleu (answers , [references ])
51+ self ._results = {'SacreBLEU' : bleu .score }
52+
53+ if self ._tokenization is not None :
54+ tok_answers = [self ._tokenization (answer ) for answer in answers ]
55+ tok_references = [self ._tokenization (target ) for target in references ]
56+ tok_bleu = corpus_bleu (tok_answers , [tok_references ], tokenize = 'none' , force = True )
57+ self ._results ['BLEU score' ] = tok_bleu .score
5058
5159 @property
5260 def has_data (self ):
0 commit comments