From a2c8974d07a07c457c144452d60b19af75c8a371 Mon Sep 17 00:00:00 2001 From: Isaiah Odhner Date: Sat, 9 May 2020 21:31:54 -0400 Subject: [PATCH] Handle smiley face :-) --- src/speech-recognition.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/speech-recognition.js b/src/speech-recognition.js index 3198aec..4165707 100644 --- a/src/speech-recognition.js +++ b/src/speech-recognition.js @@ -28,6 +28,10 @@ const recognitionFixes = { "tan-tan": "tan tan", "pan": "tan", + // interpreted as symbols + ":-)": "smiley face", // so that it can be searched for on Bing for images + ":-(": "sad face", // may also be from "frowny face" + // commands/misc "slick to the": "select the", "like the": "select the", @@ -161,7 +165,11 @@ recognition.onresult = function(event) { console.log('Confidence: ' + event.results[0][0].confidence); command = command.toLowerCase(); for (const [bad, good] of Object.entries(recognitionFixes)) { - command = command.replace(new RegExp(`\\b${bad}\\b`, "ig"), good); + if (bad.match(/^\W|\W$/)) { + command = command.replace(new RegExp(escapeRegExp(bad), "ig"), good); + } else { + command = command.replace(new RegExp(`\\b${escapeRegExp(bad)}\\b`, "ig"), good); + } } console.log(`After any fixes: "${command}"`); interpret_command(command); @@ -218,7 +226,7 @@ window.interpret_command = (command)=> { for (const tool of tools) { for (const base_tool_phrase of tool.speech_recognition) { // Note: if "select" wasn't matched here, the phrase "select text" would select the Select tool instead of the Text tool (because "select" is longer than "text") - const select_tool_match = command.match(new RegExp(`\\b(?:(?:select|pick|choose|use|activate|pick up|grab) )?(?:the )?${base_tool_phrase}(?: tool)?\\b`, "i")); + const select_tool_match = command.match(new RegExp(`\\b(?:(?:select|pick|choose|use|activate|pick up|grab) )?(?:the )?${escapeRegExp(base_tool_phrase)}(?: tool)?\\b`, "i")); if (select_tool_match) { if (select_tool_match[0].length > best_match_text.length) { best_match_text = select_tool_match[0]; @@ -408,4 +416,8 @@ function find_clipart(query) { }) } +function escapeRegExp(string) { + return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string +} + })(); \ No newline at end of file