Ejemplo n.º 1
0
        private void InputField_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.ListSimilar.Items.Clear();

            if (!string.IsNullOrWhiteSpace(this.InputField.Text))
            {
                foreach (var similar in Levenshtein.FindSimilar(this.InputField.Text))
                {
                    this.ListSimilar.Items.Add(similar);
                }
            }
        }
Ejemplo n.º 2
0
        private static void FindSimilar(DistanceTree.Node node, int radius, List <DistanceTree.Node> nodes, int distance, int searchRadius)
        {
            if (radius > searchRadius)
            {
                return;
            }

            node.Cache = distance;
            nodes.Add(node);

            foreach (var edge in node.Edges)
            {
                Levenshtein.FindSimilar(edge.End, radius + 1, nodes, distance + edge.Distance, searchRadius);
            }
        }
Ejemplo n.º 3
0
        public static IEnumerable <string> FindSimilar(string s)
        {
            var ss           = s.ToUpper();
            var searchRadius = Math.Min(s.Length - 2, Levenshtein.MAX_RADIUS);

            if (!Levenshtein.NodeDictionary.ContainsKey(ss) || searchRadius == 0)
            {
                return(new string[0]);
            }

            var node = Levenshtein.NodeDictionary[ss];

            var retval = new List <DistanceTree.Node>();

            Levenshtein.FindSimilar(node, 0, retval, 0, searchRadius);

            return(retval.Distinct().Where(x => x.Cache <= searchRadius && x.Content != ss).OrderBy(x => x.Cache).Select(x => x.Content.ToLower()));
        }