-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.py
130 lines (111 loc) · 4.63 KB
/
preprocess.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
import numpy as np
import pandas as pd
import re
from jieba import posseg
import jieba
from tokenizer import segment
# from seq2seq_tf2.bin.main import BASE_DIR
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
REMOVE_WORDS = ['|', '[', ']', '语音', '图片', ' ']
def read_stopwords(path):
lines = set()
with open(path, mode='r', encoding='utf-8') as f:
for line in f:
line = line.strip()
lines.add(line)
return lines
def remove_words(words_list):
words_list = [word for word in words_list if word not in REMOVE_WORDS]
return words_list
def parse_data(train_path, test_path):
train_df = pd.read_csv(train_path, encoding='utf-8')
train_df.dropna(subset=['Report'], how='any', inplace=True)
train_df.fillna('', inplace=True)
train_x = train_df.Question.str.cat(train_df.Dialogue)
print('train_x is ', len(train_x))
train_x = train_x.apply(preprocess_sentence)
print('train_x is ', len(train_x))
train_y = train_df.Report
print('train_y is ', len(train_y))
train_y = train_y.apply(preprocess_sentence)
print('train_y is ', len(train_y))
# if 'Report' in train_df.columns:
# train_y = train_df.Report
# print('train_y is ', len(train_y))
test_df = pd.read_csv(test_path, encoding='utf-8')
test_df.fillna('', inplace=True)
test_x = test_df.Question.str.cat(test_df.Dialogue)
test_x = test_x.apply(preprocess_sentence)
print('test_x is ', len(test_x))
test_y = []
# print('train_x is ', len(train_x))
# print('train_y is ', len(train_y))
# print('test_x is ', len(test_x))
train_x.to_csv('{}/datasets/train_set.seg_x.txt'.format(BASE_DIR), index=None, header=False)
train_y.to_csv('{}/datasets/train_set.seg_y.txt'.format(BASE_DIR), index=None, header=False)
test_x.to_csv('{}/datasets/test_set.seg_x.txt'.format(BASE_DIR), index=None, header=False)
def save_data(data_1, data_2, data_3, data_path_1, data_path_2, data_path_3, stop_words_path=''):
stopwords = read_stopwords(stop_words_path)
with open(data_path_1, 'w', encoding='utf-8') as f1:
count_1 = 0
for line in data_1:
# print(line)
if isinstance(line, str):
seg_list = segment(line.strip(), cut_type='word')
seg_list = remove_words(seg_list)
# seg_words = []
# for j in seg_list:
# if j in stopwords:
# continue
# seg_words.append(j)
if len(seg_list) > 0:
seg_line = ' '.join(seg_list)
f1.write('%s' % seg_line)
f1.write('\n')
count_1 += 1
print('train_x_length is ', count_1)
with open(data_path_2, 'w', encoding='utf-8') as f2:
count_2 = 0
for line in data_2:
if isinstance(line, str):
seg_list = segment(line.strip(), cut_type='word')
seg_list = remove_words(seg_list)
# seg_words = []
# for j in seg_list:
# if j in stopwords:
# continue
# seg_words.append(j)
# if len(seg_list) > 0:
seg_line = ' '.join(seg_list)
f2.write('%s' % seg_line)
f2.write('\n')
count_2 += 1
print('train_y_length is ', count_2)
with open(data_path_3, 'w', encoding='utf-8') as f3:
count_3 = 0
for line in data_3:
if isinstance(line, str):
seg_list = segment(line.strip(), cut_type='word')
seg_list = remove_words(seg_list)
if len(seg_list) > 0:
seg_line = ' '.join(seg_list)
f3.write('%s' % seg_line)
f3.write('\n')
count_3 += 1
print('test_y_length is ', count_3)
def preprocess_sentence(sentence):
seg_list = segment(sentence.strip(), cut_type='word')
seg_list = remove_words(seg_list)
seg_line = ' '.join(seg_list)
return seg_line
if __name__ == '__main__':
parse_data('{}/datasets/AutoMaster_TrainSet.csv'.format(BASE_DIR),
'{}/datasets/AutoMaster_TestSet.csv'.format(BASE_DIR))
# save_data(train_list_src,
# train_list_trg,
# test_list_src,
# '{}/datasets/train_set.seg_x.txt'.format(BASE_DIR),
# '{}/datasets/train_set.seg_y.txt'.format(BASE_DIR),
# '{}/datasets/test_set.seg_x.txt'.format(BASE_DIR),
# stop_words_path='{}/datasets/stop_words.txt'.format(BASE_DIR))