Added basic dependency text file. Should probably use an actual pip-env manager or something but not really sure how to handle that on github style stuff.
Made scroll lookup use fuzzy-matching.
This commit is contained in:
parent
c421a36a17
commit
d11b3481ee
|
|
@ -5,6 +5,8 @@ Only really kept separate for neatness sake.
|
||||||
|
|
||||||
import re
|
import re
|
||||||
from SlackUtil import reply
|
from SlackUtil import reply
|
||||||
|
from fuzzywuzzy import fuzz
|
||||||
|
from fuzzywuzzy import process
|
||||||
|
|
||||||
|
|
||||||
#load the family tree
|
#load the family tree
|
||||||
|
|
@ -35,30 +37,45 @@ def handleScrollMsg(slack, msg):
|
||||||
if not m:
|
if not m:
|
||||||
response = "Could not parse your query. Please invoke as \"scroll <name>\" or \"scroll <scroll#>\""
|
response = "Could not parse your query. Please invoke as \"scroll <name>\" or \"scroll <scroll#>\""
|
||||||
else:
|
else:
|
||||||
#Initially try to parse numeric
|
response = getResponseByQuery()
|
||||||
try:
|
|
||||||
#Try get scroll
|
|
||||||
scroll = int(m.group(1).strip())
|
|
||||||
|
|
||||||
for b in brothers:
|
|
||||||
if b["scroll"] == scroll:
|
|
||||||
response = "Brother {0} has scroll {1}".format(b["name"], b["scroll"])
|
|
||||||
break
|
|
||||||
|
|
||||||
#If response still none, say so
|
|
||||||
response = response or "Could not find scroll {0}".format(scroll)
|
|
||||||
|
|
||||||
except ValueError:
|
|
||||||
#Use as name
|
|
||||||
name = m.group(1).strip()
|
|
||||||
|
|
||||||
for b in brothers:
|
|
||||||
if name in b["name"]:
|
|
||||||
response = "Brother {0} has scroll {1}".format(b["name"], b["scroll"])
|
|
||||||
break
|
|
||||||
|
|
||||||
#If response still none, say so
|
|
||||||
response = response or "Could not find brother {0}".format(name)
|
|
||||||
|
|
||||||
reply(slack, msg, response, username="scrollbot")
|
reply(slack, msg, response, username="scrollbot")
|
||||||
|
|
||||||
|
def getResponseByQuery(query):
|
||||||
|
try:
|
||||||
|
#Try get scroll number
|
||||||
|
scroll = int(m.group(1).strip())
|
||||||
|
b = findBrotherByScroll(scroll)
|
||||||
|
|
||||||
|
if b:
|
||||||
|
return "Brother {0} has scroll {1}".format(b["name"], b["scroll"])
|
||||||
|
else:
|
||||||
|
return "Could not find scroll {0}".format(scroll)
|
||||||
|
except ValueError:
|
||||||
|
#Use as name
|
||||||
|
name = m.group(1).strip()
|
||||||
|
b = findBrotherByName(name)
|
||||||
|
|
||||||
|
if b:
|
||||||
|
return "Best match:\nBrother {0} has scroll {1}".format(b["name"], b["scroll"])
|
||||||
|
else:
|
||||||
|
return "Could not find brother {0}".format(name)
|
||||||
|
|
||||||
|
|
||||||
|
def findBrotherByScroll(scroll):
|
||||||
|
for b in brothers:
|
||||||
|
if b["scroll"] == scroll:
|
||||||
|
return b
|
||||||
|
return None
|
||||||
|
|
||||||
|
def findBrotherByName(name):
|
||||||
|
#Try direct lookup
|
||||||
|
"""
|
||||||
|
for b in brothers:
|
||||||
|
if name.toLower() in b["name"].toLower():
|
||||||
|
return b
|
||||||
|
"""
|
||||||
|
|
||||||
|
#Do fuzzy match
|
||||||
|
return process.extractOne(name, brothers, processor=lambda b: b["name"])
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
slackclient
|
||||||
|
python-Levenshtein
|
||||||
|
fuzzywuzzy
|
||||||
Loading…
Reference in New Issue