Skip to main content

Unofficial pip/hf compatible `CodeBLEU` implementation

Project description

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

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

codebleu-0.1.5.tar.gz (7.9 MB view details)

Uploaded Source

Built Distributions

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

codebleu-0.1.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

codebleu-0.1.5-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl (1.0 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ i686

codebleu-0.1.5-cp311-cp311-macosx_10_9_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

codebleu-0.1.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

codebleu-0.1.5-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl (1.0 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ i686

codebleu-0.1.5-cp310-cp310-macosx_10_9_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

codebleu-0.1.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

codebleu-0.1.5-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl (1.0 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ i686

codebleu-0.1.5-cp39-cp39-macosx_10_9_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.9macOS 10.9+ x86-64

codebleu-0.1.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

codebleu-0.1.5-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl (1.0 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ i686

codebleu-0.1.5-cp38-cp38-macosx_10_9_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

File details

Details for the file codebleu-0.1.5.tar.gz.

File metadata

  • Download URL: codebleu-0.1.5.tar.gz
  • Upload date:
  • Size: 7.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.4

File hashes

Hashes for codebleu-0.1.5.tar.gz
Algorithm Hash digest
SHA256 94815cd5804e83aedbd67d34632fe7d8355d0b8f66eca7f0cebb3a8eb6e41f0c
MD5 fef826632ba66e8dbd81973055418ffb
BLAKE2b-256 d5fee451375234b317887e442535d2b501cff4fd72b1c935136ca157d9b542fe

See more details on using hashes here.

File details

Details for the file codebleu-0.1.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for codebleu-0.1.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 dfc5b50f6f3149780a32b9bfcdf8e0e347f94eea776c442103bbea53948ffe63
MD5 4ddf724d280f9ae226cad0f14d776a81
BLAKE2b-256 c032affa806fd2337bb301a4ff05a93d869b47c048a56bbbfbf48201e4037c0a

See more details on using hashes here.

File details

Details for the file codebleu-0.1.5-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for codebleu-0.1.5-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 0f6834e912ae9cce5a4d2a3a268e1354a74ff720a2f648355681097aa812fcaf
MD5 e314d04e6d4378cc6480d8e166956a0d
BLAKE2b-256 bf5619d6a2c9d3554d4e29656357d5b5b51a2c6b8ae365894d8613e93c7b71ce

See more details on using hashes here.

File details

Details for the file codebleu-0.1.5-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for codebleu-0.1.5-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f63ba6ea07971936f3f5ab26c8de35b4e00b6baa00c00fe4bf7cd67937d860b2
MD5 598e0cf63787f2e2b6d09734e8219d8c
BLAKE2b-256 116c9f1350caf9e0cc30ac8be7166916082ece533e9dcbbc42338ed4feba8708

See more details on using hashes here.

File details

Details for the file codebleu-0.1.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for codebleu-0.1.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e919f62e9287777b3123c8563dc025bfa4935423cc2c1e992c77627fa57f7c6b
MD5 9d0bbaf56ecf7e42248144e575ea0ada
BLAKE2b-256 82203bc13020fffb33e6920c0a29cb8b9882b3e18f36e81f77b38cd585661f58

See more details on using hashes here.

File details

Details for the file codebleu-0.1.5-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for codebleu-0.1.5-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 283ad2a30e613019250af6a26be38d133247c6cae6abee730ff7d5de7786eb36
MD5 7b3d63aa1ffe123b2322a42906629872
BLAKE2b-256 06915276d09ae09909ea1b3cdc4a20b6681dc5edb41ed02e37721e318699644f

See more details on using hashes here.

File details

Details for the file codebleu-0.1.5-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for codebleu-0.1.5-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 16cd91e57e92d9ac336f7ed3e37f960aa8790c5d936600d76912a83bf90ec338
MD5 d2f09dc2a675913d319954149a7c4096
BLAKE2b-256 9dacfcb9e6338b6945704264dc337023edb11eecb8db4e0431dc67dae93bcfe4

See more details on using hashes here.

File details

Details for the file codebleu-0.1.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for codebleu-0.1.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e57e0ddfcccd34fd48490f188991329adc9fedc9226941cabe851854e12eb34f
MD5 8715bf1ae8ba8e56fd955cae92ca1230
BLAKE2b-256 aa706c52752d60d13706ccfd29292d2e0335f4283ea860fe52b0ea4f67ab1243

See more details on using hashes here.

File details

Details for the file codebleu-0.1.5-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for codebleu-0.1.5-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 4d35f8aec7a67e261f8ddd4f7e866a4a20e85664bcfdb82828ba0e749d00ae8b
MD5 1dd73ee779eeb7f9181c6a6a587bb3dc
BLAKE2b-256 6f5c3689d0335c9b3f62aa9e4666ad50279c8ffe2607a32f3747957e3bae1e54

See more details on using hashes here.

File details

Details for the file codebleu-0.1.5-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for codebleu-0.1.5-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1299042429422f723ab40ef85301c618a37d215f675607528d7b70ba22210248
MD5 d03c140a08261a07197d12a0a5725d45
BLAKE2b-256 5b7e18bd069c6c7dc4df85984edfc0cccb1f622124f3b6f46d81f0444bc13f8c

See more details on using hashes here.

File details

Details for the file codebleu-0.1.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for codebleu-0.1.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e0394f661eaf74432f07132ddd940175fe5487f17d4151048c0d90903ea4746f
MD5 e8be8485e90ff9aa2edcb3a9e17830e2
BLAKE2b-256 63eb2073b2a785d0411f20d886b917c0a43d0db95be7ffdf1c1dcd31266c34c5

See more details on using hashes here.

File details

Details for the file codebleu-0.1.5-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for codebleu-0.1.5-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 568a53a45f62a5754625c27b3cb69c21368066ff7e6e2db0694576bb36b2a19a
MD5 f80ec9a10c2a746e0d38fab6e3a98dca
BLAKE2b-256 df5c47957904c6519d90efc35ebe6c1f8d46c2436b09a104801a37c23e9996c7

See more details on using hashes here.

File details

Details for the file codebleu-0.1.5-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for codebleu-0.1.5-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 8d8f40b2d17a621d66bfe3c259cc94979b26afc6276f3bb8b2a27da97af5be7f
MD5 278436ffa61bb5b40a1531e87af03f7d
BLAKE2b-256 383e51edcef5e54e25142e85a8347afb487237413ea7084d83253d6aeeb7e806

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