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 ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Guess ¶
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.
Example ¶
package main
import (
"fmt"
guesslang "github.com/bradvrudney/guesslang-go"
)
func main() {
code := "package main\n\nimport \"fmt\"\n\nfunc main() {\n\tfmt.Println(\"hello\")\n}\n"
lang, ok := guesslang.Guess(code)
fmt.Println(lang, ok)
}
Output: Go true
Types ¶
type Score ¶
Score is the probability that a text is written in a language.
func Probabilities ¶
Probabilities gives the probability of each of the 54 languages the model knows for text, the most probable first.
Example ¶
package main
import (
"fmt"
guesslang "github.com/bradvrudney/guesslang-go"
)
func main() {
scores, _ := guesslang.Probabilities("SELECT id FROM users\nWHERE id = 1;\n")
fmt.Println(len(scores), scores[0].Language)
}
Output: 54 SQL