https://github.com/valkryst/vcodelanguagedetection
A lightweight Java library for automatic code language detection of an input text.
https://github.com/valkryst/vcodelanguagedetection
detection hacktoberfest language language-detection programming-language-detector syntax-analysis
Last synced: 5 months ago
JSON representation
A lightweight Java library for automatic code language detection of an input text.
- Host: GitHub
- URL: https://github.com/valkryst/vcodelanguagedetection
- Owner: Valkryst
- License: apache-2.0
- Created: 2023-04-06T17:28:05.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2025-01-03T21:45:19.000Z (over 1 year ago)
- Last Synced: 2025-01-03T22:28:11.441Z (over 1 year ago)
- Topics: detection, hacktoberfest, language, language-detection, programming-language-detector, syntax-analysis
- Language: Java
- Homepage:
- Size: 86.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
[](https://github.com/Valkryst/VCodeLanguageDetection/actions/workflows/maven.yml)
[](https://github.com/Valkryst/VCodeLanguageDetection/actions/workflows/codeql.yml)
This is a lightweight library that allows users to automatically detect the coding language of an
input text.
The current implementation is far from perfect, and will sometimes misidentify the language of a code snippet. Longer
code snippets are more likely to be correctly identified.
## Table of Contents
* [Installation](https://github.com/Valkryst/VCodeLanguageDetection#installation)
* [Gradle](https://github.com/Valkryst/VCodeLanguageDetection#-gradle)
* [Maven](https://github.com/Valkryst/VCodeLanguageDetection#-maven)
* [sbt](https://github.com/Valkryst/VCodeLanguageDetection#-scala-sbt)
* [Usage](https://github.com/Valkryst/VCodeLanguageDetection#usage)
* [Supported Languages](https://github.com/Valkryst/VCodeLanguageDetection#supported-languages)
* [Credits & Inspiration](https://github.com/Valkryst/VCodeLanguageDetection#credits--inspiration)
## Installation
_VCodeLanguageDetection_ is hosted on the
[JitPack package repository](https://jitpack.io/#Valkryst/VCodeLanguageDetection) which supports Gradle, Maven,
and sbt.
###  Gradle
Add JitPack to your `build.gradle` at the end of repositories.
```
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```
Add _VCodeLanguageDetection_ as a dependency.
```
dependencies {
implementation 'com.github.Valkryst:VCodeLanguageDetection:1.0.0'
}
```
###  Maven
Add JitPack as a repository.
``` xml
jitpack.io
https://jitpack.io
```
Add _VCodeLanguageDetection_ as a dependency.
```xml
com.github.Valkryst
VCodeLanguageDetection
1.0.0
```
###  Scala SBT
Add JitPack as a resolver.
```
resolvers += "jitpack" at "https://jitpack.io"
```
Add _VCodeLanguageDetection_ as a dependency.
```
libraryDependencies += "com.github.Valkryst" % "VCodeLanguageDetection" % "1.0.0"
```
## Usage
Get an instance of _LanguageDetector_, and then call the _detectLanguage_ method with the code whose language you wish
to detect. You can then use `.entrySet().iterator().next()` to retrieve the first entry in the map, which will be the
most likely language.
```java
public class Example {
public static void main(final String[] args) {
final var code = """
public class Example {
public static void main(final String[] args) {
System.out.println("Hello, World!");
}
}
""";
final var detector = LanguageDetector.getInstance();
final var language = detector.detect(code);
System.out.println("Probabilities:");
for (final var entry : language.entrySet()) {
System.out.println("\t" + entry.getKey() + ": " + entry.getValue());
}
}
}
```
If you're using [RSyntaxTextArea](https://github.com/bobbylight/RSyntaxTextArea), you can use the following method to
detect the language and return the appropriate syntax style.
```java
private String detectSyntaxStyle(final @NonNull String code) {
final var languages = LanguageDetector.getInstance().detect(code);
final var entry = languages.entrySet().iterator().next();
if (entry.getValue() == 0) {
return RSyntaxTextArea.SYNTAX_STYLE_NONE;
}
try {
final var styleName = "SYNTAX_STYLE_" + entry.getKey().toUpperCase();
final var field = SyntaxConstants.class.getDeclaredField(styleName);
return (String) field.get(null);
} catch (final NoSuchFieldException | IllegalAccessException e) {
return RSyntaxTextArea.SYNTAX_STYLE_NONE;
}
}
```
## Supported Languages
* C
* C++
* C#
* Clojure
* D
* Dart
* Delphi
* Fortran
* Go
* Java
* JavaScript
* Lua
* Perl
* PHP
* Python
* Ruby
* SQL
# Credits & Inspiration
* [highlight.js](https://github.com/highlightjs/highlight.js) - For the lists of keywords for each language.