Пример #1
0
        private static void AddToTree(string s)
        {
            var node = new DistanceTree.Node(s, null);

            List <DistanceTree.Node> neighbors;
            var radius      = Levenshtein.MAX_RADIUS;
            var levenshtein = new Fastenshtein.Levenshtein(s);

            do
            {
                neighbors = Levenshtein.tree.Select(n =>
                {
                    n.Cache = levenshtein.Distance(n.Content);
                    return(n.Cache <= radius);
                }).ToList();
                radius++;
            } while (neighbors.Count == 0);

            var edges = neighbors.Select(x => new DistanceTree.Edge(node, x, x.Cache));

            node.Edges = edges.ToList();

            Levenshtein.tree.Add(node);
            Levenshtein.NodeDictionary.Add(s, node);
        }
Пример #2
0
        private ProjectResource GuessProject(IReadOnlyCollection <ProjectResource> projects, string guess)
        {
            var leven   = new Fastenshtein.Levenshtein(guess);
            var topFive = projects.OrderBy(p => leven.Distance(p.Name)).Take(5).ToList();
            var i       = 0;

            WriteLine($"Found 5 projects similar to {Colors.Bold(guess)}:");
            foreach (var project in topFive)
            {
                WriteLine($"{Colors.Cyan($"[{++i}]")} {project.Name}");
            }

            Write($"Use number {Colors.Cyan("[ ]")} or hit enter to search for another project name\rUse number {Colors.Cyan("[")}");
            if (int.TryParse(Console.ReadLine()?.Trim(), out var r))
            {
                return(topFive[r - 1]);
            }
            else
            {
                Write($"Search for project with name similar to: ");
                var name = Console.ReadLine();
                return(GuessProject(projects, name));
            }
        }