Skip to content

Commit

Permalink
Merge pull request #244 from chughts/sttwords
Browse files Browse the repository at this point in the history
STT Customisation - Words Support
  • Loading branch information
chughts committed Jan 25, 2017
2 parents c642d21 + 0826942 commit c208703
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 21 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ Node-RED Watson Nodes for IBM Bluemix

<a href="https://cla-assistant.io/watson-developer-cloud/node-red-node-watson"><img src="https://cla-assistant.io/readme/badge/watson-developer-cloud/node-red-node-watson" alt="CLA assistant" /></a>

### New in version 0.4.37
- Added support for word training in Speech to Text Customizations Node.

### New in version 0.4.36
- New Node for Speech to Text Customizations - Initial support for corpus (sentances) training.
- New Node for Speech to Text Customizations - Initial support for corpus (sentences) training.
- Added option to select Customization in Speech to Text Node.

### New in version 0.4.35
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-node-watson",
"version": "0.4.36",
"version": "0.4.37",
"description": "A collection of Node-RED nodes for IBM Watson services",
"dependencies": {
"alchemy-api": "^1.3.0",
Expand Down
2 changes: 1 addition & 1 deletion services/language_translator_identify/v2.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
</script>

<script type="text/x-red" data-help-name="watson-language-translator-identify">
<p>Packaged in with release 0.4.36 of node-red-node-watson</p>
<p>Packaged in with release 0.4.37 of node-red-node-watson</p>
<p>The Watson Language Translator service can be used to identify languages used in a text input. <p>
<p>Node input : </p>
<ul>
Expand Down
46 changes: 33 additions & 13 deletions services/speech_to_text/v1-corpus-builder.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
<option value="addCorpus">Add Corpus</option>
<option value="getCorpora">Get Corpora</option>
<option value="train">Train</option>
<option value="listCustomWords">List Custom Words</option>
<option value="addWords">Add Words</option>
<option value="deleteCorpus">Delete Corpus</option>
</select>
</div>
Expand Down Expand Up @@ -81,6 +83,11 @@
<input type="text" id="node-input-stt-corpus-name" placeholder="">
</div>

<div class="form-row">
<label for="node-input-stt-allow-overwrite"><i class="fa fa-book"></i> Allow Overwrite</label>
<input type="checkbox" id="node-input-stt-allow-overwrite" placeholder="">
</div>


</script>

Expand Down Expand Up @@ -125,6 +132,25 @@
by the speech to text service.
</p>
</li>
<li>List Custom Words
<p>In this mode, a list of all the words from a custom model are
retrieved.
</p>
</li>
<li>Add Words
<p>In this mode, the specified custom words are added to the
customization. The input array of words should be passed in
as <code>msg.payload</code> in form of
<code>[{word: String, sounds_like: [String, ...], display_as: String}, ...]</code>
<br/>EG.
<code>
[ {"word": "HHonors", "sounds_like": ["hilton honors", "h honors"],
"display_as": "HHonors"},
{"word": "IEEE", "sounds_like": ["i triple e"]}
]
</code>
</p>
</li>
<li>Delete Corpus
<p>In this mode, the specified corpus is deleted.
</p>
Expand All @@ -150,17 +176,6 @@
sttv1qbb.mode_selected = null;
sttv1qbb.model_selected = '';


sttv1qbb.LANGUAGES = { 'en-US': 'US English',
'pt-BR': 'Portuguese Braziilian',
'en-UK': 'UK English' ,
'fr-FR': 'French',
'zh-CN': 'Mandarin',
'es-ES': 'Spanish',
'ar-AR': 'Arablic',
'ja-JP': 'Japanese'
};

// Function to be used at the start, as don't want to expose any fields, unless the models are
// available. The models can only be fetched if the credentials are available.
sttv1qbb.hideEverything = function () {
Expand All @@ -173,6 +188,7 @@
$('input#node-input-stt-custom-model-description').parent().hide();
$('input#node-input-stt-custom-id').parent().hide();
$('input#node-input-stt-corpus-name').parent().hide();
$('input#node-input-stt-allow-overwrite').parent().hide();
}

sttv1qbb.showSelectedFields = function(fields) {
Expand Down Expand Up @@ -203,12 +219,15 @@
case 'getCustomisation':
case 'getCorpora':
case 'train':
case 'listCustomWords':
case 'addWords':
fields.push('#node-input-stt-custom-id');
break;
case 'addCorpus':
case 'deleteCorpus':
fields.push('#node-input-stt-custom-id'
+ ', #node-input-stt-corpus-name');
+ ', #node-input-stt-corpus-name'
+ ', #node-input-stt-allow-overwrite');
break;
}
sttv1qbb.showSelectedFields(fields);
Expand Down Expand Up @@ -366,7 +385,8 @@
'base-model-hidden' : {value: ''},
'stt-custom-model-description' : {value: ''},
'stt-custom-id' : {value: ''},
'stt-corpus-name' : {value: ''}
'stt-corpus-name' : {value: ''},
'stt-allow-overwrite' : {value: false}
},
credentials: {
username: {type:'text'} //,
Expand Down
72 changes: 67 additions & 5 deletions services/speech_to_text/v1-corpus-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,31 @@ module.exports = function (RED) {
});
}

function executeGetCustomWords(node, stt, params, msg) {
stt.getWords(params, function (err, response) {
node.status({});
if (err) {
reportError(node, msg, err);
} else {
msg['words'] = response.words ? response.words : response ;
}
node.send(msg);
});
}

function executeAddWords(node, stt, params, msg) {
stt.addWords(params, function (err, response) {
node.status({});
if (err) {
reportError(node, msg, err);
} else {
msg['addwordsresponse'] = response ;
}
node.send(msg);
});
}


function executeDeleteCorpus(node, stt, params, msg) {
stt.deleteCorpus(params, function (err, response) {
node.status({});
Expand Down Expand Up @@ -164,6 +189,12 @@ module.exports = function (RED) {
case 'train':
executeTrain(node, stt, params, msg);
break;
case 'listCustomWords':
executeGetCustomWords(node, stt, params, msg);
break;
case 'addWords':
executeAddWords(node, stt, params, msg);
break;
case 'deleteCorpus':
executeDeleteCorpus(node, stt, params, msg);
break;
Expand All @@ -189,10 +220,25 @@ module.exports = function (RED) {
params['customization_id'] = config['stt-custom-id'];
}

if ('addCorpus' === config['stt-custom-mode']) {
params['allow_overwrite'] = config['stt-allow-overwrite'];
}

return params;
}

function loadCorpusFile(node, method, params, msg) {
function setFileParams(method, params, msg) {
switch (method) {
case 'addCorpus':
params.corpus = msg.payload;
break;
case 'addWords':
params.words = msg.payload;
break;
}
}

function loadFile(node, method, params, msg) {
temp.open({
suffix: '.txt'
}, function(err, info) {
Expand All @@ -217,15 +263,28 @@ module.exports = function (RED) {
throw err;
}

params.corpus = fs.createReadStream(info.path);
switch (method) {
case 'addCorpus':
params.corpus = fs.createReadStream(info.path);
break;
case 'addWords':
try {
params.words = JSON.parse(fs.readFileSync(info.path, 'utf8'));
} catch (err) {
params.words = fs.createReadStream(info.path);
}
}

executeMethod(node, method, params, msg);
temp.cleanup();
});
});
}

function checkForFile(method) {
switch (method) {
case 'addCorpus':
case 'addWords':
return true;
}
return false;
Expand Down Expand Up @@ -286,11 +345,14 @@ module.exports = function (RED) {
}

if (checkForFile(method)) {
loadCorpusFile(node, method, params, msg);
} else {
executeMethod(node, method, params, msg);
if (msg.payload instanceof Buffer) {
loadFile(node, method, params, msg);
return;
}
setFileParams(method, params, msg);
}

executeMethod(node, method, params, msg);
});
}

Expand Down

0 comments on commit c208703

Please sign in to comment.