Skip to main content

CodeBLEU

Publish Test codecov

Unofficial CodeBLEU implementation with Linux and MacOS supports available with PyPI and HF HUB.

Based on original CodeXGLUE/CodeBLEU code -- refactored, build for macos, tested and fixed multiple crutches to make it more usable.


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.

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, java 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:

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

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

Installation

Requires Python 3.8+

The metrics can be installed with pip and used as indicated above:

pip install codebleu

alternatively the metric is available as k4black/codebleu in evaluate (lib installation required):

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

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

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.1.4
File Size Uploaded
codebleu-0.1.4.tar.gz 7.9 MB Details

Built distributions (wheels)

Table of built distributions (wheels) for codebleu 0.1.4
File
codebleu-0.1.4-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.1.4-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl CPython 3.11 CPython 3.11 Linux glibc 2.17+ x86-32 Details
codebleu-0.1.4-cp311-cp311-macosx_10_9_x86_64.whl CPython 3.11 CPython 3.11 macOS 10.9+ x86-64 Details
codebleu-0.1.4-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.1.4-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl CPython 3.10 CPython 3.10 Linux glibc 2.17+ x86-32 Details
codebleu-0.1.4-cp310-cp310-macosx_10_9_x86_64.whl CPython 3.10 CPython 3.10 macOS 10.9+ x86-64 Details
codebleu-0.1.4-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.1.4-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl CPython 3.9 CPython 3.9 Linux glibc 2.17+ x86-32 Details
codebleu-0.1.4-cp39-cp39-macosx_10_9_x86_64.whl CPython 3.9 CPython 3.9 macOS 10.9+ x86-64 Details
codebleu-0.1.4-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.1.4-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl CPython 3.8 CPython 3.8 Linux glibc 2.17+ x86-32 Details
codebleu-0.1.4-cp38-cp38-macosx_10_9_x86_64.whl CPython 3.8 CPython 3.8 macOS 10.9+ x86-64 Details

Total release size: 20.4 MB

Release files / codebleu-0.1.4.tar.gz

Download URL codebleu-0.1.4.tar.gz
Size 7.9 MB
Tags Source
SHA-256 checksum
How to use checksums
93262c822a68899145d2e6abd9f38a32849cb3ea892c363645660baa602d2d4b
BLAKE2b-256 checksum
How to use checksums
9430d853fca37674242fdfeb86e5fcf5c4e460a97171253592b1df75ac0d6258
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.4

Release files / codebleu-0.1.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL codebleu-0.1.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.0 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
fb6689d021773309f947b8299c36e1b933efc0a92a0a0af62514130c390a7fd1
BLAKE2b-256 checksum
How to use checksums
48dbe0a629dc69b9a5871250d75f5796215b574a488b74f8ba371196ec856034
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.4

Release files / codebleu-0.1.4-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl

Download URL codebleu-0.1.4-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl
Size 1.0 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-32
SHA-256 checksum
How to use checksums
b595dcb0d86ed989f81e05c164affd08055518b7fc57a50353ff372f52fcfbd4
BLAKE2b-256 checksum
How to use checksums
13c014fc13802ccc2ac01b7654dce26a390ef6e853cd72ed2484b345895a1331
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.4

Release files / codebleu-0.1.4-cp311-cp311-macosx_10_9_x86_64.whl

Download URL codebleu-0.1.4-cp311-cp311-macosx_10_9_x86_64.whl
Size 1.1 MB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
bd488e215cda9fbfabb48ce0dba6353a6ab469480345131ef91f237f332d1cf5
BLAKE2b-256 checksum
How to use checksums
4e27ec993434a4261beaeb61bda0803805acf47c10a6158e71923d2b2f4f94bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.4

Release files / codebleu-0.1.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL codebleu-0.1.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.0 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
492c8ed49326f5b64d9feca368190bcab487e57559e60e061bd0fba9610f108f
BLAKE2b-256 checksum
How to use checksums
1e9dd45085ba11a4254ffee8be37ca0ed7314f0ba0dc7aaf15d6aa1d8fe102ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.4

Release files / codebleu-0.1.4-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl

Download URL codebleu-0.1.4-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl
Size 1.0 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-32
SHA-256 checksum
How to use checksums
9bdb650cb2d8811578e10a95724813c8ebd215a67183805701d4686a18a5c102
BLAKE2b-256 checksum
How to use checksums
d080203e2dba0699a2cc34d991ab89a493c331f1502f7d7999da90ae9e712ccd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.4

Release files / codebleu-0.1.4-cp310-cp310-macosx_10_9_x86_64.whl

Download URL codebleu-0.1.4-cp310-cp310-macosx_10_9_x86_64.whl
Size 1.1 MB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
a03d72c2cf13bd9b124e3a3ef8fafec8a2b6ce58f40066fcfd394f1cdb8065a8
BLAKE2b-256 checksum
How to use checksums
8412711de1a0cc0a272dd3acc85798c02618618e3af5f3972a17b81e78071601
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.4

Release files / codebleu-0.1.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL codebleu-0.1.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.0 MB
Tags CPython 3.9 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
b531712f702fa9316367507cb3d3040a5eebc226b8f6da3b1fb77352cf66fa8d
BLAKE2b-256 checksum
How to use checksums
901e76a6f92e0d6f87d2cd9f9e12510bae1c909eec9ef29b92c7ee12b4f9b6a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.4

Release files / codebleu-0.1.4-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl

Download URL codebleu-0.1.4-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl
Size 1.0 MB
Tags CPython 3.9 Linux glibc 2.17+ x86-32
SHA-256 checksum
How to use checksums
9a74a9e268fd39b8e9153dd2baaf6a4f72b3aecb70a2f42fa04ab8680263e79b
BLAKE2b-256 checksum
How to use checksums
e231f2d02e1ec82f00f442ab41fabda49973810379ffa42a2c03bffe700e8d4b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.4

Release files / codebleu-0.1.4-cp39-cp39-macosx_10_9_x86_64.whl

Download URL codebleu-0.1.4-cp39-cp39-macosx_10_9_x86_64.whl
Size 1.1 MB
Tags CPython 3.9 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
e2d68fb81157002730972af004ba570bb546f5dfa5597a4e26faa50edaa2e03f
BLAKE2b-256 checksum
How to use checksums
b5a4947f61ad6c04176274a4c48878c31f900e411bf150362dce3af1591441e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.4

Release files / codebleu-0.1.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL codebleu-0.1.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.0 MB
Tags CPython 3.8 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
f95c605672ea0b2899baee596df8b1736939536733bb35dd338ae1b1fb7c2083
BLAKE2b-256 checksum
How to use checksums
de906af0486b7fc3c9abaeb0273a00463c2a57fe59fb29c92ea02b042459bf0c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.4

Release files / codebleu-0.1.4-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl

Download URL codebleu-0.1.4-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl
Size 1.0 MB
Tags CPython 3.8 Linux glibc 2.17+ x86-32
SHA-256 checksum
How to use checksums
c67f3251407996948d317a0d5d473aa9b7e3d26ff4694bce54ffa6a444ec3e18
BLAKE2b-256 checksum
How to use checksums
b45f4b2f7cdb8f6692ef722733f18b9f938b863575731527e7f9df5efef91d8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.4

Release files / codebleu-0.1.4-cp38-cp38-macosx_10_9_x86_64.whl

Download URL codebleu-0.1.4-cp38-cp38-macosx_10_9_x86_64.whl
Size 1.1 MB
Tags CPython 3.8 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
7eb56870e903a8bb6c450ff66d40b8ad363d22e670034ced3fa13eef44322994
BLAKE2b-256 checksum
How to use checksums
fc0f3e99f5575aeb50bd30baec0295609d1825018a634b5e8108961a7abe9c8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 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

0.2.1

13 release files

0.2.0

13 release files

0.1.7

13 release files

0.1.5

13 release files

This release

0.1.4 This release

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