Fixed a ZeroDivionError by adjusting what is returned by aliashandler.all() when no aliases are given ([] instead of ['']). Also made sure src.utils.stringsuggestions() don't crash in such a situation.

This commit is contained in:
Griatch 2013-10-18 16:15:51 +02:00
parent a1646ec596
commit 55423f6a2f
4 changed files with 9 additions and 7 deletions

View file

@ -316,7 +316,6 @@ def cmdhandler(called_by, raw_string, testing=False, callertype="session", sessi
else:
# fallback to default error text
sysarg = _("Command '%s' is not available.") % raw_string
cmdset.get_all_cmd_keys_and_aliases(caller)
suggestions = string_suggestions(raw_string, cmdset.get_all_cmd_keys_and_aliases(caller), cutoff=0.7, maxnum=3)
if suggestions:
sysarg += _(" Maybe you meant %s?") % utils.list_to_string(suggestions, _('or'), addquote=True)

View file

@ -30,7 +30,7 @@ def _init_command(mcs, **kwargs):
mcs.aliases = [str(alias).strip().lower() for alias in mcs.aliases.split(',')]
except Exception:
mcs.aliases = []
mcs.aliases = list(set(alias for alias in mcs.aliases if alias != mcs.key))
mcs.aliases = list(set(alias for alias in mcs.aliases if alias and alias != mcs.key))
# optimization - a set is much faster to match against than a list
mcs._matchset = set([mcs.key] + mcs.aliases)

View file

@ -592,7 +592,7 @@ class TagHandler(object):
def all(self):
"Get all tags in this handler"
return [to_str(p[0]) for p in _GA(self.obj, self._m2m_fieldname).all().values_list("db_key")]
return [to_str(p[0]) for p in _GA(self.obj, self._m2m_fieldname).all().values_list("db_key") if p[0]]
def __str__(self):
return ",".join(self.all())

View file

@ -39,7 +39,6 @@ def is_iter(iterable):
except AttributeError:
return False
def make_iter(obj):
"Makes sure that the object is always iterable."
return not hasattr(obj, '__iter__') and [obj] or obj
@ -796,7 +795,7 @@ def string_similarity(string1, string2):
This implements a "cosine-similarity" algorithm as described for example in
Proceedings of the 22nd International Conference on Computation Linguistics
(Coling 2008), pages 593-600, Manchester, August 2008
The measure vectors used is simply a "bag of words" type histogram (but for letters).
The measure-vectors used is simply a "bag of words" type histogram (but for letters).
The function returns a value 0...1 rating how similar the two strings are. The strings can
contain multiple words.
@ -804,8 +803,12 @@ def string_similarity(string1, string2):
vocabulary = set(list(string1 + string2))
vec1 = [string1.count(v) for v in vocabulary]
vec2 = [string2.count(v) for v in vocabulary]
return float(sum(vec1[i]*vec2[i] for i in range(len(vocabulary)))) / \
(math.sqrt(sum(v1**2 for v1 in vec1)) * math.sqrt(sum(v2**2 for v2 in vec2)))
try:
return float(sum(vec1[i]*vec2[i] for i in range(len(vocabulary)))) / \
(math.sqrt(sum(v1**2 for v1 in vec1)) * math.sqrt(sum(v2**2 for v2 in vec2)))
except ZeroDivisionError:
# can happen if empty-string cmdnames appear for some reason. This is a no-match.
return 0
def string_suggestions(string, vocabulary, cutoff=0.6, maxnum=3):
"""