github.com/wit-ai/wit-go/v2@v2.0.2/language_detection.go (about)

     1  // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
     2  
     3  package witai
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  	"net/http"
     9  	"net/url"
    10  )
    11  
    12  // Locales - https://wit.ai/docs/http/20200513#get__language_link
    13  type Locales struct {
    14  	DetectedLocales []Locale `json:"detected_locales"`
    15  }
    16  
    17  // Locale - https://wit.ai/docs/http/20200513#get__language_link
    18  type Locale struct {
    19  	Locale     string  `json:"locale"`
    20  	Confidence float64 `json:"confidence"`
    21  }
    22  
    23  // Detect - returns the detected languages from query - https://wit.ai/docs/http/20200513#get__language_link
    24  func (c *Client) Detect(text string) (*Locales, error) {
    25  	resp, err := c.request(http.MethodGet, fmt.Sprintf("/language?q=%s", url.PathEscape(text)), "application/json", nil)
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  	defer resp.Close()
    30  
    31  	var locales *Locales
    32  	decoder := json.NewDecoder(resp)
    33  	err = decoder.Decode(&locales)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	return locales, nil
    39  }