README

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.

Documentation

Index

Constants

View Source
const Version = "1.0.1"

    Version is the API client version

    Variables

    This section is empty.

    Functions

    This section is empty.

    Types

    type APIError

    type APIError struct {
    	// Status is the HTTP text status of the response.
    	Status string
    	// StatusCode is the HTTP numerical status code of the response.
    	StatusCode int
    	// Code is an error code returned by the API.
    	Code int `json:"code"`
    	// Message is a human-readable error code returned by the API.
    	Message string `json:"message"`
    }

      APIError is an error returned from the API

      func (*APIError) Error

      func (e *APIError) Error() string

      type Client

      type Client struct {
      	// BaseURL specifies the location of the API. It is used with
      	// ResolveReference to create request URLs. (If 'Path' is specified, it
      	// should end with a trailing slash.) If nil, the default will be used.
      	BaseURL *url.URL
      	// Client is an HTTP client used to make API requests. If nil,
      	// default will be used.
      	Client *http.Client
      	// APIKey is the user's API key. It is required.
      	// Note: Treat your API Keys as passwords—keep them secret. API Keys give
      	// full read/write access to your account, so they should not be included in
      	// public repositories, emails, client side code, etc.
      	APIKey string
      	// UserAgent is a User-Agent to be sent with API HTTP requests. If empty,
      	// a default will be used.
      	UserAgent string
      }

        A Client provides an HTTP client for DetectLanguage API operations.

        func New

        func New(apiKey string) *Client

          New returns a new Client with the given API key.

          func (*Client) Detect

          func (c *Client) Detect(in string) (out []*DetectionResult, err error)

            Detect executes language detection for a single text

            func (*Client) DetectBatch

            func (c *Client) DetectBatch(in []string) (out [][]*DetectionResult, err error)

              DetectBatch executes language detection with multiple texts. It is significantly faster than doing a separate request for each text indivdually.

              func (*Client) DetectCode

              func (c *Client) DetectCode(in string) (out string, err error)

                DetectCode executes language detection for a single text and returns detected language code

                func (*Client) Languages

                func (c *Client) Languages() (out []*Language, err error)

                  Languages retrieves the list of supported languages

                  func (*Client) UserStatus

                  func (c *Client) UserStatus() (out *UserStatusResponse, err error)

                    UserStatus fetches account status

                    type DetectBatchRequest

                    type DetectBatchRequest struct {
                    	Query []string `json:"q"`
                    }

                      DetectBatchRequest contains batch language detection request params

                      type DetectBatchResponse

                      type DetectBatchResponse struct {
                      	Data *DetectBatchResponseData `json:"data"`
                      }

                        DetectBatchResponse is a resource batch containing language detection response

                        type DetectBatchResponseData

                        type DetectBatchResponseData struct {
                        	Detections [][]*DetectionResult `json:"detections"`
                        }

                          DetectBatchResponseData contains batch language detection response data

                          type DetectRequest

                          type DetectRequest struct {
                          	Query string `json:"q"`
                          }

                            DetectRequest contains language detection request params

                            type DetectResponse

                            type DetectResponse struct {
                            	Data *DetectResponseData `json:"data"`
                            }

                              DetectResponse is a resource containing language detection response

                              type DetectResponseData

                              type DetectResponseData struct {
                              	Detections []*DetectionResult `json:"detections"`
                              }

                                DetectResponseData contains language detection response data

                                type DetectionError

                                type DetectionError struct {
                                	Message string
                                }

                                  DetectionError is retuned when detection fails

                                  func (*DetectionError) Error

                                  func (e *DetectionError) Error() string

                                  type DetectionResult

                                  type DetectionResult struct {
                                  	Language   string  `json:"language"`
                                  	Reliable   bool    `json:"isReliable"`
                                  	Confidence float32 `json:"confidence"`
                                  }

                                    DetectionResult is single language detection result

                                    type Language

                                    type Language struct {
                                    	Code string `json:"code"`
                                    	Name string `json:"name"`
                                    }

                                      Language is the resource representing language

                                      type UserStatusResponse

                                      type UserStatusResponse struct {
                                      	Date               string `json:"date,omitempty"`
                                      	Requests           int    `json:"requests"`
                                      	Bytes              int    `json:"bytes"`
                                      	Plan               string `json:"plan"`
                                      	PlanExpires        string `json:"plan_expires,omitempty"`
                                      	DailyRequestsLimit int    `json:"daily_requests_limit"`
                                      	DailyBytesLimit    int    `json:"daily_bytes_limit"`
                                      	Status             string `json:"status"`
                                      }

                                        UserStatusResponse is the resource containing account status information