Rewrite

from lambeq.backend.grammar import Cup, Diagram, Id, Word
from lambeq.backend.drawing import draw

from lambeq import AtomicType

N = AtomicType.NOUN
S = AtomicType.SENTENCE
from lambeq import Rewriter

rewriter = Rewriter()

Auxiliary rule

diagram = ((Word('we', N) @ Word('will', (N >> S) << (N >> S)) @
            Word('go', N >> S)) >>
           Cup(N, N.r) @ Id(S) @ Diagram.cups((N >> S).l, N >> S))

draw(diagram)
print('↓ rewriting (auxiliary rule)')
draw(Rewriter(['auxiliary'])(diagram))
print('↓ normal form')
draw(rewriter(diagram).normal_form())
../_images/1f41ae0c8b7438e6fae9200916430704dea09d90e0b9aad83b84eff5a94264ff.png
↓ rewriting (auxiliary rule)
../_images/6922b834415250bf74c210a6574cbd9b51faebe267f711fe0386d5960103de2f.png
↓ normal form
../_images/dab9079dd0e42d43355920a0aefa2cbc286fe6a4869623f0cc390c2545919037.png

Connector rule

diagram = ((Word('I', N) @ Word('hope', N >> S << S) @
            Word('that', S << S) @ Word('this', N) @
            Word('succeeds', N >> S)) >>
           (Cup(N, N.r) @ Id(S) @ Cup(S.l, S) @
            Diagram.cups((N >> S).l, N >> S)))

draw(diagram)
print('↓ rewriting (connector rule)')
Rewriter(['connector'])(diagram).draw()
print('↓ normal form')
rewriter(diagram).normal_form().draw()
../_images/4b04e2d6394d0cc67a99f7e9fb9a9c4b52011b5e17d7aff175ae5bc7a1ea77c1.png
↓ rewriting (connector rule)
../_images/2f73cd84ffc763f2a9e2816da9f57c495129f537dc5f5bc26f9eb48f459d3651.png
↓ normal form
../_images/7548ac32fce546569b123e818f1330b3a0e71fbe2575d4a1f289d34967968a6a.png

Determiner rule

diagram = (Word('the', N << N) @ Word('book', N) >>
           Id(N) @ Cup(N.l, N))

draw(diagram)
print('↓ rewriting (determiner rule)')
draw(Rewriter(['determiner'])(diagram))
print('↓ normal form')
draw(rewriter(diagram).normal_form())
../_images/952409bc0d3a86bef52da967427caa7e8454932264e0fbf7a206e598381a3ba9.png
↓ rewriting (determiner rule)
../_images/b818d7a62098c61997ae37b60ffe206ff8a980016650bd34396669006dfe08cc.png
↓ normal form
../_images/83ada5679c4e01f615bc1c574ff3f55a501431622c47aec517910f7e67ba1073.png

Adverb rules

cod = (N >> S) >> (N >> S)
diagram = (Word('we', N) @ Word('go', N >> S) @ Word('quickly', cod) >>
           Diagram.cups(cod[:3].l, cod[:3]) @ Id(S))

draw(diagram)
print('↓ rewriting (postadverb rule)')
draw(Rewriter(['postadverb'])(diagram))
print('↓ normal form')
draw(rewriter(diagram).normal_form())
../_images/9684808ad40323e492c3b2eaf5d3f581c156d6b97a058936f7e60f8275b5a974.png
↓ rewriting (postadverb rule)
../_images/221cbff776f8d8269a88a27f1485d88afa099773d8a5706d5fc82fc930f38e14.png
↓ normal form
../_images/626fe51dda25a5e66fcca3b88fd165d24cdd2f799febf61c85ba2a687c282697.png
diagram = ((Word('we', N) @ Word('quickly', (N >> S) << (N >> S)) @
            Word('go', N >> S)) >>
           Cup(N, N.r) @ Id(S) @ Diagram.cups((N >> S).l, N >> S))

draw(diagram)
print('↓ rewriting (preadverb rule)')
draw(Rewriter(['preadverb'])(diagram))
print('↓ normal form')
draw(rewriter(diagram).normal_form())
../_images/7dd7fadebdf2b6b5baa8429a6fb0241dd5b82d886b7ca72cfc804b859243f1dc.png
↓ rewriting (preadverb rule)
../_images/521738c63e30077b02e674566c78e401af5acf75a794f3d3ff06d6b772b00123.png
↓ normal form
../_images/9af972830f3ebb866e9909c9984a001f7b1718e9cd3c1b80506de340260989bd.png

Prepositional phrase rule

cod = (N >> S) >> (N >> S << N)
diagram = ((Word('I', N) @ Word('go', N >> S) @ Word('to', cod) @
            Word('bed', N)) >>
           Diagram.cups(cod[:3].l, cod[:3]) @ Id(S) @ Cup(N.l, N))

draw(diagram)
print('↓ rewriting (prepositional phrase rule)')
draw(Rewriter(['prepositional_phrase'])(diagram))
print('↓ normal form')
draw(rewriter(diagram).normal_form())
../_images/9f13ab1f687b13035ffd1f7c6aeb52b7cc47e29bf4fb29ba55904443ab6b32b6.png
↓ rewriting (prepositional phrase rule)
../_images/6281f49aacb86bfce180a4599014e5be03b86306c3fac2c40e5dfaa23f4e1629.png
↓ normal form
../_images/a8724a7430e0dc214ce90c57474617a2c676a8450797851d8ec7e234c16c79b9.png

Relative Pronoun rules

cows = Word('cows', N)
that_subj = Word('that', N.r @ N @ S.l @ N)
that_obj = Word('that', N.r @ N @ N.l.l @ S.l)
eat = Word('eat', N >> S << N)
grass = Word('grass', N)

rewriter = Rewriter(['subject_rel_pronoun'])

diagram = Id().tensor(cows, that_subj, eat, grass)
diagram >>= Cup(N, N.r) @ Id(N) @ Diagram.cups(S.l @ N, N.r @ S) @ Cup(N.l, N)

draw(diagram)
print('↓ rewriting (subject relative pronoun rule)')
draw(Rewriter(['subject_rel_pronoun'])(diagram))
../_images/a0868debe001a2e65da738af330ef77c7c1dc898fae3336c94ffa2b256ac2ccd.png
↓ rewriting (subject relative pronoun rule)
../_images/e6b48fbcad898f88f1e8c6f909b6d39a43884d11cabd27416b383e9619084685.png
diagram = Id().tensor(grass, that_obj, cows, eat)
diagram >>= Cup(N, N.r) @ Id(N) @ Id(N.l.l @ S.l) @ Cup(N, N.r) @ Id(S @ N.l)
diagram >>= Id(N) @ Diagram.cups(N.l.l @ S.l, S @ N.l)

draw(diagram)
print('↓ rewriting (object relative pronoun rule)')
draw(Rewriter(['object_rel_pronoun'])(diagram))
../_images/0b33d394caa4bc4c1266d6b464de9755d9001bf2597f069005e7fe8a628f73f0.png
↓ rewriting (object relative pronoun rule)
../_images/51427664385f545ed1c48fcd3b28e28a43141fbe5c2d12fd2d3d5cb525e20312.png

Coordination

diagram = (Word('eggs', N) @ Word('and', N >> N << N)
           @ Word('ham', N) >> Cup(N, N.r) @ Id(N) @ Cup(N.l, N))

draw(diagram)
print('↓ rewriting (coordination rule)')
draw(Rewriter(['coordination'])(diagram))
print('↓ normal form')
draw(Rewriter(['coordination'])(diagram).normal_form())
../_images/8f9f9e6287a2a3b6a69e63f42d745f448036c62a5c45d607efc8fd87d5837e3b.png
↓ rewriting (coordination rule)
../_images/d73dc6baaf9d778176a4b082600193b8153f1d12a36df66ccc0cb7255f6e036d.png
↓ normal form
../_images/f2c018a266bbf80a708bd7388232f12a01970e76c4c394710cfc1586c73542a0.png

Remove cups

from lambeq import RemoveCupsRewriter


diagram = (Word('I', N) @ Word('love', N >> S << N)
           @ Word('cheese', N) >> Cup(N, N.r) @ Id(S) @ Cup(N.l, N))
remove_cups = RemoveCupsRewriter()

draw(diagram)
print('↓ remove cups by bending wires')
remove_cups(diagram).draw()
../_images/204b486b1e83931bc3fa3f89ac3b3ab7fbd188c443d3ff772643ddc2cfce0c4e.png
↓ remove cups by bending wires
../_images/099ffc68bc34dcf67b2b9ae7908ff52dc00bcdc097da5a7882a279d9156e9aa5.png

Curry functor

rewriter = Rewriter(['curry'])

diagram = (
    Word('I', N) @ Word('see', N >> S << N) @
    Word('dead', N @ N.l) @ Word('people', N) >>
    Cup(N, N.r) @ Id(S) @ Cup(N.l, N) @ Cup(N.l, N)
)
draw(diagram)
print('↓ rewrite by using the map-state duality')
rewriter(diagram).draw()
print('↓ normal form')
rewriter(diagram).normal_form().draw()
../_images/1b08d912b101c16a324c25f124bb62fe0f3d64fe279b5ba1842e578ee602bd45.png
↓ rewrite by using the map-state duality
../_images/6da5c34d589bedc67570f13c5239e9830582a4bd9f1f558ebf8e31fb4770be0e.png
↓ normal form
../_images/f7dd8b058884d1eacbcfae7375081724ab6765e466e878f12d54653308b96118.png