Spaces:
Sleeping
Sleeping
Bor Hodošček
commited on
chore: documentation fixes
Browse files
app.py
CHANGED
@@ -26,35 +26,44 @@ app = marimo.App()
|
|
26 |
def _(mo):
|
27 |
mo.md(
|
28 |
rf"""
|
29 |
-
# Aozora Bunko Text Processing Pipeline Demo
|
30 |
|
31 |
### Summary
|
32 |
|
|
|
|
|
33 |
1. Upload a text file from Aozora Bunko (or use the default sample).
|
34 |
2. Preprocess using customizable regex patterns.
|
35 |
3. Preview the first and last 50 lines of the cleaned text.
|
36 |
4. Download the cleaned text.
|
37 |
-
5. Process the XHTML version with
|
38 |
6. Compare against the regex variant.
|
39 |
-
6. Define token matching patterns.
|
40 |
7. Visualize token matches.
|
41 |
-
8. Define dependency matching patterns.
|
42 |
9. Visualize dependency matches.
|
43 |
|
44 |
### 概要
|
45 |
|
|
|
|
|
46 |
1. 青空文庫のテキストファイルをアップロードする(またはデフォルトサンプルを利用する)。
|
47 |
2. 編集可能な正規表現で前処理する。
|
48 |
-
3. 前処理済みテキストの先頭50行と末尾50
|
49 |
4. 前処理済みテキストをダウンロードする。
|
50 |
-
5. XHTML版をPythonのパッケージで処理する。
|
51 |
6. 正規表現処理版と比較する。
|
52 |
-
7.
|
53 |
8. トークンマッチ結果を可視化する。
|
54 |
-
9.
|
55 |
10. 係り受け関係マッチ結果を可視化する。
|
56 |
-
|
57 |
-
{
|
|
|
|
|
|
|
|
|
|
|
58 |
"""
|
59 |
)
|
60 |
return
|
@@ -76,10 +85,6 @@ def _():
|
|
76 |
|
77 |
@app.cell
|
78 |
def upload_aozora_text(mo):
|
79 |
-
"""
|
80 |
-
UI element to upload an Aozora‐Bunko text file.
|
81 |
-
Falls back to local file if none is provided.
|
82 |
-
"""
|
83 |
aozora_file = mo.ui.file(label="Upload Aozora-Bunko text (.txt)", multiple=False)
|
84 |
return (aozora_file,)
|
85 |
|
@@ -92,7 +97,7 @@ def select_encoding(mo):
|
|
92 |
encoding = mo.ui.dropdown(
|
93 |
options=["shift-jis", "utf-8"],
|
94 |
value="shift-jis",
|
95 |
-
label="Text file encoding",
|
96 |
full_width=False,
|
97 |
)
|
98 |
return (encoding,)
|
@@ -145,22 +150,22 @@ def show_raw_head(mo, text_raw):
|
|
145 |
def regex_inputs(mo):
|
146 |
ruby_pattern = mo.ui.text(
|
147 |
value=r"《[^》]+》",
|
148 |
-
label="
|
149 |
full_width=True,
|
150 |
)
|
151 |
ruby_bar_pattern = mo.ui.text(
|
152 |
value=r"|",
|
153 |
-
label="
|
154 |
full_width=True,
|
155 |
)
|
156 |
annotation_pattern = mo.ui.text(
|
157 |
value=r"[#[^]]+?]",
|
158 |
-
label="
|
159 |
full_width=True,
|
160 |
)
|
161 |
hajime_pattern = mo.ui.text(
|
162 |
value=r"-{55}(.|\n)+?-{55}",
|
163 |
-
label="
|
164 |
full_width=True,
|
165 |
)
|
166 |
owari_pattern = mo.ui.text(
|
@@ -168,7 +173,7 @@ def regex_inputs(mo):
|
|
168 |
r"^[ 【]?(底本:|訳者あとがき|この翻訳は|この作品.*翻訳|"
|
169 |
r"この翻訳.*全訳)"
|
170 |
),
|
171 |
-
label="
|
172 |
full_width=True,
|
173 |
)
|
174 |
|
@@ -217,7 +222,7 @@ def clean_aozora(
|
|
217 |
|
218 |
def clean_text(text: str) -> tuple[str, str, str]:
|
219 |
"""青空文庫テキスト形式の文字列textを入力とし,改行方式の統一,ルビーと各種のアノーテーションの削除,
|
220 |
-
|
221 |
|
222 |
title, author, text = (text.split("\n", 2) + ["", ""])[:3]
|
223 |
|
@@ -269,7 +274,7 @@ def download_cleaned_text(author, cleaned_text, mo, title):
|
|
269 |
mimetype="text/plain",
|
270 |
)
|
271 |
mo.md(f"""
|
272 |
-
|
273 |
{download_link}
|
274 |
""")
|
275 |
return
|
@@ -341,7 +346,7 @@ def _(aozora_xhtml_processed_text, author, mo, title):
|
|
341 |
mimetype="text/plain",
|
342 |
)
|
343 |
mo.md(f"""
|
344 |
-
HTML
|
345 |
{xhtml_download_link}
|
346 |
""")
|
347 |
return
|
@@ -437,6 +442,7 @@ def toggle_diff(mo):
|
|
437 |
|
438 |
@app.cell
|
439 |
def compare_preprocessed_vs_old(
|
|
|
440 |
aozora_xhtml_processed_text,
|
441 |
cleaned_text,
|
442 |
diff_changes,
|
@@ -453,10 +459,14 @@ def compare_preprocessed_vs_old(
|
|
453 |
diff_result = diff_changes(
|
454 |
cleaned_text, aozora_xhtml_processed_text, auto_display=False
|
455 |
)
|
456 |
-
# else:
|
457 |
-
# diff_result = mo.md("Diff comparison is turned off.")
|
458 |
|
459 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
460 |
return
|
461 |
|
462 |
|
|
|
26 |
def _(mo):
|
27 |
mo.md(
|
28 |
rf"""
|
29 |
+
# Aozora Bunko Text Processing Pipeline Demo / 青空文庫テキストの前処理パイプラインデモ
|
30 |
|
31 |
### Summary
|
32 |
|
33 |
+
This notebook allows you to upload, preprocess, compare, visualize and analyze Aozora Bunko texts.
|
34 |
+
|
35 |
1. Upload a text file from Aozora Bunko (or use the default sample).
|
36 |
2. Preprocess using customizable regex patterns.
|
37 |
3. Preview the first and last 50 lines of the cleaned text.
|
38 |
4. Download the cleaned text.
|
39 |
+
5. Process the XHTML version with the `aozora-corpus-generator` Python library for comparison.
|
40 |
6. Compare against the regex variant.
|
41 |
+
6. Define token matching patterns (not possible in App mode).
|
42 |
7. Visualize token matches.
|
43 |
+
8. Define dependency matching patterns (not possible in App mode).
|
44 |
9. Visualize dependency matches.
|
45 |
|
46 |
### 概要
|
47 |
|
48 |
+
このノートブックでは以下の手順で青空文庫テキストを読み込み、前処理、解析、可視化を行います。
|
49 |
+
|
50 |
1. 青空文庫のテキストファイルをアップロードする(またはデフォルトサンプルを利用する)。
|
51 |
2. 編集可能な正規表現で前処理する。
|
52 |
+
3. 前処理済みテキストの先頭50行と末尾50行をプレビューし、前処理が正常に本文以外のテキストを除外したか確認する。
|
53 |
4. 前処理済みテキストをダウンロードする。
|
54 |
+
5. 比較のため、XHTML版をPythonのパッケージで処理する。
|
55 |
6. 正規表現処理版と比較する。
|
56 |
+
7. トークンマッチング用パターンを定義する(アプリの場合は編集不可)。
|
57 |
8. トークンマッチ結果を可視化する。
|
58 |
+
9. 係り受け(依存)関係マッチング用パターンを定義する(アプリの場合は編集不可)。
|
59 |
10. 係り受け関係マッチ結果を可視化する。
|
60 |
+
|
61 |
+
{
|
62 |
+
mo.callout('''
|
63 |
+
- By default, this demo uses Natsume Soseki's _‘Wagahai wa neko de aru’_
|
64 |
+
- ファイルをアップロードしない場合は、デフォルトで夏目漱石『吾輩は猫である』が使用されます。
|
65 |
+
''')
|
66 |
+
}
|
67 |
"""
|
68 |
)
|
69 |
return
|
|
|
85 |
|
86 |
@app.cell
|
87 |
def upload_aozora_text(mo):
|
|
|
|
|
|
|
|
|
88 |
aozora_file = mo.ui.file(label="Upload Aozora-Bunko text (.txt)", multiple=False)
|
89 |
return (aozora_file,)
|
90 |
|
|
|
97 |
encoding = mo.ui.dropdown(
|
98 |
options=["shift-jis", "utf-8"],
|
99 |
value="shift-jis",
|
100 |
+
label="Text file encoding / 文字コード",
|
101 |
full_width=False,
|
102 |
)
|
103 |
return (encoding,)
|
|
|
150 |
def regex_inputs(mo):
|
151 |
ruby_pattern = mo.ui.text(
|
152 |
value=r"《[^》]+》",
|
153 |
+
label="ルビ",
|
154 |
full_width=True,
|
155 |
)
|
156 |
ruby_bar_pattern = mo.ui.text(
|
157 |
value=r"|",
|
158 |
+
label="ルビのかかる範囲を示す記号",
|
159 |
full_width=True,
|
160 |
)
|
161 |
annotation_pattern = mo.ui.text(
|
162 |
value=r"[#[^]]+?]",
|
163 |
+
label="注釈・アノテーション",
|
164 |
full_width=True,
|
165 |
)
|
166 |
hajime_pattern = mo.ui.text(
|
167 |
value=r"-{55}(.|\n)+?-{55}",
|
168 |
+
label="青空文庫のヘッダー",
|
169 |
full_width=True,
|
170 |
)
|
171 |
owari_pattern = mo.ui.text(
|
|
|
173 |
r"^[ 【]?(底本:|訳者あとがき|この翻訳は|この作品.*翻訳|"
|
174 |
r"この翻訳.*全訳)"
|
175 |
),
|
176 |
+
label="青空文庫のフッター",
|
177 |
full_width=True,
|
178 |
)
|
179 |
|
|
|
222 |
|
223 |
def clean_text(text: str) -> tuple[str, str, str]:
|
224 |
"""青空文庫テキスト形式の文字列textを入力とし,改行方式の統一,ルビーと各種のアノーテーションの削除,
|
225 |
+
青空文庫特有のヘッダーとフッターを取り除く処理を行う。"""
|
226 |
|
227 |
title, author, text = (text.split("\n", 2) + ["", ""])[:3]
|
228 |
|
|
|
274 |
mimetype="text/plain",
|
275 |
)
|
276 |
mo.md(f"""
|
277 |
+
前処理済みファイルのダウンロード (UTF-8):
|
278 |
{download_link}
|
279 |
""")
|
280 |
return
|
|
|
346 |
mimetype="text/plain",
|
347 |
)
|
348 |
mo.md(f"""
|
349 |
+
HTML版の前処理済みファイルをダウンロード (UTF-8):
|
350 |
{xhtml_download_link}
|
351 |
""")
|
352 |
return
|
|
|
442 |
|
443 |
@app.cell
|
444 |
def compare_preprocessed_vs_old(
|
445 |
+
mo,
|
446 |
aozora_xhtml_processed_text,
|
447 |
cleaned_text,
|
448 |
diff_changes,
|
|
|
459 |
diff_result = diff_changes(
|
460 |
cleaned_text, aozora_xhtml_processed_text, auto_display=False
|
461 |
)
|
|
|
|
|
462 |
|
463 |
+
mo.md(f"""
|
464 |
+
- 赤: 正規表現版のみにある文字列
|
465 |
+
- 青: HTML版のみにある文字列
|
466 |
+
|
467 |
+
{diff_result}
|
468 |
+
|
469 |
+
""")
|
470 |
return
|
471 |
|
472 |
|