Skip to main content

Unofficial CodeBLEU implementation that supports Linux, MacOS and Windows available on PyPI.

Project description

CodeBLEU

Publish Test codecov PyPI version

This repository contains an unofficial CodeBLEU implementation that supports Linux, MacOS (incl. M-series) and Windows. It is available through PyPI and the evaluate library.

Available for: Python, C, C#, C++, Java, JavaScript, PHP, Go, Ruby, Rust, Ballerina.


The code is based on the original CodeXGLUE/CodeBLEU and updated version by XLCoST/CodeBLEU. It has been refactored, tested, built for macOS and Windows, and multiple improvements have been made to enhance usability.

Metric Description

An ideal evaluation metric should consider the grammatical correctness and the logic correctness. We propose weighted n-gram match and syntactic AST match to measure grammatical correctness, and introduce semantic data-flow match to calculate logic correctness. CodeBLEU
[from CodeXGLUE repo]

In a nutshell, CodeBLEU is a weighted combination of n-gram match (BLEU), weighted n-gram match (BLEU-weighted), AST match and data-flow match scores.

The metric has shown higher correlation with human evaluation than BLEU and accuracy metrics.

Installation

This library requires so file compilation with tree-sitter, so it is platform dependent.
Currently available for Linux (manylinux), MacOS and Windows with Python 3.8+.

The metrics is available as pip package and can be installed as indicated above:

pip install codebleu

or directly from git repo (require internet connection to download tree-sitter):

pip install git+https://github.com/k4black/codebleu.git

Also you have to install tree-sitter language you need (e.g. python, rust, etc):

pip install tree-sitter-python

Or you can install all languages:

pip install codebleu[all]

Note: At the moment (May 2024) precompiled languages are NOT available for arm64 (M1) MacOS, so you have to install and build tree-sitter languages manually, for example:

pip install pip install git+https://github.com/tree-sitter/tree-sitter-python.git

Usage

from codebleu import calc_codebleu

prediction = "def add ( a , b ) :\n return a + b"
reference = "def sum ( first , second ) :\n return second + first"

result = calc_codebleu([reference], [prediction], lang="python", weights=(0.25, 0.25, 0.25, 0.25), tokenizer=None)
print(result)
# {
#   'codebleu': 0.5537, 
#   'ngram_match_score': 0.1041, 
#   'weighted_ngram_match_score': 0.1109, 
#   'syntax_match_score': 1.0, 
#   'dataflow_match_score': 1.0
# }

where calc_codebleu takes the following arguments:

  • refarences (list[str] or list[list[str]]): reference code
  • predictions (list[str]) predicted code
  • lang (str): code language, see codebleu.AVAILABLE_LANGS for available languages (python, c_sharp c, cpp, javascript, java, php, go and ruby at the moment)
  • weights (tuple[float,float,float,float]): weights of the ngram_match, weighted_ngram_match, syntax_match, and dataflow_match respectively, defaults to (0.25, 0.25, 0.25, 0.25)
  • tokenizer (callable): to split code string to tokens, defaults to s.split()

and outputs the dict[str, float] with following fields:

  • codebleu: the final CodeBLEU score
  • ngram_match_score: ngram_match score (BLEU)
  • weighted_ngram_match_score: weighted_ngram_match score (BLEU-weighted)
  • syntax_match_score: syntax_match score (AST match)
  • dataflow_match_score: dataflow_match score

Alternatively, you can use k4black/codebleu from HuggingFace Spaces (codebleu package required):

import evaluate
metric = evaluate.load("dvitel/codebleu")

prediction = "def add ( a , b ) :\n return a + b"
reference = "def sum ( first , second ) :\n return second + first"

result = metric.compute([reference], [prediction], lang="python", weights=(0.25, 0.25, 0.25, 0.25))

Feel free to check the HF Space with online example: k4black/codebleu

Contributing

Contributions are welcome!
If you have any questions, suggestions, or bug reports, please open an issue on GitHub.

Make your own fork and clone it:

git clone https://github.com/k4black/codebleu

For development, you need to install library with all precompiled languages and test extra:
(require internet connection to download tree-sitter)

python -m pip install -e .[all,test]
python -m pip install -e .\[all,test\]  # for macos

For testing just run pytest:

python -m pytest

To perform a style check, run:

python -m isort codebleu --check
python -m black codebleu --check
python -m ruff codebleu
python -m mypy codebleu

License

This project is licensed under the terms of the MIT license.

Citation

Official CodeBLEU paper can be cited as follows:

@misc{ren2020codebleu,
      title={CodeBLEU: a Method for Automatic Evaluation of Code Synthesis}, 
      author={Shuo Ren and Daya Guo and Shuai Lu and Long Zhou and Shujie Liu and Duyu Tang and Neel Sundaresan and Ming Zhou and Ambrosio Blanco and Shuai Ma},
      year={2020},
      eprint={2009.10297},
      archivePrefix={arXiv},
      primaryClass={cs.SE}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ballerina_platform_codebleu-0.7.1.tar.gz (183.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ballerina_platform_codebleu-0.7.1-py3-none-any.whl (33.5 kB view details)

Uploaded Python 3

File details

Details for the file ballerina_platform_codebleu-0.7.1.tar.gz.

File metadata

File hashes

Hashes for ballerina_platform_codebleu-0.7.1.tar.gz
Algorithm Hash digest
SHA256 351232ca5b030a31043e180cdee179ab683cde54c3d79070f06e9de9801f10cd
MD5 808e6659ac0f098183fca16126c1e784
BLAKE2b-256 1edf6d22e863267aa86279d5e270e02fec7ef3c32d57c878ae470143ae772861

See more details on using hashes here.

File details

Details for the file ballerina_platform_codebleu-0.7.1-py3-none-any.whl.

File metadata

File hashes

Hashes for ballerina_platform_codebleu-0.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0bddfe38509a1f986bb20897fd71e409e6deb22aeacf4f58ddfa5a2aed5a6d9c
MD5 8c03ec70a32c406fe4589df5e481bcd3
BLAKE2b-256 d28a090271e35fd32819f8e67954ccd19b53293253b54ae7d47c6a8f12064d2c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page