so I'm utilizing the Ocrad.js library in an ionic project to be able to read text from images. In the app, the user can add 'items' (as in words) to an array and then I would like to check whether or not these items (words) are present in the text from the image. For instance: if the image has a sentence: 'I like football' and the user added the word 'football' to the list, on the press of a button, the app would check whether or not 'football' exists in 'I like football' and would say so to the user. In an else occasion, it would also say so.
So far I came up with this situation:
for(let item of this.itemList) {
if(text.indexOf(item.name)>=0){
alert('word found');
} else {
alert('word not found');
}
}
The idea was to loop through the list of items(words) that the user had added to the array and give them the appropriate response. While it is working if I add just one word, like the football example I mentioned above, if I add more words to the list, the loop will obviously give me the 2 alerts. So if add the word 'soccer' to the list and therefore have an array with 'football' and 'soccer', I would get the 2 alerts, which makes sense because I wasn't stopping the loop. So I tried a switch case, which did not work for some reason (I don't really know why, but it kept giving me the two alerts). This is what my switch looked like:
for(let item of this.itemList){
switch(true){
case (text.indexOf(item.name)>=0): {
alert('word found');
break;
}
case (text.indexOf(item.name) <= 0):{
alert('word not found');
break;
}
default: {
alert('please add a word to the list');
break;
}
}
}
So after playing around and researching, I could not really find something that helped me very well. The idea again is if the image text says 'I like football' and I add 3 items to the array: 'soccer', basketball', 'football', the answer would be 'word found' only, whereas if the word is not there, I would get 'word not found' only once! I believe I might be doing something really stupid that I can't see with my noobs' eyes haha. I hope my question made sense. Can anyone help me with this? Cheers guys!
Aucun commentaire:
Enregistrer un commentaire