From 0fe64340c30b9711ec675dea0a56192f2941c6a4 Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Sat, 17 Aug 2024 19:43:48 -0700 Subject: [PATCH 1/2] Allow memories with "is" in the key --- scripts/remember.coffee | 63 +++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/scripts/remember.coffee b/scripts/remember.coffee index 508c06e..006e4b3 100644 --- a/scripts/remember.coffee +++ b/scripts/remember.coffee @@ -23,43 +23,52 @@ module.exports = (robot) -> Object.keys(memories()).filter (key) -> searchRegex.test(key) robot.respond /(?:what is|rem(?:ember)?)\s+(.*)/i, (msg) -> + msg.finish() words = msg.match[1].trim() - if match = words.match /(.*?)(\s+is\s+([\s\S]*))$/i - msg.finish() - key = match[1].toLowerCase() - value = match[3] - currently = memories()[key] - if currently - msg.send "But #{key} is already #{currently}. Forget #{key} first." + + # First check for a search expression. + if match = words.match /\|\s*(grep\s+)?(.*)$/i + searchPattern = match[2] + matchingKeys = findSimilarMemories(searchPattern) + if matchingKeys.length > 0 + msg.send "I remember:\n#{matchingKeys.join(', ')}" else - memories()[key] = value - msg.send "OK, I'll remember #{key}." - else if match = words.match /([^?]+)\??/i - msg.finish() + msg.send "I don't remember anything matching `#{searchPattern}`" + return + # Next, attempt to interpret `words` as an existing key. + if match = words.match /([^?]+)\??/i key = match[1].toLowerCase() value = memories()[key] if value memoriesByRecollection()[key] ?= 0 memoriesByRecollection()[key]++ - else - if match = words.match /\|\s*(grep\s+)?(.*)$/i - searchPattern = match[2] - matchingKeys = findSimilarMemories(searchPattern) - if matchingKeys.length > 0 - value = "I remember:\n#{matchingKeys.join(', ')}" - else - value = "I don't remember anything matching `#{searchPattern}`" + msg.send value + return + + # Next, attempt to interpret `words` as a "foo is bar" expression in order + # to store a memory. + if match = words.match /^(.*)is(.*)$/i + key = match[1].trim().toLowerCase() + value = match[2].trim() + if key and value + currently = memories()[key] + if currently + msg.send "But #{key} is already #{currently}. Forget #{key} first." else - matchingKeys = findSimilarMemories(key) - if matchingKeys.length > 0 - keys = matchingKeys.join(', ') - value = "I don't remember `#{key}`. Did you mean:\n#{keys}" - else - value = "I don't remember anything matching `#{key}`" - - msg.send value + memories()[key] = value + msg.send "OK, I'll remember #{key}." + return + + # If none of the previous actions succeeded, search existing memories for + # similar keys. + matchingKeys = findSimilarMemories(words) + if matchingKeys.length > 0 + keys = matchingKeys.join(', ') + msg.send "I don't remember `#{words}`. Did you mean:\n#{keys}" + else + msg.send "I don't remember anything matching `#{words}`" robot.respond /forget\s+(.*)/i, (msg) -> key = msg.match[1].toLowerCase() From 3b2f1dcb6d973036200ffee8469edda608fc3512 Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Sat, 17 Aug 2024 20:14:54 -0700 Subject: [PATCH 2/2] Fix handling of keys with "?" in them * Change the regex to strip the last "?" from the string, rather then stopping at the first "?". * Use the stripped string in the final fallback case to make sure the final message correctly reflects the key that we attempted to look up. --- scripts/remember.coffee | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/scripts/remember.coffee b/scripts/remember.coffee index 006e4b3..0fa9d11 100644 --- a/scripts/remember.coffee +++ b/scripts/remember.coffee @@ -36,14 +36,15 @@ module.exports = (robot) -> msg.send "I don't remember anything matching `#{searchPattern}`" return - # Next, attempt to interpret `words` as an existing key. - if match = words.match /([^?]+)\??/i - key = match[1].toLowerCase() - value = memories()[key] + # Next, attempt to interpret `words` as an existing key. This also strips + # off the last "?" character. + if match = words.match /(.+?)\??$/i + stripped_key = match[1].toLowerCase() + value = memories()[stripped_key] if value - memoriesByRecollection()[key] ?= 0 - memoriesByRecollection()[key]++ + memoriesByRecollection()[stripped_key] ?= 0 + memoriesByRecollection()[stripped_key]++ msg.send value return @@ -63,12 +64,12 @@ module.exports = (robot) -> # If none of the previous actions succeeded, search existing memories for # similar keys. - matchingKeys = findSimilarMemories(words) + matchingKeys = findSimilarMemories(stripped_key) if matchingKeys.length > 0 keys = matchingKeys.join(', ') - msg.send "I don't remember `#{words}`. Did you mean:\n#{keys}" + msg.send "I don't remember `#{stripped_key}`. Did you mean:\n#{keys}" else - msg.send "I don't remember anything matching `#{words}`" + msg.send "I don't remember anything matching `#{stripped_key}`" robot.respond /forget\s+(.*)/i, (msg) -> key = msg.match[1].toLowerCase()