Documentation
¶
Overview ¶
Package py3langid implements the py3langid language identification runtime in pure Go. Its embedded model recognizes 139 languages plus zxx, with preprocessing, scoring, and confidence calibration tested against Python. Model training remains in the upstream Python project.
Index ¶
- func SetLanguages(langs ...string) error
- type Identifier
- func (id *Identifier) Classes() []string
- func (id *Identifier) IdentifyBytes(text []byte) (Result, error)
- func (id *Identifier) IdentifyFile(path string) (Result, error)
- func (id *Identifier) IdentifyNormalizedBytes(text []byte) (Result, error)
- func (id *Identifier) IdentifyNormalizedString(text string) (Result, error)
- func (id *Identifier) IdentifyString(text string) (Result, error)
- func (id *Identifier) RankBytes(text []byte) ([]Result, error)
- func (id *Identifier) RankFile(path string) ([]Result, error)
- func (id *Identifier) RankNormalizedBytes(text []byte) ([]Result, error)
- func (id *Identifier) RankNormalizedString(text string) ([]Result, error)
- func (id *Identifier) RankString(text string) ([]Result, error)
- func (id *Identifier) ResetLanguages()
- func (id *Identifier) SetLanguages(langs ...string) error
- type Option
- type Result
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetLanguages ¶
SetLanguages restricts the shared default identifier used by package functions. Call with no languages to reset it. Use a separate Identifier for independent settings.
Types ¶
type Identifier ¶
type Identifier struct {
// contains filtered or unexported fields
}
Identifier classifies text by language using a pre-trained model.
Example ¶
identifier, err := NewDefaultIdentifier(WithNormalizedProbabilities())
if err != nil {
panic(err)
}
result, err := identifier.IdentifyString("This text is in English.")
if err != nil {
panic(err)
}
fmt.Printf("%s %.6f\n", result.Language, result.Score)
Output: en 0.720258
func LoadModel ¶
func LoadModel(path string, options ...Option) (*Identifier, error)
LoadModel reads a .lidg model file.
func NewDefaultIdentifier ¶
func NewDefaultIdentifier(options ...Option) (*Identifier, error)
NewDefaultIdentifier loads the embedded py3langid 0.4 model.
func (*Identifier) Classes ¶
func (id *Identifier) Classes() []string
Classes returns the active language classes supported by the identifier.
func (*Identifier) IdentifyBytes ¶
func (id *Identifier) IdentifyBytes(text []byte) (Result, error)
IdentifyBytes predicts a language label for bytes.
func (*Identifier) IdentifyFile ¶
func (id *Identifier) IdentifyFile(path string) (Result, error)
IdentifyFile reads the file at the specified path and predicts its language. If reading the file fails, it returns the wrapped filesystem error without swallowing context.
func (*Identifier) IdentifyNormalizedBytes ¶
func (id *Identifier) IdentifyNormalizedBytes(text []byte) (Result, error)
IdentifyNormalizedBytes returns calibrated confidence and honors abstention.
func (*Identifier) IdentifyNormalizedString ¶
func (id *Identifier) IdentifyNormalizedString(text string) (Result, error)
IdentifyNormalizedString returns calibrated confidence and honors abstention.
func (*Identifier) IdentifyString ¶
func (id *Identifier) IdentifyString(text string) (Result, error)
IdentifyString predicts a language label for text.
func (*Identifier) RankBytes ¶
func (id *Identifier) RankBytes(text []byte) ([]Result, error)
RankBytes returns all labels sorted by their configured scores.
func (*Identifier) RankFile ¶
func (id *Identifier) RankFile(path string) ([]Result, error)
RankFile reads the file at the specified path and ranks all supported languages by likelihood. If reading the file fails, it returns the wrapped filesystem error without swallowing context.
func (*Identifier) RankNormalizedBytes ¶
func (id *Identifier) RankNormalizedBytes(text []byte) ([]Result, error)
RankNormalizedBytes returns model-calibrated probabilities regardless of options.
func (*Identifier) RankNormalizedString ¶
func (id *Identifier) RankNormalizedString(text string) ([]Result, error)
RankNormalizedString returns model-calibrated probabilities regardless of options.
func (*Identifier) RankString ¶
func (id *Identifier) RankString(text string) ([]Result, error)
RankString returns all labels sorted by their configured scores.
func (*Identifier) ResetLanguages ¶
func (id *Identifier) ResetLanguages()
ResetLanguages restores the active language set of the identifier to include all languages present in the original loaded model.
func (*Identifier) SetLanguages ¶
func (id *Identifier) SetLanguages(langs ...string) error
SetLanguages restricts the active language set of the identifier to the specified subset. If langs is empty or nil, it resets the active languages to the original model languages. If any requested language is not supported by the model, it returns an error and leaves the active language set unmodified (atomic operation).
type Option ¶
type Option func(*Identifier) error
Option configures an identifier at construction time.
func WithMinConfidence ¶
WithMinConfidence returns "und" for predictions below the threshold. It requires WithNormalizedProbabilities.
func WithNormalizedProbabilities ¶
func WithNormalizedProbabilities() Option
WithNormalizedProbabilities returns probabilities instead of raw log scores. Py3langid models use their byte-length temperature calibration.
type Result ¶
Result contains a predicted class and its raw log score or configured probability.
func IdentifyFile ¶
IdentifyFile reads the file at the specified path and predicts its language using the default identifier.