Exemplo n.º 1
0
def test_tag_words():
    lx = Lexicon()

    assert tag_words(lx, ['a']) == [['AR']]
    assert tag_words(lx, ['and']) == [['AND']]
    assert tag_words(lx, ['a', 'and']) == [['AR', 'AND']]

    assert tag_words(lx, ['a', 'and', 'does']) == [['AR', 'AND', 'DOs']]
Exemplo n.º 2
0
def test_lexicon():
    lx = Lexicon()
    lx.add("John", "P")
    lx.add("Mary", "P")
    lx.add("like", "T")
    r = lx.getAll("P")
    assert 'John' in r and "Mary" in r
Exemplo n.º 3
0
def test_taging_with_trained_lexer():
    lx = Lexicon()
    lx.add("John", "P")
    lx.add("orange", "N")
    lx.add("orange", "A")
    lx.add("fish", "N")
    lx.add("fish", "I")
    lx.add("like", "T")

    word2tags = {
        "John": ["P"],
        "orange": ["A", "Ns"],
        "fish": ['Ip', 'Np', 'Ns'],
        "a": ["AR"],
        "zxghqw": [],
        "likes": ["Ts"],
    }

    for w, tags in word2tags.items():
        assert sorted(tag_word(lx, w)) == tags
Exemplo n.º 4
0
def test_processing():
    lx = Lexicon()
    fb = FactBase()

    sentences = [
        "Mary is a duck.",
        "John is purple.",
        "Mary flies.",

        # Love story
        "John likes Mary.",
        "Mary likes Joshua.",
        "Joshua likes John.",
    ]

    for s in sentences:
        assert process_statement(lx, s.rstrip('.').split(' '), fb) == ''

    assert "Mary" in lx.getAll("P")
    assert "duck" in lx.getAll("N")
    assert "purple" in lx.getAll("A")
    assert "fly" in lx.getAll("I")
    assert "like" in lx.getAll("T")

    assert fb.queryUnary("N_duck", "Mary")
    assert fb.queryUnary("A_purple", "John")
    assert fb.queryUnary("I_fly", "Mary")

    # love triangle:
    assert fb.queryBinary("T_like", "John", "Mary")
    assert not fb.queryBinary("T_like", "Mary", "John")
    assert fb.queryBinary("T_like", "Mary", "Joshua")
    assert fb.queryBinary("T_like", "Joshua", "John")
Exemplo n.º 5
0
def test_function_words():
    cases = {
        'a': ['AR'],
        'an': ['AR'],
        'and': ['AND'],
        'is': ['BEs'],
        'are': ['BEp'],
        'does': ['DOs'],
        'do': ['DOp'],
        'who': ['WHO'],
        'which': ['WHICH'],
        'Who': ['WHO'],
        'Which': ['WHICH'],
        '?': ['?'],
    }

    l = Lexicon()
    for w, tags in cases.items():
        assert tag_word(l, w) == tags
Exemplo n.º 6
0
def test_tag_word():
    # Creating and training the lexicon:
    lx = Lexicon()
    lx.add("John", "P")
    lx.add("orange", "N")
    lx.add("orange", "A")
    lx.add("fish", "N")
    lx.add("fish", "T")
    lx.add("fish", "I")
    lx.add("sell", "T")
    lx.add("fly", "I")
    lx.add("man", "N")
    # testing:

    assert sorted(pt.tag_word(lx, "John")) == sorted(["P"])
    assert sorted(pt.tag_word(lx, "orange")) == sorted(["Ns", "A"])
    assert (sorted(pt.tag_word(lx, "fish")) == sorted(["Ns", "Np", "Ip",
                                                       "Tp"]))
    assert sorted(pt.tag_word(lx, "a")) == sorted(["AR"])
    assert sorted(pt.tag_word(lx, "sells")) == sorted(["Ts"])
    assert sorted(pt.tag_word(lx, "sell")) == sorted(["Tp"])
    assert sorted(pt.tag_word(lx, "flies")) == sorted(["Is"])
    assert sorted(pt.tag_word(lx, "fly")) == sorted(["Ip"])
    assert sorted(pt.tag_word(lx, "oranges")) == sorted(["Np"])
    assert sorted(pt.tag_word(lx, "men")) == sorted(["Np"])
    assert sorted(pt.tag_word(lx, "man")) == sorted(["Ns"])
    assert pt.tag_word(lx, "ffjjdjd") == []
Exemplo n.º 7
0
def test_lexicon():
    lx = Lexicon()
    lx.add("John", "P")
    lx.add("Mary", "P")
    lx.add("like", "T")
    assert set(lx.getAll("P")) == set(["John", "Mary"])
Exemplo n.º 8
0
    def test_tag_word(self):

        l = Lexicon()
        l.add("John", "P")
        l.add("orange", "N")
        l.add("orange", "A")
        l.add("box", "N")
        l.add("cat", "N")
        l.add("fish", "N")
        l.add("fish", "T")
        l.add("fish", "I")
        l.add("fish", "N")
        l.add('like', 'T')
        l.add('like', 'N')
        l.add('like', 'I')
        l.add('police', 'N')
        l.add('animal', 'N')
        l.add('fly', 'N')
        l.add('fly', 'T')

        self.assertItemsEqual(tag_word(l, "John"), ["P"])
        self.assertItemsEqual(tag_word(l, "orange"), ["Ns", "A"])
        self.assertItemsEqual(tag_word(l, "fish"), ["Ns", "Np", "Ip", "Tp"])
        self.assertItemsEqual(tag_word(l, "a"), ["AR"])
        self.assertItemsEqual(tag_word(l, "zxghqw"), [])
        self.assertItemsEqual(tag_word(l, "likes"), ["Is", "Ts", "Np"])
        self.assertItemsEqual(tag_word(l, "like"), ["Tp", "Ns", "Ip"])
        self.assertItemsEqual(tag_word(l, "police"), ["Np", "Ns"])
        self.assertItemsEqual(tag_word(l, "animals"), ["Np"])
        self.assertItemsEqual(tag_word(l, "flies"), ["Np", "Ts"])