-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(translator): add ApertiumTranslate (#13)
- Loading branch information
Showing
6 changed files
with
236 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package apertiumtranslate | ||
|
||
var ( | ||
lang = []string{ | ||
"Afrikaans", | ||
"Arabic", | ||
"Aranese", | ||
"Aragonese", | ||
"Arpitan", | ||
"Basque", | ||
"Belarusian", | ||
"Breton", | ||
"Bulgarian", | ||
"Catalan", | ||
"Crimean Tatar", | ||
"Danish", | ||
"Dutch", | ||
"East Norwegian, vi→vi", | ||
"English", | ||
"Esperanto", | ||
"French", | ||
"Galician", | ||
"Gascon", | ||
"Hindi", | ||
"Icelandic", | ||
"Indonesian", | ||
"Italian", | ||
"Kazakh", | ||
"Macedonian", | ||
"Malay", | ||
"Maltese", | ||
"Northern Sami", | ||
"Norwegian Bokmål", | ||
"Norwegian Nynorsk", | ||
"Occitan", | ||
"Polish", | ||
"Portuguese", | ||
"Romanian", | ||
"Russian", | ||
"Serbo-Croatian", | ||
"Silesian", | ||
"Slovenian", | ||
"Spanish", | ||
"Swedish", | ||
"Tatar", | ||
"Turkish", | ||
"Ukrainian", | ||
"Urdu", | ||
"Welsh", | ||
} | ||
langCode = map[string]string{ | ||
"Afrikaans": "afr", | ||
"Arabic": "ara", | ||
"Aranese": "oci_aran", | ||
"Aragonese": "arg", | ||
"Arpitan": "frp", | ||
"Basque": "eus", | ||
"Belarusian": "bel", | ||
"Breton": "bre", | ||
"Bulgarian": "bul", | ||
"Catalan": "cat", | ||
"Crimean Tatar": "crh", | ||
"Danish": "dan", | ||
"Dutch": "nld", | ||
"East Norwegian, vi→vi": "nno_e", | ||
"English": "eng", | ||
"Esperanto": "epo", | ||
"French": "fra", | ||
"Galician": "glg", | ||
"Gascon": "oci_gascon", | ||
"Hindi": "hin", | ||
"Icelandic": "isl", | ||
"Indonesian": "ind", | ||
"Italian": "ita", | ||
"Kazakh": "kaz", | ||
"Macedonian": "mkd", | ||
"Malay": "zlm", | ||
"Maltese": "mlt", | ||
"Northern Sami": "sme", | ||
"Norwegian Bokmål": "nob", | ||
"Norwegian Nynorsk": "nno", | ||
"Occitan": "oci", | ||
"Polish": "pol", | ||
"Portuguese": "por", | ||
"Romanian": "ron", | ||
"Russian": "rus", | ||
"Serbo-Croatian": "hbs", | ||
"Silesian": "szl", | ||
"Slovenian": "slv", | ||
"Spanish": "spa", | ||
"Swedish": "swe", | ||
"Tatar": "tat", | ||
"Turkish": "tur", | ||
"Ukrainian": "ukr", | ||
"Urdu": "urd", | ||
"Welsh": "cym", | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package apertiumtranslate | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/eeeXun/gtt/internal/lock" | ||
) | ||
|
||
const ( | ||
textURL = "https://www.apertium.org/apy/translate?langpair=%s|%s&q=%s" | ||
) | ||
|
||
type ApertiumTranslate struct { | ||
srcLang string | ||
dstLang string | ||
EngineName string | ||
SoundLock *lock.Lock | ||
} | ||
|
||
func (t *ApertiumTranslate) GetEngineName() string { | ||
return t.EngineName | ||
} | ||
|
||
func (t *ApertiumTranslate) GetAllLang() []string { | ||
return lang | ||
} | ||
|
||
func (t *ApertiumTranslate) GetSrcLang() string { | ||
return t.srcLang | ||
} | ||
|
||
func (t *ApertiumTranslate) GetDstLang() string { | ||
return t.dstLang | ||
} | ||
|
||
func (t *ApertiumTranslate) SetSrcLang(srcLang string) { | ||
t.srcLang = srcLang | ||
} | ||
|
||
func (t *ApertiumTranslate) SetDstLang(dstLang string) { | ||
t.dstLang = dstLang | ||
} | ||
|
||
func (t *ApertiumTranslate) SwapLang() { | ||
t.srcLang, t.dstLang = t.dstLang, t.srcLang | ||
} | ||
|
||
func (t *ApertiumTranslate) Translate(message string) (translation, definition, partOfSpeech string, err error) { | ||
var data interface{} | ||
|
||
urlStr := fmt.Sprintf( | ||
textURL, | ||
langCode[t.srcLang], | ||
langCode[t.dstLang], | ||
url.QueryEscape(message), | ||
) | ||
res, err := http.Get(urlStr) | ||
if err != nil { | ||
return "", "", "", err | ||
} | ||
body, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
return "", "", "", err | ||
} | ||
if err = json.Unmarshal(body, &data); err != nil { | ||
return "", "", "", err | ||
} | ||
|
||
if len(data.(map[string]interface{})) > 0 { | ||
switch res.StatusCode { | ||
case 200: | ||
translation += fmt.Sprintf("%v", | ||
data.(map[string]interface{})["responseData"].(map[string]interface{})["translatedText"]) | ||
default: | ||
return "", "", "", errors.New( | ||
fmt.Sprintf("%s does not support translate from %s to %s.\nSee available pair on %s", | ||
t.EngineName, | ||
t.srcLang, | ||
t.dstLang, | ||
"https://www.apertium.org/", | ||
)) | ||
} | ||
|
||
return translation, definition, partOfSpeech, nil | ||
} | ||
return "", "", "", errors.New("Translation not found") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package apertiumtranslate | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
func (t *ApertiumTranslate) LockAvailable() bool { | ||
return t.SoundLock.Available() | ||
} | ||
|
||
func (t *ApertiumTranslate) LockAcquire() { | ||
t.SoundLock.Acquire() | ||
} | ||
|
||
func (t *ApertiumTranslate) StopTTS() { | ||
t.SoundLock.Stop = true | ||
} | ||
|
||
func (t *ApertiumTranslate) PlayTTS(lang, message string) error { | ||
t.SoundLock.Release() | ||
return errors.New(t.EngineName + " does not support text to speech") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters