def _searchFactoid(self, channel, key): """Try to typo-match input to possible factoids. Assume first letter is correct, to reduce processing time. First, try a simple wildcard search. If that fails, use the Damerau-Levenshtein edit-distance metric. """ # if you made a typo in a two-character key, boo on you. if len(key) < 3: return [] db = self.getDb(channel) cursor = db.cursor() cursor.execute("""SELECT key FROM keys WHERE key LIKE ?""", ('%' + key + '%',)) wildcardkeys = cursor.fetchall() if len(wildcardkeys) > 0: return [line[0] for line in wildcardkeys] cursor.execute("""SELECT key FROM keys WHERE key LIKE ?""", (key[0] + '%',)) flkeys = cursor.fetchall() if len(flkeys) == 0: return [] flkeys = [line[0] for line in flkeys] dl_metrics = [dameraulevenshtein(key, sourcekey) for sourcekey in flkeys] dict_metrics = dict(list(zip(flkeys, dl_metrics))) if min(dl_metrics) <= 2: return [key for key,item in list(dict_metrics.items()) if item <= 2] if min(dl_metrics) <= 3: return [key for key,item in list(dict_metrics.items()) if item <= 3] return []
def _searchFactoid(self, channel, key): """Try to typo-match input to possible factoids. Assume first letter is correct, to reduce processing time. First, try a simple wildcard search. If that fails, use the Damerau-Levenshtein edit-distance metric. """ # if you made a typo in a two-character key, boo on you. if len(key) < 3: return [] db = self.getDb(channel) cursor = db.cursor() cursor.execute("""SELECT key FROM keys WHERE key LIKE ?""", ('%' + key + '%',)) wildcardkeys = cursor.fetchall() if len(wildcardkeys) > 0: return [line[0] for line in wildcardkeys] cursor.execute("""SELECT key FROM keys WHERE key LIKE ?""", (key[0] + '%',)) flkeys = cursor.fetchall() if len(flkeys) == 0: return [] flkeys = [line[0] for line in flkeys] dl_metrics = [dameraulevenshtein(key, sourcekey) for sourcekey in flkeys] dict_metrics = dict(list(zip(flkeys, dl_metrics))) if min(dl_metrics) <= 2: return [key for key,item in dict_metrics.iteritems() if item <= 2] if min(dl_metrics) <= 3: return [key for key,item in dict_metrics.iteritems() if item <= 3] return []
def _getMarketInfo(self, input, action='ticker'): sm = getattr(self, action + '_supported_markets') sml = sm.keys()+sm.values() dl = [dameraulevenshtein(input.lower(), i.lower()) for i in sml] if (min(dl) <= 2): mkt = (sml)[dl.index(min(dl))] else: return None if mkt.lower() in sm.keys(): return [mkt.lower(), sm[mkt.lower()], getattr(self, '_get' + mkt.capitalize() + action.capitalize()),] r = filter(lambda x: sm[x].lower() == mkt.lower(), sm) if len(r) == 1: return [r[0], sm[r[0]], getattr(self, '_get' + r[0].capitalize() + action.capitalize()),] return None