vin-ni/Google-Cloud-Speech-Node-Socket-Playground

NLP Error

Closed this issue · 7 comments

Screenshot from 2019-12-24 16-13-22
The function NLP in addTimeSettingsInterim() is not working

upon checking in addTimeSettingsInterim()

let word = nlpObject[i].text;

Gives and undefined output and so does the below line

for (let j = 0; j < nlpObject[i].tags.length; j++)

the nlpobject does not have tags and hence there is an error of length.
Can Someone fix this as I am new to JS.
The error can also be present in addTimeSettingsFinal()

change your addTimeSettingsInterim and addTimeSettingsFinal functions like this

function addTimeSettingsInterim(speechData) {

let wholeString = speechData.results[0].alternatives[0].transcript;

let nlpObject = nlp(wholeString).out('terms');

let words_without_time = [];

for (let i = 0; i < nlpObject.length; i++) {
	//data
	let word = nlpObject[i].text;
	let tags = [];

	//generate span
	let newSpan = document.createElement('span');
	newSpan.innerHTML = word;

	//push all tags
	for (let j = 0; j < nlpObject[i].length; j++) {
		tags.push(nlpObject[i]);
	}

	//add all classes
	for (let j = 0; j < nlpObject[i].length; j++) {
		let cleanClassName = tags[j];
		// console.log(tags);
		let className = `nl-${cleanClassName}`;
		newSpan.classList.add(className);
	}

	words_without_time.push(newSpan);
}

finalWord = false;
endButton.disabled = true;

return words_without_time;

}

function addTimeSettingsFinal(speechData) {
let wholeString = speechData.results[0].alternatives[0].transcript;

let nlpObject = nlp(wholeString).out('terms');
let words = speechData.results[0].alternatives[0].words;

let words_n_time = [];

for (let i = 0; i < words.length; i++) {
	//data
	let word = words[i].word;
	let startTime = `${words[i].startTime.seconds}.${words[i].startTime.nanos}`;
	let endTime = `${words[i].endTime.seconds}.${words[i].endTime.nanos}`;
	let tags = [];

	//generate span
	let newSpan = document.createElement('span');
	newSpan.innerHTML = word;
	newSpan.dataset.startTime = startTime;

	//push all tags
	for (let j = 0; j < nlpObject[i].length; j++) {
		tags.push(nlpObject[i]);
	}

	//add all classes
	for (let j = 0; j < nlpObject[i].length; j++) {
		let cleanClassName = nlpObject[i];
		// console.log(tags);
		let className = `nl-${cleanClassName}`;
		newSpan.classList.add(className);
	}

	words_n_time.push(newSpan);
}

return words_n_time;

}

@MuslumYilmaz should I fix this in the code?

@vin-ni It will fix but when it tries to recognize the words it shows 'undefined' until it finishes running the function. To fix this too inside " socket.on('speechData', function (data) " i have commented out

	// // add children to empty span
	// let edit = addTimeSettingsInterim(data);
	// for (var i = 0; i < edit; i++) {
	// 	resultText.lastElementChild.appendChild(edit[i]);
	// 	resultText.lastElementChild.appendChild(document.createTextNode('\u00A0'));
	// }

it can be optimized if wanted to but it works fine for me.

@MuslumYilmaz thanks for this solution, sadly this defeats the benefit of using streaming of course. It logs properly in the console and command line so it's clearly a frontend issue.

The entire problem seems to be related to compromise not being version fixed in your view. The API had a breaking change in v12, whereas your app seems to depend on v11.

Simply change:

<script src="https://unpkg.com/compromise@latest/builds/compromise.min.js"></script>

to

<script src="https://unpkg.com/compromise@11.14.3/builds/compromise.min.js"></script>

Fixed!

thanks a lot @kevin-smets !
Maybe we can get it updated at some point!