public List <string> FindAllSuggestions(string prefix, TrieEntry root) { if (root == null) { return(new List <string>()); } var result = new List <string>(); foreach (var child in root.Children) { ConstructAddWordsRecursieve(prefix + root.Key, child, result); } return(result); }
public void BuildAutoComplete() { foreach (var word in dictionary) { var root = Root; foreach (char c in word) { if (root.Children.Any(x => x.Key == c)) { var newRoot = new TrieEntry(c); root.Children.Add(newRoot); root = newRoot; } else { root = root.Children[c]; } } root.Children.Add(new TrieEntry('/')); } }
private void ConstructAddWordsRecursieve(string prefix, TrieEntry child, List <string> result) { //if (root == null) //{ // return new List<string>(); //} //var result = new List<string>(); //foreach (var child in root.Children) //{ // ConstructAddWordsRecursieve(prefix + root.Key, child, result); //} //return result; //if (child.Key == '/') //{ // result.Add(prefix); //} //else //{ // ConstructAddWordsRecursieve(prefix + child.Key, ) //} }
public bool ExecuteCommand(string command, string rawParameters, CommandParameter[] parameters) { // Search for command and invoke it if (Commands != null) { command = command.ToLowerInvariant(); // lower command List <TrieEntry <CommandMethodInfo> > methodInfos = Commands.GetByPrefix(command).ToList(); TrieEntry <CommandMethodInfo> entry = methodInfos.OrderBy(x => x.Value.Attribute.Priority).FirstOrDefault(); // use priority to choose between conflicting commands if (entry.Value?.Attribute?.NoShortcut == true && command != entry.Key) // if command doesn't accept shortcut, inform player { Send("If you want to {0}, spell it out.", entry.Key.ToUpper()); return(true); } else if (entry.Value?.MethodInfo != null) { if (IsCommandAvailable(entry.Value?.Attribute)) { bool beforeExecute = ExecuteBeforeCommand(entry.Value, rawParameters, parameters); if (!beforeExecute) { Log.Default.WriteLine(LogLevels.Info, $"ExecuteBeforeCommand returned false for command {entry.Value.MethodInfo.Name} and parameters {rawParameters}"); return(false); } MethodInfo methodInfo = entry.Value.MethodInfo; bool executedSuccessfully; if (entry.Value.Attribute?.AddCommandInParameters == true) { // Insert command as first parameter CommandParameter[] enhancedParameters = new CommandParameter[parameters?.Length + 1 ?? 1]; if (parameters != null) { Array.ConstrainedCopy(parameters, 0, enhancedParameters, 1, parameters.Length); } enhancedParameters[0] = new CommandParameter(command, 1); string enhancedRawParameters = command + " " + rawParameters; // executedSuccessfully = (bool)methodInfo.Invoke(this, new object[] { enhancedRawParameters, enhancedParameters }); } else { executedSuccessfully = (bool)methodInfo.Invoke(this, new object[] { rawParameters, parameters }); } if (!executedSuccessfully) { Log.Default.WriteLine(LogLevels.Warning, "Error while executing command"); return(false); } bool afterExecute = ExecuteAfterCommand(entry.Value, rawParameters, parameters); if (!afterExecute) { Log.Default.WriteLine(LogLevels.Info, $"ExecuteBeforeCommand returned false for command {entry.Value.MethodInfo.Name} and parameters {rawParameters}"); return(false); } return(true); } else { Log.Default.WriteLine(LogLevels.Warning, $"Command {command} not found"); Send("Command not found."); return(false); } } else { Log.Default.WriteLine(LogLevels.Warning, $"Command {command} not found"); Send("Command not found."); return(false); } } else { Log.Default.WriteLine(LogLevels.Warning, $"No command found for {GetType().FullName}"); Send("Command not found."); return(false); } }
/// <summary> /// Adds an item to the <see cref="Trie{TValue}"/>. /// </summary> /// <param name="item">The object to add to the <see cref="Trie{TValue}"/>.</param> /// <exception cref="T:System.ArgumentException">An element with the same charKey already exists in the <see cref="Trie{TValue}"/>.</exception> public void Add(TrieEntry <TValue> item) { Add(item.Key, item.Value); }