Calculate BLEU scores with different n-gram settings.
(prediction: str, reference: str)
| 48 | } |
| 49 | |
| 50 | def calculate_bleu_scores(prediction: str, reference: str) -> Dict[str, float]: |
| 51 | """Calculate BLEU scores with different n-gram settings.""" |
| 52 | pred_tokens = nltk.word_tokenize(prediction.lower()) |
| 53 | ref_tokens = [nltk.word_tokenize(reference.lower())] |
| 54 | |
| 55 | weights_list = [(1, 0, 0, 0), (0.5, 0.5, 0, 0), (0.33, 0.33, 0.33, 0), (0.25, 0.25, 0.25, 0.25)] |
| 56 | smooth = SmoothingFunction().method1 |
| 57 | |
| 58 | scores = {} |
| 59 | for n, weights in enumerate(weights_list, start=1): |
| 60 | try: |
| 61 | score = sentence_bleu(ref_tokens, pred_tokens, weights=weights, smoothing_function=smooth) |
| 62 | except Exception: |
| 63 | score = 0.0 |
| 64 | scores[f'bleu{n}'] = score |
| 65 | |
| 66 | return scores |
| 67 | |
| 68 | def calculate_bert_scores(prediction: str, reference: str) -> Dict[str, float]: |
| 69 | """Calculate BERTScore for semantic similarity.""" |