예제 #1
0
def test_Tensor_adjoint_eval():
    alice = Box("Alice", Dim(1), Dim(2), [1, 2])
    eats = Box("eats", Dim(1), Dim(2, 3, 2), [3] * 12)
    food = Box("food", Dim(1), Dim(2), [4, 5])

    diagram = alice @ eats @ food >>\
        Cup(Dim(2), Dim(2)) @ Id(Dim(3)) @ Cup(Dim(2), Dim(2))

    tensor1 = diagram.eval()
    tensor2 = diagram.transpose_box(2).transpose_box(0, left=True).eval()
    assert tensor1 == tensor2
예제 #2
0
def test_brute_force():
    s, n = Ty('s'), Ty('n')
    Alice = Word('Alice', n)
    loves = Word('loves', n.r @ s @ n.l)
    Bob = Word('Bob', n)
    grammar = Cup(n, n.r) @ Id(s) @ Cup(n.l, n)
    gen = brute_force(Alice, loves, Bob)
    assert next(gen) == Alice @ loves @ Alice >> grammar
    assert next(gen) == Alice @ loves @ Bob >> grammar
    assert next(gen) == Bob @ loves @ Alice >> grammar
    assert next(gen) == Bob @ loves @ Bob >> grammar
    gen = brute_force(Alice, loves, Bob, target=n)
    assert next(gen) == Word('Alice', Ty('n'))
    assert next(gen) == Word('Bob', Ty('n'))
예제 #3
0
def test_eager_parse():
    s, n = Ty('s'), Ty('n')
    Alice = Word('Alice', n)
    loves = Word('loves', n.r @ s @ n.l)
    Bob = Word('Bob', n)
    grammar = Cup(n, n.r) @ Id(s) @ Cup(n.l, n)
    assert eager_parse(Alice, loves, Bob) == grammar << Alice @ loves @ Bob
    who = Word('who', n.r @ n @ s.l @ n)
    assert eager_parse(Bob, who, loves, Alice, target=n).offsets ==\
        [0, 1, 5, 8, 0, 2, 1, 1]
    with raises(NotImplementedError):
        eager_parse(Alice, Bob, loves)
    with raises(NotImplementedError):
        eager_parse(Alice, loves, Bob, who, loves, Alice)
예제 #4
0
def test_normal_form():
    n = Ty('n')
    w1, w2 = Word('a', n), Word('b', n)
    diagram = w1 @ w2 >>\
        Id(n) @ Cap(n, n.r) @ Id(n) >> Id(n @ n) @ Cup(n.r, n)
    expected_result = w1 @ w2
    assert expected_result == normal_form(diagram)

    with raises(ValueError) as err:
        normal_form(w2 >> w1 @ Id(n))
예제 #5
0
def test_pregroup_draw_errors():
    n = Ty('n')
    with raises(TypeError):
        draw(0)
    with raises(ValueError) as err:
        draw(Cap(n, n.l))
    with raises(ValueError) as err:
        draw(Cup(n, n.r))
    with raises(ValueError) as err:
        draw(Word('Alice', n) >> Word('Alice', n) @ Id(n))
    assert str(err.value) is messages.expected_pregroup()
예제 #6
0
def test_Tensor_adjoint_functor():
    from discopy.rigid import Ty, Cup
    from discopy.grammar import Word

    n, s = map(Ty, 'ns')
    alice = Word("Alice", n)
    eats = Word("eats", n.r @ s @ n.l)
    food = Word("food", n)

    diagram = alice @ eats @ food >> Cup(n, n.r) @ Id(s) @ Cup(n.l, n)

    f = Functor(ob={
        n: Dim(2),
        s: Dim(3)
    },
                ar={
                    alice: Tensor(Dim(1), Dim(2), [1, 2]),
                    eats: Tensor(Dim(1), Dim(2, 3, 2), [3] * 12),
                    food: Tensor(Dim(1), Dim(2), [4, 5])
                })

    tensor1 = f(diagram)
    tensor2 = f(diagram.transpose_box(2).transpose_box(0))
    assert tensor1 == tensor2
예제 #7
0
파일: pregroup.py 프로젝트: silky/discopy
def eager_parse(*words, target=Ty('s')):
    """
    Tries to parse a given list of words in an eager fashion.
    """
    result = fold(lambda x, y: x @ y, words)
    scan = result.cod
    while True:
        fail = True
        for i in range(len(scan) - 1):
            if scan[i:i + 1].r != scan[i + 1:i + 2]:
                continue
            cup = Cup(scan[i:i + 1], scan[i + 1:i + 2])
            result = result >> Id(scan[:i]) @ cup @ Id(scan[i + 2:])
            scan, fail = result.cod, False
            break
        if result.cod == target:
            return result
        if fail:
            raise NotImplementedError
예제 #8
0
def test_from_tree():
    s, n = Ty('s'), Ty('n')
    Alice, Bob = Word('Alice', n), Word('Bob', n)
    loves = Word('loves', n.r @ s @ n.l)
    sentence = Alice @ loves @ Bob >> Cup(n, n.r) @ Id(s) @ Cup(n.l, n)
    sentence == from_tree(sentence.to_tree())
예제 #9
0
def test_tree2diagram():
    tree = {
        'type':
        'ba',
        'cat':
        'S[dcl]',
        'children': [{
            'word': 'that',
            'lemma': 'XX',
            'pos': 'XX',
            'entity': 'XX',
            'chunk': 'XX',
            'cat': 'NP'
        }, {
            'type':
            'fa',
            'cat':
            'S[dcl]\\NP',
            'children': [{
                'type':
                'bx',
                'cat':
                '(S[dcl]\\NP)/NP',
                'children': [{
                    'word': "'s",
                    'lemma': 'XX',
                    'pos': 'XX',
                    'entity': 'XX',
                    'chunk': 'XX',
                    'cat': '(S[dcl]\\NP)/NP'
                }, {
                    'word': 'exactly',
                    'lemma': 'XX',
                    'pos': 'XX',
                    'entity': 'XX',
                    'chunk': 'XX',
                    'cat': '(S\\NP)\\(S\\NP)'
                }]
            }, {
                'type':
                'fa',
                'cat':
                'NP',
                'children': [{
                    'word': 'what',
                    'lemma': 'XX',
                    'pos': 'XX',
                    'entity': 'XX',
                    'chunk': 'XX',
                    'cat': 'NP/(S[dcl]/NP)'
                }, {
                    'type':
                    'fc',
                    'cat':
                    'S[dcl]/NP',
                    'children': [{
                        'type':
                        'tr',
                        'cat':
                        'S[X]/(S[X]\\NP)',
                        'children': [{
                            'word': 'i',
                            'lemma': 'XX',
                            'pos': 'XX',
                            'entity': 'XX',
                            'chunk': 'XX',
                            'cat': 'NP'
                        }]
                    }, {
                        'type':
                        'bx',
                        'cat':
                        '(S[dcl]\\NP)/NP',
                        'children': [{
                            'word': 'showed',
                            'lemma': 'XX',
                            'pos': 'XX',
                            'entity': 'XX',
                            'chunk': 'XX',
                            'cat': '(S[dcl]\\NP)/NP'
                        }, {
                            'type':
                            'fa',
                            'cat':
                            '(S\\NP)\\(S\\NP)',
                            'children': [{
                                'word': 'to',
                                'lemma': 'XX',
                                'pos': 'XX',
                                'entity': 'XX',
                                'chunk': 'XX',
                                'cat': '((S\\NP)\\(S\\NP))/NP'
                            }, {
                                'word': 'her',
                                'lemma': 'XX',
                                'pos': 'XX',
                                'entity': 'XX',
                                'chunk': 'XX',
                                'cat': 'NP'
                            }]
                        }]
                    }]
                }]
            }]
        }]
    }
    diagram = tree2diagram(tree)
    from discopy.biclosed import (Ty, Over, Under, Box, FA, BA, Functor,
                                  biclosed2rigid)
    from discopy.grammar.ccg import Word
    assert diagram.boxes == [
        Word('that', Ty('NP')),
        Word("'s", Over(Under(Ty('NP'), Ty('S')), Ty('NP'))),
        Word('exactly',
             Under(Under(Ty('NP'), Ty('S')), Under(Ty('NP'), Ty('S')))),
        Box(
            'bx',
            Ty(Over(Under(Ty('NP'), Ty('S')), Ty('NP')),
               Under(Under(Ty('NP'), Ty('S')), Under(Ty('NP'), Ty('S')))),
            Over(Under(Ty('NP'), Ty('S')), Ty('NP'))),
        Word('what', Over(Ty('NP'), Over(Ty('S'), Ty('NP')))),
        Word('i', Ty('NP')),
        Box('tr', Ty('NP'), Over(Ty('S'), Under(Ty('NP'), Ty('S')))),
        Word('showed', Over(Under(Ty('NP'), Ty('S')), Ty('NP'))),
        Word(
            'to',
            Over(Under(Under(Ty('NP'), Ty('S')), Under(Ty('NP'), Ty('S'))),
                 Ty('NP'))),
        Word('her', Ty('NP')),
        FA(
            Over(Under(Under(Ty('NP'), Ty('S')), Under(Ty('NP'), Ty('S'))),
                 Ty('NP'))),
        Box(
            'bx',
            Ty(Over(Under(Ty('NP'), Ty('S')), Ty('NP')),
               Under(Under(Ty('NP'), Ty('S')), Under(Ty('NP'), Ty('S')))),
            Over(Under(Ty('NP'), Ty('S')), Ty('NP'))),
        Box(
            'FC((S << (NP >> S)), ((NP >> S) << NP))',
            Ty(Over(Ty('S'), Under(Ty('NP'), Ty('S'))),
               Over(Under(Ty('NP'), Ty('S')), Ty('NP'))),
            Over(Ty('S'), Ty('NP'))),
        FA(Over(Ty('NP'), Over(Ty('S'), Ty('NP')))),
        FA(Over(Under(Ty('NP'), Ty('S')), Ty('NP'))),
        BA(Under(Ty('NP'), Ty('S')))
    ]
    assert diagram.offsets == [0, 1, 2, 1, 2, 3, 3, 4, 5, 6, 5, 4, 3, 2, 1, 0]
    from discopy.rigid import Ob, Ty, Box, Cup
    biclosed2rigid(diagram).boxes == [
        Box('that', Ty(), Ty('NP')),
        Box("'s", Ty(), Ty(Ob('NP', z=1), 'S', Ob('NP', z=-1))),
        Box('exactly', Ty(), Ty(Ob('S', z=1), Ob('NP', z=2), Ob('NP', z=1),
                                'S')),
        Box(
            'bx',
            Ty(Ob('NP', z=1), 'S', Ob('NP', z=-1), Ob('S', z=1), Ob('NP', z=2),
               Ob('NP', z=1), 'S'), Ty(Ob('NP', z=1), 'S', Ob('NP', z=-1))),
        Box('what', Ty(), Ty('NP', Ob('NP', z=-2), Ob('S', z=-1))),
        Box('i', Ty(), Ty('NP')),
        Box('tr', Ty('NP'), Ty('S', Ob('S', z=-1), 'NP')),
        Box('showed', Ty(), Ty(Ob('NP', z=1), 'S', Ob('NP', z=-1))),
        Box(
            'to', Ty(),
            Ty(Ob('S', z=1), Ob('NP', z=2), Ob('NP', z=1), 'S', Ob('NP',
                                                                   z=-1))),
        Box('her', Ty(), Ty('NP')),
        Cup(Ty(Ob('NP', z=-1)), Ty('NP')),
        Box(
            'bx',
            Ty(Ob('NP', z=1), 'S', Ob('NP', z=-1), Ob('S', z=1), Ob('NP', z=2),
               Ob('NP', z=1), 'S'), Ty(Ob('NP', z=1), 'S', Ob('NP', z=-1))),
        Cup(Ty('NP'), Ty(Ob('NP', z=1))),
        Cup(Ty(Ob('S', z=-1)), Ty('S')),
        Cup(Ty(Ob('S', z=-1)), Ty('S')),
        Cup(Ty(Ob('NP', z=-2)), Ty(Ob('NP', z=-1))),
        Cup(Ty(Ob('NP', z=-1)), Ty('NP')),
        Cup(Ty('NP'), Ty(Ob('NP', z=1)))
    ]
    biclosed2rigid(diagram).offsets ==\
        [0, 1, 4, 1, 4, 7, 7, 10, 13, 18, 17, 10, 9, 8, 6, 5, 3, 0]