support for multiple languages
This commit is contained in:
parent
8f20ef32e7
commit
bd284e9e44
4
app.js
4
app.js
|
@ -45,7 +45,7 @@ class WebSocketServer {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('startRecognition', (lang) => {
|
client.on('startRecognition', (lang, target) => {
|
||||||
if (this.google.speech) {
|
if (this.google.speech) {
|
||||||
this.google.speech.stopRecognition();
|
this.google.speech.stopRecognition();
|
||||||
this.google.speech = null;
|
this.google.speech = null;
|
||||||
|
@ -53,7 +53,7 @@ class WebSocketServer {
|
||||||
|
|
||||||
console.log("Speech Recognition Started");
|
console.log("Speech Recognition Started");
|
||||||
this.google.speech = new Speech()
|
this.google.speech = new Speech()
|
||||||
this.google.speech.startRecognition(lang);
|
this.google.speech.startRecognition(lang, target);
|
||||||
})
|
})
|
||||||
|
|
||||||
client.on('stopRecognition', () => {
|
client.on('stopRecognition', () => {
|
||||||
|
|
|
@ -34,10 +34,10 @@ class Speech {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts the Google API Stream
|
* Starts the Google API Stream
|
||||||
* @param {string} lang - Language Code e.g en-CA
|
* @param {string} lang - Language Code e.g en-CA for Canadian English
|
||||||
|
* @param {string} target - Language Code e.g. "en" for English
|
||||||
*/
|
*/
|
||||||
startRecognition(lang) {
|
startRecognition(lang, target) {
|
||||||
this.lang = lang;
|
|
||||||
this.enabled = true;
|
this.enabled = true;
|
||||||
|
|
||||||
const request = {
|
const request = {
|
||||||
|
@ -55,40 +55,22 @@ class Speech {
|
||||||
.streamingRecognize(request)
|
.streamingRecognize(request)
|
||||||
.on("error", console.error)
|
.on("error", console.error)
|
||||||
.on("data", data => {
|
.on("data", data => {
|
||||||
process.stdout.write(
|
|
||||||
data.results[0] && data.results[0].alternatives[0]
|
|
||||||
? `Transcription: ${data.results[0].alternatives[0].transcript}\n`
|
|
||||||
: `\n\nReached transcription time limit, press Ctrl+C\n`
|
|
||||||
);
|
|
||||||
//client.emit("speechData", data);
|
|
||||||
|
|
||||||
if (data.results[0].alternatives[0] !== undefined) {
|
if (data.results[0].alternatives[0] !== undefined) {
|
||||||
let text = data.results[0].alternatives[0].transcript;
|
let text = data.results[0].alternatives[0].transcript;
|
||||||
|
|
||||||
translate.speech(text, "fr").then(translation => {
|
translate.speech(text, target).then(translation => {
|
||||||
console.log("Translation: " + translation);
|
console.log(`${lang}: ${text} | ${target}: ${translation} (${data.results[0].alternatives[0].confidence})`);
|
||||||
|
|
||||||
}).catch(err => console.error(err));
|
}).catch(err => console.error(err));
|
||||||
|
|
||||||
// translate
|
|
||||||
// .translate(text, target)
|
|
||||||
// .then(results => {
|
|
||||||
// const translation = results[0];
|
|
||||||
// //client.emit("translateData", translation);
|
|
||||||
|
|
||||||
// console.log(`Text: ${text}`);
|
|
||||||
// console.log(`Translation: ${translation}`);
|
|
||||||
// })
|
|
||||||
// .catch(err => {
|
|
||||||
// console.error("ERROR:", err);
|
|
||||||
// });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// if end of utterance, let's restart stream
|
// if end of utterance, let's restart stream
|
||||||
// this is a small hack. After 65 seconds of silence, the stream will still throw an error for speech length limit
|
// this is a small hack. After 65 seconds of silence, the stream will still throw an error for speech length limit
|
||||||
if (data.results[0] && data.results[0].isFinal) {
|
if (data.results[0] && data.results[0].isFinal) {
|
||||||
this.stopRecognition();
|
this.stopRecognition();
|
||||||
this.startRecognition(this.lang);
|
this.startRecognition(lang, target);
|
||||||
// console.log('restarted stream serverside');
|
// console.log('restarted stream serverside');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Reference in New Issue