p1atdev commited on
Commit
59bd07b
·
1 Parent(s): e9e9cc7

Delete open2ch.py

Browse files
Files changed (1) hide show
  1. open2ch.py +0 -348
open2ch.py DELETED
@@ -1,348 +0,0 @@
1
- """おーぷん2ちゃんねる対話コーパス"""
2
-
3
- from typing import Iterable
4
-
5
- import csv
6
- import os
7
-
8
- import datasets as ds
9
-
10
-
11
- _CITATION = """\
12
- @inproceedings{open2chdlc2019,
13
- title={おーぷん2ちゃんねる対話コーパスを用いた用例ベース対話システム},
14
- author={稲葉 通将},
15
- booktitle={第87回言語・音声理解と対話処理研究会(第10回対話システムシンポジウム), 人工知能学会研究会資料 SIG-SLUD-B902-33},
16
- pages={129--132},
17
- year={2019}
18
- }
19
- """
20
-
21
- _DESCRIPTION = """\
22
- おーぷん2ちゃんねるの「なんでも実況(ジュピター)」「ニュー速VIP」「ニュース速報+」の3つの掲示板をクロールして作成した対話コーパスの HuggingFace Datasets 変換版です。
23
- """
24
-
25
- _HOMEPAGE = "https://github.com/1never/open2ch-dialogue-corpus"
26
-
27
- _LICENSE = "Apache-2.0"
28
-
29
- _URLS = {
30
- "corpus": "https://media.githubusercontent.com/media/1never/open2ch-dialogue-corpus/master/corpus.zip",
31
- "ranking": "https://raw.githubusercontent.com/1never/open2ch-dialogue-corpus/master/ranking.zip",
32
- "ng_words": "https://raw.githubusercontent.com/1never/open2ch-dialogue-corpus/master/data/ng_words.txt",
33
- }
34
-
35
-
36
- # Load corpus data from file
37
- def load_corpus(filepath: str, ng_words: list[str] = []) -> Iterable[list]:
38
- with open(filepath, "r", encoding="utf-8") as f:
39
- reader = csv.reader(f, delimiter="\t")
40
-
41
- for row in reader:
42
- dialogue = []
43
- should_skip = False
44
-
45
- for i, content in enumerate(row):
46
- for word in ng_words: # NG ワード含んでいたらスキップ
47
- if word in content:
48
- should_skip = True
49
- break
50
- if should_skip:
51
- break
52
-
53
- if i % 2 == 0:
54
- dialogue.append(
55
- {"speaker": 1, "content": content.replace("__BR__", "\n")}
56
- )
57
- else:
58
- dialogue.append(
59
- {"speaker": 2, "content": content.replace("__BR__", "\n")}
60
- )
61
-
62
- if should_skip:
63
- continue
64
-
65
- yield dialogue
66
-
67
-
68
- # Load ranking data from file
69
- def load_ranking(filepath: str, split: str) -> Iterable[dict]:
70
- with open(os.path.join(filepath, f"{split}.tsv"), "r", encoding="utf-8") as f:
71
- reader = csv.reader(f, delimiter="\t")
72
-
73
- data = {
74
- "dialogue": [],
75
- "next": {},
76
- "random": [],
77
- }
78
- dialogue_len = 0
79
-
80
- for i, row in enumerate(reader):
81
- if i % 10 == 0:
82
- dialogue_len = len(row)
83
- for j, content in enumerate(row[1:-1]): # [0] は使わない
84
- if j % 2 == 0:
85
- data["dialogue"].append(
86
- {"speaker": 1, "content": content.replace("__BR__", "\n")}
87
- )
88
- else:
89
- data["dialogue"].append(
90
- {"speaker": 2, "content": content.replace("__BR__", "\n")}
91
- )
92
- data["next"] = {
93
- "speaker": 1 if dialogue_len % 2 == 1 else 2,
94
- "content": row[-1].replace("__BR__", "\n"),
95
- }
96
-
97
- elif i % 10 >= 1 and i % 10 <= 8:
98
- data["random"].append(row[-1].replace("__BR__", "\n"))
99
- elif i % 10 == 9:
100
- data["random"].append(row[-1].replace("__BR__", "\n"))
101
-
102
- yield data
103
-
104
- data = {
105
- "dialogue": [],
106
- "next": {},
107
- "random": [],
108
- }
109
- dialogue_len = 0
110
-
111
-
112
- # Load NG words from file
113
- def load_ng_words(filepath) -> list[str]:
114
- data = []
115
- with open(filepath, "r", encoding="utf-8") as f:
116
- for line in f:
117
- data.append(line.strip())
118
- return data
119
-
120
-
121
- class Open2chDataset(ds.GeneratorBasedBuilder):
122
- VERSION = ds.Version("1.0.0")
123
-
124
- BUILDER_CONFIGS = [
125
- ds.BuilderConfig(
126
- name="all-corpus",
127
- version=VERSION,
128
- description="All concatenated corpus data",
129
- ),
130
- ds.BuilderConfig(
131
- name="livejupiter",
132
- version=VERSION,
133
- description="A subset of corpus data from livejupiter board",
134
- ),
135
- ds.BuilderConfig(
136
- name="news4vip",
137
- version=VERSION,
138
- description="A subset of corpus data from news4vip board",
139
- ),
140
- ds.BuilderConfig(
141
- name="newsplus",
142
- version=VERSION,
143
- description="A subset of corpus data from newsplus board",
144
- ),
145
- ds.BuilderConfig(
146
- name="all-corpus-cleaned",
147
- version=VERSION,
148
- description="Cleaned version of all concatenated corpus data",
149
- ),
150
- ds.BuilderConfig(
151
- name="livejupiter-cleaned",
152
- version=VERSION,
153
- description="Cleaned version of a subset of corpus data from livejupiter board",
154
- ),
155
- ds.BuilderConfig(
156
- name="news4vip-cleaned",
157
- version=VERSION,
158
- description="Cleaned version of a subset of corpus data from news4vip board",
159
- ),
160
- ds.BuilderConfig(
161
- name="newsplus-cleaned",
162
- version=VERSION,
163
- description="Cleaned version of a subset of corpus data from newsplus board",
164
- ),
165
- ds.BuilderConfig(
166
- name="ranking",
167
- version=VERSION,
168
- description="Dataset for response ranking task",
169
- ),
170
- ]
171
-
172
- DEFAULT_CONFIG_NAME = "all-corpus"
173
-
174
- def __init__(self, *args, **kwargs):
175
- super().__init__(*args, **kwargs)
176
-
177
- OVER_SIZE_LIMIT = 6_000_000
178
- csv.field_size_limit(OVER_SIZE_LIMIT)
179
-
180
- def _info(self):
181
- if self.config.name in ["all-corpus", "all-corpus-cleaned"]:
182
- features = ds.Features(
183
- {
184
- "dialogue": ds.Sequence(
185
- {
186
- "speaker": ds.Value("int8"), # 1 or 2
187
- "content": ds.Value("string"),
188
- }
189
- ),
190
- "board": ds.Value("string"),
191
- }
192
- )
193
- elif self.config.name in [
194
- "livejupiter",
195
- "news4vip",
196
- "newsplus",
197
- "livejupiter-cleaned",
198
- "news4vip-cleaned",
199
- "newsplus-cleaned",
200
- ]:
201
- features = ds.Features(
202
- {
203
- "dialogue": ds.Sequence(
204
- {
205
- "speaker": ds.Value("int8"), # 1 or 2
206
- "content": ds.Value("string"),
207
- }
208
- ),
209
- }
210
- )
211
- elif self.config.name == "ranking":
212
- features = ds.Features(
213
- {
214
- "dialogue": ds.Sequence(
215
- {
216
- "speaker": ds.Value("int8"), # 1 or 2
217
- "content": ds.Value("string"),
218
- }
219
- ),
220
- "next": { # 正しい返答
221
- "speaker": ds.Value("int8"), # 1 or 2
222
- "content": ds.Value("string"),
223
- },
224
- "random": ds.Sequence( # ランダムに選ばれた返答
225
- ds.Value("string"),
226
- ),
227
- }
228
- )
229
- else:
230
- raise ValueError("Invalid config name")
231
-
232
- return ds.DatasetInfo(
233
- description=_DESCRIPTION,
234
- features=features,
235
- homepage=_HOMEPAGE,
236
- license=_LICENSE,
237
- citation=_CITATION,
238
- )
239
-
240
- def _split_generators(self, dl_manager):
241
- corpus_url = _URLS["corpus"]
242
- ng_words_url = _URLS["ng_words"]
243
-
244
- if self.config.name in ["all-corpus", "livejupiter", "news4vip", "newsplus"]:
245
- data_dir = dl_manager.download_and_extract(corpus_url)
246
-
247
- return [
248
- ds.SplitGenerator(
249
- name=ds.Split.TRAIN,
250
- gen_kwargs={
251
- "filepath": os.path.join(data_dir, "corpus"),
252
- },
253
- ),
254
- ]
255
- elif self.config.name in [
256
- "all-corpus-cleaned",
257
- "livejupiter-cleaned",
258
- "news4vip-cleaned",
259
- "newsplus-cleaned",
260
- ]:
261
- data_dir = dl_manager.download_and_extract(corpus_url)
262
- ng_words_dir = dl_manager.download(ng_words_url)
263
-
264
- return [
265
- ds.SplitGenerator(
266
- name=ds.Split.TRAIN,
267
- gen_kwargs={
268
- "filepath": os.path.join(data_dir, "corpus"),
269
- "ng_words_path": ng_words_dir,
270
- },
271
- ),
272
- ]
273
- elif self.config.name == "ranking":
274
- ranking_url = _URLS["ranking"]
275
- data_dir = dl_manager.download_and_extract(ranking_url)
276
-
277
- return [
278
- ds.SplitGenerator(
279
- name=ds.Split.TRAIN,
280
- gen_kwargs={
281
- "filepath": os.path.join(data_dir, "ranking"),
282
- "split": "dev",
283
- },
284
- ),
285
- ds.SplitGenerator(
286
- name=ds.Split.TEST,
287
- gen_kwargs={
288
- "filepath": os.path.join(data_dir, "ranking"),
289
- "split": "test",
290
- },
291
- ),
292
- ]
293
-
294
- def _generate_examples(
295
- self,
296
- filepath: str,
297
- split: str = "train",
298
- ng_words_path: str | None = None,
299
- ):
300
- livejupiter_path = os.path.join(filepath, "livejupiter.tsv")
301
- news4vip_path = os.path.join(filepath, "news4vip.tsv")
302
- newsplus_path = os.path.join(filepath, "newsplus.tsv")
303
-
304
- ng_words = load_ng_words(ng_words_path) if ng_words_path is not None else []
305
-
306
- if self.config.name == "all-corpus":
307
- for i, data in enumerate(load_corpus(livejupiter_path, ng_words)):
308
- yield f"livejupiter-{i}", {
309
- "dialogue": data,
310
- "board": "livejupiter",
311
- }
312
-
313
- for i, data in enumerate(load_corpus(news4vip_path, ng_words)):
314
- yield f"news4vip-{i}", {
315
- "dialogue": data,
316
- "board": "news4vip",
317
- }
318
-
319
- for i, data in enumerate(load_corpus(newsplus_path, ng_words)):
320
- yield f"newsplus-{i}", {
321
- "dialogue": data,
322
- "board": "newsplus",
323
- }
324
-
325
- elif self.config.name.startswith("livejupiter"):
326
- for i, data in enumerate(load_corpus(livejupiter_path, ng_words)):
327
- yield f"livejupiter-{i}", {
328
- "dialogue": data,
329
- }
330
-
331
- elif self.config.name.startswith("news4vip"):
332
- for i, data in enumerate(load_corpus(news4vip_path, ng_words)):
333
- yield f"news4vip-{i}", {
334
- "dialogue": data,
335
- }
336
-
337
- elif self.config.name.startswith("newsplus"):
338
- for i, data in enumerate(load_corpus(newsplus_path, ng_words)):
339
- yield f"newsplus-{i}", {
340
- "dialogue": data,
341
- }
342
-
343
- elif self.config.name == "ranking":
344
- for i, data in enumerate(load_ranking(filepath, split)):
345
- yield f"ranking-{split}-{i}", data
346
-
347
- else:
348
- raise ValueError("Invalid config name")