Пример #1
0
        public void TestGet()
        {
            HierarchicalNGram gram = new HierarchicalNGram(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();

            Assert.IsTrue(FoundValue(compiledGram, "c", new string[] { "b", "a" }, 1000));
            Assert.IsTrue(FoundValue(compiledGram, "d", new string[] { "b", "a" }, 1000));
            Assert.IsTrue(FoundValue(compiledGram, "a", new string[] { "b", "a" }, 1000));

            Assert.IsTrue(FoundValue(compiledGram, "c", new string[] { "b", "c" }, 1000));
            Assert.IsTrue(FoundValue(compiledGram, "d", new string[] { "b", "c" }, 1000));
            Assert.IsTrue(FoundValue(compiledGram, "a", new string[] { "b", "c" }, 1000));

            Assert.IsTrue(FoundValue(compiledGram, "c", new string[] { "a", "a" }, 1000));
            Assert.IsTrue(FoundValue(compiledGram, "d", new string[] { "a", "a" }, 1000));
            Assert.IsTrue(FoundValue(compiledGram, "a", new string[] { "a", "a" }, 1000));

            Assert.IsTrue(FoundValue(compiledGram, "c", new string[] { "z", "d" }, 1000));
            Assert.IsTrue(FoundValue(compiledGram, "d", new string[] { "z", "d" }, 1000));
            Assert.IsTrue(FoundValue(compiledGram, "a", new string[] { "z", "d" }, 1000));

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

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

            Assert.Throws <UnityEngine.Assertions.AssertionException>(() =>
            {
                compiledGram.Get(new string[] { "z", "a", "d" });
            });
        }
Пример #2
0
        private bool FoundValue(ICompiledGram compiledGram, string expected, string[] input, int iterations)
        {
            bool found = false;

            for (int i = 0; i < iterations; ++i)
            {
                if (compiledGram.Get(input) == expected)
                {
                    found = true;
                    break;
                }
            }

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

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

                List <string> output = new List <string>();
                while (size > 0 && gram.HasNextStep(prior.ToArray()))
                {
                    string nextToken = gram.Get(prior.ToArray());
                    output.Add(nextToken);
                    prior.Add(nextToken);
                    --size;
                }

                if (size == 0)
                {
                    best = output;
                    break;
                }

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

            return(best);
        }
Пример #4
0
        public void TestGet()
        {
            NGram ngram = new NGram(2);

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

            ngram.AddData(new string[] { "a" }, "e");
            ngram.AddData(new string[] { "a" }, "e");
            ngram.AddData(new string[] { "a" }, "z");
            ngram.AddData(new string[] { "a" }, "z");

            ICompiledGram comipledGram = ngram.Compile();

            bool seenA = false;
            bool seenC = false;
            bool seenE = false;
            bool seenZ = false;

            for (int i = 0; i < 500; ++i)
            {
                string val = comipledGram.Get(new string[] { "b" });
                switch (val)
                {
                case "a":
                    seenA = true;
                    break;

                case "c":
                    seenC = true;
                    break;

                default:
                    Assert.Fail($"{val} should not be possible.");
                    break;
                }
            }

            for (int i = 0; i < 500; ++i)
            {
                string val = comipledGram.Get(new string[] { "a" });
                switch (val)
                {
                case "e":
                    seenE = true;
                    break;

                case "z":
                    seenZ = true;
                    break;

                default:
                    Assert.Fail($"{val} should not be possible.");
                    break;
                }
            }

            // in theory, we could potentially not see one of these but it is very unlikely.
            Assert.IsTrue(seenA);
            Assert.IsTrue(seenC);
            Assert.IsTrue(seenE);
            Assert.IsTrue(seenZ);

            Assert.Throws <UnityEngine.Assertions.AssertionException>(() =>
            {
                comipledGram.Get(null);
            });

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

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

            Assert.Throws <UnityEngine.Assertions.AssertionException>(() =>
            {
                comipledGram.Get(new string[] { "z" });
            });
        }