Skip to content

Commit

Permalink
Merge pull request #192 from chughts/STTFix
Browse files Browse the repository at this point in the history
Fixed name space polution in STT node
  • Loading branch information
chughts authored Sep 20, 2016
2 parents 575bd08 + 543de08 commit 995d314
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 169 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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.15
- Name space fixes to Speech to Text Node

### New in version 0.4.14
- The dialog for the Text to Speech service now loads available voices dynamically. This allows
Expand Down
200 changes: 101 additions & 99 deletions services/speech_to_text/v1.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
</div>
</div>

<div>
<label id="node-label-message"><i class="fa fa-exclamation-triangle"></i></label>
<div>
<label id="node-label-message"><i class="fa fa-exclamation-triangle"></i></label>
</div>

<div class="form-row">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"></input>
</div>
Expand Down Expand Up @@ -61,7 +61,7 @@
<label for="node-input-continuous"><i class="fa fa-audio-o"></i> Continuous</label>
<input type="checkbox" id="node-input-continuous"></input>
</select>
</div>
</div>

</script>

Expand All @@ -83,7 +83,7 @@
automatically but the is not true in reverse.</p>
<p>Use the continuous property to choose whether the decoding should stop at the first pause or wait until the end of the file.</p>
<p>The returned audio transcription will be returned on <code>msg.transcription</code>.</p>
<p>The full response, including alternative transcriptions can be found on
<p>The full response, including alternative transcriptions can be found on
<code>msg.fullresult</code>.</p>
<p>For more information about the Speech To Text service, read the <a href="https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/speech-to-text.html">documentation</a>.</p>
</script>
Expand All @@ -92,81 +92,82 @@

// Need to simulate a namespace, as some of the variables had started to leak across nodes
function STT () {
var models = null;
var LANGUAGES = null;
var languages = null;
var bands = null;
var language_selected = '';
var band_selected = '';
}

// This is the namespace for stt. Currently only contains models, but more vars and functions may need to be
}


// This is the namespace for stt. Currently only contains models, but more vars and functions may need to be
// moved in if there is a clash with other nodes.
var stt = new STT();

stt.LANGUAGES = { 'en-US' : 'US English',
'pt-BR': 'Portuguese Braziilian',
'en-UK': 'UK English' ,
'zh-CN': 'Mandarin',
'es-ES': 'Spanish',
'ar-AR': 'Arablic',
stt.models = null;
stt.languages = null;
stt.bands = null;
stt.language_selected = '';
stt.band_selected = '';

stt.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'
};

// sorting functions
function onlyUnique(value, index, self) {
stt.onlyUnique = function (value, index, self) {
return self.indexOf(value) === index;
}

// 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.
function hideEverything() {
// 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.
stt.hideEverything = function () {
if (!stt.models) {
$('#credentials-not-found').show();
$('label#node-label-message').parent().hide();
$('input#node-input-continuous').parent().hide();
$('label#node-label-message').parent().hide();
$('input#node-input-continuous').parent().hide();
$('select#node-input-lang').parent().hide();
$('select#node-input-band').parent().hide();
}
$('select#node-input-band').parent().hide();
}
}

// Check if there is a model then can show the fields.
// available. The models can only be fetched if the credentials are available.
function visibilityCheck() {
// Check if there is a model then can show the fields.
// available. The models can only be fetched if the credentials are available.
stt.VisibilityCheck = function () {
if (stt.models) {
$('label#node-label-message').parent().hide();
$('input#node-input-continuous').parent().show();
$('select#node-input-lang').parent().show();
$('select#node-input-band').parent().show();
$('label#node-label-message').parent().hide();
$('input#node-input-continuous').parent().show();
$('select#node-input-lang').parent().show();
$('select#node-input-band').parent().show();
} else {
$('label#node-label-message').parent().hide();
$('input#node-input-continuous').parent().hide();
$('select#node-input-lang').parent().hide();
$('select#node-input-band').parent().hide();
}
$('label#node-label-message').parent().hide();
$('input#node-input-continuous').parent().hide();
$('select#node-input-lang').parent().hide();
$('select#node-input-band').parent().hide();
}
}


// Simple check that is only invoked if the service is not bound into bluemix. In this case the
// user has to provide credentials. Once there are credentials, then the stt.models are retrieved.
function checkCredentials() {
// user has to provide credentials. Once there are credentials, then the stt.models are retrieved.
stt.checkCredentials = function () {
var u = $('#node-input-username').val();
var p = $('#node-input-password').val();

if (u && u.length && p) {
if (u && u.length && p) {
if (!stt.models) {
getModels();
}
}
stt.getModels();
}
}
}

// Populate the quality select field
function populateBands() {
stt.populateBands = function () {
if (!stt.bands && stt.models) {
stt.bands = stt.models.map(function(m) {
return m.name.split('_')[1];
});
var unique_bands = stt.bands.filter(onlyUnique);
var unique_bands = stt.bands.filter(stt.onlyUnique);
stt.bands = unique_bands;
}

Expand All @@ -183,52 +184,54 @@
+ '"' + b + '"'
+ selectedText
+ '>'
+ b
+ '</option>');
+ b
+ '</option>');
});
}
}

// Register the handlers for the fields
function handlersUI() {
stt.handlersUI = function () {
$('#node-input-username').change(function(val){
checkCredentials();
});
stt.checkCredentials();
});
$('#node-input-password').change(function(val){
checkCredentials();
});
stt.checkCredentials();
});

$('#node-input-lang').change(function (val) {
stt.language_selected = $('#node-input-lang').val();
populateBands();
});
stt.populateBands();
});

$('#node-input-band').change(function (val) {
stt.band_selected = $('#node-input-band').val();
});
});
}

// Function called when either when the models have been retrieved, or
// on dialog load, if the model has already been retrieved
function postModelCheck() {
processModels();
visibilityCheck();
stt.postModelCheck = function () {
stt.processModels();
stt.VisibilityCheck();
}

// Retrieve the available models from the server, if data is returned, then
// can enable the dynamic selection fields.
function getModels(haveCredentials) {
// Retrieve the available models from the server, if data is returned, then
// can enable the dynamic selection fields.
stt.getModels = function (haveCredentials) {
var u = $('#node-input-username').val();
var p = $('#node-input-password').val();

$.getJSON('watson-speech-to-text/models/', {un: u, pwd: p}).done(function (data) {
if (data.error) {
$('label#node-label-message').parent().show();
$('label#node-label-message').text(data.error);
$('label#node-label-message').text(data.error);
} else if (data.models) {
console.log('models found');
console.log(data.models);
stt.models = data.models;
//have_credentials = true;
postModelCheck();
//have_credentials = true;
stt.postModelCheck();
}
}).fail(function (err) {
$('label#node-label-message').parent().show();
Expand All @@ -238,15 +241,15 @@
}

// Called to complete the languages selection table
function processLanguages() {
stt.processSTTLanguages = function () {
if (!stt.languages && stt.models) {
stt.languages = stt.models.map(function(m) {
return m.language;
});
}
if (stt.languages) {
$('select#node-input-lang').empty();
var unique_langs = stt.languages.filter(onlyUnique);
var unique_langs = stt.languages.filter(stt.onlyUnique);

unique_langs.forEach(function(l) {
var selectedText = '';
Expand All @@ -258,49 +261,48 @@
+ '"' + l + '"'
+ selectedText
+ '>'
+ (stt.LANGUAGES[l] ? stt.LANGUAGES[l] : l)
+ '</option>');
+ (stt.LANGUAGES[l] ? stt.LANGUAGES[l] : l)
+ '</option>');
});

}
}

// Called to work through the models, completing the dyanmic selection fields.
function processModels() {
stt.processModels = function () {
if (stt.models) {
processLanguages();
populateBands();
stt.processSTTLanguages();
stt.populateBands();
}
}

// The dynamic nature of the selection fields in this node has caused problems.
// Whenever there is a fetch for the models, on a page refresh or applicaiton
// restart, the settings for the dynamic fields are lost.
// Whenever there is a fetch for the models, on a page refresh or applicaiton
// restart, the settings for the dynamic fields are lost.
// So hidden (text) fields are being used to squirrel away the values, so that
// they can be restored.
function restoreFromHidden() {
stt.language_selected = $('#node-input-langhidden').val();
stt.restoreFromHidden = function () {
stt.language_selected = $('#node-input-langhidden').val();
$('select#node-input-lang').val(stt.language_selected);

stt.band_selected = $('#node-input-bandhidden').val();
stt.band_selected = $('#node-input-bandhidden').val();
$('select#node-input-band').val(stt.band_selected);
}
}

// This is the on edit prepare function, which will be invoked everytime the dialog
// is shown.
function oneditprepare() {
hideEverything();
restoreFromHidden();
handlersUI();
// is shown.
function sttoneditprepare() {
stt.hideEverything();
stt.restoreFromHidden();
stt.handlersUI();

$.getJSON('watson-speech-to-text/vcap/')
// for some reason the getJSON resets the vars so need to restoreFromHidden again
// for some reason the getJSON resets the vars so need to restoreFromHidden again
// so again.
.done(function (service) {
restoreFromHidden();
stt.restoreFromHidden();
$('.credentials').toggle(!service);
if (!stt.models) {getModels(service);}
else {postModelCheck();}
if (!stt.models) {stt.getModels(service);}
else {stt.postModelCheck();}
})
.fail(function () {
$('.credentials').show();
Expand All @@ -310,9 +312,9 @@
}

// Save the values in the dyanmic lists to the hidden fields.
function oneditsave(){
$('#node-input-langhidden').val(stt.language_selected);
$('#node-input-bandhidden').val(stt.band_selected);
function sttoneditsave(){
$('#node-input-langhidden').val(stt.language_selected);
$('#node-input-bandhidden').val(stt.band_selected);
}

(function() {
Expand All @@ -324,8 +326,8 @@
lang: {value: ''},
langhidden: {value: ''},
band: {value: ''},
bandhidden: {value: ''},
password: {value: ''}
bandhidden: {value: ''},
password: {value: ''}
},
credentials: {
username: {type:'text'} //,
Expand All @@ -342,9 +344,9 @@
labelStyle: function() {
return this.name ? 'node_label_italic' : '';
},
oneditprepare: oneditprepare,
oneditsave: oneditsave
oneditprepare: sttoneditprepare,
oneditsave: sttoneditsave
});
})();

</script>
</script>
Loading

0 comments on commit 995d314

Please sign in to comment.