コード例 #1
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]);
        }
コード例 #2
0
        public void TestWeightBuilding()
        {
            CompiledBackOffNGram compiled;
            BackOffNGram         gram;

            // test two detailed
            gram     = new BackOffNGram(2, 0.6f);
            compiled = gram.Compile() as CompiledBackOffNGram;

            Assert.AreEqual(0.375f, compiled.Weights[0]);
            Assert.AreEqual(0.625f, compiled.Weights[1]);
            Assert.AreEqual(1f, compiled.Weights[0] + compiled.Weights[1]);

            // test three detailed
            gram     = new BackOffNGram(3, 0.6f);
            compiled = gram.Compile() as CompiledBackOffNGram;

            Assert.IsTrue(
                Mathf.Approximately(0.183673469f, compiled.Weights[0]),
                $"Expected ~0.18 and received ${compiled.Weights[0]}");

            Assert.IsTrue(
                Mathf.Approximately(0.3061224f, compiled.Weights[1]),
                $"Expected ~0.306f and received ${compiled.Weights[1]}");

            Assert.IsTrue(
                Mathf.Approximately(0.5102041f, compiled.Weights[2]),
                $"Expected ~0.510f and received {compiled.Weights[2]}");

            Assert.AreEqual(1f, compiled.Weights[0] + compiled.Weights[1] + compiled.Weights[2]);

            // test 4 to 100 more broadly
            for (int i = 4; i < 100; ++i)
            {
                gram     = new BackOffNGram(i, 0.6f);
                compiled = gram.Compile() as CompiledBackOffNGram;
                Assert.IsNotNull(compiled);

                float previousWeight = 0;
                float total          = 0;
                foreach (float weight in compiled.Weights)
                {
                    total += weight;
                    Assert.IsFalse(weight <= 0, $"{i}: has a negative weight {weight}");

                    Assert.IsTrue(previousWeight < weight);
                    previousWeight = weight;
                }

                Assert.IsTrue(
                    Mathf.Approximately(1f, total),
                    $"Total weight is {total} which should atleast be very close to 1.");
            }
        }
コード例 #3
0
        public void TestHasNextStep()
        {
            BackOffNGram gram = new BackOffNGram(3, 0.6f);

            Assert.IsFalse(gram.Compile().HasNextStep(new string[] { "a", "b" }));

            gram.AddData(new string[] { "a", "b" }, "c");
            Assert.IsTrue(gram.Compile().HasNextStep(new string[] { "a", "b" }));
            Assert.IsTrue(gram.Compile().HasNextStep(new string[] { "a", "c" }));

            gram.AddData(new string[] { "a", "d" }, "c");
            Assert.IsTrue(gram.Compile().HasNextStep(new string[] { "a", "b" }));
            Assert.IsTrue(gram.Compile().HasNextStep(new string[] { "a", "d" }));
            Assert.IsTrue(gram.Compile().HasNextStep(new string[] { "a", "z" }));
        }
コード例 #4
0
        public void TestConstructor()
        {
            BackOffNGram gram = new BackOffNGram(3, 0.6f);

            Assert.DoesNotThrow(() => { gram.Compile(); });
            gram.AddData(new string[] { "b", "a" }, "a");
            Assert.DoesNotThrow(() => { gram.Compile(); });
            gram.AddData(new string[] { "b", "a" }, "z");
            Assert.DoesNotThrow(() => { gram.Compile(); });
            gram.AddData(new string[] { "b", "f" }, "2");
            gram.AddData(new string[] { "c", "g" }, "a");
            gram.AddData(new string[] { "d", "h" }, "e");
            gram.AddData(new string[] { "e", "h" }, "c");
            Assert.DoesNotThrow(() => { gram.Compile(); });
        }
コード例 #5
0
        public void TestGet()
        {
            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();

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

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

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

            Assert.IsTrue(FoundValue(compiledGram, "a", new string[] { "z", "d" }, 1000));
            Assert.IsTrue(FoundValue(compiledGram, "c", new string[] { "z", "d" }, 1000));
            Assert.IsTrue(FoundValue(compiledGram, "d", 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" });
            });
        }
コード例 #6
0
        public void TestSequenceProbability()
        {
            BackOffNGram gram = new BackOffNGram(3, 0.9f);

            Assert.AreEqual(
                0,
                gram.Compile().SequenceProbability(new string[] { "a", "b", "c" }));

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

            double triWeight = 0.9;
            double biWeight  = 0.81;

            double expected = (2 / 3d) * 0.5 * (triWeight / (triWeight + 0.5 * biWeight));
            double actual   = gram.Compile().SequenceProbability(input);

            Assert.IsTrue(
                Mathf.Approximately((float)expected, (float)actual),
                $"Expected {expected} but received {actual}.");
        }
コード例 #7
0
        public void TestGetValues()
        {
            BackOffNGram  uncompiled = new BackOffNGram(2, 0.6f);
            ICompiledGram compiled   = uncompiled.Compile();

            Assert.Throws <UnityEngine.Assertions.AssertionException>(() =>
            {
                compiled.GetValues(null);
            });

            Assert.Throws <UnityEngine.Assertions.AssertionException>(() =>
            {
                compiled.GetValues(new string[] { "a", "b" });
            });

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

            Assert.AreEqual(0, compiled.GetValues(new string[] { "a" }).Keys.Count);

            // Test with one entry a->c
            uncompiled.AddData(new string[] { "a" }, "c");
            compiled = uncompiled.Compile();

            Assert.Throws <UnityEngine.Assertions.AssertionException>(() =>
            {
                compiled.GetValues(null);
            });

            Assert.Throws <UnityEngine.Assertions.AssertionException>(() =>
            {
                compiled.GetValues(new string[] { "a", "b" });
            });

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

            float uniWeight = (0.36f / (0.6f + 0.36f));
            float biWeight  = (0.6f / (0.6f + 0.36f));

            Dictionary <string, float> values = compiled.GetValues(new string[] { "z" });

            Assert.AreEqual(2, values.Keys.Count);
            Assert.AreEqual(0.5f, values["a"]);
            Assert.AreEqual(0.5f, values["c"]);

            values = compiled.GetValues(new string[] { "a" });
            Assert.AreEqual(2, values.Keys.Count);

            float expected = 0.5f * uniWeight / (0.5f * uniWeight + biWeight);

            Assert.IsTrue(
                Mathf.Approximately(expected, values["a"]),
                $"Expected {expected} but received {values["a"]}.");

            expected = biWeight / (0.5f * uniWeight + biWeight);
            Assert.IsTrue(
                Mathf.Approximately(expected, values["c"]),
                $"Expected {expected} but received {values["c"]}.");

            // test with three entries a->c, b->c & b->d
            uncompiled.AddData(new string[] { "b" }, "c");
            uncompiled.AddData(new string[] { "b" }, "d");
            compiled = uncompiled.Compile();

            // in this case we haven't seen the prior "z" so we only have the
            // unigram to work with
            values = compiled.GetValues(new string[] { "z" });

            Assert.AreEqual(4, values.Keys.Count);
            Assert.IsTrue(values.ContainsKey("a")); // 1
            Assert.IsTrue(values.ContainsKey("b")); // 2
            Assert.IsTrue(values.ContainsKey("c")); // 2
            Assert.IsTrue(values.ContainsKey("d")); // 1

            Assert.AreEqual(1 / 6f, values["a"]);
            Assert.AreEqual(2 / 6f, values["b"]);
            Assert.AreEqual(2 / 6f, values["c"]);
            Assert.AreEqual(1 / 6f, values["d"]);
        }
コード例 #8
0
        public void TestClone()
        {
            BackOffNGram gram = new BackOffNGram(3, 0.6f);

            gram.AddData(new string[] { "b", "a" }, "a");
            gram.AddData(new string[] { "b", "a" }, "z");
            gram.AddData(new string[] { "b", "f" }, "2");
            gram.AddData(new string[] { "c", "g" }, "a");
            gram.AddData(new string[] { "d", "h" }, "e");
            gram.AddData(new string[] { "e", "h" }, "c");

            CompiledBackOffNGram compiledGram = gram.Compile() as CompiledBackOffNGram;

            Assert.IsNotNull(compiledGram);

            CompiledBackOffNGram clone = compiledGram.Clone() as CompiledBackOffNGram;

            Assert.IsNotNull(clone);

            Assert.AreEqual(compiledGram.GetN(), clone.GetN());

            // uni-gram
            TestValues(
                compiledGram.CompiledGrammars[0] as CompiledUniGram,
                clone.CompiledGrammars[0] as CompiledUniGram,
                null);

            // bi-gram
            TestValues(
                compiledGram.CompiledGrammars[1] as CompiledNGram,
                clone.CompiledGrammars[1] as CompiledNGram,
                new string[] { "a" });
            TestValues(
                compiledGram.CompiledGrammars[1] as CompiledNGram,
                clone.CompiledGrammars[1] as CompiledNGram,
                new string[] { "f" });
            TestValues(
                compiledGram.CompiledGrammars[1] as CompiledNGram,
                clone.CompiledGrammars[1] as CompiledNGram,
                new string[] { "g" });
            TestValues(
                compiledGram.CompiledGrammars[1] as CompiledNGram,
                clone.CompiledGrammars[1] as CompiledNGram,
                new string[] { "h" });

            // tri-gram
            TestValues(
                compiledGram.CompiledGrammars[2] as CompiledNGram,
                clone.CompiledGrammars[2] as CompiledNGram,
                new string[] { "b", "a" });
            TestValues(
                compiledGram.CompiledGrammars[2] as CompiledNGram,
                clone.CompiledGrammars[2] as CompiledNGram,
                new string[] { "b", "f" });
            TestValues(
                compiledGram.CompiledGrammars[2] as CompiledNGram,
                clone.CompiledGrammars[2] as CompiledNGram,
                new string[] { "c", "g" });
            TestValues(
                compiledGram.CompiledGrammars[2] as CompiledNGram,
                clone.CompiledGrammars[2] as CompiledNGram,
                new string[] { "d", "h" });
            TestValues(
                compiledGram.CompiledGrammars[2] as CompiledNGram,
                clone.CompiledGrammars[2] as CompiledNGram,
                new string[] { "e", "h" });
        }