monaka 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
monaka/__init__.py ADDED
@@ -0,0 +1 @@
1
+ __version__ = "0.1.0"
monaka/bccwj_util.py ADDED
@@ -0,0 +1,204 @@
1
+ """bccwj_util.py
2
+
3
+ BCCWJ関連のファイルを処理するためのツール群をまとめたモジュール
4
+ """
5
+
6
+ import typer
7
+ import json
8
+ import csv
9
+ import stanza
10
+
11
+ from typing import List
12
+
13
+ app = typer.Typer(pretty_exceptions_show_locals=False)
14
+
15
+ BCPEXPORT_LIST = [
16
+ "corpusName(S)",
17
+ "file(S)",
18
+ "start(S)",
19
+ "end(S)",
20
+ "boundary(S)",
21
+ "orthToken(S)",
22
+ "pronToken(S)",
23
+ "reading(S)",
24
+ "lemma(S)",
25
+ "originalText(S)",
26
+ "pos(S)",
27
+ "sysCType(S)",
28
+ "cForm(S)",
29
+ "apply(S)",
30
+ "additionalInfo(S)",
31
+ "lid(S)",
32
+ "meaning(S)",
33
+ "UpdUser(S)",
34
+ "UpdDate(S)",
35
+ "order(S)",
36
+ "note(S)",
37
+ "open(S)",
38
+ "close(S)",
39
+ "wType(S)",
40
+ "fix(S)",
41
+ "variable(S)",
42
+ "formBase(S)",
43
+ "lemmaID(S)",
44
+ "usage(S)",
45
+ "sentenceId(S)",
46
+ "s_memo(S)",
47
+ "origChar(S)",
48
+ "pSampleID(S)",
49
+ "pStart(S)",
50
+ "orthBase(S)",
51
+ "file(L)",
52
+ "l_orthToken(L)",
53
+ "l_pos(L)",
54
+ "l_cType(L)",
55
+ "l_cForm(L)",
56
+ "l_reading(L)",
57
+ "l_lemma(L)",
58
+ "luw(L)",
59
+ "memo(L)",
60
+ "UpdUser(L)",
61
+ "UpdDate(L)",
62
+ "l_start(L)",
63
+ "l_end(L)",
64
+ "bunsetsu1(L)",
65
+ "bunsetsu2(L)",
66
+ "corpusName(L)",
67
+ "diffSuw(L)",
68
+ "l_lemmaNew(L)",
69
+ "l_readingNew(L)",
70
+ "l_orthBase(L)",
71
+ "l_formBase(L)",
72
+ "l_pronToken(L)",
73
+ "l_wType(L)",
74
+ "l_originalText(L)",
75
+ "complex(L)",
76
+ "l_meaning(L)",
77
+ "l_kanaToken(L)",
78
+ "l_formOrthBase(L)",
79
+ "l_origChar(L)",
80
+ "note(L)",
81
+ "pSampleID(L)",
82
+ "pStart(L)",
83
+ "rn"
84
+ ]
85
+
86
+ @app.command()
87
+ def diff(gold: str, pred: str):
88
+ """長単位解析前後で変わらない要素に変化があるかを調べる
89
+
90
+ Args:
91
+ gold (str): 入力前、あるいは正解のBCCWJ用TSV
92
+ pred (str): 長単位解析後のBCCWJ用出力TSV
93
+ """
94
+ with open(gold) as f1:
95
+ with open(pred) as f2:
96
+ grd = csv.reader(f1, delimiter="\t")
97
+ prd = csv.reader(f2, delimiter="\t")
98
+ i = 0
99
+ for rp in prd:
100
+ p_text = "_".join(rp[:5])
101
+ rg = next(grd)
102
+ i += 1
103
+ g_text = "_".join(rg[:5])
104
+ if p_text == g_text:
105
+ continue
106
+ while p_text != g_text:
107
+ print(f"line: {i} {g_text} {p_text}")
108
+ i+=1
109
+ rg = next(grd)
110
+ g_text = "_".join(rg[:5])
111
+
112
+
113
+ @app.command()
114
+ def export_tsv(base: str, target: str):
115
+ """JSON-L形式のデータをBCCWJ用のTSV形式に変換する
116
+ * JSON-L形式のデータへの変換には、utii.py chj2jsonl を用いる
117
+ * 学習用JSON-Lデータは 上記のJSON-Lデータを util.py chjjsonl2luwjson で変換したもの
118
+ * 結果は標準出力に出る
119
+
120
+ Args:
121
+ base (str): JSON-L形式のBCCWJデータ
122
+ target (str): 学習用JSON-Lデータ
123
+ """
124
+ based = dict()
125
+ with open(base) as f:
126
+ for line in f:
127
+ js = json.loads(line)
128
+ based[js["sentence"]] = js
129
+
130
+ with open(target) as f:
131
+ for line in f:
132
+ js = json.loads(line)
133
+ name = js["sentence"]
134
+ d = based[name]
135
+ for token in d["tokens"]:
136
+ out = {"corpusName(S)": d["corpusName(S)"], "file(S)": d["file(S)"]}
137
+ out.update(token)
138
+ print("\t".join(out[k] for k in BCPEXPORT_LIST))
139
+
140
+ @app.command()
141
+ def evaluate(gold: str, pred: str, corrects :List[str]=["luw(L)"], errors :List[str]=["l_orthToken(L)"], fields: List[str]=["bunsetsu1(L)", "luw(L)", "l_orthToken(L)", "l_reading(L)", "l_pos(L)", "l_cType(L)", "l_cForm(L)"]):
142
+ """精度評価スクリプト
143
+ * 結果は標準出力
144
+
145
+ Args:
146
+ gold (str): 正解のBCCWJ用TSV
147
+ pred (str): 解析結果のBCCWJ用TSV
148
+ corrects (List[str], optional): 正解として表示するフィールド. Defaults to ["luw(L)"].
149
+ errors (List[str], optional): 不正解として表示するフィールド. Defaults to ["l_orthToken(L)"].
150
+ fields (List[str], optional): 評価対象のフィールド. Defaults to ["bunsetsu1(L)", "luw(L)", "l_orthToken(L)", "l_reading(L)", "l_pos(L)", "l_cType(L)", "l_cForm(L)"].
151
+ """
152
+ output = dict()
153
+ with open(gold) as f1:
154
+ grd = csv.reader(f1, delimiter="\t")
155
+ with open(pred) as f2:
156
+ prd = csv.reader(f2, delimiter="\t")
157
+ for rg, rp in zip(grd, prd):
158
+ crrct = False
159
+ error = False
160
+ for field in fields:
161
+ i = BCPEXPORT_LIST.index(field)
162
+ d = output.get(field, {"a":0 , "c": 0})
163
+ d["a"] +=1
164
+ if field in ["bunsetsu1(L)", "luw(L)"]:
165
+ if 'B' in rg[i]:
166
+ if 'B' in rp[i]:
167
+ if field in corrects:
168
+ crrct = True
169
+ d["c"] += 1
170
+ else:
171
+ if field in errors:
172
+ error = True
173
+ else:
174
+ if 'B' not in rp[i]:
175
+ if field in corrects:
176
+ crrct = True
177
+ d["c"] += 1
178
+ else:
179
+ if field in errors:
180
+ error = True
181
+
182
+ elif rg[i] == rp[i]:
183
+ d["c"] += 1
184
+ if field in corrects:
185
+ crrct = True
186
+ elif len(rg[i]) == 0 and rp[i] == '*':
187
+ d["c"] += 1
188
+ if field in corrects:
189
+ crrct = True
190
+ else:
191
+ if field in errors:
192
+ error = True
193
+
194
+ output[field] = d
195
+ if crrct and error:
196
+ print("gold:", "\t".join([rg[BCPEXPORT_LIST.index(name)] for name in corrects + errors ]))
197
+ print("pred:", "\t".join([rp[BCPEXPORT_LIST.index(name)] for name in corrects + errors ]))
198
+
199
+ for field, d in output.items():
200
+ print(f"{field} count: {d['a']} correct: {d['c']} acc: {d['c']/d['a']}")
201
+
202
+
203
+ if __name__ == "__main__":
204
+ app()