Skip to main content

Unofficial pip/hf compatible `CodeBLEU` implementation

Project description

CodeBLEU

Publish Test codecov

Unofficial CodeBLEU implementation with pip and hf hub support. Based on CodeXGLUE/code-to-code-trans/evaluator/CodeBLEU implementation.


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)

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.


This repository refactor the original implementation to make it more usable and comparable with pip and hf hub.

Usage

from codebleu import calc_codebleu

pred = "def add ( a , b ) :\n return a + b"
ref = "def sum ( first , second ) :\n return second + first"

res = calc_codebleu([ref], [pred], "python")
print(res)
# {
#   '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]] of reference codes
  • predictions: list[str] of predicted codes
  • lang: str of language, see codebleu.AVAILABLE_LANGS for available languages (python, c_sharp, java at the moment)
  • weights: weights of the ngram_match, weighted_ngram_match, syntax_match, and dataflow_match respectively
  • 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

Installation

Requires Python 3.8+

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

pip install codebleu

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

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

pred = "def add ( a , b ) :\n return a + b"
ref = "def sum ( first , second ) :\n return second + first"

res = metric.compute([ref], [pred], "python")
print(res)
# {
#   'codebleu': 0.5537, 
#   'ngram_match_score': 0.1041, 
#   'weighted_ngram_match_score': 0.1109, 
#   'syntax_match_score': 1.0, 
#   'dataflow_match_score': 1.0
# }

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.2.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.2-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.2-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.2-cp311-cp311-macosx_10_9_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

codebleu-0.1.2-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.2-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.2-cp310-cp310-macosx_10_9_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

codebleu-0.1.2-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.2-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.2-cp39-cp39-macosx_10_9_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.9macOS 10.9+ x86-64

codebleu-0.1.2-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.2-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.2-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.2.tar.gz.

File metadata

  • Download URL: codebleu-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 7887423f8bc070835c26e64f44aacd510635bb7fd9c4cc3efb0772d527fb2ef6
MD5 7da33f4637d373b31a5397636b10ba85
BLAKE2b-256 df4df6a8703bd1497839b5e1eae64a5abee3d3e06ccba63ab785c120b7f9ad21

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codebleu-0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ff465544474c64a1912be10589788303fd920c673bd6f7689a1e35effcf028bd
MD5 3355336b86402bed93f69ebdfc9be918
BLAKE2b-256 5303e87d71310cb4267708d83b90c6f204c926da5f77834f58cd14e519d68a08

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codebleu-0.1.2-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 14d36c01b1f3530b5dd66272ef0bd300a7aac8228596383769650d6e07ad04a0
MD5 e582344d5b295b85becb9665a0051642
BLAKE2b-256 6bf10a9169ba2e81bc3982e15c90b74acce67eec4e7e58a367209e822ecf7a9a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codebleu-0.1.2-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 0b5b599620fd2b075fe2cb7a94d1a09fa638b6dc43c6ca7deb06d5b124ee24e2
MD5 bc4c132699e3b343342fed886445e5a3
BLAKE2b-256 5880f1aadeac98151148f79c3cc9f95617107351bcf043ca00f084831cc391d0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codebleu-0.1.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 69aa38223a4791bfe4c52622124b1a33a18044849787afd48e420b2dcfc6dca9
MD5 351f47f2ac9d5ddf199dba1085ba5d97
BLAKE2b-256 b7b8277520fb7ba2b5a5992ffc7e40e0dc05b1d99aed8fdd65a899c928ab9c16

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codebleu-0.1.2-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 e593be26616c110af5d3aa9784725c141437e2faa667b1e74e0b24e70c812d1b
MD5 fdcbd06ddc56f01e3e45f2d27d5a7769
BLAKE2b-256 b44ce89224d1c4411a26385b8038fac5ab0501f8f5b104b272bafcbebe73f576

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codebleu-0.1.2-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 047d33f100a61f9ce38909ac410375e08126d1075b8a9a575a7bf4cd31f79dc1
MD5 ebe4df203fedae26c5d75f6097163029
BLAKE2b-256 d777f9ef8b5c44e218ceed4600bc02aef1abb53969dce4326ff3d998179e1107

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codebleu-0.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 dd7ec3e9f089155df5e865a88c948c9f46155dcb711606c3e7ad8461f3285e8e
MD5 226db4b11829652924c3f76c5e8aac08
BLAKE2b-256 8ff4e05d47e51e5902eddfead994d7cc19d95fd620b3f426c71a606d78172715

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codebleu-0.1.2-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 4fd672bb22f909ad90a03de9927fe47a6aa7a8f0ed8c2b2a48bff0fed8a20bb5
MD5 db3e16847c6c8a0000c176d693cf67f4
BLAKE2b-256 95adf01fb9ce25fa5237e5d0e42c815948dc4aee96a8ee521f717ad0cf21c4ef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codebleu-0.1.2-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 67dbb4e4ebbd5fb3c89a23d5cb0539befbedc95ff47d355dc0694e7608c95a80
MD5 1f927c345e40f4e917d1cf6b790873d2
BLAKE2b-256 04dd964cc1050360720c1216f678d094ce492d78c54ed286f009acad4d48ae9c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codebleu-0.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 201d5659faa38b3895e779b888a5b8f9013d4ccb0238c2de652269f150f21a4f
MD5 f13b1e8ed9c120dc5634e7fa43475e1c
BLAKE2b-256 799c3ed50f9f2c2a9ddc78afdbd4c850d948b8df02d9e98eeeaa3abb0273be46

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codebleu-0.1.2-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 fe1bd34aaad73f178bafdc05e2a6d40d0abab924a660435391ef611c8e50a663
MD5 ac9f30f601e4f1697de9395de321051f
BLAKE2b-256 1cf5c1036ed76dc377aebd51f15cd2395d14993c88c6ebbf7f04962db4ebf84d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for codebleu-0.1.2-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f28e11d55e6ca89ea0bd3c62d3f8a14fae18459b61f160ee361b6540f6eb3296
MD5 6aea83b98cad9f2513a0c16e44f0cee2
BLAKE2b-256 a47efb8f6c53e6a44f99a5eb7eb7ed266d96e82a4154e0ca3f79446912834733

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