示例#1
0
    def test_case2(self):
        # case2: C = 1.0, phi = 2.0

        scw = SCW(2.0, 1.0)

        assert_equals(scw.psi, 3.0)
        assert_equals(scw.zeta, 5.0)

        x = {}
        Trie.insert(x, ["a", "b", "c"], 1.0)
        Trie.insert(scw.mu, ["a", "b", "c"], 1.0)

        margin = scw.calc_margin(x, 1)
        assert_equals(margin, 1.0)

        variance = scw.calc_variance(x)
        assert_equals(variance, 1.0)

        alpha = scw.calc_alpha(margin, variance)
        assert_almost_equals(alpha, (math.sqrt(24)-3)/5)

        beta = scw.calc_beta(margin, variance, alpha)
        desired = ((2 * (math.sqrt(24) - 3) / 5) /
                   (0.5 *
                    (-2 * (math.sqrt(24) - 3) / 5 +
                        math.sqrt(4 * (33 - 6 * math.sqrt(24)) / 25 + 4)) +
                    2 * (math.sqrt(24) - 3) / 5))
        assert_almost_equals(beta, desired)

        Trie.insert(x, ["a", "b", "d"], 2.0)
        scw.update_mu_sigma(x, -1, 0.2, 0.5)
        assert_equals(Trie.find(scw.mu, ["a", "b", "c"]), 0.8)
        assert_equals(Trie.find(scw.mu, ["a", "b", "d"]), -0.4)
        assert_equals(Trie.find(scw.sigma, ["a", "b", "c"]), 0.5)
        assert_equals(Trie.find(scw.sigma, ["a", "b", "d"]), -1.0)
示例#2
0
 def test_toString(self):
     trie = {}
     Trie.insert(trie, ["a", "b"], 1)
     Trie.insert(trie, ["a", "c"], 2)
     actual = Trie.toString(trie)
     desired = set(["a b\t1", "a c\t2"])
     assert_equals(set(actual.splitlines()), desired)
示例#3
0
 def get_trie2(self):
     trie2 = {}
     Trie.insert(trie2, ["t", "e", "a"], 3)
     Trie.insert(trie2, ["t", "e", "d"], 2)
     Trie.insert(trie2, ["t", "e", "n"], 1)
     Trie.insert(trie2, ["t", "e", "x"], 10)
     Trie.insert(trie2, ["t", "e"], 10)
     return trie2
    def test_train_one(self):
        rma = RakutenMA()
        rma.featset = ["w0"]

        res = rma.train_one([["foo", "N-nc"], ["bar", "N-nc"]])
        assert_true(res["updated"])
        assert_true(Trie.find(rma.model["mu"], ["w0", "f", "B-N"]) > 0)
        assert_true(Trie.find(rma.model["mu"], ["w0", "o", "I-N"]) > 0)
        assert_true(Trie.find(rma.model["mu"], ["w0", "o", "E-N"]) > 0)
        assert_equals(rma.tokenize("foobar"), [["foo", "N-nc"], ["bar", "N-nc"]])
示例#5
0
 def test_find(self):
     trie = self.get_trie1()
     assert_equals(Trie.find(trie, ["A"]), 15)
     assert_equals(Trie.find(trie, ["t", "o"]), 7)
     assert_equals(Trie.find(trie, ["t", "e", "a"]), 3)
     assert_equals(Trie.find(trie, ["t", "e", "d"]), 4)
     assert_equals(Trie.find(trie, ["t", "e", "n"]), 12)
     assert_equals(Trie.find(trie, ["i"]), 11)
     assert_equals(Trie.find(trie, ["i", "n"]), 5)
     assert_equals(Trie.find(trie, ["i", "n", "n"]), 9)
     assert_equals(Trie.find(trie, ["i", "n", "n", "n"]), None)
     assert_equals(Trie.find(trie, ["z"]), None)
示例#6
0
    def test_mult(self):
        trie1 = self.get_trie1()
        trie2 = self.get_trie2()
        Trie.mult(trie1, trie2)

        assert_equals(Trie.find(trie1, ["A"]), 15)
        assert_equals(Trie.find(trie1, ["t", "e", "a"]), 9)
        assert_equals(Trie.find(trie1, ["t", "e", "d"]), 8)
        assert_equals(Trie.find(trie1, ["t", "e", "n"]), 12)
        assert_equals(Trie.find(trie1, ["t", "e", "x"]), None)
        assert_equals(Trie.find(trie1, ["t", "e"]), None)
示例#7
0
 def test_each(self):
     trie1 = self.get_trie1()
     trie2 = {}
     Trie.each(trie1, lambda key, val: Trie.insert(trie2, key, val))
     assert_equals(trie2, trie1)
示例#8
0
 def test_find_partial(self):
     trie = self.get_trie1()
     assert_equals(Trie.find_partial(trie, ["t", "e"])["a"]["v"], 3)
     assert_equals(Trie.find_partial(trie, ["x"]), None)
示例#9
0
 def get_trie1(self):
     trie = {}
     Trie.insert(trie, ["A"], 15)
     Trie.insert(trie, ["t", "o"], 7)
     Trie.insert(trie, ["t", "e", "a"], 3)
     Trie.insert(trie, ["t", "e", "d"], 4)
     Trie.insert(trie, ["t", "e", "n"], 12)
     Trie.insert(trie, ["i"], 11)
     Trie.insert(trie, ["i", "n"], 5)
     Trie.insert(trie, ["i", "n", "n"], 9)
     return trie
示例#10
0
    def test_add_coef(self):
        trie1 = self.get_trie1()
        trie2 = self.get_trie2()
        Trie.add_coef(trie1, trie2, 0.1)  # with default value = 0

        assert_equals(Trie.find(trie1, ["A"]), 15)
        assert_equals(Trie.find(trie1, ["t", "e", "a"]), 3.3)
        assert_equals(Trie.find(trie1, ["t", "e", "d"]), 4.2)
        assert_equals(Trie.find(trie1, ["t", "e", "n"]), 12.1)
        assert_equals(Trie.find(trie1, ["t", "e", "x"]), 1)
        assert_equals(Trie.find(trie1, ["t", "e"]), 1)

        trie1 = self.get_trie1()
        Trie.add_coef(trie1, trie2, 0.1, 1.0)  # with default value = 1

        assert_equals(Trie.find(trie1, ["A"]), 15)
        assert_equals(Trie.find(trie1, ["t", "e", "a"]), 3.3)
        assert_equals(Trie.find(trie1, ["t", "e", "d"]), 4.2)
        assert_equals(Trie.find(trie1, ["t", "e", "n"]), 12.1)
        assert_equals(Trie.find(trie1, ["t", "e", "x"]), 2)
        assert_equals(Trie.find(trie1, ["t", "e"]), 2)
示例#11
0
 def test_inner_prod(self):
     trie1 = self.get_trie1()
     trie2 = self.get_trie2()
     assert_equals(Trie.inner_prod(trie1, trie2), 29)
示例#12
0
    def test_prune(self):
        scw = SCW(0.0, 1.0)
        Trie.insert(scw.mu, ["a", "b", "c"], 0.5)
        Trie.insert(scw.mu, ["a", "b", "d"], 1.5)
        Trie.insert(scw.sigma, ["a", "b", "c"], 0.5)
        Trie.insert(scw.sigma, ["a", "b", "d"], 0.5)

        scw.prune(1.0, 0.8)
        assert_equals(Trie.find(scw.mu, ["a", "b", "c"]), 0)
        assert_equals(Trie.find(scw.mu, ["a", "b", "d"]), 0.5)
        assert_equals(Trie.find(scw.sigma, ["a", "b", "c"]), 0.5)
        assert_equals(Trie.find(scw.sigma, ["a", "b", "d"]), 0.5)

        scw.prune(1.0, 0.4)
        assert_equals(Trie.find(scw.mu, ["a", "b", "c"]), None)
        assert_equals(Trie.find(scw.mu, ["a", "b", "d"]), None)
        assert_equals(Trie.find(scw.sigma, ["a", "b", "c"]), None)
        assert_equals(Trie.find(scw.sigma, ["a", "b", "d"]), None)
示例#13
0
    def test_case1(self):
        scw = SCW(0.0, 1.0)

        assert_equals(scw.psi, 1.0)
        assert_equals(scw.zeta, 1.0)

        x = {}

        Trie.insert(x, ["a", "b", "c"], 1.0)
        Trie.insert(scw.mu, ["a", "b", "c"], 1.0)

        margin = scw.calc_margin(x, 1)
        assert_equals(margin, 1.0)

        variance = scw.calc_variance(x)
        assert_equals(variance, 1.0)

        alpha = scw.calc_alpha(margin, variance)
        assert_equals(alpha, 0.0)
        beta = scw.calc_beta(margin, variance, alpha)
        assert_equals(beta, 0.0)

        Trie.insert(x, ["a", "b", "d"], 2.0)
        Trie.insert(scw.mu, ["a", "b", "d"], 0.5)
        Trie.insert(scw.sigma, ["a", "b", "d"], 0.5)

        assert_equals(scw.calc_margin(x, -1), -2.0)
        assert_equals(scw.calc_variance(x), 3.0)

        scw.update_mu_sigma(x, 1, 1.0, 1.0)

        assert_equals(Trie.find(scw.mu, ["a", "b", "c"]), 2.0)
        assert_equals(Trie.find(scw.mu, ["a", "b", "d"]), 1.5)
        assert_equals(Trie.find(scw.sigma, ["a", "b", "c"]), 0.0)
        assert_equals(Trie.find(scw.sigma, ["a", "b", "d"]), -0.5)