<jupyter_start><jupyter_text>Rewrite<jupyter_code>from discopy import grammar
from discopy.rewriting import InterchangerError

def draw(diagram):
    for i, box in enumerate(diagram.boxes):
        if isinstance(box, Word):
            for j in reversed(range(i)):
                if isinstance(diagram.boxes[j], Word):
                    break

                try:
                    diagram = diagram.interchange(j, j+1)
                except InterchangerError:
                    diagram.draw()
                    return
        elif not isinstance(box, Cup):
            diagram.draw()
            return
    grammar.draw(diagram)
from discopy import Word
from discopy.rigid import Cup, Diagram, Id

from lambeq.core.types import AtomicType

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

rewriter = Rewriter()<jupyter_output><empty_output><jupyter_text>Auxiliary rule<jupyter_code>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())<jupyter_output><empty_output><jupyter_text>Connector rule<jupyter_code>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)')
draw(Rewriter(['connector'])(diagram))
print('↓ normal form')
draw(rewriter(diagram).normal_form())<jupyter_output><empty_output><jupyter_text>Determiner rule<jupyter_code>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())<jupyter_output><empty_output><jupyter_text>Adverb rules<jupyter_code>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())
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())<jupyter_output><empty_output><jupyter_text>Prepositional phrase rule<jupyter_code>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())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1912039925140362, 'fraction_numerical': 0.0015595757953836558, 'mean_word_length': 3.7440828402366866, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '454729', 'license': 'Apache-2.0', 'n_tokens_mistral': 1173, 'n_tokens_neox': 1159, 'n_words': 233, 'path': 'docs/examples/rewrite.ipynb', 'repo_name': 'Thommy257/lambeq-pub', 'size': '3206'}