# codelang-detect
A fast, lightweight, regex-based programming language detector for Python.
[](https://pypi.org/project/codelang-detect/)
[](https://github.com/cbarkinozer/codelang-detect/actions)
[](https://pypi.org/project/codelang-detect/)
[](https://opensource.org/licenses/Apache-2.0)
---
Codelang-detect identifies the programming language of a given code snippet. It is designed from the ground up to be **fast**, **accurate**, and have **zero external dependencies**. It's the perfect tool for pre-processing code, routing files, or any application where you need a quick and reliable language check without pulling in heavy libraries.
### Key Features
- โก๏ธ **Blazing Fast:** Built on a system of weighted, compiled regular expressions. Performance is measured in microseconds.
- ๐ฏ **Highly Accurate:** Demonstrably more accurate than popular alternatives on a curated suite of real-world and tricky code snippets.
- ๐ฆ **Zero Dependencies:** Pure Python. `pip install codelang-detect` is all you need. No heavyweight models, no external binaries.
- ๐ง **Simple API:** A single function call: `detect(code)`.
- ๐ป **CLI Included:** Use it directly from your terminal or in shell scripts.
### Why `codelang-detect`?
Many existing language detectors have significant trade-offs:
- **Heavy ML Models (e.g., `guesslang`):** Often have complex or outdated dependencies (like older TensorFlow versions) that make installation difficult. They are also significantly slower for single detections.
- **Comprehensive Tools (e.g., `pygments`):** Excellent for syntax highlighting, but its primary goal isn't detection. As the benchmarks show, its guessing can be unreliable on complex snippets.
- **Platform-Specific Tools (e.g., GitHub's `linguist`):** The industry standard, but it's a Ruby Gem, making it difficult to integrate into a Python environment.
`codelang-detect` fills the gap for a "just right" solution: a lightweight, portable, and fast detector that delivers best-in-class accuracy.
### Benchmark: Accuracy & Performance
The results speak for themselves. On a [curated set of 36 code snippets](https://github.com/cbarkinozer/codelang-detect/blob/main/tests/test_data.json) designed to test real-world accuracy, `codelang-detect` is both significantly more accurate and an order of magnitude faster than other popular, lightweight libraries.
| Library | Accuracy | Avg. Time / Sample (ยตs) | Dependencies |
| -------------------------- | :-------: | :---------------------: | ---------------- |
| **`codelang-detect` (Ours)** | **100%** | **~173 ยตs** | **None** |
| `Pygments` | 22.2% | ~1395 ยตs | None |
| `WhatsThatCode` | 30.6% | ~1881 ยตs | None |
*Benchmarks run on Python 3.13. Your results may vary.*
As the results show, `codelang-detect` is not only the most accurate solution on this test suite but also **~8x faster than `Pygments`** and **~11x faster than `WhatsThatCode`**, all while maintaining zero dependencies.
<details>
<summary>Click to see detailed accuracy breakdown</summary>
```
--- Accuracy Benchmark ---
| Test Case | Expected | Codelang-Detect (Ours) | Pygments | WhatsThatCode |
--------------------------------------------------------------------------------------------------------------
| cs_simple | cs | cs โ
| unknown โ | java โ |
| cs_lambda | cs | cs โ
| scdoc โ | unknown โ |
| cs_full | cs | cs โ
| gdscript โ | unknown โ |
| py_simple | py | py โ
| py โ
| py โ
|
| py_class | py | py โ
| perl6 โ | py โ
|
| java_simple | java | java โ
| py โ | java โ
|
| java_full | java | java โ
| teratermmacro โ | unknown โ |
| js_arrow | js | js โ
| gdscript โ | unknown โ |
| yaml_k8s | yaml | yaml โ
| actionscript3 โ | unknown โ |
| sh_shebang | sh | sh โ
| sh โ
| sh โ
|
| kt_data_class | kt | kt โ
| ssp โ | unknown โ |
| swift_func | swift | swift โ
| gdscript โ | unknown โ |
| scala_case_class | scala | scala โ
| unknown โ | unknown โ |
| sql_select | sql | sql โ
| scdoc โ | unknown โ |
| cbl_simple | cbl | cbl โ
| componentpascal โ | unknown โ |
| plain_text | unknown | unknown โ
| unknown โ
| unknown โ
|
| cs_async_method | cs | cs โ
| gdscript โ | cs โ
|
| cs_linq_query | cs | cs โ
| gdscript โ | js โ |
| py_async_http | py | py โ
| py โ
| unknown โ |
| py_pandas | py | py โ
| py โ
| unknown โ |
| java_streams | java | java โ
| py โ | unknown โ |
| js_promise_fetch | js | js โ
| gdscript โ | unknown โ |
| js_react_component | js | js โ
| py โ | unknown โ |
| ts_interface | ts | ts โ
| gdscript โ | unknown โ |
| kt_coroutine | kt | kt โ
| py โ | py โ |
| swift_struct | swift | swift โ
| gdscript โ | unknown โ |
| scala_future | scala | scala โ
| py โ | unknown โ |
| go_http_server | go | go โ
| py โ | go โ
|
| sql_join | sql | sql โ
| scdoc โ | unknown โ |
| yaml_dockercompose | yaml | yaml โ
| scdoc โ | unknown โ |
| sh_env_check | sh | sh โ
| sh โ
| sh โ
|
| rb_class | rb | rb โ
| tsql โ | rb โ
|
| php_router | php | php โ
| javascript+php โ | php โ
|
| rust_result | rs | rs โ
| ecl โ | unknown โ |
| c_function_pointer | c | c โ
| c โ
| unknown โ |
| plain_text_doc | unknown | unknown โ
| unknown โ
| unknown โ
|
```
</details>
*Note: Libraries like `guesslang` and `enry` were excluded from the final benchmark due to significant installation issues with modern Python versions and their respective dependencies.*
### Installation
```bash
pip install codelang-detect
```
### Usage
#### As a Python Library
The API is dead simple. The `detect` function takes a string of code and returns the file extension of the detected language.
```python
from codelang_detect import detect
# Example 1: Python
python_code = "class User:\n def __init__(self, name): self.name = name"
print(detect(python_code))
# Output: py
# Example 2: C#
csharp_code = "public class Person { public string Name { get; set; } }"
print(detect(csharp_code))
# Output: cs
# Example 3: Non-code
unknown_text = "This is just a regular sentence."
print(detect(unknown_text))
# Output: unknown
```
#### As a Command-Line Tool (CLI)
You can also use `codelang-detect` directly from your terminal to analyze files or `stdin`.
```bash
# Analyze a file
codelang-detect my_script.js
# Output: js
# Pipe content into the CLI
cat deployment.yaml | codelang-detect
# Output: yaml
```
### Supported Languages
`codelang-detect` currently provides high-quality detection for the following languages, sorted by their returned extension:
- C (`c`)
- C++ (`cpp`)
- C# (`cs`)
- COBOL (`cbl`)
- Dart (`dart`)
- Go (`go`)
- Java (`java`)
- JavaScript (`js`)
- Kotlin (`kt`)
- PHP (`php`)
- Python (`py`)
- R (`r`)
- Ruby (`rb`)
- Rust (`rs`)
- Scala (`scala`)
- Shell (`sh`)
- Solidity (`sol`)
- SQL (`sql`)
- Swift (`swift`)
- TypeScript (`ts`)
- YAML (`yaml`)
### How It Works
No magic here. `codelang-detect` uses a curated list of regular expressions for each language. Each regex is assigned a "weight" based on how uniquely it identifies a language.
For example:
- The pattern `async Task<` is a very strong signal for **C#** and gets a high weight.
- The keyword `def` is a strong signal for **Python** but could also appear in Scala or Ruby, so it gets a moderate weight.
- The keyword `class` is a weak signal, as it appears in many languages, and requires more context to be useful.
The library runs all regexes against the input code, sums the weights for each language, and returns the language with the highest score. It's simple, transparent, and incredibly fast.
### Contributing
Contributions are welcome and appreciated! This project was started to fill a gap, and community help is the best way to make it the definitive tool for this job.
Whether it's improving regexes, adding support for a new language, or fixing a bug, please feel free to:
1. [Open an issue](https://github.com/cbarkinozer/codelang-detect/issues) to discuss the change.
2. Fork the repository and submit a pull request.
When adding a language or fixing a misidentification, please add relevant code snippets to `tests/test_data.json`. This helps verify your changes and prevents future regressions. We follow a simple principle: if a human can't reliably distinguish a short snippet, the detector probably can't either, so focus on realistic test cases.
### License
This project is licensed under the Apache 2.0 License - see the [LICENSE](LICENSE) file for details.
Raw data
{
"_id": null,
"home_page": null,
"name": "codelang-detect",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "language-detection, code-analysis, regex, developer-tools, lightweight",
"author": null,
"author_email": "cbarkinozer <cbarkinozer@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/f7/02/884b8130493d2cab9a64f2d038402979b881d23b8e80efc24aa7c9986c87/codelang_detect-1.0.1.tar.gz",
"platform": null,
"description": "# codelang-detect\r\n\r\nA fast, lightweight, regex-based programming language detector for Python.\r\n\r\n[](https://pypi.org/project/codelang-detect/)\r\n[](https://github.com/cbarkinozer/codelang-detect/actions)\r\n[](https://pypi.org/project/codelang-detect/)\r\n[](https://opensource.org/licenses/Apache-2.0)\r\n\r\n---\r\n\r\nCodelang-detect identifies the programming language of a given code snippet. It is designed from the ground up to be **fast**, **accurate**, and have **zero external dependencies**. It's the perfect tool for pre-processing code, routing files, or any application where you need a quick and reliable language check without pulling in heavy libraries.\r\n\r\n### Key Features\r\n\r\n- \u26a1\ufe0f **Blazing Fast:** Built on a system of weighted, compiled regular expressions. Performance is measured in microseconds.\r\n- \ud83c\udfaf **Highly Accurate:** Demonstrably more accurate than popular alternatives on a curated suite of real-world and tricky code snippets.\r\n- \ud83d\udce6 **Zero Dependencies:** Pure Python. `pip install codelang-detect` is all you need. No heavyweight models, no external binaries.\r\n- \ud83d\udd27 **Simple API:** A single function call: `detect(code)`.\r\n- \ud83d\udcbb **CLI Included:** Use it directly from your terminal or in shell scripts.\r\n\r\n### Why `codelang-detect`?\r\n\r\nMany existing language detectors have significant trade-offs:\r\n\r\n- **Heavy ML Models (e.g., `guesslang`):** Often have complex or outdated dependencies (like older TensorFlow versions) that make installation difficult. They are also significantly slower for single detections.\r\n- **Comprehensive Tools (e.g., `pygments`):** Excellent for syntax highlighting, but its primary goal isn't detection. As the benchmarks show, its guessing can be unreliable on complex snippets.\r\n- **Platform-Specific Tools (e.g., GitHub's `linguist`):** The industry standard, but it's a Ruby Gem, making it difficult to integrate into a Python environment.\r\n\r\n`codelang-detect` fills the gap for a \"just right\" solution: a lightweight, portable, and fast detector that delivers best-in-class accuracy.\r\n\r\n### Benchmark: Accuracy & Performance\r\n\r\nThe results speak for themselves. On a [curated set of 36 code snippets](https://github.com/cbarkinozer/codelang-detect/blob/main/tests/test_data.json) designed to test real-world accuracy, `codelang-detect` is both significantly more accurate and an order of magnitude faster than other popular, lightweight libraries.\r\n\r\n| Library | Accuracy | Avg. Time / Sample (\u00b5s) | Dependencies |\r\n| -------------------------- | :-------: | :---------------------: | ---------------- |\r\n| **`codelang-detect` (Ours)** | **100%** | **~173 \u00b5s** | **None** |\r\n| `Pygments` | 22.2% | ~1395 \u00b5s | None |\r\n| `WhatsThatCode` | 30.6% | ~1881 \u00b5s | None |\r\n\r\n*Benchmarks run on Python 3.13. Your results may vary.*\r\n\r\nAs the results show, `codelang-detect` is not only the most accurate solution on this test suite but also **~8x faster than `Pygments`** and **~11x faster than `WhatsThatCode`**, all while maintaining zero dependencies.\r\n\r\n<details>\r\n<summary>Click to see detailed accuracy breakdown</summary>\r\n\r\n```\r\n--- Accuracy Benchmark ---\r\n| Test Case | Expected | Codelang-Detect (Ours) | Pygments | WhatsThatCode |\r\n--------------------------------------------------------------------------------------------------------------\r\n| cs_simple | cs | cs \u2705 | unknown \u274c | java \u274c |\r\n| cs_lambda | cs | cs \u2705 | scdoc \u274c | unknown \u274c |\r\n| cs_full | cs | cs \u2705 | gdscript \u274c | unknown \u274c |\r\n| py_simple | py | py \u2705 | py \u2705 | py \u2705 |\r\n| py_class | py | py \u2705 | perl6 \u274c | py \u2705 |\r\n| java_simple | java | java \u2705 | py \u274c | java \u2705 |\r\n| java_full | java | java \u2705 | teratermmacro \u274c | unknown \u274c |\r\n| js_arrow | js | js \u2705 | gdscript \u274c | unknown \u274c |\r\n| yaml_k8s | yaml | yaml \u2705 | actionscript3 \u274c | unknown \u274c |\r\n| sh_shebang | sh | sh \u2705 | sh \u2705 | sh \u2705 |\r\n| kt_data_class | kt | kt \u2705 | ssp \u274c | unknown \u274c |\r\n| swift_func | swift | swift \u2705 | gdscript \u274c | unknown \u274c |\r\n| scala_case_class | scala | scala \u2705 | unknown \u274c | unknown \u274c |\r\n| sql_select | sql | sql \u2705 | scdoc \u274c | unknown \u274c |\r\n| cbl_simple | cbl | cbl \u2705 | componentpascal \u274c | unknown \u274c |\r\n| plain_text | unknown | unknown \u2705 | unknown \u2705 | unknown \u2705 |\r\n| cs_async_method | cs | cs \u2705 | gdscript \u274c | cs \u2705 |\r\n| cs_linq_query | cs | cs \u2705 | gdscript \u274c | js \u274c |\r\n| py_async_http | py | py \u2705 | py \u2705 | unknown \u274c |\r\n| py_pandas | py | py \u2705 | py \u2705 | unknown \u274c |\r\n| java_streams | java | java \u2705 | py \u274c | unknown \u274c |\r\n| js_promise_fetch | js | js \u2705 | gdscript \u274c | unknown \u274c |\r\n| js_react_component | js | js \u2705 | py \u274c | unknown \u274c |\r\n| ts_interface | ts | ts \u2705 | gdscript \u274c | unknown \u274c |\r\n| kt_coroutine | kt | kt \u2705 | py \u274c | py \u274c |\r\n| swift_struct | swift | swift \u2705 | gdscript \u274c | unknown \u274c |\r\n| scala_future | scala | scala \u2705 | py \u274c | unknown \u274c |\r\n| go_http_server | go | go \u2705 | py \u274c | go \u2705 |\r\n| sql_join | sql | sql \u2705 | scdoc \u274c | unknown \u274c |\r\n| yaml_dockercompose | yaml | yaml \u2705 | scdoc \u274c | unknown \u274c |\r\n| sh_env_check | sh | sh \u2705 | sh \u2705 | sh \u2705 |\r\n| rb_class | rb | rb \u2705 | tsql \u274c | rb \u2705 |\r\n| php_router | php | php \u2705 | javascript+php \u274c | php \u2705 |\r\n| rust_result | rs | rs \u2705 | ecl \u274c | unknown \u274c |\r\n| c_function_pointer | c | c \u2705 | c \u2705 | unknown \u274c |\r\n| plain_text_doc | unknown | unknown \u2705 | unknown \u2705 | unknown \u2705 |\r\n```\r\n\r\n</details>\r\n\r\n*Note: Libraries like `guesslang` and `enry` were excluded from the final benchmark due to significant installation issues with modern Python versions and their respective dependencies.*\r\n\r\n### Installation\r\n\r\n```bash\r\npip install codelang-detect\r\n```\r\n\r\n### Usage\r\n\r\n#### As a Python Library\r\n\r\nThe API is dead simple. The `detect` function takes a string of code and returns the file extension of the detected language.\r\n\r\n```python\r\nfrom codelang_detect import detect\r\n\r\n# Example 1: Python\r\npython_code = \"class User:\\n def __init__(self, name): self.name = name\"\r\nprint(detect(python_code))\r\n# Output: py\r\n\r\n# Example 2: C#\r\ncsharp_code = \"public class Person { public string Name { get; set; } }\"\r\nprint(detect(csharp_code))\r\n# Output: cs\r\n\r\n# Example 3: Non-code\r\nunknown_text = \"This is just a regular sentence.\"\r\nprint(detect(unknown_text))\r\n# Output: unknown\r\n```\r\n\r\n#### As a Command-Line Tool (CLI)\r\n\r\nYou can also use `codelang-detect` directly from your terminal to analyze files or `stdin`.\r\n\r\n```bash\r\n# Analyze a file\r\ncodelang-detect my_script.js\r\n# Output: js\r\n\r\n# Pipe content into the CLI\r\ncat deployment.yaml | codelang-detect\r\n# Output: yaml\r\n```\r\n\r\n### Supported Languages\r\n\r\n`codelang-detect` currently provides high-quality detection for the following languages, sorted by their returned extension:\r\n\r\n- C (`c`)\r\n- C++ (`cpp`)\r\n- C# (`cs`)\r\n- COBOL (`cbl`)\r\n- Dart (`dart`)\r\n- Go (`go`)\r\n- Java (`java`)\r\n- JavaScript (`js`)\r\n- Kotlin (`kt`)\r\n- PHP (`php`)\r\n- Python (`py`)\r\n- R (`r`)\r\n- Ruby (`rb`)\r\n- Rust (`rs`)\r\n- Scala (`scala`)\r\n- Shell (`sh`)\r\n- Solidity (`sol`)\r\n- SQL (`sql`)\r\n- Swift (`swift`)\r\n- TypeScript (`ts`)\r\n- YAML (`yaml`)\r\n\r\n### How It Works\r\n\r\nNo magic here. `codelang-detect` uses a curated list of regular expressions for each language. Each regex is assigned a \"weight\" based on how uniquely it identifies a language.\r\n\r\nFor example:\r\n- The pattern `async Task<` is a very strong signal for **C#** and gets a high weight.\r\n- The keyword `def` is a strong signal for **Python** but could also appear in Scala or Ruby, so it gets a moderate weight.\r\n- The keyword `class` is a weak signal, as it appears in many languages, and requires more context to be useful.\r\n\r\nThe library runs all regexes against the input code, sums the weights for each language, and returns the language with the highest score. It's simple, transparent, and incredibly fast.\r\n\r\n### Contributing\r\n\r\nContributions are welcome and appreciated! This project was started to fill a gap, and community help is the best way to make it the definitive tool for this job.\r\n\r\nWhether it's improving regexes, adding support for a new language, or fixing a bug, please feel free to:\r\n\r\n1. [Open an issue](https://github.com/cbarkinozer/codelang-detect/issues) to discuss the change.\r\n2. Fork the repository and submit a pull request.\r\n\r\nWhen adding a language or fixing a misidentification, please add relevant code snippets to `tests/test_data.json`. This helps verify your changes and prevents future regressions. We follow a simple principle: if a human can't reliably distinguish a short snippet, the detector probably can't either, so focus on realistic test cases.\r\n\r\n### License\r\n\r\nThis project is licensed under the Apache 2.0 License - see the [LICENSE](LICENSE) file for details.\r\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "A fast, lightweight, zero-dependency programming language detector for Python.",
"version": "1.0.1",
"project_urls": {
"Bug Tracker": "https://github.com/cbarkinozer/codelang-detect/issues",
"Homepage": "https://github.com/cbarkinozer/codelang-detect"
},
"split_keywords": [
"language-detection",
" code-analysis",
" regex",
" developer-tools",
" lightweight"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "323f47fbef3797a3302a9f135e4a784ca4e86d5acb526bb7074f3a459e418ea2",
"md5": "7008dcca698bca951b1dfd587da5bbeb",
"sha256": "6869c7ba984bc9f67efc7cebc032eb29f0e4dffe1f2c1a9ea9244e66c615d241"
},
"downloads": -1,
"filename": "codelang_detect-1.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "7008dcca698bca951b1dfd587da5bbeb",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 13250,
"upload_time": "2025-11-01T13:44:49",
"upload_time_iso_8601": "2025-11-01T13:44:49.965322Z",
"url": "https://files.pythonhosted.org/packages/32/3f/47fbef3797a3302a9f135e4a784ca4e86d5acb526bb7074f3a459e418ea2/codelang_detect-1.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "f702884b8130493d2cab9a64f2d038402979b881d23b8e80efc24aa7c9986c87",
"md5": "ea86a518ff9db7f17b35e68f2116b9d5",
"sha256": "2ee533b226c675fb66bb0b8cacc20e535ebbd5b53c183bac1d73dbf5f60365af"
},
"downloads": -1,
"filename": "codelang_detect-1.0.1.tar.gz",
"has_sig": false,
"md5_digest": "ea86a518ff9db7f17b35e68f2116b9d5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 15977,
"upload_time": "2025-11-01T13:44:51",
"upload_time_iso_8601": "2025-11-01T13:44:51.350564Z",
"url": "https://files.pythonhosted.org/packages/f7/02/884b8130493d2cab9a64f2d038402979b881d23b8e80efc24aa7c9986c87/codelang_detect-1.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-11-01 13:44:51",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "cbarkinozer",
"github_project": "codelang-detect",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "pygments",
"specs": [
[
"==",
"2.19.2"
]
]
},
{
"name": "whats-that-code",
"specs": [
[
"==",
"0.2.0"
]
]
},
{
"name": "pygments-github-lexers",
"specs": [
[
"==",
"0.0.5"
]
]
}
],
"lcname": "codelang-detect"
}