Skip to content
Language Detection API Go Client
Go
Branch: master
Clone or download
Latest commit 5413041 Dec 27, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.travis.yml Add more travis go versions Dec 15, 2019
LICENSE
README.md Add goreportcard Dec 15, 2019
client.go Remove unused client functions Dec 27, 2019
client_test.go Add client specs Dec 27, 2019
detect.go Add comments Dec 15, 2019
detect_test.go
detectlanguage_test.go Add user status Dec 15, 2019
error.go Add comments Dec 15, 2019
error_test.go
go.mod
go.sum Add languages endpoint Dec 15, 2019
languages.go Add comments Dec 15, 2019
languages_test.go Add languages endpoint Dec 15, 2019
user.go Add comments Dec 15, 2019
user_test.go
version.go Bump version Dec 15, 2019

README.md

Language Detection API Go Client

GoDoc Build Status Go Report Card

Detects language of the given text. Returns detected language codes and scores.

Before using Detect Language API client you have to setup your personal API key. You can get it by signing up at https://detectlanguage.com

Installation

go get -u github.com/detectlanguage/detectlanguage-go

Configuration

client := detectlanguage.New("YOUR API KEY")

Usage

Language detection

detections, err := client.Detect("Buenos dias señor")

if err != nil {
    fmt.Fprintln(os.Stderr, "error detecting language:", err)
    os.Exit(1)
    return
}

fmt.Fprintln(os.Stdout, "Language:", detections[0].Language)
fmt.Fprintln(os.Stdout, "Reliable:", detections[0].Reliable)
fmt.Fprintln(os.Stdout, "Confidence:", detections[0].Confidence)

Single language code detection

If you need just a language code you can use DetectCode. It returns first detected language code.

language, err := client.DetectCode("Buenos dias señor")

if err != nil {
    fmt.Fprintln(os.Stderr, "error detecting language:", err)
    os.Exit(1)
    return
}

fmt.Fprintln(os.Stdout, "Language:", language)

Batch detection

It is possible to detect language of several texts with one request. This method is significantly faster than doing one request per text. To use batch detection just pass multiple texts to DetectBatch method.

texts := []string{"labas rytas", "good morning"}
results, err := client.DetectBatch(texts)

if err != nil {
    fmt.Fprintln(os.Stderr, "error detecting language:", err)
    os.Exit(1)
    return
}

fmt.Fprintln(os.Stdout, "First text language:", detections[0][0].Language)
fmt.Fprintln(os.Stdout, "Second text language:", detections[1][0].Language)

Getting your account status

result, err := client.UserStatus()

if err != nil {
    fmt.Fprintln(os.Stderr, "error getting user status:", err)
    os.Exit(1)
    return
}

fmt.Fprintln(os.Stdout, "Status:", result.Status)
fmt.Fprintln(os.Stdout, "Requests sent today:", result.Requests)
fmt.Fprintln(os.Stdout, "Bytes sent today:", result.Bytes)
fmt.Fprintln(os.Stdout, "Plan:", result.Plan)
fmt.Fprintln(os.Stdout, "Plan expires:", result.PlanExpires)
fmt.Fprintln(os.Stdout, "Daily requests limit:", result.DailyRequestsLimit)
fmt.Fprintln(os.Stdout, "Daily bytes limit:", result.DailyBytesLimit)
fmt.Fprintln(os.Stdout, "Date:", result.Date)

Getting list supported languages

languages, err := client.Languages()

if err != nil {
    fmt.Fprintln(os.Stderr, "error getting languages list:", err)
    os.Exit(1)
    return
}

fmt.Fprintln(os.Stdout, "Supported languages:", len(languages))
fmt.Fprintln(os.Stdout, "First language code:", languages[0].Code)
fmt.Fprintln(os.Stdout, "First language name:", languages[0].Name)

License

Detect Language API Go Client is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.

You can’t perform that action at this time.