-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (62 loc) · 1.38 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"log"
"os"
"strings"
"sync"
"toshyak/translate/aws"
"toshyak/translate/spelling"
"toshyak/translate/yadictionary"
"unicode"
)
type tranaslator interface {
Translate(string) chan string
}
var translationDirections = map[string]string{
"ru": "en",
"en": "ru",
}
func main() {
if len(os.Args) < 2 {
log.Fatal("Pass translated text as an argument")
}
textToTranslate := strings.Join(os.Args[1:], " ")
sourceLanguage := getSourceLanguage(textToTranslate)
spellingCh := spelling.CheckSpelling(textToTranslate, sourceLanguage)
awsTranslator := aws.NewTranslator(sourceLanguage, translationDirections[sourceLanguage])
awsCh := awsTranslator.Translate(textToTranslate)
yaDictTranslator := yadictionary.NewTranslator(sourceLanguage, translationDirections[sourceLanguage])
yaCh := yaDictTranslator.Translate(textToTranslate)
out := newOutput()
var wg sync.WaitGroup
wg.Add(3)
go func() {
for s := range spellingCh {
out.add(s, "", "speller", false)
}
wg.Done()
}()
go func() {
for s := range yaCh {
out.add(s, "", "ydict", true)
}
wg.Done()
}()
go func() {
for s := range awsCh {
out.add(s, "", "aws", true)
}
wg.Done()
}()
wg.Wait()
out.print()
}
func getSourceLanguage(text string) string {
f := func(r rune) bool {
return unicode.Is(unicode.Cyrillic, r)
}
if strings.IndexFunc(text, f) != -1 {
return "ru"
}
return "en"
}