Beispiel #1
0
        public static List <CommandArgument> PredictUnspecifiedArguments(Command command, List <CommandArgument> arguments)
        {
            var usages       = GetCommandUsages(command);
            var scoredUsages = new List <Tuple <CommandUsage, double> >();

            foreach (var usage in usages)
            {
                var nScored         = 0;
                var totalSimilarity = 0.0;
                var maxArgIndex     = Math.Min(arguments.Count, usage.Executor.Arguments.Count);
                var useful          = false;

                for (var index = 0; index < maxArgIndex; index++)
                {
                    var usageArgument = usage.Executor.Arguments[index];

                    if (!usageArgument.IsSpecified)
                    {
                        continue;
                    }

                    var curArgument = arguments[index];

                    if (!curArgument.IsSpecified)
                    {
                        useful = useful || usageArgument.IsSpecified;
                        continue;
                    }

                    totalSimilarity += CommandGenerator.ScoreSimilarity(curArgument.FacetMoniker, usageArgument.FacetMoniker);

                    nScored++;
                }

                if (useful && nScored > 0)
                {
                    scoredUsages.Add(Tuple.Create(usage, totalSimilarity / nScored));
                }
            }

            scoredUsages = scoredUsages.OrderByDescending(x => x.Item2).ToList();

            var rvl = new List <CommandArgument>();

            for (var i = 0; i < arguments.Count; i++)
            {
                if (arguments[i].IsSpecified)
                {
                    rvl.Add(arguments[i]);
                    continue;
                }

                var parameter     = command.Parameters[i];
                var argument      = CommandArgument.Unspecified;
                var usageMonikers = scoredUsages
                                    .Select(x => x.Item1.Executor.Arguments[i])
                                    .Where(x => x.IsSpecified && parameter.IsUsableAsArgument(x.FacetMoniker))
                                    .Select(x => x.FacetMoniker)
                                    .ToArray();

                if (usageMonikers.Length > 0)
                {
                    argument = new CommandArgument(usageMonikers, !parameter.Optional);
                }
                else
                {
                    var mostUsed = GetMostUsedFacets(parameter, 5);

                    if (mostUsed.Length > 0)
                    {
                        argument = new CommandArgument(mostUsed, !parameter.Optional);
                    }
                }

                rvl.Add(argument);
            }

            return(rvl);
        }