12
12
from zeeguu .api .utils .translator import (
13
13
get_next_results ,
14
14
contribute_trans ,
15
+ google_contextual_translate ,
16
+ microsoft_contextual_translate ,
15
17
)
16
18
from zeeguu .core .crowd_translations import (
17
19
get_own_past_translation ,
@@ -71,38 +73,37 @@ def get_one_translation(from_lang_code, to_lang_code):
71
73
user , word_str , from_lang_code , to_lang_code , context
72
74
)
73
75
if bookmark :
74
- best_guess = bookmark .meaning .translation .content
76
+ translation = bookmark .meaning .translation .content
75
77
likelihood = 1
76
78
source = "Own past translation"
77
79
print (f"about to return { bookmark } " )
78
80
else :
79
81
# TODO: must remove theurl, and title - they are not used in the calling method.
80
82
if IS_DEV_SKIP_TRANSLATION :
81
83
print ("Dev Skipping Translation" )
82
- best_guess = f"T-({ to_lang_code } )-'{ word_str } '"
84
+ translation = f"T-({ to_lang_code } )-'{ word_str } '"
83
85
likelihood = None
84
86
source = "DEV_SKIP"
85
87
else :
86
- translations = get_next_results (
87
- {
88
- "from_lang_code" : from_lang_code ,
89
- "to_lang_code" : to_lang_code ,
90
- "word" : word_str ,
91
- "query" : query ,
92
- "context" : context ,
93
- },
94
- number_of_results = 3 ,
95
- ).translations
96
- best_guess = translations [0 ]["translation" ]
97
- likelihood = translations [0 ].pop ("quality" )
98
- source = translations [0 ].pop ("service_name" )
88
+ # The API Mux is misbehaving and will only serve the non-contextual translators after a while
89
+ # For now hardcoding google on the first place
90
+
91
+ data = {
92
+ "source_language" : from_lang_code ,
93
+ "target_language" : to_lang_code ,
94
+ "word" : word_str ,
95
+ "query" : query ,
96
+ "context" : context ,
97
+ }
98
+ t1 = google_contextual_translate (data )
99
+
99
100
user = User .find_by_id (flask .g .user_id )
100
101
bookmark = Bookmark .find_or_create (
101
102
db_session ,
102
103
user ,
103
104
word_str ,
104
105
from_lang_code ,
105
- best_guess ,
106
+ t1 [ "translation" ] ,
106
107
to_lang_code ,
107
108
context ,
108
109
article_id ,
@@ -120,10 +121,10 @@ def get_one_translation(from_lang_code, to_lang_code):
120
121
121
122
return json_result (
122
123
{
123
- "translation" : best_guess ,
124
+ "translation" : t1 [ "translation" ] ,
124
125
"bookmark_id" : bookmark .id ,
125
- "source" : source ,
126
- "likelihood" : likelihood ,
126
+ "source" : t1 [ " source" ] ,
127
+ "likelihood" : t1 [ " likelihood" ] ,
127
128
}
128
129
)
129
130
@@ -161,32 +162,16 @@ def get_multiple_translations(from_lang_code, to_lang_code):
161
162
query = TranslationQuery .for_word_occurrence (word_str , context , 1 , 7 )
162
163
163
164
data = {
164
- "from_lang_code " : from_lang_code ,
165
- "to_lang_code " : to_lang_code ,
165
+ "source_language " : from_lang_code ,
166
+ "target_language " : to_lang_code ,
166
167
"word" : word_str ,
167
168
"query" : query ,
168
169
"context" : context ,
169
170
}
171
+ t1 = google_contextual_translate (data )
172
+ t2 = microsoft_contextual_translate (data )
170
173
171
- translations = get_next_results (
172
- data ,
173
- exclude_services = exclude_services ,
174
- exclude_results = exclude_results ,
175
- number_of_results = number_of_results ,
176
- ).translations
177
-
178
- # translators talk about quality, but our users expect likelihood.
179
- # rename the key in the dictionary
180
- for t in translations :
181
- t ["likelihood" ] = t .pop ("quality" )
182
- t ["source" ] = t ["service_name" ]
183
-
184
- # ML: Note: We used to save the first bookmark here;
185
- # but that does not make sense; this is used to show all
186
- # alternatives; why save the first to the DB?
187
- # But leaving this note here just in case...
188
-
189
- return json_result (dict (translations = translations ))
174
+ return json_result (dict (translations = [t1 , t2 ]))
190
175
191
176
192
177
@api .route ("/update_bookmark/<bookmark_id>" , methods = ["POST" ])
0 commit comments