Example #1
0
def test_graph():
    _check_jena()
    t = SyntaxTree(TEST_SAF, sentence_id=1)
    g = t.get_graphviz()
    assert_equal(
        set(g.edges()), {(u"t_3_Mary", u"t_2_marry"), (u"t_1_John", u"t_2_marry"), (u"t_4_little", u"t_1_John")}
    )
    assert_in("lemma: John", g.get_node("t_1_John").attr["label"])

    # Can we 'gray out' syntax relations after applying rules
    t.apply_ruleset(TEST_RULES)
    t.apply_lexicon([{"lexclass": "man", "lemma": "john"}])
    g = t.get_graphviz()  # triple_args_function=VIS_GREY_REL)
    assert_equal(
        set(g.edges()),
        {
            (u"t_3_Mary", u"t_2_marry"),
            (u"t_1_John", u"t_2_marry"),
            (u"t_4_little", u"t_1_John"),
            (u"t_1_John", u"t_3_Mary"),
        },
    )
    # assert_false(g.get_edge(u't_1_John', u't_3_Mary').attr.get("color"))
    # assert_equal(g.get_edge(u't_1_John', u't_2_marry').attr.get("color"),
    # "grey")

    # can we draw it? can't check output, but we can check for errors
    with tempfile.NamedTemporaryFile() as f:
        g.draw(f.name, prog="dot")
        g.draw("/tmp/test.png", prog="dot")
Example #2
0
def test_load():
    _check_jena()
    t = SyntaxTree(TEST_SAF, sentence_id=2)
    triples = list(t.get_triples())
    assert_equal(len(triples), 1)
    assert_equal(triples[0].predicate, "rel_nsubj")
    assert_equal(triples[0].subject.lemma, "it")
    assert_equal(triples[0].object.lemma, "rain")
Example #3
0
def test_rules():
    _check_jena()
    t = SyntaxTree(TEST_SAF, sentence_id=1)
    t.apply_ruleset(TEST_RULES)
    triples = [tr for tr in t.get_triples() if tr.predicate == "marry"]

    assert_equal(len(triples), 1)
    assert_equal(triples[0].subject.lemma, "John")
    assert_equal(triples[0].object.lemma, "Mary")
Example #4
0
def test_lexicon():
    _check_jena()
    t = SyntaxTree(TEST_SAF, sentence_id=1)
    t.apply_lexicon(TEST_RULES["lexicon"])

    classes = {k.replace(NS_AMCAT, ":"): v.get("lexclass") for (k, v) in t.get_tokens().iteritems()}
    assert_equal(
        classes, {":t_2_marry": ["marry"], ":t_3_Mary": ["person"], ":t_1_John": ["person"], ":t_4_little": None}
    )
Example #5
0
def test_get_triples():
    _check_jena()
    t = SyntaxTree(TEST_SAF, sentence_id=1)
    t.apply_ruleset(TEST_RULES)
    rels = list(t.get_relations())
    marry = dict(predicate="marry", object=3, object_nodes=[3], subject=1, subject_nodes=[1, 4])
    assert_equal(rels, [marry])

    # add new relation, see if percolation is 'blocked' by relation
    t.apply_rule({"condition": "?x :rel_dobj ?y", "insert": "?y :test ?x"})
    rels = sorted(t.get_relations(), key=lambda rel: rel["predicate"])
    assert_equal(rels[0], marry)
    assert_equal(rels[1], dict(predicate="test", object=3, object_nodes=[3], subject=2, subject_nodes=[2]))
Example #6
0
def test_rules_multiple():
    """Can we apply rules to multiple sentences at once"""
    _check_jena()
    triples_single = set()
    for sid in {t["sentence"] for t in TEST_SAF["tokens"]}:
        t = SyntaxTree(TEST_SAF, sentence_id=sid)
        t.apply_ruleset(TEST_RULES)
        triples_single |= set((s.id, p, o.id) for (s, p, o) in t.get_triples())

    t = SyntaxTree(TEST_SAF)
    t.apply_ruleset(TEST_RULES)
    triples_all = set((s.id, p, o.id) for (s, p, o) in t.get_triples())

    assert_equal(triples_single, triples_all)
def test_sparql():
    from tools import _check_jena
    _check_jena()
    r = SparqlRunner()

    data = """
    @prefix vCard:   <http://www.w3.org/2001/vcard-rdf/3.0#> .
    @prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    <http://somewhere/MattJones/>  vCard:FN   "Matt Jones" .
    <http://somewhere/MattJones/>  vCard:N    _:b0 .
    _:b0  vCard:Family "Jones" .
    _:b0  vCard:Given  "Matthew" .
    """
    update = '''PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#>
                DELETE {?x vCard:FN "Matt Jones"}
                INSERT {?x vCard:FN  "Matty" }
                WHERE {?x vCard:FN "Matt Jones"}'''

    g = r.run(data, [update])
    rels = {tuple(map(str, spo)) for spo in list(g)}
    MATT, FN = 'http://somewhere/MattJones/', 'http://www.w3.org/2001/vcard-rdf/3.0#FN'
    # insert worked?
    assert_in((MATT, FN, 'Matty'), rels)
    # delete worked?
    assert_not_in((MATT, FN, 'Matt Jones'), rels)

    # test rdflib Graph input
    Graph().parse(data=data, format='n3')
    g = r.run(data, [update])
    assert_in((MATT, FN, 'Matty'), rels)

    # test error handling, is the sparql exception reproduced
    try:
        r.run(data, ["THIS IS NOT VALID SPARQL"])
    except Exception, e:
        assert_in("Lexical error", str(e))