Changed how command not found errors are handled by default: Implemented a cos-likeness algorithm (Coling 2008) for comparing strings, which allows for decent suggestions and speed.

This commit is contained in:
Griatch 2012-04-22 16:36:31 +02:00
parent 4678234e9a
commit 0c292b5ff2
4 changed files with 76 additions and 12 deletions

View file

@ -42,6 +42,7 @@ from django.conf import settings
from src.comms.channelhandler import CHANNELHANDLER
from src.utils import logger, utils
from src.commands.cmdparser import at_multimatch_cmd
from src.utils.utils import string_suggestions
__all__ = ("cmdhandler",)
@ -191,7 +192,12 @@ def cmdhandler(caller, raw_string, testing=False):
if syscmd:
sysarg = raw_string
else:
sysarg = "Huh? (Type \"help\" for help)"
sysarg = "Command '%s' is not available." % raw_string
suggestions = string_suggestions(raw_string, cmdset.get_all_cmd_keys_and_aliases(), cutoff=0.7, maxnum=3)
if suggestions:
sysarg += " Did you maybe mean %s?" % utils.list_to_string(suggestions, 'or', addquote=True)
else:
sysarg += " Type \"help\" for help."
raise ExecSystemCommand(syscmd, sysarg)
if len(matches) > 1:

View file

@ -382,3 +382,12 @@ class CmdSet(object):
by use of self.add().
"""
pass
def get_all_cmd_keys_and_aliases(self):
"""
Returns a list of all command keys and aliases
available in this cmdset.
"""
names = [cmd.key for cmd in self.commands]
[names.extend(cmd.aliases) for cmd in self.commands]
return names

View file

@ -6,6 +6,7 @@ set. The normal, database-tied help system is used for collaborative
creation of other help topics such as RP help or game-world aides.
"""
from collections import defaultdict
from src.utils.utils import fill, dedent
from src.commands.command import Command
from src.help.models import HelpEntry
@ -100,20 +101,14 @@ class CmdHelp(Command):
if query in LIST_ARGS:
# we want to list all available help entries, grouped by category.
hdict_cmd = {}
hdict_cmd = defaultdict(list)
for cmd in (cmd for cmd in cmdset if cmd.auto_help and not cmd.is_exit
and not cmd.key.startswith('__') and cmd.access(caller)):
try:
hdict_cmd[cmd.help_category].append(cmd.key)
except KeyError:
hdict_cmd[cmd.help_category] = [cmd.key]
hdict_db = {}
hdict_cmd[cmd.help_category].append(cmd.key)
hdict_db = defaultdict(list)
for topic in (topic for topic in HelpEntry.objects.get_all_topics()
if topic.access(caller, 'view', default=True)):
try:
hdict_db[topic.help_category].append(topic.key)
except KeyError:
hdict_db[topic.help_category] = [topic.key]
hdict_db[topic.help_category].append(topic.key)
help_entry = format_help_list(hdict_cmd, hdict_db)
caller.msg(help_entry)
return