Skip to main content

CodeBLEU

Publish Test codecov PyPI version

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

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

Built distributions (wheels)

Table of built distributions (wheels) for codebleu 0.1.7
File
codebleu-0.1.7-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.7-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.7-cp311-cp311-macosx_10_9_x86_64.whl CPython 3.11 CPython 3.11 macOS 10.9+ x86-64 Details
codebleu-0.1.7-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.7-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.7-cp310-cp310-macosx_10_9_x86_64.whl CPython 3.10 CPython 3.10 macOS 10.9+ x86-64 Details
codebleu-0.1.7-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.7-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.7-cp39-cp39-macosx_10_9_x86_64.whl CPython 3.9 CPython 3.9 macOS 10.9+ x86-64 Details
codebleu-0.1.7-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.7-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.7-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.7.tar.gz

Download URL codebleu-0.1.7.tar.gz
Size 7.9 MB
Tags Source
SHA-256 checksum
How to use checksums
db6b11c0bc159f7e872f03f45e2cae9cf803aa49a98aad804521489bd488d328
BLAKE2b-256 checksum
How to use checksums
7437f892ce219dfccd3bf40e6017375e79e0d72544b31ea06364382c3be45504
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.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL codebleu-0.1.7-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
24ff7bf63736eb0e0e70d78ccd1a73d25e5a2a956c9c1b36289ca2e267e044dc
BLAKE2b-256 checksum
How to use checksums
cc04a0cc826bd74217ec05a04950bd931ea688766a2e40e09c57b3992883b6d7
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.7-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl

Download URL codebleu-0.1.7-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
5b76ad46d9f86c6c3dd62444c2a2372c8dabbc615418af28abfdf367c39ba8be
BLAKE2b-256 checksum
How to use checksums
dd8e98d51b6fae1db908f6d69dbd098c165f1fcee1ee427b4ef9c01480e05a87
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.7-cp311-cp311-macosx_10_9_x86_64.whl

Download URL codebleu-0.1.7-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
c25830bfe1680be0c5d7f0a5dc4c192024b68a55aae5b09c681104eb08e0734b
BLAKE2b-256 checksum
How to use checksums
1884555db719976d116adb47e2b95509350a2ecc1e7e83a59ce4a00b0315bfe1
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.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL codebleu-0.1.7-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
296cd70494b498e4f62d7fe5393d2987b99b8d5257ff44052869cd6a76daba4f
BLAKE2b-256 checksum
How to use checksums
84f571de688f5fe9422b205d7e3ce06c519f467dee3037900c59d086944b58e3
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.7-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl

Download URL codebleu-0.1.7-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
a3ae27f57f607179ae3dd1056a8a0991eef162e613f47f612ca1b599d9ef9dae
BLAKE2b-256 checksum
How to use checksums
a1e6a5514b229467da55c59c39a0eb01fda49d682c93c8b3651782876daf77b2
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.7-cp310-cp310-macosx_10_9_x86_64.whl

Download URL codebleu-0.1.7-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
510a38de66910e890922fc1fab055023fb7c95e6bbc3625410ebe9c98f5489e0
BLAKE2b-256 checksum
How to use checksums
a51ead9ae5806418b75ecfb0843d424d6335a33c4da8e986002d40ec07905e39
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.7-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL codebleu-0.1.7-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
c25cbcc9545dcb931875e8b7291a32178ac536bc7152b333b85b1bcf35c7256d
BLAKE2b-256 checksum
How to use checksums
92d6f638846125b5151312df3c860395ef4e28eab5f5326cca610a3d70facdb8
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.7-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl

Download URL codebleu-0.1.7-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
271d1ea8854da10e9d23264f90d7c052be829d1101d9be2bae301c2179a80a53
BLAKE2b-256 checksum
How to use checksums
44ac38a529206673a59d5259d9830994b8321a2077221ce598caaec4a4907b3f
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.7-cp39-cp39-macosx_10_9_x86_64.whl

Download URL codebleu-0.1.7-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
9b3092c6cb6da7b8dfc3c4b8d148a5f0bbaf4a822a7b60e2f3c468f5ef4c1056
BLAKE2b-256 checksum
How to use checksums
cfe2da04f0cce9bb9b768769b197da0384ddb1f6d89356f3bb8a5d55329d46be
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.7-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL codebleu-0.1.7-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
bee91cc3a60637631542b67efacb33d7a2f85979eac9d4a87343fc28b94c766f
BLAKE2b-256 checksum
How to use checksums
2d26cb2a45a1dcde4489da03f2590f5d3fb97fe851ec66b5d50b7c25d392d5d6
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.7-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl

Download URL codebleu-0.1.7-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
d9baeb8868ca4ad0f5b0b3655311517012a96bc109589f910947ddebac83d1e2
BLAKE2b-256 checksum
How to use checksums
edb7fd5f6583145b7df05e3426ebfc8186969b24b1251c06262f676b04907f72
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.7-cp38-cp38-macosx_10_9_x86_64.whl

Download URL codebleu-0.1.7-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
53ec66d76bc860e3752b2f357305664ddd0b46585d9febc280d9ec6727642455
BLAKE2b-256 checksum
How to use checksums
2021f170a0d801eb38d9768d2b501e599794df9390a92862f94c7878d60eb553
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

This release

0.1.7 This release

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