示例#1
0
def test_names():
    G = get_graph()
    pattern = ngrex.compile('{}=t >{dependency:/aaa|bbb/} {}')
    actual = {m.group('t') for m in pattern.finditer(G)}
    assert actual == {'xxx', 'yyy'}

    with pytest.raises(IndexError):
        pattern = ngrex.compile('{}=t >{dependency:/aaa|bbb/} {}')
        m = next(pattern.finditer(G))
        m.group('x')

    with pytest.raises(TypeError):
        m.group(1.1)
示例#2
0
def helper(G, p, expected):
    pattern = ngrex.compile(p)
    print(pattern.pattern)
    # actual = {m.group(0) for m in pattern.finditer(G)}
    actual = set()
    for m in pattern.finditer(G):
        actual.add(m.group(0))
    assert actual == expected, '{} vs {}'.format(actual, expected)
示例#3
0
def test_MatcherObj():
    G = get_graph()
    pattern = ngrex.compile('{lemma:/xxx/} >{dependency:/aaa/} {lemma:/yyy/}')
    matcher = next(pattern.finditer(G))
    assert bool(matcher)
    assert matcher.graph == G
    assert matcher.pattern == pattern
    groups = list(matcher.groups())
    assert len(groups) == 2
    assert groups[0] == 'xxx'
    assert groups[1] == 'yyy'
示例#4
0
def test_yacc():
    # and
    _test_yacc("{} <{} {} <{} {}", "({}) <{} ({}) & ({}) <{} ({})")
    _test_yacc("{} <{} {} & <{} {}", "({}) <{} ({}) & ({}) <{} ({})")

    # or
    _test_yacc("{} <{} {} | <{} {}", "({}) <{} ({}) | ({}) <{} ({})")

    # ()
    _test_yacc("({lemma:/xxx/})", "{lemma:/xxx/}")
    _test_yacc(
        "({lemma:/xxx/}) <{dependency:/nmod:without|x/} ({lemma:/yyy/})",
        "({lemma:/xxx/}) <{dependency:/nmod:without|x/} ({lemma:/yyy/})")

    _test_yacc(
        "{lemma:/xxx/} <{dependency:/nmod:without|x/} {lemma:/yyy/}",
        "({lemma:/xxx/}) <{dependency:/nmod:without|x/} ({lemma:/yyy/})")
    _test_yacc("{lemma:/xxx/} >{dependency:/nmod:without/} {lemma:/yyy/}",
               "({lemma:/xxx/}) >{dependency:/nmod:without/} ({lemma:/yyy/})")
    _test_yacc(
        "{lemma:/xxx/} >{dependency:/nmod:without/} ({lemma:/yyy/} >{} {lemma:/zzz/})",
        "({lemma:/xxx/}) >{dependency:/nmod:without/} (({lemma:/yyy/}) >{} ({lemma:/zzz/}))"
    )
    _test_yacc("{} >{} {lemma:/left/} <{} {lemma:/question/}",
               "({}) >{} ({lemma:/left/}) & ({}) <{} ({lemma:/question/})")
    _test_yacc("{} <{dependency:/nsubj/} {lemma:/suspect/,tag:/VBN/}",
               "({}) <{dependency:/nsubj/} ({lemma:/suspect/,tag:/VBN/})")
    _test_yacc(
        "{}=t <{dependency:/nsubj/} {lemma:/suspect/,tag:/VBN/}=key",
        "({}=t) <{dependency:/nsubj/} ({lemma:/suspect/,tag:/VBN/}=key)")

    with pytest.raises(KeyError):
        ngrex.compile(
            "{}=t <{dependency:/nsubj/} {lemma:/suspect/,tag:/VBN/}=t")

    with pytest.raises(TypeError):
        ngrex.compile("xxx ? xxx")
示例#5
0
    def match_uncertainty(self, graph, node):
        for pattern in self.uncertain_patterns:
            for m in pattern.finditer(graph):
                n0 = m.group(0)
                if n0 == node:
                    return m

        # parsing error
        # suggestive of XXX
        p = ngrex.compile('{} <{dependency:/nmod:of/} {lemma:/suggestive/}')
        for m in p.finditer(graph):
            n0 = m.group(0)
            if n0 == node:
                if semgraph.has_out_node(graph, m.group(1), ['most']):
                    return None
                elif semgraph.has_out(graph, n0, ['new', 'develop'], ['amod']):
                    continue
                else:
                    return m
        return None
示例#6
0
def test_validate_names():
    with pytest.raises(KeyError):
        ngrex.compile('{}=t >{dependency:/aaa|bbb/} {}=t')
示例#7
0
def _test_yacc(s):
    pattern = ngrex.compile(s)
    print(pattern)
示例#8
0
def _test_yacc(s, expected):
    pattern = ngrex.compile(s)
    actual = re.sub('[$^]', '', str(pattern))
    assert actual == expected, '{} vs {}'.format(actual, expected)
示例#9
0
def test_variables():
    G = get_graph()
    pattern = ngrex.compile('{}=t >{dependency:/aaa|bbb/} {}')
    print(pattern.pattern)
    actual = {m.get('t') for m in pattern.finditer(G)}
    assert actual == {'xxx', 'yyy'}