Пример #1
0
        public void TestGetGuesses()
        {
            HierarchicalNGram gram = new HierarchicalNGram(3, 0.6f);

            gram.AddData(new string[] { "b", "a" }, "c");
            gram.AddData(new string[] { "b", "c" }, "c");
            gram.AddData(new string[] { "b", "a" }, "a");
            gram.AddData(new string[] { "a", "a" }, "d");

            ICompiledGram compiledGram = gram.Compile();

            string[] guesses = compiledGram.GetGuesses(new string[] { "b", "a" });
            Assert.IsTrue(guesses.Contains("a"));
            Assert.IsTrue(guesses.Contains("c"));
            Assert.IsTrue(guesses.Contains("d"));

            guesses = compiledGram.GetGuesses(new string[] { "b", "c" });
            Assert.IsTrue(guesses.Contains("a"));
            Assert.IsTrue(guesses.Contains("c"));
            Assert.IsTrue(guesses.Contains("d"));

            guesses = compiledGram.GetGuesses(new string[] { "a", "a" });
            Assert.IsTrue(guesses.Contains("a"));
            Assert.IsTrue(guesses.Contains("c"));
            Assert.IsTrue(guesses.Contains("d"));
        }
Пример #2
0
        public void TestGetGuesses()
        {
            BackOffNGram gram = new BackOffNGram(3, 0.6f);

            gram.AddData(new string[] { "b", "a" }, "a");
            gram.AddData(new string[] { "b", "a" }, "c");
            gram.AddData(new string[] { "b", "c" }, "c");
            gram.AddData(new string[] { "a", "a" }, "d");

            ICompiledGram compiledGram = gram.Compile();

            string[] guesses = compiledGram.GetGuesses(new string[] { "b", "a" });
            Assert.AreEqual(4, guesses.Length);
            Assert.IsTrue(guesses.Contains("a"));
            Assert.IsTrue(guesses.Contains("b"));
            Assert.IsTrue(guesses.Contains("c"));
            Assert.IsTrue(guesses.Contains("d"));

            Assert.IsTrue(guesses[0] == "a" || guesses[0] == "c");
            Assert.IsTrue(guesses[1] == "a" || guesses[1] == "c");
            Assert.AreNotEqual(guesses[0], guesses[1]);

            Assert.IsTrue(guesses[2] == "b" || guesses[2] == "d");
            Assert.IsTrue(guesses[3] == "b" || guesses[3] == "d");
            Assert.AreNotEqual(guesses[2], guesses[3]);

            guesses = compiledGram.GetGuesses(new string[] { "b", "c" });
            Assert.AreEqual(4, guesses.Length);
            Assert.IsTrue(guesses.Contains("a"));
            Assert.IsTrue(guesses.Contains("b"));
            Assert.IsTrue(guesses.Contains("c"));
            Assert.IsTrue(guesses.Contains("d"));

            Assert.AreEqual("c", guesses[0]);

            Assert.IsTrue(guesses[1] == "a" || guesses[1] == "b" || guesses[1] == "d");
            Assert.IsTrue(guesses[2] == "a" || guesses[2] == "b" || guesses[2] == "d");
            Assert.IsTrue(guesses[3] == "a" || guesses[3] == "b" || guesses[3] == "d");
            Assert.AreNotEqual(guesses[1], guesses[2]);
            Assert.AreNotEqual(guesses[2], guesses[3]);

            guesses = compiledGram.GetGuesses(new string[] { "a", "a" });
            Assert.AreEqual(4, guesses.Length);
            Assert.IsTrue(guesses.Contains("a"));
            Assert.IsTrue(guesses.Contains("b"));
            Assert.IsTrue(guesses.Contains("c"));
            Assert.IsTrue(guesses.Contains("d"));

            Assert.AreEqual("d", guesses[0]);

            Assert.IsTrue(guesses[1] == "a" || guesses[1] == "b" || guesses[1] == "c");
            Assert.IsTrue(guesses[2] == "a" || guesses[2] == "b" || guesses[2] == "c");
            Assert.IsTrue(guesses[3] == "a" || guesses[3] == "b" || guesses[3] == "c");
            Assert.AreNotEqual(guesses[1], guesses[2]);
            Assert.AreNotEqual(guesses[2], guesses[3]);
        }
Пример #3
0
        public string[] GetGuesses(string[] inData)
        {
            HashSet <string> seenGuesses = new HashSet <string>();
            List <string>    guesses     = new List <string>();
            int length = inData.Length;

            for (int i = CompiledGrammars.Length - 1; i >= 0; --i)
            {
                ICompiledGram gram = CompiledGrammars[i];
                int           n    = gram.GetN() - 1;

                ArraySegment <string> segment = new ArraySegment <string>(inData, length - n, n);
                string[] grammarGuesses       = gram.GetGuesses(segment.ToArray());
                foreach (string guess in grammarGuesses)
                {
                    if (seenGuesses.Contains(guess) == false)
                    {
                        seenGuesses.Add(guess);
                        guesses.Add(guess);
                    }
                }
            }

            return(guesses.ToArray());
        }
Пример #4
0
        public void TestGetGuesses()
        {
            NGram         ngram        = new NGram(3);
            ICompiledGram compiledGram = ngram.Compile();

            Assert.Throws <UnityEngine.Assertions.AssertionException>(() =>
            {
                compiledGram.GetGuesses(null);
            });

            Assert.Throws <UnityEngine.Assertions.AssertionException>(() =>
            {
                compiledGram.GetGuesses(new string[] { "b" });
            });

            Assert.Throws <UnityEngine.Assertions.AssertionException>(() =>
            {
                compiledGram.GetGuesses(new string[] { "b", "c", "d" });
            });

            string[] res = compiledGram.GetGuesses(new string[] { "b", "c" });
            Assert.IsNotNull(res);
            Assert.AreEqual(0, res.Length);

            ngram.AddData(new string[] { "a", "b" }, "c");
            string[] guesses = ngram.Compile().GetGuesses(new string[] { "a", "b" });
            Assert.AreEqual(1, guesses.Length);
            Assert.AreEqual("c", guesses[0]);

            ngram.AddData(new string[] { "a", "b" }, "c");
            ngram.AddData(new string[] { "a", "b" }, "d");
            guesses = ngram.Compile().GetGuesses(new string[] { "a", "b" });
            Assert.AreEqual(2, guesses.Length);
            Assert.IsTrue(guesses.Contains("c"));
            Assert.IsTrue(guesses.Contains("d"));

            ngram.AddData(new string[] { "a", "e" }, "e");
            guesses = ngram.Compile().GetGuesses(new string[] { "a", "e" });
            Assert.AreEqual(1, guesses.Length);
            Assert.AreEqual("e", guesses[0]);
        }
Пример #5
0
        private static List <string> GenerateTree(
            ICompiledGram gram,
            CircularQueue <string> prior,
            int size,
            int index,
            List <string> acceptedTypes,
            Func <string, string> classifier)
        {
            string[] guesses = gram.GetGuesses(prior.ToArray());
            foreach (string guess in guesses)
            {
                if (classifier != null &&
                    !classifier(guess).Equals(acceptedTypes[index]))
                {
                    continue;
                }

                CircularQueue <string> newPrior = prior.Clone();
                newPrior.Add(guess);

                if (size <= 1)
                {
                    return(new List <string>()
                    {
                        guess
                    });
                }
                else if (gram.HasNextStep(newPrior.ToArray()))
                {
                    List <string> returnVal = GenerateTree(
                        gram,
                        newPrior,
                        size - 1,
                        index + 1,
                        acceptedTypes,
                        classifier);

                    if (returnVal != null)
                    {
                        returnVal.Insert(0, guess);
                        return(returnVal);
                    }
                }
            }

            return(null);
        }
Пример #6
0
        public string Get(string[] inData)
        {
            Assert.IsNotNull(inData);
            Assert.AreEqual(n - 1, inData.Length);

            int length = inData.Length;

            for (int i = CompiledGrammars.Length - 1; i >= 0; --i)
            {
                ICompiledGram gram = CompiledGrammars[i];
                int           n    = gram.GetN() - 1;

                ArraySegment <string> segment = new ArraySegment <string>(inData, length - n, n);
                string[] grammarGuesses       = gram.GetGuesses(segment.ToArray());
                if (grammarGuesses.Length > 0)
                {
                    return(grammarGuesses[0]);
                }
            }

            return(null);
        }
Пример #7
0
        // @NOTE: this is used for simulation. Do not use outside of it.
        public static List <string> GenerateBestRestrictedAttempt(
            ICompiledGram gram,
            List <string> start,
            List <string> acceptedTypes,
            Func <string, string> classifier,
            int maxAttempts)
        {
            List <string> best = null;

            for (int attempt = 0; attempt < maxAttempts; ++attempt)
            {
                CircularQueue <string> prior = new CircularQueue <string>(gram.GetN() - 1);
                prior.AddRange(start);

                int           acceptedTypeIndex = 0;
                List <string> output            = new List <string>();
                string        token;
                int           i;

                while (acceptedTypeIndex < acceptedTypes.Count && gram.HasNextStep(prior.ToArray()))
                {
                    string[] tokens    = gram.GetGuesses(prior.ToArray());
                    string   nextToken = null;

                    string acceptedType = acceptedTypes[acceptedTypeIndex];
                    for (i = 0; i < tokens.Length; ++i)
                    {
                        token = tokens[i];
                        if (classifier.Invoke(token).Equals(acceptedType))
                        {
                            nextToken = token;
                        }
                    }

                    if (nextToken != null)
                    {
                        output.Add(nextToken);
                        prior.Add(nextToken);
                        acceptedTypeIndex += 1;
                    }
                    else
                    {
                        break;
                    }
                }

                if (output.Count == acceptedTypes.Count)
                {
                    best = output;
                    break;
                }

                if (best == null)
                {
                    best = output;
                }
                else if (output.Count > best.Count)
                {
                    best = output;
                }
            }

            return(best);
        }