Exemplo n.º 1
0
        // recherche les mots dans la boule centré sur "word" de rayon "maxDistance"
        private IEnumerable <TreeNode> FindCloserNodes(TreeNode currentNode, string word, int maxDistance)
        {
            int distance = CloserWordComparer.Compare(word.ToLowerInvariant(), currentNode.Label.Word);

            if (distance >= 0 && distance <= maxDistance)
            {
                yield return(currentNode);
            }

            foreach (
                TreeNode child in
                currentNode.Children.Where(x => Math.Abs(x.Key - distance) <= maxDistance).Select(x => x.Value))
            {
                foreach (TreeNode node in FindCloserNodes(child, word, maxDistance))
                {
                    yield return(node);
                }
            }
        }
Exemplo n.º 2
0
        // trouve le mot le plus proche de word, en resolvant toute ambiguité
        public int FindCloserWord(string word, out string result)
        {
            // trouve les mots à une distance "DistanceThreshold"
            var matching =
                FindCloserNodes(Root, word, DistanceThreshold)
                .GroupBy(x => CloserWordComparer.Compare(word, x.Label.Word))
                .OrderBy(x => x.Key).First().ToList();


            // aucune correspondance
            if (matching.Count == 0)
            {
                result = null;
                return(-1);
            }

            // resout les ambiguité
            result = AmbiguityResolver.FindBetter(word, matching.Select(x => x.Label)).Word;

            // retourne la distance entre word et result
            return(TreeBuildingWordComparer.Compare(word, result));
        }