File size: 24,020 Bytes
8cef38d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328461e
 
8cef38d
328461e
 
 
8cef38d
 
 
 
15986f6
8cef38d
 
 
15986f6
 
8cef38d
 
 
 
15986f6
8cef38d
15986f6
8cef38d
15986f6
8cef38d
 
 
 
15986f6
 
8cef38d
 
15986f6
8cef38d
15986f6
8cef38d
15986f6
8cef38d
15986f6
8cef38d
 
 
 
 
 
3d692b2
 
 
 
 
 
 
 
 
 
328461e
8cef38d
 
328461e
8cef38d
 
 
328461e
8cef38d
 
 
 
328461e
 
 
8cef38d
 
 
328461e
 
 
8cef38d
 
 
 
 
 
 
15986f6
8cef38d
328461e
8cef38d
328461e
 
8cef38d
 
 
 
328461e
 
 
8cef38d
 
 
 
 
 
 
 
 
 
 
 
 
 
328461e
 
8cef38d
 
328461e
8cef38d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328461e
 
8cef38d
 
 
 
15986f6
8cef38d
 
 
 
15986f6
8cef38d
 
 
 
15986f6
8cef38d
 
 
 
15986f6
8cef38d
 
 
 
 
 
 
15986f6
8cef38d
 
328461e
8cef38d
 
 
 
 
 
 
 
328461e
 
8cef38d
328461e
8cef38d
 
 
 
 
 
 
 
 
 
328461e
 
 
 
8cef38d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15986f6
8cef38d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328461e
 
8cef38d
 
 
 
 
 
 
 
 
328461e
8cef38d
15986f6
8cef38d
 
328461e
 
 
8cef38d
 
 
 
328461e
 
8cef38d
 
 
 
 
328461e
 
8cef38d
 
328461e
8cef38d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63c91a0
8cef38d
 
 
 
 
 
328461e
 
8cef38d
328461e
8cef38d
 
328461e
8cef38d
 
 
328461e
8cef38d
 
 
 
 
 
 
328461e
 
 
8cef38d
 
 
 
 
 
 
15986f6
8cef38d
 
328461e
 
 
 
8cef38d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328461e
8cef38d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d692b2
8cef38d
3d692b2
8cef38d
 
 
328461e
 
 
8cef38d
 
 
 
328461e
 
 
8cef38d
 
 
 
63c91a0
8cef38d
 
 
 
 
 
3d692b2
8cef38d
3d692b2
 
 
8cef38d
15986f6
 
 
 
 
 
 
328461e
 
 
8cef38d
 
328461e
8cef38d
 
328461e
8cef38d
328461e
8cef38d
 
328461e
8cef38d
 
 
 
328461e
 
 
 
8cef38d
3d692b2
 
 
 
 
 
 
 
 
 
8cef38d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328461e
 
8cef38d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328461e
8cef38d
 
 
 
 
 
 
 
 
328461e
 
 
 
8cef38d
328461e
 
8cef38d
328461e
8cef38d
328461e
8cef38d
 
 
 
328461e
 
8cef38d
 
 
328461e
8cef38d
 
 
 
 
328461e
8cef38d
 
328461e
 
8cef38d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328461e
8cef38d
 
 
 
 
 
 
328461e
8cef38d
 
 
328461e
 
 
 
8cef38d
328461e
 
8cef38d
328461e
8cef38d
 
 
 
328461e
 
 
 
 
8cef38d
 
328461e
8cef38d
328461e
8cef38d
 
 
 
 
 
 
 
 
328461e
8cef38d
 
328461e
8cef38d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328461e
 
 
 
8cef38d
 
 
328461e
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
# /// script
# requires-python = ">=3.12"
# dependencies = [
#     "aozora-corpus-generator==0.1.1",
#     "cdifflib==1.2.9",
#     "ginza",
#     "ja-ginza",
#     "ipython==7.23.1",
#     "marimo",
#     "polars==1.30.0",
#     "spacy==3.8.7",
#     "wcwidth",
# ]
#
# [tool.uv.sources]
# aozora-corpus-generator = { git = "https://github.com/borh/aozora-corpus-generator.git" }
# ///

import marimo

__generated_with = "0.13.15"
app = marimo.App()


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        rf"""
    # Aozora Bunko Text Processing Pipeline Demo / 青空文庫テキストの前処理パイプラインデモ

    ### Summary

    This notebook allows you to upload, preprocess, compare, visualize and analyze Aozora Bunko texts.

    1. Upload a text file from Aozora Bunko (or use the default sample).
    2. Preprocess using customizable regex patterns.
    3. Preview the first and last 50 lines of the cleaned text.
    4. Download the cleaned text.
    5. Process the XHTML version with the `aozora-corpus-generator` Python library for comparison.
    6. Compare against the regex variant.
    6. Define token matching patterns (not possible in App mode).
    7. Visualize token matches.
    8. Define dependency matching patterns (not possible in App mode).
    9. Visualize dependency matches.

    ### 概要

    このノートブックでは以下の手順で青空文庫テキストを読み込み、前処理、解析、可視化を行います。

    1. 青空文庫のテキストファイルをアップロードする(またはデフォルトサンプルを利用する)。
    2. 編集可能な正規表現で前処理する。
    3. 前処理済みテキストの先頭50行と末尾50行をプレビューし、前処理が正常に本文以外のテキストを除外したか確認する。
    4. 前処理済みテキストをダウンロードする。
    5. 比較のため、XHTML版をPythonのパッケージで処理する。
    6. 正規表現処理版と比較する。
    7. トークンマッチング用パターンを定義する(アプリの場合は編集不可)。
    8. トークンマッチ結果を可視化する。
    9. 係り受け(依存)関係マッチング用パターンを定義する(アプリの場合は編集不可)。
    10. 係り受け関係マッチ結果を可視化する。
    """
    )
    return


@app.cell
def _(mo):
    mo.md('''
    -   By default, this demo uses Natsume Soseki's _‘Wagahai wa neko de aru’_

    -   ファイルをアップロードしない場合は、デフォルトで夏目漱石『吾輩は猫である』が使用されます。
    ''').callout(kind="info")
    return


@app.cell
def _():
    import re
    import marimo as mo
    import polars as pl
    import spacy
    from spacy.tokens import Doc

    nlp = spacy.load(
        "ja_ginza"
    )  # or "ja_ginza_electra"/"ja_ginza_bert_large" if installed
    return Doc, mo, nlp, pl, re, spacy


@app.cell
def upload_aozora_text(mo):
    aozora_file = mo.ui.file(label="Upload Aozora-Bunko text (.txt)", multiple=False)
    return (aozora_file,)


@app.cell
def select_encoding(mo):
    """
    Let the user choose the text‐file encoding.
    """
    encoding = mo.ui.dropdown(
        options=["shift-jis", "utf-8"],
        value="shift-jis",
        label="Text file encoding / 文字コード",
        full_width=False,
    )
    return (encoding,)


@app.cell
def _(aozora_file, encoding, mo):
    ab_upload_ui = mo.hstack([aozora_file, encoding])
    mo.md(f"## 青空文庫テキストファイル設定\n{ab_upload_ui}")
    return


@app.cell
def load_aozora_text(aozora_file, encoding):
    """
    Load the uploaded file if provided; otherwise read the local wagahaiwa_nekodearu.txt.
    Returns the raw text.
    """
    enc = encoding.value
    if aozora_file.value:
        uploaded = aozora_file.contents()
        text_raw = uploaded.decode(enc)
    else:
        with open("wagahaiwa_nekodearu.txt", encoding="shift-jis") as f:
            text_raw = f.read()
    return (text_raw,)


@app.cell
def show_raw_head(mo, text_raw):
    mo.md(
        f"""
    ## 青空文庫のヘッダーとフッターを確認

    最初の500字
    ```raw
    {text_raw[:500]}
    ```

    最後の500字
    ```raw
    {text_raw[-500:]}
    ```
    """
    )
    return


@app.cell
def regex_inputs(mo):
    ruby_pattern = mo.ui.text(
        value=r"《[^》]+》",
        label="ルビ",
        full_width=True,
    )
    ruby_bar_pattern = mo.ui.text(
        value=r"|",
        label="ルビのかかる範囲を示す記号",
        full_width=True,
    )
    annotation_pattern = mo.ui.text(
        value=r"[#[^]]+?]",
        label="注釈・アノテーション",
        full_width=True,
    )
    hajime_pattern = mo.ui.text(
        value=r"-{55}(.|\n)+?-{55}",
        label="青空文庫のヘッダー",
        full_width=True,
    )
    owari_pattern = mo.ui.text(
        value=(
            r"^[ 【]?(底本:|訳者あとがき|この翻訳は|この作品.*翻訳|"
            r"この翻訳.*全訳)"
        ),
        label="青空文庫のフッター",
        full_width=True,
    )

    regexes = mo.vstack(
        [
            ruby_pattern,
            ruby_bar_pattern,
            annotation_pattern,
            hajime_pattern,
            owari_pattern,
        ]
    )

    mo.md(f"""## 正規表現による前処理

    (必要な場合は修正)

    {regexes}
    """)
    return (
        annotation_pattern,
        hajime_pattern,
        owari_pattern,
        ruby_bar_pattern,
        ruby_pattern,
    )


@app.cell
def clean_aozora(
    annotation_pattern,
    hajime_pattern,
    mo,
    owari_pattern,
    re,
    ruby_bar_pattern,
    ruby_pattern,
    text_raw,
):
    # compile from user‐editable patterns
    ruby_rx = re.compile(ruby_pattern.value)
    ruby_bar_rx = re.compile(ruby_bar_pattern.value)
    annotation_rx = re.compile(annotation_pattern.value)
    hajime_rx = re.compile(hajime_pattern.value)
    owari_rx = re.compile(owari_pattern.value, re.M)

    def clean_text(text: str) -> tuple[str, str, str]:
        """青空文庫テキスト形式の文字列textを入力とし,改行方式の統一,ルビーと各種のアノーテーションの削除,
        青空文庫特有のヘッダーとフッターを取り除く処理を行う。"""

        title, author, text = (text.split("\n", 2) + ["", ""])[:3]

        # 青空文庫では改行がDOS形式の\r\nのため,それをUNIX形式の\nに統一する。
        cleaned_text = re.sub(r"(\r\n)+", "\n", text)
        # ルビ《...》の記号とその中身を削除
        cleaned_text = re.sub(ruby_rx, "", cleaned_text)
        # ルビのもう一つの書き方に対応:「一番|獰悪《どうあく》」
        cleaned_text = re.sub(ruby_bar_rx, "", cleaned_text)
        # 注釈対応:「※[#「言+墟のつくり」、第4水準2-88-74]」
        cleaned_text = re.sub(annotation_rx, "", cleaned_text)
        # 本文までのテキストを削除
        cleaned_text = re.sub(hajime_rx, "", cleaned_text)
        # 本文の後のテキストを削除
        maybe_owari = owari_rx.search(cleaned_text)
        if maybe_owari:
            return (title, author, cleaned_text[0 : maybe_owari.start()].strip())

        return (title, author, cleaned_text.strip())

    title, author, cleaned_text = clean_text(text_raw)

    mo.md(f"""### 前処理結果の確認

    -   著者:`{author}`
    -   タイトル:`{title}`

    最初の100字
    ```raw
    {cleaned_text[:100]}
    ```

    最後の100字
    ```raw
    {cleaned_text[-100:]}
    ```
    """)
    return author, cleaned_text, title


@app.cell
def download_cleaned_text(author, cleaned_text, mo, title):
    """
    Provide a download link for the cleaned Aozora text.
    """
    download_link = mo.download(
        data=cleaned_text.encode("utf-8"),
        filename=f"{author}_{title}.txt",
        mimetype="text/plain",
    )
    mo.md(f"""
    前処理済みファイルのダウンロード (UTF-8):
    {download_link}
    """)
    return


@app.cell
def get_alternative_file(mo):
    aozora_xhtml_file = mo.ui.file(
        label="Upload Aozora-Bunko text (.html)", multiple=False
    )

    xhtml_encoding = mo.ui.dropdown(
        options=["shift-jis", "utf-8"],
        value="shift-jis",
        label="Text file encoding",
        full_width=False,
    )

    mo.md(f"""
    ## HTMLを使用した前処理との比較(オプショナル)

    プレインテキスト版を正規表現で前処理した結果を、(X)HTML版をPythonで処理した結果を比較したい場合は同じ作品のHTMLファイルをアップします。

    {aozora_xhtml_file}
    {xhtml_encoding}
    """)
    return aozora_xhtml_file, xhtml_encoding


@app.cell
def show_natsume_head(aozora_xhtml_file, mo, xhtml_encoding):
    from aozora_corpus_generator.aozora import parse_aozora_bunko_xml_content

    xhtml_enc = xhtml_encoding.value
    if aozora_xhtml_file.value:
        uploaded_xhtml = aozora_xhtml_file.contents()
        xhtml_raw = uploaded_xhtml
    else:
        with open("789_14547.html", "rb") as xhtml_f:
            xhtml_raw = xhtml_f.read()

    aozora_xhtml_processed = parse_aozora_bunko_xml_content(
        xhtml_raw, do_tokenize=False
    )

    aozora_xhtml_processed_text = aozora_xhtml_processed["text"]

    mo.md(f"""
    HTML版の最初の200字

    ```raw
    {aozora_xhtml_processed_text[:200]}
    ```

    HTML版の最後の200字

    ```raw
    {aozora_xhtml_processed_text[-200:]}
    ```
    """)
    return (aozora_xhtml_processed_text,)


@app.cell
def _(aozora_xhtml_processed_text, author, mo, title):
    xhtml_download_link = mo.download(
        data=aozora_xhtml_processed_text.encode("utf-8"),
        filename=f"{author}_{title}_xhtml.txt",
        mimetype="text/plain",
    )
    mo.md(f"""
    HTML版の前処理済みファイルをダウンロード (UTF-8):
    {xhtml_download_link}
    """)
    return


@app.cell
def _():
    import difflib
    import html
    from cdifflib import CSequenceMatcher
    from IPython.display import HTML
    from IPython.display import display_html as display

    difflib.SequenceMatcher = CSequenceMatcher

    DEL_STYLE = "background-color:#f6c6c6;color:#000;"  # red bg, black text
    INS_STYLE = "background-color:#c6f6c6;color:#000;"  # green bg, black text
    WRAP_STYLE = (
        "font-family: ui-monospace, monospace; "
        "white-space: pre-wrap; line-height:1.4; color:#000;"
    )

    WS_MAP = str.maketrans({" ": "␣", "\t": "⇥", "\n": "↩\n"})

    def _escape(txt: str) -> str:
        return html.escape(txt.translate(WS_MAP))

    def _char_changes(a: str, b: str) -> str:
        """Return HTML for *only* the changed chars between a and b."""
        sm = difflib.SequenceMatcher(None, a, b, autojunk=False)
        pieces = []
        for tag, i1, i2, j1, j2 in sm.get_opcodes():
            if tag == "delete":
                pieces.append(f'<span style="{DEL_STYLE}">{_escape(a[i1:i2])}</span>')
            elif tag == "insert":
                pieces.append(f'<span style="{INS_STYLE}">{_escape(b[j1:j2])}</span>')
            elif tag == "replace":
                pieces.append(f'<span style="{DEL_STYLE}">{_escape(a[i1:i2])}</span>')
                pieces.append(f'<span style="{INS_STYLE}">{_escape(b[j1:j2])}</span>')
            # equal → ignore
        return "".join(pieces)

    def diff_changes(a: str, b: str, auto_display: bool = True):
        """
        Colab/Jupyter-friendly inline diff that shows *only the changed segments*.
        Lightning-fast on large, mostly-identical texts.
        """
        a_lines = a.splitlines(keepends=True)
        b_lines = b.splitlines(keepends=True)

        outer = difflib.SequenceMatcher(None, a_lines, b_lines, autojunk=True)
        html_chunks = []

        for tag, i1, i2, j1, j2 in outer.get_opcodes():
            if tag == "replace":  # both sides present
                for la, lb in zip(a_lines[i1:i2], b_lines[j1:j2]):
                    html_chunks.append(_char_changes(la, lb))
                # handle length mismatch
                for la in a_lines[i1 + (j2 - j1) : i2]:
                    html_chunks.append(
                        f'<span style="{DEL_STYLE}">{_escape(la)}</span>'
                    )
                for lb in b_lines[j1 + (i2 - i1) : j2]:
                    html_chunks.append(
                        f'<span style="{INS_STYLE}">{_escape(lb)}</span>'
                    )
            elif tag == "delete":
                for la in a_lines[i1:i2]:
                    html_chunks.append(
                        f'<span style="{DEL_STYLE}">{_escape(la)}</span>'
                    )
            elif tag == "insert":
                for lb in b_lines[j1:j2]:
                    html_chunks.append(
                        f'<span style="{INS_STYLE}">{_escape(lb)}</span>'
                    )
            # equal → skip entirely (we want only changes)

        rendered = f'<div style="{WRAP_STYLE}">{"".join(html_chunks)}</div>'
        if auto_display:
            display(HTML(rendered))
        return rendered

    return (diff_changes,)


@app.cell
def toggle_diff(mo):
    run_diff = mo.ui.switch(label="文章間の比較(差分)を表示", value=False)
    run_diff
    return (run_diff,)


@app.cell
def compare_preprocessed_vs_old(
    aozora_xhtml_processed_text,
    cleaned_text,
    diff_changes,
    mo,
    run_diff,
):
    """
    Compare our cleaned text against the original Aozora‐processed text.
    """

    mo.stop(not run_diff.value)

    diff_result = diff_changes(
        cleaned_text, aozora_xhtml_processed_text, auto_display=False
    )

    mo.md(f"""
    -   赤: 正規表現版のみにある文字列
    -   青: HTML版のみにある文字列

    {diff_result}

    """)
    return


@app.cell
def _(mo):
    mo.md(
        r"""
    ## spaCy (GiNZA) による解析

    以下からは、正規表現で前処理したテキストに対して、

    -   形態素解析
    -   係り受け解析

    を行う。

    > 作品によっては時間がかかる。
    """
    )
    return


@app.cell
def _(mo):
    run_spacy = mo.ui.switch(label="spaCyで解析する", value=False)
    run_spacy
    return (run_spacy,)


@app.cell
def process_aozora_text(Doc, cleaned_text, mo, nlp, re, run_spacy):
    mo.stop(not run_spacy.value)

    """
    Turn each paragraph into one Doc.  If any paragraph > MAX_BYTES,
    fall back to sentence‐splitting, then raw‐byte‐splitting, and only
    in that fallback re‐assemble via Doc.from_docs.
    """

    def split_text_to_paragraphs(text: str) -> list[str]:
        """Split on one or more blank lines."""
        return re.split(r"\n+\s*", text)

    MAX_BYTES = 40000
    paras = split_text_to_paragraphs(cleaned_text)
    aozora_docs: list[Doc] = []

    with mo.status.progress_bar(total=len(paras), title="spaCy processing") as bar:
        for para in paras:
            b = len(para.encode("utf-8"))
            if b <= MAX_BYTES:
                doc = nlp(para)
            else:
                # 1) try sentence‐level split
                parts = re.split(r"([。!?])", para)
                sents = [
                    parts[i] + (parts[i + 1] if i + 1 < len(parts) else "")
                    for i in range(0, len(parts), 2)
                ]
                # 2) accumulate into <= MAX_BYTES
                chunks: list[str] = []
                cur, cur_b = "", 0
                for s in sents:
                    sb = len(s.encode("utf-8"))
                    if cur_b + sb > MAX_BYTES:
                        if cur:
                            chunks.append(cur)
                        cur, cur_b = s, sb
                    else:
                        cur += s
                        cur_b += sb
                if cur:
                    chunks.append(cur)
                # 3) raw‐byte fallback for any too‐large piece
                final_chunks: list[str] = []
                for c in chunks:
                    if len(c.encode("utf-8")) <= MAX_BYTES:
                        final_chunks.append(c)
                    else:
                        rem = c
                        while rem:
                            pb = rem.encode("utf-8")[:MAX_BYTES]
                            part = pb.decode("utf-8", "ignore")
                            final_chunks.append(part)
                            rem = rem[len(part) :]
                # 4) merge into one Doc for this paragraph
                subdocs = list(nlp.pipe(final_chunks, batch_size=20))
                doc = Doc.from_docs(subdocs)
            aozora_docs.append(doc)
            bar.update()
    return (aozora_docs,)


@app.cell
def display_noun_chunks(aozora_docs: "list[Doc]", mo, pl):
    """
    Show the most frequent noun-chunks in the entire text made up of at least two tokens, along with the number of tokens in each chunk.
    """
    # build, filter (>=2 tokens), group and sort in one go
    top_chunks = (
        pl.DataFrame(
            {
                "chunk_text": [c.text for doc in aozora_docs for c in doc.noun_chunks],
                "token_count": [len(c) for doc in aozora_docs for c in doc.noun_chunks],
            }
        )
        .filter(pl.col("token_count") >= 2)
        .group_by("chunk_text")
        .agg([pl.len().alias("frequency"), pl.first("token_count")])
        .sort("frequency", descending=True)
    )

    mo.md(f"""
    spaCyには様々な機能が内蔵されていて、例えば、`noun_chunks`では[名詞句](https://spacy.io/usage/linguistic-features#noun-chunks)を構文(係り受け)解析結果に基づいて。ここでいう名詞句、すなわち「NPチャンク」とは、他の名詞句がその中に入れ子にならない名詞句のことで、名詞句レベルの並列や前置詞句、関係節は含まない。

    ### 2語以上からなる名詞句トップ25
    {mo.ui.dataframe(top_chunks, page_size=25)}

    > カスタマイズも[可能](https://github.com/explosion/spaCy/blob/41e07772dc5805594bab2997a090a9033e26bf56/spacy/lang/ja/syntax_iterators.py#L12)
    """)
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        """
    ## Token Pattern Matching

    トークンベースのルールを使用して、短単位で分割された動詞の塊をまとめ上げて観察する。

    > ここで使用されるルールはあくまでも例で、完璧に動詞の塊をまとめ上げていない。また、短単位より長い単位でテキスト分析する場合は長単位による解析も[可能](https://github.com/komiya-lab/monaka)。
    """
    )
    return


@app.cell
def token_pattern():
    ###### ここにサイトからコピーしたパターンを入れ変える

    pattern = [
        {"POS": "NOUN", "OP": "+"},
        {"POS": "VERB", "OP": "+"},
        {"POS": {"REGEX": "VERB|AUX"}, "OP": "+"},
    ]

    #####################################################
    return (pattern,)


@app.cell
def token_pattern_match(aozora_docs: "list[Doc]", mo, nlp, pattern, pl, spacy):
    # https://spacy.io/usage/rule-based-matching#example1
    from spacy.matcher import Matcher

    matcher = Matcher(nlp.vocab)
    matched_sentences = []  # Collect data of matched sentences to be visualized
    match_texts: list[str] = []

    def collect_sents(matcher, doc, i, matches):
        match_id, start, end = matches[i]
        span = doc[start:end]  # Matched span
        sent = span.sent  # Sentence containing matched span
        # get the match span by offsetting the start/end of the span
        match_ents = [
            {
                "start": span.start_char - sent.start_char,
                "end": span.end_char - sent.start_char,
                "label": "ヒット",
            }
        ]
        matched_sentences.append({"text": sent.text, "ents": match_ents})
        match_texts.append(span.text)

    matcher.add("MyPattern", [pattern], on_match=collect_sents)  # add pattern
    # run matcher over each paragraph
    for p_doc2 in aozora_docs:
        matcher(p_doc2)

    # only show first 10 matches
    MAX_PATTERN_MATCHES = 10
    viz_html = spacy.displacy.render(
        matched_sentences[:MAX_PATTERN_MATCHES], style="ent", manual=True
    )

    # build top‐25 frequency table of matched span texts
    df = pl.DataFrame({"match_text": match_texts})
    top_matches = (
        df.group_by("match_text")
        .agg(pl.len().alias("frequency"))
        .sort("frequency", descending=True)
        .head(25)
    )

    # display the displaCy‐rendered HTML *and* the frequency table
    mo.vstack([mo.Html(viz_html), top_matches])
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        """
    ## Dependency Pattern Matching

    係り受けパターンのルールを記述し、動詞と名詞が[nsubj](https://universaldependencies.org/ja/dep/nsubj.html) (nominal subject) という係り受け関係にあるもの、すなわち動詞とその主語を抽出する。

    > 係り受け解析は形態素解析のタスクより複雑、その解析制度がより低い。ここでは`ja_ginza`という軽量なモデルを使用しているが、解析制度を求めるのであれば、Transformerベースモデルを使用するとよい。
    """
    )
    return


@app.cell
def dependency_pattern():
    ###### ここにサイトからコピーしたパターンを入れ変える

    # this is your dependency‐matcher pattern

    dep_pattern = [
        {"RIGHT_ID": "anchor_verb", "RIGHT_ATTRS": {"POS": "VERB"}},
        {
            "LEFT_ID": "anchor_verb",
            "REL_OP": ">",
            "RIGHT_ID": "verb_subject",
            "RIGHT_ATTRS": {"DEP": "nsubj"},
        },
    ]

    #####################################################
    return (dep_pattern,)


@app.cell
def show_dependency_matches(
    aozora_docs: "list[Doc]",
    dep_pattern,
    mo,
    nlp,
    pl,
    spacy,
):
    from spacy.matcher import DependencyMatcher

    dep_matcher = DependencyMatcher(nlp.vocab)
    viz_dep_sents: list[dict] = []
    dep_pairs: list[dict[str, str]] = []

    def collect_deps(matcher, doc, i, matches):
        _, token_ids = matches[i]
        sent = doc[token_ids[0]].sent
        # map each RIGHT_ID to its matched Token
        rid_to_tok = {
            pat["RIGHT_ID"]: doc[tok_id] for pat, tok_id in zip(dep_pattern, token_ids)
        }
        verb = rid_to_tok["anchor_verb"]
        subj = rid_to_tok["verb_subject"]

        # build ents for displaCy
        ents = []
        for rid, tok in rid_to_tok.items():
            label = "subject" if rid == "verb_subject" else "verb"
            ents.append(
                {
                    "start": tok.idx - sent.start_char,
                    "end": tok.idx + len(tok) - sent.start_char,
                    "label": label,
                    "text": tok.text,
                }
            )

        viz_dep_sents.append({"text": sent.text, "ents": ents})
        dep_pairs.append({"subject": subj.text, "verb": verb.text})

    dep_matcher.add("MyDepPattern", [dep_pattern], on_match=collect_deps)
    for dep_doc in aozora_docs:
        dep_matcher(dep_doc)

    dep_viz_html = spacy.displacy.render(viz_dep_sents[:10], style="ent", manual=True)

    dep_df = pl.DataFrame(dep_pairs)
    top_dep_matches = (
        dep_df.group_by(["subject", "verb"])
        .agg(pl.len().alias("frequency"))
        .sort("frequency", descending=True)
    )

    mo.vstack(
        [
            mo.Html(dep_viz_html),
            top_dep_matches,
        ]
    )
    return


@app.cell
def _():
    return


if __name__ == "__main__":
    app.run()