Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 28, 2010, 4:48:48 PM (14 years ago)
Author:
landauf
Message:

added a function to compute the Levenshtein distance between two strings to StringUtils.
used this in CommandEvaluation to correct misspelled commands (not automatically though, just to print a suggestion to the user)

Location:
code/branches/consolecommands3/src/libraries/core/command
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/consolecommands3/src/libraries/core/command/CommandEvaluation.cc

    r7236 r7238  
    124124                }
    125125            }
    126             else
    127             {
    128                 COUT(5) << "CE_execute: " << this->string_ << "\n";
    129                 return this->execCommand_->getExecutor()->parse(this->tokens_.subSet(this->execArgumentsOffset_), error, " ");
    130             }
    131         }
    132         else
    133             return MT_Type::Null;
     126            else if (error)
     127                *error = CommandExecutor::Incomplete;
     128        }
     129
     130        return MT_Type::Null;
    134131    }
    135132
     
    213210            {
    214211                std::string groupLC = getLowercase(this->getToken(0));
    215                 std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommand::getCommands().begin();
    216                 for ( ; it_group != ConsoleCommand::getCommands().end(); ++it_group)
    217                     if (getLowercase(it_group->first) == groupLC)
     212                for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommand::getCommandsLC().begin(); it_group != ConsoleCommand::getCommandsLC().end(); ++it_group)
     213                    if (it_group->first == groupLC)
    218214                        return std::string("Error: There is no command in group \"") + this->getToken(0) + "\" starting with \"" + this->getToken(1) + "\".";
    219215
     
    221217            }
    222218        }
     219    }
     220
     221    std::string CommandEvaluation::getCommandSuggestion() const
     222    {
     223        std::string token0_LC = getLowercase(this->getToken(0));
     224        std::string token1_LC = getLowercase(this->getToken(1));
     225
     226        std::string nearestCommand;
     227        unsigned int nearestDistance = (unsigned int)-1;
     228
     229        for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommand::getCommandsLC().begin(); it_group != ConsoleCommand::getCommandsLC().end(); ++it_group)
     230        {
     231            if (it_group->first != "")
     232            {
     233                for (std::map<std::string, ConsoleCommand*>::const_iterator it_name = it_group->second.begin(); it_name != it_group->second.end(); ++it_name)
     234                {
     235                    std::string command = it_group->first + " " + it_name->first;
     236                    unsigned int distance = getLevenshteinDistance(command, token0_LC + " " + token1_LC);
     237                    if (distance < nearestDistance)
     238                    {
     239                        nearestCommand = command;
     240                        nearestDistance = distance;
     241                    }
     242                }
     243            }
     244        }
     245
     246        std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommand::getCommandsLC().find("");
     247        if (it_group !=  ConsoleCommand::getCommandsLC().end())
     248        {
     249            for (std::map<std::string, ConsoleCommand*>::const_iterator it_name = it_group->second.begin(); it_name != it_group->second.end(); ++it_name)
     250            {
     251                std::string command = it_name->first;
     252                unsigned int distance = getLevenshteinDistance(command, token0_LC);
     253                if (distance < nearestDistance)
     254                {
     255                    nearestCommand = command;
     256                    nearestDistance = distance;
     257                }
     258            }
     259        }
     260
     261        return nearestCommand;
    223262    }
    224263
  • code/branches/consolecommands3/src/libraries/core/command/CommandEvaluation.h

    r7236 r7238  
    5454            std::string hint();
    5555
     56            std::string getCommandSuggestion() const;
     57
    5658            int evaluateParams(bool bPrintError = false);
    5759
  • code/branches/consolecommands3/src/libraries/core/command/TclBind.cc

    r7236 r7238  
    135135
    136136        int error;
    137         const std::string& result = CommandExecutor::query(command, &error, false);
     137        CommandEvaluation evaluation = CommandExecutor::evaluate(command);
     138        const std::string& result = evaluation.query(&error);
    138139        switch (error)
    139140        {
     
    143144            case CommandExecutor::Denied:      COUT(1) << "Error: Can't execute command \"" << command << "\", access denied. (B)" << std::endl; break;
    144145        }
     146
     147        if (error == CommandExecutor::Error)
     148            COUT(3) << "Did you mean \"" << evaluation.getCommandSuggestion() << "\"?" << std::endl;
    145149
    146150        return result;
Note: See TracChangeset for help on using the changeset viewer.