-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeatures.py
158 lines (138 loc) · 5.17 KB
/
features.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
#!/usr/bin/env python
#-*- coding: utf-8 -*-
__author__ = "Martin Kondra"
import csv
import StanfordFromNLTK
import json
import nltk
import pickle
import nltk.classify
from sklearn.svm import LinearSVC
from sklearn import cross_validation
from random import shuffle
data = pickle.load(open("TaggedCorpus.p", "rb")) #lista de diccionarios (sentences)
def order(relation):
if relation[2]<relation[3]: #CARGO está antes de PER
return 0
else:
return 1
def distance(relation):
distance = abs((relation[2])-(relation[3]))
return distance
def bag(relation):
d = []
empresa = relation[0]
sentence_index = relation[1]
CARGO_index = int(relation[2])
PER_index = int(relation[3])
sentence = data[empresa]['sentence']
min_boundarie = min(CARGO_index, PER_index)
max_boundarie = max(CARGO_index, PER_index)
for tup in sentence[min_boundarie+1:max_boundarie]:
token = tup[0]
d.append(token)
d = tuple(d)
return d
def bag_of_entities(relation):
d = []
empresa = relation[0]
sentence_index = relation[1]
CARGO_index = int(relation[2])
PER_index = int(relation[3])
sentence = data[empresa]['sentence']
min_boundarie = min(CARGO_index, PER_index)
max_boundarie = max(CARGO_index, PER_index)
for tup in sentence[min_boundarie+1:max_boundarie]:
token = tup[1]
d.append(token)
d = tuple(d)
return d
def cargo_ner(relation):
empresa = relation[0]
sentence_index = relation[1]
CARGO_index = relation[2]
PER_index = relation[3]
sentence = data[empresa]['sentence']
entitie = sentence[CARGO_index][1]
return entitie
def people_ner(relation):
empresa = relation[0]
sentence_index = relation[1]
PER_index = relation[3]
sentence = data[empresa]['sentence']
entitie = sentence[PER_index][1]
return entitie
def entitie_in_bag(relation):
entities = [u'PERS', 'CARGO']
empresa = relation[0]
sentence_index = relation[1]
CARGO_index = int(relation[2])
PER_index = int(relation[3])
sentence = data[empresa]['sentence']
min_boundarie = min(CARGO_index, PER_index)
max_boundarie = max(CARGO_index, PER_index)
for tup in sentence[min_boundarie + 1:max_boundarie]:
if tup[1] in entities:
return True
return False
test_relation = ['ACO GROUP\xc2\xa0S.A.', '15', '0', '6', 'True', '', '', '', '']
test_relation = [test_relation[0], int(test_relation[1]), int(test_relation[2]), int(test_relation[3]), test_relation[4]]
#print bag(relation)
#print order(test_relation)
#print tokens_between(relation)
#print entities(relation)
def feature_extractor(relation):
features = {"people_ner": people_ner(relation),
"cargo_ner": cargo_ner(relation),
"order": order(relation),
"bag": bag(relation),
"bag_of_entities": bag_of_entities(relation),
"entitie_in_bag": entitie_in_bag(relation),
"distance": distance(relation)
}
return features
#print feature_extractor(relation)
#CROSS VALIDATION
def cross_validate():
print 'Cross validating...'
accuracy = []
training_set = feature_sets
cv = cross_validation.KFold(len(training_set), n_folds=10)
print cv
for traincv, testcv in cv:
classifier = maxEnt.train(training_set[traincv[0]:traincv[len(traincv)-1]])
single_accuracy = nltk.classify.util.accuracy(classifier, training_set[testcv[0]:testcv[len(testcv)-1]])
accuracy.append(single_accuracy)
for item in accuracy:
print 'accuracy', item
total = sum(accuracy)/len(accuracy)
print "Average", total
if __name__ == "__main__":
with open('anotacion.csv', 'rb') as csvfile:
relations = csv.reader(csvfile, delimiter=',', quotechar='|')
relations = [((relation[0], int(relation[1]), int(relation[2]), int(relation[3])), relation[4]) for relation in
relations]
# print relations[1]
#for item in relations:
# print item
# r=item[0]
# a = feature_extractor(r)
shuffle(relations)
feature_sets = [(feature_extractor(r), t) for (r, t) in relations]
size = int(len(feature_sets) * 0.1) # split train_set (90%) and test_set (10$)
# train_set, test_set, devtest_set = featuresets[size:-(size)], featuresets[:size], featuresets[-(size):]
train_set = feature_sets[size:]
test_set = feature_sets[:size]
print 'Training...'
#naiveBayes = nltk.NaiveBayesClassifier.train(train_set)
#print naiveBayes.show_most_informative_features(50)
#decisionTree = nltk.DecisionTreeClassifier.train(train_set)
#print decisionTree.pseudocode()
maxEnt = nltk.MaxentClassifier.train(train_set,algorithm="iis")
print maxEnt.show_most_informative_features(50)
print 'Dumping pickle...'
pickle.dump(maxEnt, open("maxEntClassifier.p", "wb"))
#print('naiveBayesAccuracy: {:4.2f}'.format(nltk.classify.accuracy(naiveBayes, test_set)))
#print('decisionTreeAccuracy: {:4.2f}'.format(nltk.classify.accuracy(decisionTree, test_set)))
print('maxEntAccuracy: {:4.2f}'.format(nltk.classify.accuracy(maxEnt, test_set)))
cross_validate()