CodeBLEU
This repository contains an unofficial CodeBLEU implementation that supports Linux and MacOS. It is available through PyPI and the evaluate library.
The code is based on the original CodeXGLUE/CodeBLEU and updated version by XLCoST/CodeBLEU. It has been refactored, tested, built for macOS, and multiple improvements have been made to enhance usability
Available for: Python, C, C#, C++, Java, JavaScript, PHP.
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.
(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
As this library require so file compilation it is platform dependent.
Currently available for Linux (manylinux) and MacOS 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:
pip install git+https://github.com/k4black/codebleu.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]orlist[list[str]]): reference codepredictions(list[str]) predicted codelang(str): code language, seecodebleu.AVAILABLE_LANGSfor available languages (python, c_sharp c, cpp, javascript, java, php at the moment)weights(tuple[float,float,float,float]): weights of thengram_match,weighted_ngram_match,syntax_match, anddataflow_matchrespectively, defaults to(0.25, 0.25, 0.25, 0.25)tokenizer(callable): to split code string to tokens, defaults tos.split()
and outputs the dict[str, float] with following fields:
codebleu: the finalCodeBLEUscorengram_match_score:ngram_matchscore (BLEU)weighted_ngram_match_score:weighted_ngram_matchscore (BLEU-weighted)syntax_match_score:syntax_matchscore (AST match)dataflow_match_score:dataflow_matchscore
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.
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}
}
Metadata
Release files for codebleu 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| codebleu-0.2.0.tar.gz | 10.1 MB | Details |
Built distributions (wheels)
Total release size: 25.9 MB
Release files / codebleu-0.2.0.tar.gz
| Download URL | codebleu-0.2.0.tar.gz |
|---|---|
| Size | 10.1 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9559ec926a799f5786a214cd4eeee3525cef3eb3bb434e34adf4bf4c154635ed
|
|
BLAKE2b-256 checksum How to use checksums |
ca1916b377115cb2e414db391902ee42fd5fb7d79fe99ec7a33f2a60a704c11f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Release files / codebleu-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | codebleu-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 1.3 MB |
| Tags | CPython 3.11 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
7b2229030540bec4ea63f809e1168e6434397e371934d4499fc82185e5c59e18
|
|
BLAKE2b-256 checksum How to use checksums |
2b6fc2796b867172046543148580677b0f960cbd85f7eac3c066e53950a1fb1e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Release files / codebleu-0.2.0-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl
| Download URL | codebleu-0.2.0-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl |
|---|---|
| Size | 1.3 MB |
| Tags | CPython 3.11 Linux glibc 2.17+ x86-32 |
|
SHA-256 checksum How to use checksums |
f2ddc9b8cd745073449c574acd311afa87b2237604f20f10f179e6adac6d4600
|
|
BLAKE2b-256 checksum How to use checksums |
56e4bd8fe6ff81d0146c1270be80c9a1f43198219ec1978a654896c60c3e52f7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Release files / codebleu-0.2.0-cp311-cp311-macosx_10_9_x86_64.whl
| Download URL | codebleu-0.2.0-cp311-cp311-macosx_10_9_x86_64.whl |
|---|---|
| Size | 1.4 MB |
| Tags | CPython 3.11 macOS 10.9+ x86-64 |
|
SHA-256 checksum How to use checksums |
98e18a1c0658cfff90a7a8d65e5146c0dce2b612ac3c9c228c2197ab4150450a
|
|
BLAKE2b-256 checksum How to use checksums |
807e467632c61097aaa892728f14478508c7ca47e7951cff30d30cb9b5a55c35
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Release files / codebleu-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | codebleu-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 1.3 MB |
| Tags | CPython 3.10 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
0201c7b196cd4f341b3c129cdbb244119735042bfa11fcd57bbe62cf1b31f349
|
|
BLAKE2b-256 checksum How to use checksums |
6dc4370b1fa3388186220bbdcdcdf07ecd5adeef6d10c86f3df825762873c65e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Release files / codebleu-0.2.0-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl
| Download URL | codebleu-0.2.0-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl |
|---|---|
| Size | 1.3 MB |
| Tags | CPython 3.10 Linux glibc 2.17+ x86-32 |
|
SHA-256 checksum How to use checksums |
2e9e457735aeb49f8c45ea7b1832aa9c1b192aa016317dc4748bd7749fc132f0
|
|
BLAKE2b-256 checksum How to use checksums |
159e9c541b9d108eb93ede0eaf4a0a61d025030135cbadaa8d3513790365616c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Release files / codebleu-0.2.0-cp310-cp310-macosx_10_9_x86_64.whl
| Download URL | codebleu-0.2.0-cp310-cp310-macosx_10_9_x86_64.whl |
|---|---|
| Size | 1.4 MB |
| Tags | CPython 3.10 macOS 10.9+ x86-64 |
|
SHA-256 checksum How to use checksums |
bb72c03b41d5a67cecff9c5ec9191727d7f2a96fa8b88550cc834cb72ec6269c
|
|
BLAKE2b-256 checksum How to use checksums |
4c7fa5157e638a227a68812759002b71526837c9be7bf96b452b2e6ced964e3d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Release files / codebleu-0.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | codebleu-0.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 1.3 MB |
| Tags | CPython 3.9 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
22318577637a9e64a62baa64d8611a0cbe7e64d79ab0eff4ddd2e94e5c4305c3
|
|
BLAKE2b-256 checksum How to use checksums |
dad75129c19b4bc4c8a8aa2efaa10dd5152c3060930f1e420ead6d79120a0226
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Release files / codebleu-0.2.0-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl
| Download URL | codebleu-0.2.0-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl |
|---|---|
| Size | 1.3 MB |
| Tags | CPython 3.9 Linux glibc 2.17+ x86-32 |
|
SHA-256 checksum How to use checksums |
2f2e3976001ac7c8befdf4dda87906b8d28a29666b5523cf26004f1977b9256c
|
|
BLAKE2b-256 checksum How to use checksums |
238f8623935e0b82a4ffa0aba5b245b0c927aeeaa26389cdc08eb89d1e730dbe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Release files / codebleu-0.2.0-cp39-cp39-macosx_10_9_x86_64.whl
| Download URL | codebleu-0.2.0-cp39-cp39-macosx_10_9_x86_64.whl |
|---|---|
| Size | 1.4 MB |
| Tags | CPython 3.9 macOS 10.9+ x86-64 |
|
SHA-256 checksum How to use checksums |
8655f0fd2515f364a112abb5c177812e78233522d9a97ae10ba07ae4ab0f941a
|
|
BLAKE2b-256 checksum How to use checksums |
89ee52196434297de2bee8f8c8effed09c6926bf19c7ff1f9f7766ffcd47b055
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Release files / codebleu-0.2.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | codebleu-0.2.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 1.3 MB |
| Tags | CPython 3.8 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
3c6ede2b1c968d421becb34d500e7198ca9d74c819793f8708263e78717f509f
|
|
BLAKE2b-256 checksum How to use checksums |
b8a116efc2a84784ed16cd4b8b5b482743efdad7b8fca359dbd61d0c759fe8f2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Release files / codebleu-0.2.0-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl
| Download URL | codebleu-0.2.0-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl |
|---|---|
| Size | 1.3 MB |
| Tags | CPython 3.8 Linux glibc 2.17+ x86-32 |
|
SHA-256 checksum How to use checksums |
20ec2f93b184565e6e4a3dde61267bcd5155caa9962d47c6db56c2fae32980c6
|
|
BLAKE2b-256 checksum How to use checksums |
72616179c5500e3502b5987ac035ef2dc8933b5979d1db7b42a0feb561c381d4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Release files / codebleu-0.2.0-cp38-cp38-macosx_10_9_x86_64.whl
| Download URL | codebleu-0.2.0-cp38-cp38-macosx_10_9_x86_64.whl |
|---|---|
| Size | 1.4 MB |
| Tags | CPython 3.8 macOS 10.9+ x86-64 |
|
SHA-256 checksum How to use checksums |
86671f832a32e4f3380fd201965650532d99081059f933c51448ead69bc96a97
|
|
BLAKE2b-256 checksum How to use checksums |
4e808847314eb868e4fda73b996d2a2c5fd29f7ecec92153c729250d1a5552f0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|