mirror of
https://github.com/eeeXun/GTT.git
synced 2025-05-28 21:40:12 -07:00
fix: contextResults is nil
This commit is contained in:
parent
2a9c52bcb2
commit
40fe5397ab
@ -90,24 +90,26 @@ func (t *ReversoTranslate) Translate(message string) (translation, definition, p
|
||||
// translation
|
||||
translation += fmt.Sprintf("%v", data["translation"].([]interface{})[0])
|
||||
// definition and part of speech
|
||||
for _, results := range data["contextResults"].(map[string]interface{})["results"].([]interface{}) {
|
||||
results := results.(map[string]interface{})
|
||||
// definition
|
||||
srcExample := results["sourceExamples"].([]interface{})
|
||||
dstExample := results["targetExamples"].([]interface{})
|
||||
if len(srcExample) > 0 && len(dstExample) > 0 {
|
||||
for i := 0; i < len(srcExample) && i < len(dstExample); i++ {
|
||||
definition += fmt.Sprintf("- %v\n\t\"%v\"\n", srcExample[i], dstExample[i])
|
||||
if data["contextResults"] != nil {
|
||||
for _, results := range data["contextResults"].(map[string]interface{})["results"].([]interface{}) {
|
||||
results := results.(map[string]interface{})
|
||||
// definition
|
||||
srcExample := results["sourceExamples"].([]interface{})
|
||||
dstExample := results["targetExamples"].([]interface{})
|
||||
if len(srcExample) > 0 && len(dstExample) > 0 {
|
||||
for i := 0; i < len(srcExample) && i < len(dstExample); i++ {
|
||||
definition += fmt.Sprintf("- %v\n\t\"%v\"\n", srcExample[i], dstExample[i])
|
||||
}
|
||||
}
|
||||
// part of speech
|
||||
if results["partOfSpeech"] == nil {
|
||||
partOfSpeech += fmt.Sprintf("%v\n", results["translation"])
|
||||
} else {
|
||||
partOfSpeech += fmt.Sprintf("%v [%v]\n", results["translation"], results["partOfSpeech"])
|
||||
}
|
||||
}
|
||||
// part of speech
|
||||
if results["partOfSpeech"] == nil {
|
||||
partOfSpeech += fmt.Sprintf("%v\n", results["translation"])
|
||||
} else {
|
||||
partOfSpeech += fmt.Sprintf("%v [%v]\n", results["translation"], results["partOfSpeech"])
|
||||
}
|
||||
definition = regexp.MustCompile("<(|/)em>").ReplaceAllString(definition, "")
|
||||
}
|
||||
definition = regexp.MustCompile("<(|/)em>").ReplaceAllString(definition, "")
|
||||
|
||||
return translation, definition, partOfSpeech, nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user