-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
81 lines (73 loc) · 2.1 KB
/
test.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
import numpy as np
import re
import itertools
from collections import Counter
import cPickle as pickle
import os
import csv
#input = ["fear,Every time I imagine that someone I love or I could contact a serious illness, even death", "angry, I am an angry man"]
def clean_string(string):
"""
Tokenization/string cleaning for all datasets except for SST.
"""
string = re.sub(r"[^A-Za-z0-9:(),!?\'\`]", " ", string)
string = re.sub(r" : ", ":", string)
string = re.sub(r"\'s", " \'s", string)
string = re.sub(r"\'ve", " \'ve", string)
string = re.sub(r"n\'t", " n\'t", string)
string = re.sub(r"\'re", " \'re", string)
string = re.sub(r"\'d", " \'d", string)
string = re.sub(r"\'ll", " \'ll", string)
string = re.sub(r",", " , ", string)
string = re.sub(r"!", " ! ", string)
string = re.sub(r"\(", " \( ", string)
string = re.sub(r"\)", " \) ", string)
string = re.sub(r"\?", " \? ", string)
string = re.sub(r"\s{2,}", " ", string)
return string.strip().lower()
file_reader= open('data/Emotion Phrases.csv', "rt")
read = csv.reader(file_reader)
data = []
for row in read :
x_text = [clean_string(sent) for sent in row]
data.append(x_text)
#print(data)
#x_text = [clean_string(sent) for sent in data]
x = [x_text[0] for x_text in data]
y = [x_text[1] for x_text in data]
#print(y)
#print(x)
#print(x)
all_label = dict()
for label in x:
if not label in all_label:
all_label[label] = len(all_label) + 1
#print(all_label)
one_hot = np.identity(len(all_label))
x = [one_hot[ all_label[label]-1 ] for label in x]
x = [l.tolist() for l in x]
x = np.array(x)
print(x)
#print(x[0])
#print(x)
"""
x_text = list(open('data/Emotion Phrases.csv').readlines())
print(x_text)
x_text = [clean_string(sent) for sent in x_text]
y = [s.split(' ')[0].split(',')[0] for s in x_text]
#print(y)
#print(x_text)
#print(read)
"""
#data =
"""
out = clean_string(input)
y = [s.split(' ')[0].split(',')[0] for s in out]
print(y)
x = out.split(" ")[2:]
all_label = dict()
for label in y:
if not label in all_label:
all_label[label] = len(all_label) + 1
print(x)
"""