Guesslang package reference

guesslang.Guess

class guesslang.Guess(model_dir: Optional[str] = None)

Guess the programming language of a source code.

Parameters:model_dir – Guesslang machine learning model directory.
is_trained

Check if the current machine learning model is trained. Only trained models can be used for prediction.

Returns:the model training status.
language_name(source_code: str) → Optional[str]

Predict the programming language name of the given source code.

Parameters:source_code – source code.
Returns:the language name or None if no programming language is detected.
probabilities(source_code: str) → List[Tuple[str, float]]

Gives the probability that the source code is written in each of the supported languages.

The probabilities are sorted from the most to the least probable programming language.

Parameters:source_code – source code.
Returns:list of language names associated with their probability.
supported_languages

List supported programming languages

Returns:language name list.
train(source_files_dir: str, max_steps: int) → float

Train guesslang to recognize programming languages.

The machine learning model is trained from source code files. The files should be split in three subdirectories named “train”, “valid” and “test”.

Raises:GuesslangError – when the training cannot be run.
Parameters:source_files_dir – directory that contains the “train”, “valid” and “test” datasets.
Returns:training accuracy, a value between 0 and 1.

guesslang.GuesslangError

Guesslang exception class