@basmail
Main / Tools / Execute code
Not tested very much.
function findSentencesWithKeyword(keyword, sentences) {
return sentences.filter(function (sentence) {
var words = sentence.toLowerCase().split(" ");
var found = false;
words.forEach(function (word) {
if (word === keyword.toLowerCase()) {
found = true;
}
});
return found;
});
}
log("running ... ");
var sentences = [
"I love reading ebooks on my tablet.",
"Do you have an audiobook recommendation?",
"She prefers printed books to ebooks.",
"I can't find my ebub anywhere.",
"Where can I buy an audiobook online?"
];
var keywords = ["ebub", "ebook", "audiobook"];
keywords.forEach(function (keyword) {
var matchingSentences = findSentencesWithKeyword(keyword, sentences);
log("Keyword: " + keyword);
log(matchingSentences);
log("---------------");
});
Result:
[704642835] [02:19:41] Thread #1 : running ...
[704642835] [02:19:41] Thread #1 : Keyword: ebub
[704642835] [02:19:41] Thread #1 : I can't find my ebub anywhere.
[704642835] [02:19:41] Thread #1 : ---------------
[704642835] [02:19:41] Thread #1 : Keyword: ebook
[704642835] [02:19:41] Thread #1 :
[704642835] [02:19:41] Thread #1 : ---------------
[704642835] [02:19:41] Thread #1 : Keyword: audiobook
[704642835] [02:19:41] Thread #1 : Do you have an audiobook recommendation?,Where can I buy an audiobook online?
[704642835] [02:19:41] Thread #1 : ---------------