Skip to content

Commit 514988e

Browse files
committed
ready for PR
1 parent ca510e5 commit 514988e

1 file changed

Lines changed: 79 additions & 6 deletions

File tree

  • services/language_translation

services/language_translation/v2.js

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,26 +118,25 @@ module.exports = function (RED) {
118118
if (!err) {
119119
fs.write(info.fd, msg.payload);
120120
var params = {};
121-
122121
switch (filetype) {
123122
case 'forcedglossary':
124123
params = {
125124
name: msg.filename.replace(/[^0-9a-z]/gi, ''),
126-
base_model_id: 'en-es',
125+
base_model_id: model_id,
127126
forced_glossary: fs.createReadStream(info.path)
128127
};
129128
break;
130129
case 'parallelcorpus':
131130
params = {
132131
name: msg.filename.replace(/[^0-9a-z]/gi, ''),
133-
base_model_id: 'en-es',
132+
base_model_id: model_id,
134133
parallel_corpus: fs.createReadStream(info.path)
135134
};
136135
break;
137136
case 'monolingualcorpus':
138137
params = {
139138
name: msg.filename.replace(/[^0-9a-z]/gi, ''),
140-
base_model_id: 'en-es',
139+
base_model_id: model_id,
141140
monolingual_corpus: fs.createReadStream(info.path)
142141
};
143142
break;
@@ -160,6 +159,7 @@ module.exports = function (RED) {
160159
text: 'model sent to training'
161160
});
162161
msg.payload = 'Model ' + model.name + ' successfully sent for training with id: ' + model.model_id;
162+
msg.id = model_id;
163163
node.send(msg);
164164
node.status({});
165165
}
@@ -168,6 +168,66 @@ module.exports = function (RED) {
168168
});
169169
}
170170

171+
this.doGetStatus = function(msg, trainid) {
172+
language_translation = watson.language_translation({
173+
username: username,
174+
password: password,
175+
version: 'v2'
176+
});
177+
178+
node.status({
179+
fill: 'blue',
180+
shape: 'dot',
181+
text: 'requesting status'
182+
});
183+
184+
language_translation.getModel({ model_id: trainid},
185+
function(err, model) {
186+
node.status({});
187+
if (err) {
188+
node.status({
189+
fill: 'red',
190+
shape: 'ring',
191+
text: 'call to translation service failed'
192+
});
193+
node.error(err, msg);
194+
} else {
195+
msg.payload = model.status;
196+
node.send(msg);
197+
node.status({});
198+
}
199+
}
200+
);
201+
}
202+
203+
this.doDelete = function(msg, trainid) {
204+
language_translation = watson.language_translation({
205+
username: username,
206+
password: password,
207+
version: 'v2'
208+
});
209+
210+
node.status({
211+
fill: 'blue',
212+
shape: 'dot',
213+
text: 'deleting'
214+
});
215+
216+
language_translation.deleteModel({ model_id:'{model_id}'},
217+
function(err) {
218+
node.status({});
219+
if (err) {
220+
node.status({
221+
fill: 'red',
222+
shape: 'ring',
223+
text: 'could not delete'
224+
});
225+
node.error(err, msg);
226+
}
227+
}
228+
);
229+
}
230+
171231
this.on('input', function (msg) {
172232
var message = '';
173233

@@ -217,6 +277,9 @@ module.exports = function (RED) {
217277
return;
218278
}
219279

280+
var trainid = msg.trainid || config.trainid;
281+
var basemodel = msg.basemodel || config.basemodel;
282+
220283
username = username || this.credentials.username;
221284
password = password || this.credentials.password;
222285

@@ -237,8 +300,12 @@ module.exports = function (RED) {
237300
version: 'v2'
238301
});
239302

240-
var model_id = srclang + '-' + destlang +
241-
(domain === 'news' ? '' : '-conversational');
303+
var model_id = "";
304+
if(domain === "news") {
305+
model_id = srclang + '-' + destlang;
306+
} else {
307+
model_id = srclang + '-' + destlang + '-' + domain;
308+
}
242309

243310
switch (action) {
244311
case 'translate':
@@ -247,6 +314,12 @@ module.exports = function (RED) {
247314
case 'train':
248315
this.doTrain(msg, model_id, filetype);
249316
break;
317+
case 'getstatus':
318+
this.doGetStatus(msg, trainid);
319+
break;
320+
case 'delete':
321+
this.doDelete(msg, trainid);
322+
break;
250323
}
251324
});
252325
}

0 commit comments

Comments
 (0)