guesslang

package module
v0.0.0-...-ee60e7d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

guesslang-go

Guess the programming language of a piece of source code, in pure Go.

A port of the inference half of guesslang (MIT, by Y. Somda): the same trained model, run without Python or TensorFlow. The 2.7 MB model is embedded in the package, so there is nothing to download or install at run time.

import "github.com/bradvrudney/guesslang-go"

lang, ok := guesslang.Guess(text)        // "Python", true
scores, _ := guesslang.Probabilities(text) // all 54 languages, most probable first
  • Guess returns the top language and whether guesslang's own rule finds it reliable (the top probability is more than two standard deviations above the mean of all of them).
  • Probabilities gives the whole answer, for a caller that wants its own bar. The model always names a language, even for ordinary writing, so a program that colours text should also ask for a minimum probability and a minimum length.
  • Languages lists the 54 languages: Assembly, Batchfile, C, C#, C++, Clojure, CMake, COBOL, CoffeeScript, CSS, CSV, Dart, DM, Dockerfile, Elixir, Erlang, Fortran, Go, Groovy, Haskell, HTML, INI, Java, JavaScript, JSON, Julia, Kotlin, Lisp, Lua, Makefile, Markdown, Matlab, Objective-C, OCaml, Pascal, Perl, PHP, PowerShell, Prolog, Python, R, Ruby, Rust, Scala, Shell, SQL, Swift, TeX, TOML, TypeScript, Verilog, Visual Basic, XML, YAML.

How it works

The model is a TensorFlow DNNLinearCombinedClassifier. A text is cut into every pair of neighbouring bytes (the first 10,000 pairs), each pair is hashed (FarmHash Fingerprint64) into one of 5,000 buckets, and the buckets feed two parts that are added and turned into probabilities by a softmax: the mean of their 70-number embeddings through two ReLU layers (512 and 32 units), and the sum of a per-bucket weight for each language.

Checked against the original

testdata/reference.json holds the probabilities the original guesslang (2.2.1 with TensorFlow 2.5.0) gives for the 32 files in testdata/samples (code in many languages, ordinary writing, non-ASCII text, one tiny, one over 10,000 bytes). The test requires every one of the 54 probabilities of every sample to agree within 0.0001; the largest difference is about 2e-7.

tools/regenerate.sh rebuilds the embedded model and the reference answers from the original, in Docker.

Licence

MIT. The model and the language list are guesslang's, copyright Y. Somda.

Documentation

Overview

Package guesslang guesses the programming language of source code, in pure Go. It is a port of the inference half of guesslang (https://github.com/yoeo/guesslang, MIT): the same trained model, embedded in the program, run without Python or TensorFlow.

The model knows 54 languages and always names one, even for text that is not code. Probabilities exposes the whole answer so that a caller can set its own bar; Guess applies guesslang's own rule for when to trust it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Guess

func Guess(text string) (string, bool)

Guess returns the most probable language of text, and whether guesslang's rule finds that answer reliable: the probability of the top language must be more than two standard deviations above the mean of all of them. It returns false for empty text.

func Languages

func Languages() []string

Languages lists the languages the model knows.

Types

type Score

type Score struct {
	Language    string
	Probability float64
}

Score is the probability that a text is written in a language.

func Probabilities

func Probabilities(text string) ([]Score, error)

Probabilities gives the probability of each of the 54 languages the model knows for text, the most probable first.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL