nakamura196 commited on
Commit
9c91424
·
1 Parent(s): 29fa847

feat: initial commit

Browse files
Files changed (5) hide show
  1. .gitignore +2 -0
  2. app.py +43 -0
  3. data.json +0 -0
  4. requirements.txt +2 -0
  5. src/01_create_data.ipynb +79 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ .venv
2
+ __pycache__
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from Levenshtein import ratio
3
+ import json
4
+
5
+ data_path = "./data.json"
6
+
7
+ with open(data_path, "r") as f:
8
+ documents_data = json.load(f)
9
+
10
+ def search_similar_texts(query, top_n=5):
11
+ # クエリをベクトル化
12
+ results = []
13
+
14
+ for doc in documents_data:
15
+
16
+ score = ratio(query, doc["text"])
17
+ results.append({
18
+ "vol": doc["vol"],
19
+ "page": doc["page"],
20
+ "score": score,
21
+ "text": doc["text"]
22
+ })
23
+ # print(score)
24
+
25
+ results.sort(key=lambda x: x["score"], reverse=True)
26
+
27
+ return results[:top_n]
28
+
29
+ # Gradioインターフェースの作成
30
+ demo = gr.Interface(
31
+ fn=search_similar_texts,
32
+ inputs=[
33
+ gr.Textbox(label="検索テキスト", placeholder="検索したいテキストを入力してください"),
34
+ gr.Slider(minimum=1, maximum=10, value=5, step=1, label="表示件数")
35
+ ],
36
+ outputs=gr.JSON(),
37
+ title="校異源氏物語 類似テキスト検索",
38
+ description="テキストを入力すると、校異源氏物語の類似する箇所を検索します。",
39
+ allow_flagging="never",
40
+ )
41
+
42
+ # インターフェースの起動
43
+ demo.launch()
data.json ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ levenshtein
2
+ gradio
src/01_create_data.ipynb ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "from glob import glob\n",
10
+ "import xml.etree.ElementTree as ET\n",
11
+ "import json"
12
+ ]
13
+ },
14
+ {
15
+ "cell_type": "code",
16
+ "execution_count": 2,
17
+ "metadata": {},
18
+ "outputs": [],
19
+ "source": [
20
+ "files = glob(\"../../kouigenjimonogatari.github.io/xml/lw/*.xml\")\n",
21
+ "files.sort()\n",
22
+ "data = []\n",
23
+ "\n",
24
+ "for file in files:\n",
25
+ " tree = ET.parse(file)\n",
26
+ " root = tree.getroot()\n",
27
+ "\n",
28
+ " vol = int(file.split(\"/\")[-1].split(\".\")[0])\n",
29
+ " segs = root.findall(\".//{http://www.tei-c.org/ns/1.0}seg\")\n",
30
+ " texts = {}\n",
31
+ "\n",
32
+ " for seg in segs: \n",
33
+ " corresp = seg.attrib[\"corresp\"]\n",
34
+ " page = int(corresp.split(\"/\")[-1].split(\"-\")[0])\n",
35
+ " text = seg.text\n",
36
+ "\n",
37
+ " if page not in texts:\n",
38
+ " texts[page] = []\n",
39
+ "\n",
40
+ " texts[page].append(text)\n",
41
+ "\n",
42
+ " for page in texts:\n",
43
+ " text = \" \".join(texts[page])\n",
44
+ " data.append({\"vol\": vol, \"page\": page, \"text\": text})"
45
+ ]
46
+ },
47
+ {
48
+ "cell_type": "code",
49
+ "execution_count": 23,
50
+ "metadata": {},
51
+ "outputs": [],
52
+ "source": [
53
+ "with open(\"../data.json\", \"w\") as f:\n",
54
+ " json.dump(data, f, ensure_ascii=False, indent=4)"
55
+ ]
56
+ }
57
+ ],
58
+ "metadata": {
59
+ "kernelspec": {
60
+ "display_name": ".venv",
61
+ "language": "python",
62
+ "name": "python3"
63
+ },
64
+ "language_info": {
65
+ "codemirror_mode": {
66
+ "name": "ipython",
67
+ "version": 3
68
+ },
69
+ "file_extension": ".py",
70
+ "mimetype": "text/x-python",
71
+ "name": "python",
72
+ "nbconvert_exporter": "python",
73
+ "pygments_lexer": "ipython3",
74
+ "version": "3.9.11"
75
+ }
76
+ },
77
+ "nbformat": 4,
78
+ "nbformat_minor": 2
79
+ }