-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathdata_helper.py
More file actions
46 lines (37 loc) · 1.46 KB
/
Copy pathdata_helper.py
File metadata and controls
46 lines (37 loc) · 1.46 KB
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
import re
import logging
import numpy as np
import pandas as pd
def clean_str(s):
s = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", s)
s = re.sub(r"\'s", " \'s", s)
s = re.sub(r"\'ve", " \'ve", s)
s = re.sub(r"n\'t", " n\'t", s)
s = re.sub(r"\'re", " \'re", s)
s = re.sub(r"\'d", " \'d", s)
s = re.sub(r"\'ll", " \'ll", s)
s = re.sub(r",", " , ", s)
s = re.sub(r"!", " ! ", s)
s = re.sub(r"\(", r" \( ", s)
s = re.sub(r"\)", r" \) ", s)
s = re.sub(r"\?", r" \? ", s)
s = re.sub(r"\s{2,}", " ", s)
s = re.sub(r'\S*(x{2,}|X{2,})\S*', "xxx", s)
s = re.sub(r'[^\x00-\x7F]+', "", s)
return s.strip().lower()
def load_data_and_labels(filename):
df = pd.read_csv(filename, compression='zip', dtype={'consumer_complaint_narrative': object})
selected = ['product', 'consumer_complaint_narrative']
non_selected = list(set(df.columns) - set(selected))
df = df.drop(columns=non_selected)
df = df.dropna(axis=0, how='any', subset=selected)
labels = sorted(list(set(df[selected[0]].tolist())))
one_hot = np.zeros((len(labels), len(labels)), int)
np.fill_diagonal(one_hot, 1)
label_dict = dict(zip(labels, one_hot))
x_raw = df[selected[1]].apply(lambda x: clean_str(x)).tolist()
y_raw = df[selected[0]].apply(lambda y: label_dict[y]).tolist()
return x_raw, y_raw, df, labels
if __name__ == '__main__':
input_file = './data/consumer_complaints.csv.zip'
load_data_and_labels(input_file)