-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_utils.py
112 lines (106 loc) · 3.32 KB
/
data_utils.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
from gensim.parsing import PorterStemmer
from nltk import sent_tokenize, word_tokenize
from gensim.models import KeyedVectors
def words_to_glove(sentences, model):
filename = 'glove.6B.100d.txt' # add [voc_size dimension_size]. 400000 50/100/200/300 to the top file,
model = KeyedVectors.load_word2vec_format(filename, binary=False)
words = []
sents = []
for sentence in sentences:
for word in sentence:
try:
word_vector =model[word]
#print(word_vector)
#word_vector = model['a']
except:
print('Found unknown word: {0}'.format(word))
word_vector = model['a']
#print(word_vector)
#words.append(word_vector)
#sents.append((words))
sents.append((word_vector)) #
words = []
return sents
def words_to_word2vec(sentences, model):
words = []
sents = []
for sentence in sentences:
for word in sentence:
try:
word_vector =model[word]
#print(word_vector)
#word_vector = model['a']
except:
print('Found unknown word: {0}'.format(word))
word_vector = model['a']
#print(word_vector)
#words.append(word_vector)
#sents.append((words))
sents.append((word_vector)) #
words = []
return sents
def sentences_to_lists(filename):
filename = open(filename, 'r')
sentences = []
labels = []
sentence = []
label = []
for line in filename:
if (('-DOCSTART-') in line):
continue
tokens = line.split(' ')
#print(tokens[0])
if len(tokens) > 3:
sentence.append(tokens[0])
label.append(tokens[3].replace('\n', ''))
if len(tokens) < 3 and len(sentence) != 0:
sentences.append(sentence)
labels.append(label)
sentence = []
label = []
return sentences, labels
def labels_to_integer(labels):
tags = []
tag = []
for label in labels:
for word in label:
if word == 'PER':
#tag.append(0.0)
tags.append(0.0)#
elif word == 'ORG':
#tag.append(1.0)
tags.append(1.0)#
elif word == 'LOC':
#tag.append(2.0)
tags.append(2.0)#
elif word == 'MISC':
#tag.append(3.0)
tags.append(3.0)#
elif word == 'O':
#tag.append(8.0)
tags.append(4.0)#
else:
print('Catching different tag: {0}'.format(word))
#tags.append(tag)
tag = []
return tags
def integer_to_label(word):
if word == 0.0:
#tag.append(0.0)
return "PER"
elif word == 1.0:
#tag.append(1.0)
return 'ORG'
elif word == 2.0:
#tag.append(2.0)
return 'LOC'
elif word == 3.0:
#tag.append(3.0)
return 'MISC'
elif word == 4.0:
#tag.append(8.0)
return 'O'
else:
print('Catching different tag: {0}'.format(word))
#tags.append(tag)
return 'Error'