I have an input field where I expect users to type text that contains 1 of many keywords that will trigger different audio files depending on the keyword. (I know that's not really smart from UX point of view but that's just a mockup/demo of a virtual assistant).
I'm using this code but I feel I can do much better, can you suggest some alternatives?
keyword1 = "music";
keyword2 = "news";
keyword3 = "weather";
keyword4 = "cooking";
keyword5 = "pasta";
keyword6 = "tech";
if(text.search(keyword1)!=-1) {
audio.src = a_music;
audio.play();
} else if(text.search(keyword2)!=-1){
audio.src = a_news;
audio.play();
}
[...]
}
I have an input field where I expect users to type text that contains 1 of many keywords that will trigger different audio files depending on the keyword. (I know that's not really smart from UX point of view but that's just a mockup/demo of a virtual assistant).
I'm using this code but I feel I can do much better, can you suggest some alternatives?
keyword1 = "music";
keyword2 = "news";
keyword3 = "weather";
keyword4 = "cooking";
keyword5 = "pasta";
keyword6 = "tech";
if(text.search(keyword1)!=-1) {
audio.src = a_music;
audio.play();
} else if(text.search(keyword2)!=-1){
audio.src = a_news;
audio.play();
}
[...]
}
You could create an object with the keyword as key
and the file url as value
and then iterate over the keys to check if the text matches a keyword.
const config = {
'music': 'musicUrl',
'news': 'newsUrl',
'weather': 'weatherUrl',
'cooking': 'cookingUrl',
'pasta': 'pastaUrl',
'tech': 'techUrl'
};
function match(input, obj) {
var matched = Object.keys(obj).find(key => input.toLowerCase().search(key) > -1);
return obj[matched] || null;
}
console.log(match('cats weather dogs', config));
console.log(match('cats tech dogs', config));
console.log(match('cats dogs', config));
You should use an array of keywords instead of one different variable per word. Then it's a piece of cake :
const keywords = ["music","news","weather","cooking","pasta","tech"]
const text = "let's play some music"
if( keywords.some(keyword => text.includes(keyword) )) {
console.log("Found")
// audio.src = a_music;
// audio.play();
}
Using a regex here seems a nice approach to me
let keywords = {
music: 'musicSample',
news: 'newsSample',
weather: 'weatherSample',
cooking: 'cookingSample',
pasta: 'pastaSample',
tech: 'techSample'
}
function searchKeywords (text) {
let keys = Object.keys(keywords)
let re = new RegExp('(' + keys.join('|') + ')', 'g')
return text.toLowerCase().match(re)
}
console.log(searchKeywords('I love music and tech'))
// Play the first encountered word or queue every word to play consecutively what you found
console.log(searchKeywords('Mama is cooking some pasta')[0])
Perhaps an array?
var array = ["music", "news", "weather", "cooking", "pasta", "tech"];
for(var i = 0; i < array.length; i++){
if(text.includes(array[i])){
//do something with i
break;
}
}
Here's an example of searching for keywords in a list of strings that allows for ambiguous order of present keywords.
var keywords = [
'music',
// 'news',
// 'weather',
// 'cooking',
// 'pasta',
'tech',
]
var source = [
"Create a database and tables",
"What is up with singing",
"Why is it cold outside",
"What time is it",
"How can I set up an appointment",
"How to make you're own music",
"Combining music and technology",
]
function search(keywords, source) {
var re = new RegExp('(' + keywords.join('|') + ')', 'g')
var results = []
for (var i = 0; i < source.length; i++) {
const text = source[i];
var result = text.toLowerCase().match(re)
if (result != null || result != null) {
results.push(text)
}
}
return results
}
var results = search(keywords, source)
console.log(results)