From c00ca2e96d56dce4bf38464302df32adb0e85470 Mon Sep 17 00:00:00 2001 From: "Maximilian R." Date: Sat, 30 Sep 2023 17:40:39 -0500 Subject: [PATCH] Clean up code --- codebleu/codebleu.py | 17 +++-------------- evaluate_app/codebleu.py | 2 +- tests/test_codebleu.py | 5 +++-- 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/codebleu/codebleu.py b/codebleu/codebleu.py index 76aa6b3..b35e597 100644 --- a/codebleu/codebleu.py +++ b/codebleu/codebleu.py @@ -7,7 +7,6 @@ from . import bleu, dataflow_match, syntax_match, weighted_ngram_match PACKAGE_DIR = Path(__file__).parent -# AVAILABLE_LANGS = ['java', 'javascript', 'c_sharp', 'php', 'go', 'python', 'ruby'] AVAILABLE_LANGS = ["java", "javascript", "c_sharp", "php", "c", "cpp", "python"] # keywords available @@ -56,7 +55,8 @@ def tokenizer(s): ngram_match_score = bleu.corpus_bleu(tokenized_refs, tokenized_hyps) # calculate weighted ngram match - keywords = [x.strip() for x in open(keywords_dir / (lang + ".txt"), "r", encoding="utf-8").readlines()] + with open(keywords_dir / (lang + ".txt"), "r", encoding="utf-8") as f: + keywords = [x.strip() for x in f.readlines()] def make_weights(reference_tokens, key_word_list): return {token: 1 if token in key_word_list else 0.2 for token in reference_tokens} @@ -74,25 +74,14 @@ def make_weights(reference_tokens, key_word_list): # calculate dataflow match dataflow_match_score = dataflow_match.corpus_dataflow_match(references, hypothesis, lang, lang_so_file) - # print( - # "ngram match: {0}, weighted ngram match: {1}, syntax_match: {2}, dataflow_match: {3}".format( - # ngram_match_score, - # weighted_ngram_match_score, - # syntax_match_score, - # dataflow_match_score, - # ) - # ) - alpha, beta, gamma, theta = weights code_bleu_score = ( alpha * ngram_match_score + beta * weighted_ngram_match_score + gamma * syntax_match_score - + theta * (dataflow_match_score or 1) + + theta * (dataflow_match_score or 0) ) - # print("CodeBLEU score: ", code_bleu_score) - return { "codebleu": code_bleu_score, "ngram_match_score": ngram_match_score, diff --git a/evaluate_app/codebleu.py b/evaluate_app/codebleu.py index dd86973..bb365ba 100644 --- a/evaluate_app/codebleu.py +++ b/evaluate_app/codebleu.py @@ -106,7 +106,7 @@ def _info(self): def _download_and_prepare(self, dl_manager): """Optional: download external resources useful to compute the scores""" # workarounds as this file have to be named codebleu (evaluate library requirement) - self.codebleu_package = importlib.import_module('codebleu') + self.codebleu_package = importlib.import_module("codebleu") pass def _compute(self, predictions, references, lang, weights=(0.25, 0.25, 0.25, 0.25), tokenizer=None): diff --git a/tests/test_codebleu.py b/tests/test_codebleu.py index 1102415..ec016cd 100644 --- a/tests/test_codebleu.py +++ b/tests/test_codebleu.py @@ -7,7 +7,8 @@ @pytest.mark.parametrize(['predictions', 'references', 'codebleu'], [ - (['some rannnndom words in length more than 3'], ['def test ( ) :\n pass'], 0.25), # 'cause data_flow is 0 and considered as 1 + (['some rannnndom words in length more than 3'], + ['def test ( ) :\n pass'], 0.25), # 'cause data_flow is 0 and considered as 1 (['def bar ( y , x ) :\n a = x * x\n return a'], ['def foo ( x ) :\n return x'], 0.4), (['def foo ( x ) :\n return x * x'], ['def bar ( x ) :\n return x'], 0.6), (['def bar ( x ) :\n return x'], ['def foo ( x ) :\n return x'], 0.8), @@ -19,7 +20,7 @@ def test_simple_cases(predictions: List[Any], references: List[Any], codebleu: f assert result['codebleu'] == pytest.approx(codebleu, 0.1) -@pytest.mark.parametrize(['lang'], [(l,) for l in AVAILABLE_LANGS]) +@pytest.mark.parametrize(['lang'], [(lang,) for lang in AVAILABLE_LANGS]) def test_exact_match_works_for_all_langs(lang: str) -> None: predictions = references = ['some matching string a couple of times'] assert calc_codebleu(references, predictions, lang)['codebleu'] == 1.0