Skip to content

Commit

Permalink
Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
maximus12793 committed Sep 30, 2023
1 parent dda27ee commit c00ca2e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 17 deletions.
17 changes: 3 additions & 14 deletions codebleu/codebleu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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}
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion evaluate_app/codebleu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions tests/test_codebleu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
Expand Down

0 comments on commit c00ca2e

Please sign in to comment.