Exemple #1
0
        public IList <Word> SemanticallyNear(string word, int maxEdits, IDistanceResolver distanceResolver = null)
        {
            if (distanceResolver == null)
            {
                distanceResolver = new LevenshteinDistanceResolver(word, maxEdits);
            }

            var words = new List <Word>();

            LcrsNode node;

            var prefix = word[0].ToString();

            if (TryFindDepthFirst(prefix, out node))
            {
                if (node.EndOfWord &&
                    distanceResolver.IsValid(node.Value, 0) &&
                    distanceResolver.GetDistance(word, prefix) <= maxEdits)
                {
                    words.Add(new Word(word[0].ToString()));
                }

                WithinEditDistanceDepthFirst(
                    word, word[0].ToString(), words, 1, maxEdits, distanceResolver);
            }

            return(words);
        }
Exemple #2
0
        public IEnumerable<Word> SemanticallyNear(string word, int maxEdits, IDistanceResolver distanceResolver = null)
        {
            if (distanceResolver == null) distanceResolver = new LevenshteinDistanceResolver(word, maxEdits);

             var compressed = new List<Word>();
            if (LeftChild != null)
            {
                LeftChild.WithinEditDistanceDepthFirst(word, string.Empty, compressed, 0, maxEdits, distanceResolver);
            }
            return compressed;
        }
Exemple #3
0
        public IEnumerable <Word> SemanticallyNear(string word, int maxEdits, IDistanceResolver distanceResolver = null)
        {
            if (distanceResolver == null)
            {
                distanceResolver = new LevenshteinDistanceResolver(word, maxEdits);
            }

            var words = new List <Word>();

            while (true)
            {
                LcrsNode node;

                var prefix = word[0].ToString();

                if (TryFindDepthFirst(prefix, out node))
                {
                    if (node.EndOfWord &&
                        distanceResolver.IsValid(node.Value, 0) &&
                        distanceResolver.GetDistance(word, prefix) <= maxEdits)
                    {
                        words.Add(new Word(word[0].ToString()));
                    }

                    WithinEditDistanceDepthFirst(
                        word, word[0].ToString(), words, 1, maxEdits, distanceResolver);
                }

                if (HasMoreSegments())
                {
                    GoToNextSegment();
                }
                else
                {
                    break;
                }
            }

            return(words);
        }
        public void Can_calculate_distance()
        {
            var query = "rambo";
            var auto  = new LevenshteinDistanceResolver(query, 1);

            Assert.IsTrue(auto.IsValid('r', 0));
            Assert.IsTrue(auto.IsValid('a', 1));
            Assert.IsTrue(auto.IsValid('m', 2));
            Assert.IsTrue(auto.IsValid('b', 3));
            Assert.IsTrue(auto.IsValid('o', 4));

            Assert.IsTrue(auto.IsValid('x', 0));
            Assert.IsTrue(auto.IsValid('a', 1));
            Assert.IsTrue(auto.IsValid('m', 2));
            Assert.IsTrue(auto.IsValid('b', 3));
            Assert.IsTrue(auto.IsValid('o', 4));

            Assert.IsTrue(auto.IsValid('x', 0));
            Assert.IsTrue(auto.IsValid('a', 1));
            Assert.IsTrue(auto.IsValid('m', 2));
            Assert.IsTrue(auto.IsValid('b', 3));
            Assert.IsTrue(auto.IsValid('o', 4));
            Assert.IsFalse(auto.IsValid('x', 5));

            Assert.IsTrue(auto.IsValid('r', 0));
            Assert.IsTrue(auto.IsValid('a', 1));
            Assert.IsTrue(auto.IsValid('m', 2));
            Assert.IsTrue(auto.IsValid('b', 3));
            Assert.IsTrue(auto.IsValid('o', 4));
            Assert.IsTrue(auto.IsValid('x', 5));

            Assert.IsTrue(auto.IsValid('r', 0));
            Assert.IsTrue(auto.IsValid('a', 1));
            Assert.IsTrue(auto.IsValid('m', 2));
            Assert.IsTrue(auto.IsValid('b', 3));
            Assert.IsTrue(auto.IsValid('o', 4));
            Assert.IsTrue(auto.IsValid('x', 5));
            Assert.IsFalse(auto.IsValid('x', 6));
        }