-
-
Notifications
You must be signed in to change notification settings - Fork 440
/
corpus.py
291 lines (238 loc) · 9.71 KB
/
corpus.py
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
# encoding: utf-8
# author: BrikerMan
# contact: eliyar917@gmail.com
# blog: https://eliyar.biz
# file: corpus.py
# time: 12:38 下午
import os
from typing import List
from typing import Tuple
import numpy as np
import pandas as pd
from tensorflow.keras.utils import get_file
from kashgari import macros as K
from kashgari import utils
from kashgari.logger import logger
from kashgari.tokenizers.base_tokenizer import Tokenizer
from kashgari.tokenizers.bert_tokenizer import BertTokenizer
CORPUS_PATH = os.path.join(K.DATA_PATH, 'corpus')
class DataReader:
@staticmethod
def read_conll_format_file(file_path: str,
text_index: int = 0,
label_index: int = 1) -> Tuple[List[List[str]], List[List[str]]]:
"""
Read conll format data_file
Args:
file_path: path of target file
text_index: index of text data, default 0
label_index: index of label data, default 1
Returns:
"""
x_data, y_data = [], []
with open(file_path, 'r', encoding='utf-8') as f:
lines = f.read().splitlines()
x: List[str] = []
y: List[str] = []
for line in lines:
rows = line.split(' ')
if len(rows) == 1:
x_data.append(x)
y_data.append(y)
x = []
y = []
else:
x.append(rows[text_index])
y.append(rows[label_index])
return x_data, y_data
class ChineseDailyNerCorpus:
"""
Chinese Daily New New Corpus
https://github.com/zjy-ucas/ChineseNER/
Example:
>>> from kashgari.corpus import ChineseDailyNerCorpus
>>> train_x, train_y = ChineseDailyNerCorpus.load_data('train')
>>> test_x, test_y = ChineseDailyNerCorpus.load_data('test')
>>> valid_x, valid_y = ChineseDailyNerCorpus.load_data('valid')
>>> print(train_x)
[['海', '钓', '比', '赛', '地', '点', '在', '厦', '门', ...], ...]
>>> print(train_y)
[['O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-LOC', 'I-LOC', ...], ...]
"""
__corpus_name__ = 'china-people-daily-ner-corpus'
__zip_file__name = 'http://s3.bmio.net/kashgari/china-people-daily-ner-corpus.tar.gz'
@classmethod
def load_data(cls,
subset_name: str = 'train',
shuffle: bool = True) -> Tuple[List[List[str]], List[List[str]]]:
"""
Load dataset as sequence labeling format, char level tokenized
Args:
subset_name: {train, test, valid}
shuffle: should shuffle or not, default True.
Returns:
dataset_features and dataset labels
"""
corpus_path = get_file(cls.__corpus_name__,
cls.__zip_file__name,
cache_dir=K.DATA_PATH,
untar=True)
if subset_name == 'train':
file_path = os.path.join(corpus_path, 'example.train')
elif subset_name == 'test':
file_path = os.path.join(corpus_path, 'example.test')
else:
file_path = os.path.join(corpus_path, 'example.dev')
x_data, y_data = DataReader.read_conll_format_file(file_path)
if shuffle:
x_data, y_data = utils.unison_shuffled_copies(x_data, y_data)
logger.debug(f"loaded {len(x_data)} samples from {file_path}. Sample:\n"
f"x[0]: {x_data[0]}\n"
f"y[0]: {y_data[0]}")
return x_data, y_data
class SMP2018ECDTCorpus:
"""
https://worksheets.codalab.org/worksheets/0x27203f932f8341b79841d50ce0fd684f/
This dataset is released by the Evaluation of Chinese Human-Computer Dialogue Technology (SMP2018-ECDT)
task 1 and is provided by the iFLYTEK Corporation, which is a Chinese human-computer dialogue dataset.
Sample::
label query
0 weather 今天东莞天气如何
1 map 从观音桥到重庆市图书馆怎么走
2 cookbook 鸭蛋怎么腌?
3 health 怎么治疗牛皮癣
4 chat 唠什么
Example:
>>> from kashgari.corpus import SMP2018ECDTCorpus
>>> train_x, train_y = SMP2018ECDTCorpus.load_data('train')
>>> test_x, test_y = SMP2018ECDTCorpus.load_data('test')
>>> valid_x, valid_y = SMP2018ECDTCorpus.load_data('valid')
>>> print(train_x)
[['听', '新', '闻', '。'], ['电', '视', '台', '在', '播', '什', '么'], ...]
>>> print(train_y)
['news', 'epg', ...]
"""
__corpus_name__ = 'SMP2018ECDTCorpus'
__zip_file__name = 'http://s3.bmio.net/kashgari/SMP2018ECDTCorpus.tar.gz'
@classmethod
def load_data(cls,
subset_name: str = 'train',
shuffle: bool = True,
cutter: str = 'char') -> Tuple[List[List[str]], List[str]]:
"""
Load dataset as sequence classification format, char level tokenized
Args:
subset_name: {train, test, valid}
shuffle: should shuffle or not, default True.
cutter: sentence cutter, {char, jieba}
Returns:
dataset_features and dataset labels
"""
corpus_path = get_file(cls.__corpus_name__,
cls.__zip_file__name,
cache_dir=K.DATA_PATH,
untar=True)
if cutter not in ['char', 'jieba', 'none']:
raise ValueError('cutter error, please use one onf the {char, jieba}')
df_path = os.path.join(corpus_path, f'{subset_name}.csv')
df = pd.read_csv(df_path)
if cutter == 'jieba':
try:
import jieba
except ModuleNotFoundError:
raise ModuleNotFoundError(
"please install jieba, `$ pip install jieba`")
x_data = [list(jieba.cut(item)) for item in df['query'].to_list()]
elif cutter == 'char':
x_data = [list(item) for item in df['query'].to_list()]
y_data = df['label'].to_list()
if shuffle:
x_data, y_data = utils.unison_shuffled_copies(x_data, y_data)
logger.debug(f"loaded {len(x_data)} samples from {df_path}. Sample:\n"
f"x[0]: {x_data[0]}\n"
f"y[0]: {y_data[0]}")
return x_data, y_data
class JigsawToxicCommentCorpus:
"""
Kaggle Toxic Comment Classification Challenge corpus
You need to download corpus from https://www.kaggle.com/c/jigsaw-toxic-comment-classification-challenge/overview
to a folder. Then init a JigsawToxicCommentCorpus object with `train.csv` path.
Examples:
>>> from kashgari.corpus import JigsawToxicCommentCorpus
>>> corpus = JigsawToxicCommentCorpus('<train.csv file-path>')
>>> train_x, train_y = corpus.load_data('train')
>>> test_x, test_y = corpus.load_data('test')
>>> print(train_x)
[['Please', 'stop', 'being', 'a', 'penis—', 'and', 'Grow', 'Up', 'Regards-'], ...]
>>> print(train_y)
[['obscene', 'insult'], ...]
"""
def __init__(self,
corpus_train_csv_path: str,
sample_count: int = None,
tokenizer: Tokenizer = None) -> None:
self.file_path = corpus_train_csv_path
self.train_ids = []
self.test_ids = []
self.valid_ids = []
self.tokenizer: Tokenizer
if tokenizer is None:
self.tokenizer = BertTokenizer()
else:
self.tokenizer = tokenizer
if sample_count is None:
df = pd.read_csv(self.file_path)
sample_count = len(df)
del df
self.sample_count = sample_count
for i in range(self.sample_count):
prob = np.random.random()
if prob <= 0.7:
self.train_ids.append(i)
elif prob <= 0.85:
self.test_ids.append(i)
else:
self.valid_ids.append(i)
@classmethod
def _extract_label(cls, row: pd.Series) -> List[str]:
y = []
for label in ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']:
if row[label] == 1:
y.append(label)
return y
def _text_process(self, text: str) -> List[str]:
return self.tokenizer.tokenize(text)
def load_data(self,
subset_name: str = 'train',
shuffle: bool = True) -> Tuple[List[List[str]], List[List[str]]]:
"""
Load dataset as sequence labeling format, char level tokenized
Args:
subset_name: {train, test, valid}
shuffle: should shuffle or not, default True.
Returns:
dataset_features and dataset labels
"""
df = pd.read_csv(self.file_path)
df = df[:self.sample_count]
df['y'] = df.apply(self._extract_label, axis=1)
df['x'] = df['comment_text'].apply(self._text_process)
df = df[['x', 'y']]
if subset_name == 'train':
df = df.loc[self.train_ids]
elif subset_name == 'valid':
df = df.loc[self.valid_ids]
else:
df = df.loc[self.test_ids]
xs, ys = list(df['x'].values), list(df['y'].values)
if shuffle:
xs, ys = utils.unison_shuffled_copies(xs, ys)
return xs, ys
if __name__ == "__main__":
corpus = JigsawToxicCommentCorpus(
'/Users/brikerman/Downloads/jigsaw-toxic-comment-classification-challenge/train.csv')
x, y = corpus.load_data()
for i in x[:20]:
print(i)
for i in y[:20]:
print(i)