Skip to main content

CodeBLEU

Publish Test codecov PyPI version

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. 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

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] 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 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.

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.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for codebleu 0.2.1
File Size Uploaded
codebleu-0.2.1.tar.gz 10.1 MB Details

Built distributions (wheels)

Table of built distributions (wheels) for codebleu 0.2.1
File
codebleu-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.17+ x86-64 Details
codebleu-0.2.1-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl CPython 3.11 CPython 3.11 Linux glibc 2.17+ x86-32 Details
codebleu-0.2.1-cp311-cp311-macosx_10_9_x86_64.whl CPython 3.11 CPython 3.11 macOS 10.9+ x86-64 Details
codebleu-0.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.17+ x86-64 Details
codebleu-0.2.1-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl CPython 3.10 CPython 3.10 Linux glibc 2.17+ x86-32 Details
codebleu-0.2.1-cp310-cp310-macosx_10_9_x86_64.whl CPython 3.10 CPython 3.10 macOS 10.9+ x86-64 Details
codebleu-0.2.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.17+ x86-64 Details
codebleu-0.2.1-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl CPython 3.9 CPython 3.9 Linux glibc 2.17+ x86-32 Details
codebleu-0.2.1-cp39-cp39-macosx_10_9_x86_64.whl CPython 3.9 CPython 3.9 macOS 10.9+ x86-64 Details
codebleu-0.2.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.17+ x86-64 Details
codebleu-0.2.1-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl CPython 3.8 CPython 3.8 Linux glibc 2.17+ x86-32 Details
codebleu-0.2.1-cp38-cp38-macosx_10_9_x86_64.whl CPython 3.8 CPython 3.8 macOS 10.9+ x86-64 Details

Total release size: 26.0 MB

Release files / codebleu-0.2.1.tar.gz

Download URL codebleu-0.2.1.tar.gz
Size 10.1 MB
Tags Source
SHA-256 checksum
How to use checksums
0e3027d678dc78257be265a6c69603919d535911c19f0dab3173d5ef24154c63
BLAKE2b-256 checksum
How to use checksums
83143fc23f62be19d12c9bb4d123a037c7bc0428dcd3e1739e40e8df86933c8a
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.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL codebleu-0.2.1-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
65789c1734ae65b25671879db55ce237522fbeefde47876d3d7228ab2cf4a97e
BLAKE2b-256 checksum
How to use checksums
e4a407f67c483fc674c9f9e5135fde8aa8df9f72c707f1d8fa9321e33a07f3f9
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.1-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl

Download URL codebleu-0.2.1-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
98b1e321cb8a57531d6af2541e507918a5ff6cabd4e29409dfc80b22be5d9ca2
BLAKE2b-256 checksum
How to use checksums
33ad1a1d9e82bacb8ef7e76100e980f942ec7593a2c42ff2e2c126423e9c715f
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.1-cp311-cp311-macosx_10_9_x86_64.whl

Download URL codebleu-0.2.1-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
4a6094853393d4630032c9cdd279f9e31bf244c434157c9b078f202a3f64d7f4
BLAKE2b-256 checksum
How to use checksums
32feef5bcf3830e44bc80a6f2c0cfcf67503a52790c37f764dfee3a627bdf817
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.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL codebleu-0.2.1-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
940f6798d2d7b986de415250ac6728f15bdb7523c6c6d9fd8bb6f3339e754a0a
BLAKE2b-256 checksum
How to use checksums
ad9b2cedfb557ac68704a82d2e0b7c7461ae16f81eae8c6f037cfa2da90eb79a
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.1-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl

Download URL codebleu-0.2.1-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
303e80457a9bd916bd38da04d379e8e6c7d2a8fea4372db6a86114e19e0467f4
BLAKE2b-256 checksum
How to use checksums
0f468c326831b9b74cfb6d837a82c144f6833cf7eba3c3cd1f91eb45a9d1ae50
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.1-cp310-cp310-macosx_10_9_x86_64.whl

Download URL codebleu-0.2.1-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
48a23d8f4469f183bb291314aa6373ef4bfb8daf5268d94960e716159166214b
BLAKE2b-256 checksum
How to use checksums
08fb2d86e7ece1acb86ccbbcfcb9c50317be77ed05970e349243bd67b4e99748
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.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL codebleu-0.2.1-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
b3fa602865609e1ea6689ca6c5b2517d889314a292e0593ba1fce2102af737f0
BLAKE2b-256 checksum
How to use checksums
6be4bbd3b2fc0834f7be032f868287f230d907ff68fa848631578a91595fd7b1
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.1-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl

Download URL codebleu-0.2.1-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
a865ca2ccfd1e4ae3d164de7b6fb91e6ec7ef1d76459712e34a6b9a7fed9a519
BLAKE2b-256 checksum
How to use checksums
c18a80f005181899deb74a838434aab911025febf043e5bd132ba4db5170a41e
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.1-cp39-cp39-macosx_10_9_x86_64.whl

Download URL codebleu-0.2.1-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
3f1c43ac396e5e9be658c623410ebe14b55021ee1fc7533c7f52280cb4692e6a
BLAKE2b-256 checksum
How to use checksums
7ba02da8389186340af550858a4a969a7bef7cb15f446d84d6f070e080e3a88c
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.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL codebleu-0.2.1-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
4eaf01a3a7a530a9ecb26db9aa41fdc1eecf562c21bd8514184c026c545e1e5a
BLAKE2b-256 checksum
How to use checksums
f3a42e34d90f3a95c9b067a457e678aeb03d1f7ca34df8d3af4faf7f0a7f486e
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.1-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl

Download URL codebleu-0.2.1-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
eb11e655bada4f69652e1f46c2739b04b5475b78994482b07b38ae6835f606b5
BLAKE2b-256 checksum
How to use checksums
e398d12ece1109d72774d94ce526b110015498d0438ee716aa9d7122ba32e589
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.1-cp38-cp38-macosx_10_9_x86_64.whl

Download URL codebleu-0.2.1-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
bd4b5062f6416ae59cdd8af6da1f90608896d5536f7851d148c354402d066ed9
BLAKE2b-256 checksum
How to use checksums
58a8d4caa0cc6105981c768f1f673254838287d72a96ad2974688850944f84fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release history Release notifications | RSS feed

0.7.0

2 release files

0.6.1

1 release file

0.6.0

1 release file

0.5.0

1 release file

0.4.0

26 release files

This release

0.2.1 This release

13 release files

0.2.0

13 release files

0.1.7

13 release files

0.1.5

13 release files

0.1.4

13 release files

0.1.2

13 release files

0.0.2

14 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page