getlang

package module
Version: v0.0.0-...-9e7f44f Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2020 License: MIT Imports: 7 Imported by: 8

README

getlang

GoDoc Go Report Card Build Status cover.run go

getlang provides fast natural language detection in Go.

Features

  • Offline -- no internet connection required
  • Supports 29 languages
  • Provides ISO 639 language codes
  • Fast

Getting started

Installation:

    go get -u github.com/rylans/getlang

example:

package main

import (
	"fmt"
	"github.com/rylans/getlang"
)

func main(){
  info := getlang.FromString("Wszyscy ludzie rodzą się wolni i równi w swojej godności i prawach")
  fmt.Println(info.LanguageCode(), info.Confidence())
}

Documentation

getlang on godoc

License

MIT

Acknowledgements and Citations

  • Thanks to abadojack for the trigram generation logic in whatlanggo
  • Cavnar, William B., and John M. Trenkle. "N-gram-based text categorization." Ann arbor mi 48113.2 (1994): 161-175.

Documentation

Overview

Package getlang provides fast natural language detection for various languages

getlang compares input text to a characteristic profile of each supported language and returns the language that best matches the input text

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Info

type Info struct {
	// contains filtered or unexported fields
}

Info is the language detection result

func FromReader

func FromReader(reader io.Reader) (Info, error)

FromReader detects the language from an io.Reader

This function will read all bytes until an EOF is reached

func FromString

func FromString(text string) Info

FromString detects the language from the given string

func (Info) Confidence

func (info Info) Confidence() float64

Confidence returns a measure of reliability for the language classification

The output value is in the range [0, 1.0] inclusive

Example
package main

import (
	"fmt"
	"github.com/rylans/getlang"
)

func main() {
	short := getlang.FromString("short text")
	long := getlang.FromString("this sentence is a bit longer")
	fmt.Println(long.Confidence() > short.Confidence())
}
Output:

true

func (Info) LanguageCode

func (info Info) LanguageCode() string

LanguageCode returns the ISO 639-1 code for the detected language

Example
package main

import (
	"fmt"
	"github.com/rylans/getlang"
)

func main() {
	fmt.Println(getlang.FromString("статей на русском").LanguageCode())
}
Output:

ru

func (Info) LanguageName

func (info Info) LanguageName() string

LanguageName returns the English name of the detected language

Example
package main

import (
	"fmt"
	"github.com/rylans/getlang"
)

func main() {
	fmt.Println(getlang.FromString("何ですか?").LanguageName())
}
Output:

Japanese

func (Info) SelfName

func (info Info) SelfName() string

SelfName returns the name of the language in the language itself

Example
package main

import (
	"fmt"
	"github.com/rylans/getlang"
)

func main() {
	fmt.Println(getlang.FromString("何ですか?").SelfName())
}
Output:

日本語

func (Info) Tag

func (info Info) Tag() language.Tag

Tag returns the language.Tag of the detected language

Example
package main

import (
	"fmt"
	"github.com/rylans/getlang"
)

func main() {
	fmt.Println(getlang.FromString("何ですか?").Tag().IsRoot())
}
Output:

false

Jump to

Keyboard shortcuts

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