Exemple #1
0
        public IndexItem[] GetBestItems(Index index, string user_text, List <string> user_tokens, ref Dictionary <IndexItem, List <string> > item_tokens, LearnedContent learned_commands)
        {
            Dictionary <string, IndexItemSearchResult[]> matches       = new Dictionary <string, IndexItemSearchResult[]>();
            Dictionary <IndexItem, List <string> >       tokens        = new Dictionary <IndexItem, List <string> >();
            Dictionary <IndexItem, List <short> >        tokens_values = new Dictionary <IndexItem, List <short> >();
            ushort n_results = (ushort)(SystemCore.Settings.SettingsManager.Instance.GetNumberOfSuggestions() * 10);

            LearnedItem[] cmds = learned_commands.GetSortedItems(user_text);
            Dictionary <IndexItem, List <string> > learned_tokens = new Dictionary <IndexItem, List <string> >();
            List <IndexItemSearchResult>           learned_items  = new List <IndexItemSearchResult>(index.GetLearnedMatches(cmds, n_results, ref learned_tokens));

            foreach (IndexItemSearchResult item in learned_items)
            {
                tokens.Add(item.Result, FixLearnedTokens(item.Result, user_tokens, learned_tokens[item.Result]));
                tokens_values.Add(item.Result, new List <short> (new short[] { item.Error }));
            }

            foreach (string token in user_tokens)
            {
                IndexItemSearchResult[] results;
                if (_cache.Contains(token))
                {
                    results = _cache.Get(token);
                }
                else
                {
                    results = index.GetBestMatches(token, n_results, (ushort)5);
                }
                foreach (IndexItemSearchResult result in results)
                {
                    if (!learned_items.Contains(result))
                    {
                        if (!tokens.ContainsKey(result.Result))
                        {
                            tokens.Add(result.Result, new List <string>(new string[] { token }));
                            tokens_values.Add(result.Result, new List <short>(new short[] { result.Error }));
                        }
                        else
                        {
                            if (result.Result.IsCommand &&
                                tokens[result.Result].Count == result.Result.NTokens)
                            {
                                short lsi = LeastSimilarIndex(tokens_values[result.Result], result.Error);
                                if (lsi != -1)
                                {
                                    tokens[result.Result][lsi]        = token;
                                    tokens_values[result.Result][lsi] = result.Error;
                                }
                            }
                            else
                            {
                                tokens[result.Result].Add(token);
                                tokens_values[result.Result].Add(result.Error);
                            }
                        }
                    }
                }
                _cache.Add(token, results);
                matches.Add(token,
                            results
                            );
            }
            Dictionary <IndexItemSearchResult, IndexItemSearchResult> command_items_dic = new Dictionary <IndexItemSearchResult, IndexItemSearchResult>();
            List <IndexItemSearchResult> non_command_items = null;

            foreach (string token in user_tokens)
            {
                List <IndexItemSearchResult> aux = new List <IndexItemSearchResult>();
                foreach (IndexItemSearchResult result in matches[token])
                {
                    if (result.Result.IsCommand)
                    {
                        if (!command_items_dic.ContainsKey(result))
                        {
                            command_items_dic.Add(result, result);
                        }
                        else
                        {
                            if (command_items_dic[result].Error > result.Error)
                            {
                                command_items_dic[result] = result;
                            }
                        }
                    }
                    else
                    {
                        aux.Add(result);
                    }
                }
                if (non_command_items != null)
                {
                    aux = aux.Intersect(non_command_items).ToList();
                }
                non_command_items = aux;
            }

            if (non_command_items == null)
            {
                non_command_items = new List <IndexItemSearchResult>();
            }
            List <IndexItemSearchResult> command_items = command_items_dic.Values.ToList();

            List <IndexItemSearchResult> items = Enumerable.Union(non_command_items, command_items).ToList();

            foreach (IndexItemSearchResult item in learned_items)
            {
                items.Remove(item);
            }
            learned_items.AddRange(items);
            items = learned_items;
            if (items != null)
            {
                item_tokens = tokens;
                items.Sort();
                return(new List <IndexItem>(from item in items select item.Result).Take(SystemCore.Settings.SettingsManager.Instance.GetNumberOfSuggestions()).ToArray());
            }
            else
            {
                return(null);
            }
        }
        public List <InterpreterItem> RetrieveTopItems(string text)
        {
            List <InterpreterItem> ret                  = new List <InterpreterItem>();
            string                   user_text          = text.Trim().ToLower();
            List <string>            assisting_commands = new List <string>();
            List <string>            assisting_plugins  = new List <string>();
            List <InterpreterPlugin> plugins            = new List <InterpreterPlugin>(_plugins);

            plugins.Add(_menuEngine);
            bool filesystem_assisted = false;

            // take care of high priority commands
            foreach (InterpreterPlugin plugin in plugins)
            {
                Command command = plugin.GetAssistingCommand(Command.PriorityType.High, text);
                if (command != null)
                {
                    InterpreterItem item = new InterpreterItem(plugin.GetItemName(command.Name, text, null),
                                                               plugin.GetItemDescription(command.Name, text, null),
                                                               plugin.GetItemAutoComplete(command.Name, text, null),
                                                               plugin.GetItemIcon(command.Name, text, null),
                                                               InterpreterItem.OwnerType.Plugin,
                                                               user_text);
                    item.CommandName   = command.Name;
                    item.CommandTokens = null;
                    item.CommandUsage  = plugin.GetUsage(command.Name, text, null);
                    item.PluginId      = plugin.Name;
                    assisting_commands.Add(plugin.Name);
                    ret.Add(item);
                }
            }

            if (_systemBrowser.IsOwner(user_text))
            {
                //List<string> user_tokens = new List<string>();
                //user_tokens.Add(user_text.ToLower());

                //Dictionary<string, double> distances = new Dictionary<string, double>();
                //Dictionary<string, string[]> displacements = new Dictionary<string, string[]>();
                //List<string> suited_names = new List<string>();
                //List<string> min_names = new List<string>();

                List <string> paths = _systemBrowser.RetrieveItems(user_text);
                //paths.Sort();

                //Dictionary<string, List<string>> keywords = new Dictionary<string, List<string>>();
                //foreach (string s in paths)
                //    keywords.Add(s, new List<string>(new string[] { s.ToLower() }));

                //TextPredictor.Instance.PredictNamesAndDistance(user_text, user_tokens, paths, keywords, ref suited_names, ref distances, ref displacements, false);

                //while (suited_names.Count > 0)
                //{
                //    string min_name = string.Empty;
                //    for (int i = 0; i < suited_names.Count; i++)
                //    {
                //        if (min_name == string.Empty)
                //        {
                //            min_name = suited_names[i];
                //        }
                //        else if (distances[min_name] > distances[suited_names[i]])
                //        {
                //            min_name = suited_names[i];
                //        }
                //    }
                //    if (min_name != string.Empty)
                //    {
                //        min_names.Add(min_name);
                //        suited_names.Remove(min_name);
                //    }
                //}

                foreach (string s in paths)
                {
                    ret.Add(new InterpreterItem(FileNameManipulator.GetFolderName(s),
                                                s,
                                                s,
                                                _systemBrowser.GetItemIcon(s),
                                                InterpreterItem.OwnerType.FileSystem,
                                                user_text));
                }

                // clean up
                //user_tokens = null;
                //distances = null;
                //suited_names = null;
                //min_names = null;
                paths = null;
                //keywords = null;

                filesystem_assisted = true;
            }
            if (!filesystem_assisted)
            {
                List <string> user_tokens      = new List <string>(StringUtility.GenerateKeywords(user_text, true, true));
                int           user_tokens_size = user_tokens.Count;

                Index index = _fileIndexer.RetrieveItems();
                //_commandCache.Update(plugins);
                //index.Merge(_commandCache.Index);
                if (!_fileIndexer.IsCacheMerged)
                {
                    Predictor.Instance.InvalidateCache();
                }
                if (_commandCache.NeedsUpdate() || !_fileIndexer.IsCacheMerged)
                {
                    _commandCache.Update(plugins);
                    index.Merge(_commandCache.Index);
                    _fileIndexer.IsCacheMerged = true;
                }

                Dictionary <IndexItem, List <string> > tokens = null;
                LearnedContent learned_commands = SettingsManager.Instance.GetLearnedContents();
                IndexItem[]    items            = Predictor.Instance.GetBestItems(index, user_text, user_tokens, ref tokens, learned_commands);
                // TODO
                foreach (IndexItem i in items)
                {
                    // take care of medium priority commands
                    string   command_name       = i.Name;
                    string[] item_tokens        = tokens[i].ToArray();
                    bool     assisted_by_plugin = false;
                    foreach (InterpreterPlugin plugin in _plugins)
                    {
                        if (command_name == string.Empty)
                        {
                            continue;
                        }
                        Command command = plugin.GetCommandByName(command_name);
                        if (command != null && command.FitsPriority(Command.PriorityType.Medium) /*&& !assisting_plugins.Contains(plugin.Name)*/ && !assisting_commands.Contains(command_name))
                        {
                            InterpreterItem item = new InterpreterItem(plugin.GetItemName(command_name, text, item_tokens),
                                                                       plugin.GetItemDescription(command_name, text, item_tokens),
                                                                       plugin.GetItemAutoComplete(command_name, text, item_tokens),
                                                                       plugin.GetItemIcon(command_name, text, item_tokens),
                                                                       InterpreterItem.OwnerType.Plugin,
                                                                       user_text);
                            item.CommandName   = command_name;
                            item.CommandTokens = item_tokens;
                            item.CommandUsage  = plugin.GetUsage(command.Name, text, item_tokens);
                            item.PluginId      = plugin.Name;
                            assisting_commands.Add(command_name);
                            ret.Add(item);
                            assisted_by_plugin = true;
                        }
                    }
                    if (!assisted_by_plugin)
                    {
                        if (_menuEngine.IsOwner(i.Name))
                        {
                            InterpreterItem item = new InterpreterItem(command_name,
                                                                       _menuEngine.GetItemDescription(command_name, user_text, item_tokens),
                                                                       command_name,
                                                                       _menuEngine.GetItemIcon(command_name, text, item_tokens),
                                                                       InterpreterItem.OwnerType.Menu,
                                                                       user_text);
                            item.CommandName   = command_name;
                            item.CommandTokens = item_tokens;
                            item.CommandUsage  = _menuEngine.GetUsage(command_name, text, item_tokens);
                            ret.Add(item);
                            item = null;
                        }
                        else
                        {
                            ret.Add(new InterpreterItem(i.Name, i.Path, i.Name, i.GetIcon(), InterpreterItem.OwnerType.Indexer, user_text));
                        }
                    }
                }
            }

            // take care of low priority commands
            foreach (InterpreterPlugin plugin in _plugins)
            {
                Command command = plugin.GetAssistingCommand(Command.PriorityType.Low, text);
                if (command != null /*&& !assisting_plugins.Contains(plugin.Name)*/ && !assisting_commands.Contains(command.Name))
                {
                    InterpreterItem item = new InterpreterItem(plugin.GetItemName(command.Name, text, null),
                                                               plugin.GetItemDescription(command.Name, text, null),
                                                               plugin.GetItemAutoComplete(command.Name, text, null),
                                                               plugin.GetItemIcon(command.Name, text, null),
                                                               InterpreterItem.OwnerType.Plugin,
                                                               user_text);
                    item.CommandName   = command.Name;
                    item.CommandTokens = null;
                    item.CommandUsage  = plugin.GetUsage(command.Name, text, null);
                    item.PluginId      = plugin.Name;
                    assisting_commands.Add(command.Name);
                    ret.Add(item);
                    item = null;
                }
            }

            //LearnedContent lc = SettingsManager.Instance.GetLearnedContents();
            //int pos = 0;
            //foreach (string token in lc.GetSortedKeywords(user_text))
            //{
            //    int curr_pos = -1;
            //    string distinguisher = lc.Distinguishers[token];
            //    switch (lc.Types[token])
            //    {
            //        case InterpreterItem.OwnerType.Indexer:
            //            curr_pos = ret.FindIndex(delegate(InterpreterItem a)
            //            {
            //                return a.Desciption == distinguisher;
            //            });
            //            break;
            //        default:
            //            curr_pos = ret.FindIndex(delegate(InterpreterItem a)
            //            {
            //                return a.Name == distinguisher;
            //            });
            //            break;
            //    }
            //    if (curr_pos != -1)
            //    {
            //        InterpreterItem to_insert = ret[curr_pos];
            //        ret.RemoveAt(curr_pos);
            //        ret.Insert(0, to_insert);
            //        pos++;
            //    }
            //}

            user_text          = null;
            assisting_commands = null;
            return(ret);
        }