@@ -44,39 +44,49 @@ def _extractParts(self, text):
44
44
tmp = [x for x in tmp if (len (x ) == 2 ) and (len (x [0 ]) > 0 ) and (len (x [1 ]) > 0 )]
45
45
return {k : v for k , v in tmp }
46
46
47
- def translate (self , text , fastTranslation , language ):
48
- # run shallow translation
49
- res = self ._translateShallow .run ({
50
- 'UserInput' : text ,
51
- 'FastTranslation' : fastTranslation ,
52
- 'Language' : language
53
- })
47
+ def _executePrompt (self , prompt , variables ):
48
+ res = prompt .run (variables )
54
49
logging .info (res )
55
- # extract translation
56
- translation = self ._extractParts (res )
57
- logging .info (json .dumps (translation , indent = 2 ))
50
+ res = self ._extractParts (res )
51
+ logging .info (json .dumps (res , indent = 2 ))
58
52
flags = {
59
53
k : v .lower () == 'yes'
60
- for k , v in translation .items ()
54
+ for k , v in res .items ()
61
55
if v .lower () in ['yes' , 'no' ]
62
56
}
57
+ # remove flags from result
58
+ res = {k : v for k , v in res .items () if k not in flags }
59
+ # add flags as separate variable
60
+ res ['Flags' ] = flags
61
+ return res
62
+
63
+ def translate (self , text , fastTranslation , language ):
64
+ # run shallow translation
65
+ res = self ._executePrompt (
66
+ self ._translateShallow ,
67
+ {
68
+ 'UserInput' : text ,
69
+ 'FastTranslation' : fastTranslation ,
70
+ 'Language' : language
71
+ }
72
+ )
73
+ flags = res ['Flags' ]
63
74
totalIssues = sum ([int (v ) for k , v in flags .items ()])
64
75
if totalIssues < 2 :
65
- yield translation ['Translation' ]
76
+ yield res ['Translation' ]
66
77
return # all ok, no need to run deep translation
67
- yield translation ['Translation' ] + '\n \n \n ' + translation .get ('Notification' , '' )
78
+ yield res ['Translation' ] + '\n \n \n ' + res .get ('Notification' , '' )
68
79
69
80
# run deep translation
70
- res = self ._translateDeep .run ({
71
- 'UserInput' : text ,
72
- 'FastTranslation' : translation ['Translation' ], # use shallow translation as reference
73
- 'Language' : language ,
74
- 'InputLanguage' : translation .get ('Input language' , 'unknown' ),
75
- 'Flags' : ', ' .join ([k for k , v in flags .items () if v ])
76
- })
77
- logging .info (res )
78
- # extract final translation
79
- translation = self ._extractParts (res )
80
- logging .info (json .dumps (translation , indent = 2 ))
81
- yield translation ['Translation' ]
81
+ res = self ._executePrompt (
82
+ self ._translateDeep ,
83
+ {
84
+ 'UserInput' : text ,
85
+ 'FastTranslation' : res ['Translation' ], # use shallow translation as reference
86
+ 'Language' : language ,
87
+ 'InputLanguage' : res .get ('Input language' , 'unknown' ),
88
+ 'Flags' : ', ' .join ([k for k , v in flags .items () if v ])
89
+ }
90
+ )
91
+ yield res ['Translation' ]
82
92
return
0 commit comments