mstranslator

package module
v0.0.0-...-40eaef9 Latest Latest
Warning

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

Go to latest
Published: May 26, 2021 License: MIT Imports: 10 Imported by: 0

README

mstranslator

GitHub license GoDoc Build Status

The "mstranslator" is a Micrsoft Translator Service client which written by Golang.

What is Microsoft Translator

image

Microsoft Translator is a cloud based automatic translation service. (Refer here for more detail).

Here is another site for Microsoft Translator API.

Installation

    go get github.com/kkdai/mstranslator

How to use it

Sign-up for Microsoft Translator API (see here for more detail) and get your developer credentials. Use the client ID and secret to instantiate a translator as shown below.

        package main
        
        import (
        	"fmt"
        	"log"
        
                ms "github.com/kkdai/mstranslator"
                )
        
        func main() {
        	msClient := ms.NewClient("YourClientID", "YourClientSecret")
        
        	//Translate "Hello World" from English to France.
        	translation, err := msClient.Translate("Hello World!", "en", "de")
        	if err != nil {
        		log.Panicf("Error : %s", err.Error())
        	}
        	fmt.Println(translation) //Hallo Welt!        
        }

Check example for more detail.

Example on mstranslator

image

Implemented APIs

Unimplement APIs (Yet)

Contribute

Please open up an issue on GitHub before you put a lot efforts on pull request. The code submitting to PR must be filtered with gofmt

Inspired

This project is inspired by https://github.com/st3v/translator.

Project52

It is one of my project 52.

License

This package is licensed under MIT license. See LICENSE for details.

Bitdeli Badge

Documentation

Overview

Package mstranslator is a tools to access Microsoft Translator marketplace API. For more detail, please refer to https://www.microsoft.com/translator/api.aspx

Index

Constants

View Source
const (
	API_URL   = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/"
	API_SCOPE = "http://api.microsofttranslator.com"

	TransformTextURL = "http://api.microsofttranslator.com/V3/json/TransformText"

	ServiceURL                  = "http://api.microsofttranslator.com/v2/Http.svc/"
	TranslationURL              = ServiceURL + "Translate"
	GetTranslationsURL          = ServiceURL + "GetTranslations"
	DetectURL                   = ServiceURL + "Detect"
	DetectArrayURL              = ServiceURL + "DetectArray"
	SpeakURL                    = ServiceURL + "Speak"
	GetLanguageNamesURL         = ServiceURL + "GetLanguageNames"
	GetLanguagesForTranslateURL = ServiceURL + "GetLanguagesForTranslate"
	GetLanguagesForSpeakURL     = ServiceURL + "GetLanguagesForSpeak"
)

Store all API URI address.

Variables

This section is empty.

Functions

This section is empty.

Types

type Authenicator

type Authenicator struct {
	ClientId     string
	ClientSecret string
	ClientToken  string
}

func NewAuthenicator

func NewAuthenicator(cid, csecret string) *Authenicator

func (*Authenicator) GetToken

func (a *Authenicator) GetToken() string

type Client

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

func NewClient

func NewClient(clientID, clientSecret string) *Client

func (*Client) Detect

func (c *Client) Detect(text string) (string, error)

Use the Detect Method to identify the language of a selected piece of text.

func (*Client) DetectArray

func (c *Client) DetectArray(textArray []string) ([]string, error)

Use the DetectArray Method to identify the language of an array of string at once. Performs independent detection of each individual array element and returns a result for each row of the array.

func (*Client) GetLanguageNames

func (c *Client) GetLanguageNames(codes []string) ([]string, error)

Retrieves friendly names for the languages passed in as the parameter languageCodes, and localized using the passed locale language.

func (*Client) GetLanguagesForSpeak

func (c *Client) GetLanguagesForSpeak() ([]string, error)

Retrieves the languages available for speech synthesis.

func (*Client) GetLanguagesForTranslate

func (c *Client) GetLanguagesForTranslate() ([]string, error)

Obtain a list of language codes representing languages that are supported by the Translation Service. Translate() and TranslateArray() can translate between any two of these languages.

func (*Client) GetTranslations

func (c *Client) GetTranslations(text, from, to string, maxTranslations int) ([]ResponseTranslationMatch, error)

Retrieves an array of translations for a given language pair from the store and the MT engine. GetTranslations differs from Translate as it returns all available translations.

func (*Client) Speak

func (c *Client) Speak(text, lang, outFormat string) ([]byte, error)

Returns a wave or mp3 stream of the passed-in text being spoken in the desired language.

func (*Client) TransformText

func (c *Client) TransformText(lang, category, text string) (string, error)

The TransformText method is a text normalization function for social media, which returns a normalized form of the input. The method can be used as a preprocessing step in Machine Translation or other applications, which expect clean input text than is typically found in social media or user-generated content. The function currently works only with English input.

func (*Client) Translate

func (c *Client) Translate(text, from, to string) (string, error)

Translates a text string from one language to another.

type GetTranslationsResponse

type GetTranslationsResponse struct {
	XMLName      xml.Name             `xml:"GetTranslationsResponse"`
	Translations ResponseTranslations `xml:"Translations"`
}

type LanguageProvider

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

func NewLanguageProvider

func NewLanguageProvider(auth *Authenicator) *LanguageProvider

func (*LanguageProvider) Detect

func (l *LanguageProvider) Detect(text string) (string, error)

func (*LanguageProvider) DetectArray

func (l *LanguageProvider) DetectArray(text []string) ([]string, error)

func (*LanguageProvider) GetLanguageNames

func (l *LanguageProvider) GetLanguageNames(codes []string) ([]string, error)

Retrieves friendly names for the languages passed in as the parameter languageCodes, and localized using the passed locale language.

func (*LanguageProvider) GetLanguagesForSpeak

func (l *LanguageProvider) GetLanguagesForSpeak() ([]string, error)

func (*LanguageProvider) GetLanguagesForTranslate

func (l *LanguageProvider) GetLanguagesForTranslate() ([]string, error)

func (*LanguageProvider) GetTranslations

func (l *LanguageProvider) GetTranslations(text, from, to string, maxTranslations int) ([]ResponseTranslationMatch, error)

type ResponseArray

type ResponseArray struct {
	XMLName           xml.Name `xml:"ArrayOfstring"`
	Namespace         string   `xml:"xmlns,attr"`
	InstanceNamespace string   `xml:"xmlns:i,attr"`
	Strings           []string `xml:"string"`
}

type ResponseToken

type ResponseToken struct {
	TokenType   string `json:"token_type"`
	AccessToken string `json:"access_token"`
	ExpiresIn   string `json:"expires_in"`
	Scope       string `json:"scope"`
}

type ResponseTranslationMatch

type ResponseTranslationMatch struct {
	Count               XMLIntValue    `xml:"Count"`
	MatchDegree         XMLIntValue    `xml:"MatchDegree"`
	MatchedOriginalText XMLStringValue `xml:"MatchedOriginalText"`
	Rating              XMLIntValue    `xml:"Rating"`
	TranslatedText      XMLStringValue `xml:"TranslatedText"`
}

type ResponseTranslations

type ResponseTranslations struct {
	TransMatch []ResponseTranslationMatch `xml:"TranslationMatch"`
}

type ResponseXML

type ResponseXML struct {
	XMLName   xml.Name `xml:"string"`
	Namespace string   `xml:"xmlns,attr"`
	Value     string   `xml:",innerxml"`
}

type TransformTextResponse

type TransformTextResponse struct {
	ErrorCondition   int    `json:"ec"`       // A positive number representing an error condition
	ErrorDescriptive string `json:"em"`       // A descriptive error message
	Sentence         string `json:"sentence"` // transformed text
}

type TranslateProvider

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

func NewTranslateProvider

func NewTranslateProvider(auth *Authenicator) *TranslateProvider

func (*TranslateProvider) Speak

func (t *TranslateProvider) Speak(text, lang, outFormat string) ([]byte, error)

func (*TranslateProvider) TransformText

func (t *TranslateProvider) TransformText(lang, category, text string) (string, error)

func (*TranslateProvider) Translate

func (t *TranslateProvider) Translate(text, from, to string) (string, error)

type XMLIntValue

type XMLIntValue struct {
	Text int `xml:",chardata"`
}

type XMLStringValue

type XMLStringValue struct {
	Text string `xml:",chardata"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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