-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContentProcessor.cs
343 lines (271 loc) · 14.2 KB
/
ContentProcessor.cs
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
using Sdl.FileTypeSupport.Framework.BilingualApi;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
namespace python_caller
{
public class ContentProcessor : AbstractBilingualContentProcessor
{
private MyCustomBatchTaskSettings Settings;
public bool debug;
public ContentProcessor(MyCustomBatchTaskSettings ArgSettings)
{
Settings = ArgSettings;
if (Settings.SettingsPythonFileForDebug.Trim() != "")
{
this.debug = true;
}
}
public override void ProcessParagraphUnit(IParagraphUnit paragraphUnit)
{
base.ProcessParagraphUnit(paragraphUnit);
if (paragraphUnit.IsStructure)
{
return;
}
Dictionary<Sdl.Core.Globalization.ConfirmationLevel, string> statuses_text = new Dictionary<Sdl.Core.Globalization.ConfirmationLevel, string> {
{Sdl.Core.Globalization.ConfirmationLevel.Unspecified ,"Not Translated" },
{Sdl.Core.Globalization.ConfirmationLevel.Draft ,"Draft"},
{Sdl.Core.Globalization.ConfirmationLevel.Translated ,"Translated"},
{Sdl.Core.Globalization.ConfirmationLevel.RejectedTranslation ,"Translation Rejected"},
{Sdl.Core.Globalization.ConfirmationLevel.ApprovedTranslation ,"Translation Approved"},
{Sdl.Core.Globalization.ConfirmationLevel.RejectedSignOff ,"Sign-off Rejected"},
{Sdl.Core.Globalization.ConfirmationLevel.ApprovedSignOff ,"Signed Off"}
};
List<Sdl.Core.Globalization.ConfirmationLevel> statuses = statuses_text.Keys.ToList();
foreach (var segmentPair in paragraphUnit.SegmentPairs)
{
if (segmentPair.Target == null)
{
continue;
}
string start_time = debug ? DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ff") : "";
var segment_number = int.Parse(segmentPair.Properties.Id.ToString());
string old_target = segmentPair.Target.ToString();
string old_source = segmentPair.Source.ToString();
bool skip_for_range = segment_number < Settings.SettingsLimitRangeStart || (segment_number > Settings.SettingsLimitRangeEnd && Settings.SettingsLimitRangeEnd != 0);
string segment_status = statuses_text[segmentPair.Properties.ConfirmationLevel];
string translation_origin = "";
foreach (var b in segmentPair.Properties.TranslationOrigin?.MetaData ?? new Dictionary<string, string>())
{
string temp_key = b.Key.ToString().Trim('"', '\'');
if (temp_key == "SegmentIdentityHash" || temp_key == "SDL:OriginalTranslationHash" || temp_key == "SDL:ProjectTranslationHash") continue;
// translation_origin += " \"" + temp_key + "\" \"" + b.Value.ToString().Trim('"', '\'').Replace(",", " ") + "\"";
translation_origin += $"\"{temp_key}\" \"{b.Value.ToString().Trim('"', '\'').Replace(",", " ")}\" ";
}
bool skip_for_status;
switch (Settings.SettingsFilterStatusIndex)
{
case 0:
skip_for_status = false;
break;
case int n when (1 <= n && n <= 7):
skip_for_status = !segmentPair.Properties.ConfirmationLevel.Equals(statuses[Settings.SettingsFilterStatusIndex - 1]);
break;
default:
skip_for_status = true;
break;
}
bool skip_for_apply_mode;
switch (Settings.SettingsApplyModeIndex)
{
case 0:
skip_for_apply_mode = false;
break;
case 1:
skip_for_apply_mode = old_target.Trim() == "";
break;
case 2:
skip_for_apply_mode = segmentPair.Target.Count != 1 || old_target.Trim() == "";
break;
case 3:
skip_for_apply_mode = segmentPair.Source.Count != 1 || old_source.Trim() == "";
break;
case 4:
skip_for_apply_mode = old_target.Trim() != "";
break;
default:
skip_for_apply_mode = true;
break;
}
if (skip_for_range || skip_for_apply_mode || skip_for_status)
{
if (debug && !skip_for_range)
{
debug_with_python(
segment_number,
segment_status,
segment_status,
old_source,
old_source,
old_target,
old_target,
segmentPair.Target.Count,
start_time,
skip_for_apply_mode ? 1 : 0,
skip_for_range ? 1 : 0,
skip_for_status ? 1 : 0,
-1,
translation_origin);
}
continue;
}
string result = run_python(segmentPair.Properties.Id.ToString(), segment_status, old_source, old_target, translation_origin);
bool skip_for_skipping_keyword = Settings.SettingsUseSkippingKeyword ? result == Settings.SettingsSkippingKeyword : false;
if (!skip_for_skipping_keyword)
{
if (Settings.SettingsUseReturnText)
{
switch (Settings.SettingsReturnTextUsageIndex)
{
case 0:
if (result.Trim() != old_target.Trim())
{
segmentPair.Target.Clear();
segmentPair.Target.Insert(segmentPair.Target.Count, ItemFactory.CreateText(PropertiesFactory.CreateTextProperties(result)));
}
break;
case 2:
if (result.Trim() != old_source.Trim())
{
segmentPair.Source.Clear();
segmentPair.Source.Insert(segmentPair.Source.Count, ItemFactory.CreateText(PropertiesFactory.CreateTextProperties(result)));
}
break;
case 1:
try
{
// check whethere returned value is a number
int temp_index = int.Parse(result.Trim());
if (temp_index >= 0 && temp_index <= 6)
{
segmentPair.Properties.ConfirmationLevel = statuses[temp_index];
}
}
catch (FormatException)
{
int temp_status_index;
switch (result.ToLower().Trim())
{
case "not translated":
temp_status_index = 0;
break;
case "draft":
temp_status_index = 1;
break;
case "translated":
temp_status_index = 2;
break;
case "translation rejected":
temp_status_index = 3;
break;
case "translation approved":
temp_status_index = 4;
break;
case "signed off":
temp_status_index = 6;
break;
case string n when (n == "sign-off rejected" || n == "sign off rejected"):
temp_status_index = 5;
break;
default:
temp_status_index = -1;
break;
}
if (temp_status_index != -1)
{
segmentPair.Properties.ConfirmationLevel = statuses[temp_status_index];
}
}
break;
}
}
if (Settings.SettingsChangeStatus && ((Settings.SettingsUseReturnText && Settings.SettingsReturnTextUsageIndex != 1) || !Settings.SettingsUseReturnText))
{
segmentPair.Properties.ConfirmationLevel = statuses[Settings.SettingsStatusIndex];
}
}
if (debug)
{
debug_with_python(
segment_number,
segment_status,
statuses_text[segmentPair.Properties.ConfirmationLevel],
old_source,
segmentPair.Source.ToString(),
old_target,
segmentPair.Target.ToString(),
segmentPair.Target.Count(),
start_time,
0,
0,
0,
skip_for_skipping_keyword ? 1 : 0,
translation_origin,
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ff"));
}
}
}
public string run_python(string SegmentNumber, string SegmentStatus, string SourceText, string TargetText, string TranslationOrigin)
{
SourceText = SourceText.Replace("\"", "\\\"");
TargetText = TargetText.Replace("\"", "\\\"");
Process start = new Process()
{
StartInfo = new ProcessStartInfo()
{
CreateNoWindow = true,
FileName = Settings.SettingsPythonPath,
Arguments = $"\"{Settings.SettingsPythonFile}\" \"{SegmentNumber}\" \"{SourceText}\" \"{TargetText}\" \"{SegmentStatus}\" {TranslationOrigin}",
RedirectStandardOutput = true,
UseShellExecute = false,
StandardOutputEncoding = Encoding.UTF8,
WorkingDirectory = Path.GetDirectoryName(Settings.SettingsPythonFile)
}
};
start.Start();
// string output = start.StandardOutput.ReadToEnd();
string output = "";
while (!start.StandardOutput.EndOfStream)
{
output += start.StandardOutput.ReadLine();
}
start.Close();
return output;
}
public void debug_with_python(
int segment_id,
string OldSegmentStatus,
string NewSegmentStatus,
string old_source_text,
string new_source_text,
string old_target_text,
string new_target_text,
int old_target_count,
string send_time,
int skip_for_selection_mode,
int skip_for_range,
int skip_for_status,
int skip_for_skipping_keyword,
string TranslationOrigin,
string recieved_time = "0000-00-00 00:00:00.00")
{
Process start = new Process()
{
StartInfo = new ProcessStartInfo()
{
CreateNoWindow = true,
FileName = Settings.SettingsPythonPath,
Arguments = $"\"{Settings.SettingsPythonFileForDebug}\" \"{segment_id}\" \"{OldSegmentStatus}\" \"{NewSegmentStatus}\" \"{old_source_text}\" \"{new_source_text}\" \"{old_target_text}\" \"{new_target_text}\" \"{old_target_count}\" \"{send_time}\" \"{recieved_time}\" \"{skip_for_selection_mode}\" \"{skip_for_range}\" \"{skip_for_status}\" \"{skip_for_skipping_keyword}\" {TranslationOrigin}",
UseShellExecute = false,
WorkingDirectory = Path.GetDirectoryName(Settings.SettingsPythonFileForDebug)
}
};
start.Start();
start.Close();
}
}
}