-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
43 lines (36 loc) · 1.14 KB
/
util.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
package godeepl
import "strings"
// Sentences returns an array of the translated sentences
// using the first result beam of each sentence.
func (tr *TranslationResult) Sentences() []string {
sentences := make([]string, 0, len(tr.Translations))
for _, t := range tr.Translations {
if len(t.Beams) > 0 {
sentences = append(sentences, t.Beams[0].ProcessedSentence)
}
}
return sentences
}
// Assemble takes all sentences of a translation using the first
// beam of each translation and assembles them to one string.
func (tr *TranslationResult) Assemble() string {
return strings.Join(tr.Sentences(), " ")
}
// Translation is a safe getter for a translation with
// the given index. If there is no entry at the given
// index, nil is returned.
func (tr *TranslationResult) Translation(i int) *Translation {
if tr == nil || i < 0 || len(tr.Translations) <= i {
return nil
}
return tr.Translations[i]
}
// Beam is a safe getter for a beam with
// the given index. If there is no entry at the given
// index, nil is returned.
func (t *Translation) Beam(i int) *Beam {
if t == nil || i < 0 || len(t.Beams) <= i {
return nil
}
return t.Beams[i]
}