def test_decode(self): rma = RakutenMA() rma.hash_func = None csent = rma.tokens2csent([["foo", "N"], ["bar", "N"]], "SBIEO") csent = rma.add_efeats(csent) for i in range(len(csent)): csent[i].l = "" rma.model["mu"] = WEIGHTS csent = rma.decode(csent) assert_equals(csent[0].l, "_") assert_equals(csent[1].l, "B-N") assert_equals(csent[2].l, "I-N") assert_equals(csent[3].l, "E-N") assert_equals(csent[4].l, "B-N") assert_equals(csent[5].l, "I-N") assert_equals(csent[6].l, "E-N") assert_equals(csent[7].l, "_") csent = rma.tokens2csent([["foX", "N"], ["bar", "N"]], "SBIEO") csent = rma.add_efeats(csent) csent = rma.decode(csent) assert_equals(csent[0].l, "_") assert_equals(csent[1].l, "B-N") assert_equals(csent[2].l, "I-N") assert_equals(csent[3].l, "O") assert_equals(csent[4].l, "B-N") assert_equals(csent[5].l, "I-N") assert_equals(csent[6].l, "E-N") assert_equals(csent[7].l, "_")
def test_csent2feats(self): rma = RakutenMA() rma.hash_func = None rma.featset = ["w0"] csent = rma.tokens2csent([["foo", "N"], ["bar", "N"]], "SBIEO") csent = rma.add_efeats(csent) feats = rma.csent2feats(csent) desired = ( ["w0", "", "_"], ["w0", "f", "B-N"], ["w0", "o", "I-N"], ["w0", "o", "E-N"], ["w0", "b", "B-N"], ["w0", "a", "I-N"], ["w0", "r", "E-N"], ["t", "B-N", "_"], ["t", "I-N", "B-N"], ["t", "E-N", "I-N"], ["t", "B-N", "E-N"], ["t", "_", "E-N"]) for d in desired: assert_true(d in feats) assert_true(["t", "E-N", "B-N"] not in feats) assert_true(["t", "B-N", "I-N"] not in feats)
def test_calc_states0(self): rma = RakutenMA() rma.hash_func = None rma.featset = ["c0", "w0"] csent = rma.tokens2csent([["foo", "N"], ["bar", "N"]], "SBIEO") csent = rma.add_efeats(csent) assert_equals(rma.calc_states0(csent[1].f, WEIGHTS), {"B-N": 2, "I-N": 1, "E-N": 1}) assert_equals(rma.calc_states0(csent[2].f, WEIGHTS), {"B-N": 1, "I-N": 2, "E-N": 2}) assert_equals(rma.calc_states0(csent[3].f, WEIGHTS), {"B-N": 1, "I-N": 2, "E-N": 2}) assert_equals(rma.calc_states0(csent[4].f, WEIGHTS), {"B-N": 2, "I-N": 1, "E-N": 1}) assert_equals(rma.calc_states0(csent[5].f, WEIGHTS), {"B-N": 1, "I-N": 2, "E-N": 1}) assert_equals(rma.calc_states0(csent[6].f, WEIGHTS), {"B-N": 1, "I-N": 1, "E-N": 2})
def test_add_efeats(self): # feature functions test rma = RakutenMA() rma.hash_func = None rma.featset = ["w0"] csent = rma.str2csent("A1-b") csent = rma.add_efeats(csent) assert_equals(csent[0].f, [["w0", ""]]) assert_equals(csent[1].f, [["w0", "A"]]) assert_equals(csent[2].f, [["w0", "1"]]) assert_equals(csent[3].f, [["w0", "-"]]) assert_equals(csent[4].f, [["w0", "b"]]) assert_equals(csent[5].f, [["w0", ""]]) rma.featset = ["b1"] csent = rma.add_efeats(csent) assert_equals(csent[0].f, [["b1", "", "A"]]) assert_equals(csent[1].f, [["b1", "A", "1"]]) assert_equals(csent[2].f, [["b1", "1", "-"]]) assert_equals(csent[3].f, [["b1", "-", "b"]]) assert_equals(csent[4].f, [["b1", "b", ""]]) assert_equals(csent[5].f, [["b1", "", ""]]) rma.featset = ["c0"] csent = rma.add_efeats(csent) assert_equals(csent[0].f, [["c0", ""]]) assert_equals(csent[1].f, [["c0", "A"]]) assert_equals(csent[2].f, [["c0", "N"]]) assert_equals(csent[3].f, [["c0", "O"]]) assert_equals(csent[4].f, [["c0", "a"]]) assert_equals(csent[5].f, [["c0", ""]]) rma.featset = ["d9"] csent = rma.add_efeats(csent) assert_equals(csent[0].f, [["d9", "", ""]]) assert_equals(csent[1].f, [["d9", "", "A"]]) assert_equals(csent[2].f, [["d9", "A", "N"]]) assert_equals(csent[3].f, [["d9", "N", "O"]]) assert_equals(csent[4].f, [["d9", "O", "a"]]) assert_equals(csent[5].f, [["d9", "a", ""]]) rma.featset = ["t0"] csent = rma.add_efeats(csent) assert_equals(csent[0].f, [["t0", "", "", "A"]]) assert_equals(csent[1].f, [["t0", "", "A", "1"]]) assert_equals(csent[2].f, [["t0", "A", "1", "-"]]) assert_equals(csent[3].f, [["t0", "1", "-", "b"]]) assert_equals(csent[4].f, [["t0", "-", "b", ""]]) assert_equals(csent[5].f, [["t0", "b", "", ""]]) # test a custom function for feature # args _t: a function which receives position i and returns the token, # taking care of boundary cases # i: current position # sample function -> returns if the character is a capitalized letter rma.featset = [lambda _t, i: ["CAP", "T" if _t(i).t == "A" else "F"]] csent = rma.add_efeats(csent) assert_equals(csent[0].f, [["CAP", "F"]]) assert_equals(csent[1].f, [["CAP", "T"]]) assert_equals(csent[2].f, [["CAP", "F"]]) assert_equals(csent[3].f, [["CAP", "F"]]) assert_equals(csent[4].f, [["CAP", "F"]]) assert_equals(csent[5].f, [["CAP", "F"]]) rma.featset = ["NONEXISTENT_FEATURE"] assert_raises(Exception, rma.add_efeats, csent)