|
1
|
|
|
#!/usr/bin/env python |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
from .utils import post_json |
|
4
|
|
|
from .ds import Document |
|
5
|
|
|
import re |
|
6
|
|
|
import json |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class OdinAPI(object): |
|
10
|
|
|
""" |
|
11
|
|
|
API for performing sentiment analysis |
|
12
|
|
|
""" |
|
13
|
|
|
|
|
14
|
|
|
validator = re.compile("^(https?|ftp):.+?\.?ya?ml$") |
|
15
|
|
|
|
|
16
|
|
|
def __init__(self, address): |
|
17
|
|
|
self._service = "{}/odin/extract".format(address) |
|
18
|
|
|
|
|
19
|
|
|
def _extract(self, json_data): |
|
20
|
|
|
try: |
|
21
|
|
|
mentions = [Mention.load_from_JSON(m) for m in post_json(self._service, json_data)] |
|
22
|
|
|
return mentions |
|
23
|
|
|
except Exception as e: |
|
24
|
|
|
print(e) |
|
25
|
|
|
return None |
|
26
|
|
|
|
|
27
|
|
|
@staticmethod |
|
28
|
|
|
def valid_rule_url(url): |
|
29
|
|
|
return True if OdinAPI.validator.match(url) else False |
|
30
|
|
|
|
|
31
|
|
|
def extract_from_text(self, text, rules): |
|
32
|
|
|
""" |
|
33
|
|
|
Sends text to the server with rules for IE |
|
34
|
|
|
Returns a list of Mentions on None |
|
35
|
|
|
""" |
|
36
|
|
|
if OdinAPI.valid_rule_url(rules): |
|
37
|
|
|
# this is actually a URL to a yaml file |
|
38
|
|
|
url = rules |
|
39
|
|
|
container = TextWithURL(text, url) |
|
40
|
|
|
else: |
|
41
|
|
|
container = TextWithRules(text, rules) |
|
42
|
|
|
return self._extract(container.to_JSON()) |
|
43
|
|
|
|
|
44
|
|
|
def extract_from_document(self, doc, rules): |
|
45
|
|
|
""" |
|
46
|
|
|
Sends a Document to the server with rules for IE |
|
47
|
|
|
Returns a list of Mentions or None |
|
48
|
|
|
""" |
|
49
|
|
|
if OdinAPI.valid_rule_url(rules): |
|
50
|
|
|
# this is actually a URL to a yaml file |
|
51
|
|
|
url = rules |
|
52
|
|
|
container = DocumentWithURL(doc, rules) |
|
53
|
|
|
else: |
|
54
|
|
|
container = DocumentWithRules(doc, rules) |
|
55
|
|
|
return self._extract(container.to_JSON()) |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
class TextWithRules(object): |
|
59
|
|
|
|
|
60
|
|
|
def __init__(self, text, rules): |
|
61
|
|
|
self.text = text |
|
62
|
|
|
self.rules = rules |
|
63
|
|
|
|
|
64
|
|
|
def to_JSON_dict(self): |
|
65
|
|
|
jdict = dict() |
|
66
|
|
|
jdict["text"] = self.text |
|
67
|
|
|
jdict["rules"] = self.rules |
|
68
|
|
|
return jdict |
|
69
|
|
|
|
|
70
|
|
|
def to_JSON(self): |
|
71
|
|
|
return json.dumps(self.to_JSON_dict(), sort_keys=True, indent=4) |
|
72
|
|
|
|
|
73
|
|
|
class TextWithURL(object): |
|
74
|
|
|
|
|
75
|
|
|
def __init__(self, text, url): |
|
76
|
|
|
self.text = text |
|
77
|
|
|
# TODO: throw exception if url is invalid |
|
78
|
|
|
self.url = url |
|
79
|
|
|
|
|
80
|
|
|
def to_JSON_dict(self): |
|
81
|
|
|
jdict = dict() |
|
82
|
|
|
jdict["text"] = self.text |
|
83
|
|
|
jdict["url"] = self.url |
|
84
|
|
|
return jdict |
|
85
|
|
|
|
|
86
|
|
|
def to_JSON(self): |
|
87
|
|
|
return json.dumps(self.to_JSON_dict(), sort_keys=True, indent=4) |
|
88
|
|
|
|
|
89
|
|
View Code Duplication |
class DocumentWithRules(object): |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
def __init__(self, document, rules): |
|
92
|
|
|
# TODO: throw exception if isinstance(document, Document) is False |
|
93
|
|
|
self.document = document |
|
94
|
|
|
self.rules = rules |
|
95
|
|
|
|
|
96
|
|
|
def to_JSON_dict(self): |
|
97
|
|
|
jdict = dict() |
|
98
|
|
|
jdict["document"] = self.document.to_JSON_dict() |
|
99
|
|
|
jdict["rules"] = self.rules |
|
100
|
|
|
return jdict |
|
101
|
|
|
|
|
102
|
|
|
def to_JSON(self): |
|
103
|
|
|
return json.dumps(self.to_JSON_dict(), sort_keys=True, indent=4) |
|
104
|
|
|
|
|
105
|
|
View Code Duplication |
class DocumentWithURL(object): |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
def __init__(self, document, url): |
|
108
|
|
|
# TODO: throw exception if isinstance(document, Document) is False |
|
109
|
|
|
self.document = document |
|
110
|
|
|
# TODO: throw exception if url is invalid |
|
111
|
|
|
self.url = url |
|
112
|
|
|
|
|
113
|
|
|
def to_JSON_dict(self): |
|
114
|
|
|
jdict = dict() |
|
115
|
|
|
jdict["document"] = self.document.to_JSON_dict() |
|
116
|
|
|
jdict["url"] = self.url |
|
117
|
|
|
return jdict |
|
118
|
|
|
|
|
119
|
|
|
def to_JSON(self): |
|
120
|
|
|
return json.dumps(self.to_JSON_dict(), sort_keys=True, indent=4) |
|
121
|
|
|
|
|
122
|
|
|
class Mention(object): |
|
123
|
|
|
|
|
124
|
|
|
def __init__(self, |
|
125
|
|
|
label, |
|
126
|
|
|
start, |
|
127
|
|
|
end, |
|
128
|
|
|
sentence, |
|
129
|
|
|
document, |
|
130
|
|
|
foundBy, |
|
131
|
|
|
labels=None, |
|
132
|
|
|
trigger=None, |
|
133
|
|
|
arguments=None, |
|
134
|
|
|
keep=True): |
|
135
|
|
|
|
|
136
|
|
|
self.label = label |
|
137
|
|
|
self.labels = labels if labels else [self.label] |
|
138
|
|
|
self.start = start |
|
139
|
|
|
self.end = end |
|
140
|
|
|
self.sentence = sentence |
|
141
|
|
|
self.document = document |
|
142
|
|
|
self.trigger = trigger |
|
143
|
|
|
self.arguments = arguments |
|
144
|
|
|
self.keep = keep |
|
145
|
|
|
self.foundBy = foundBy |
|
146
|
|
|
# other |
|
147
|
|
|
self.sentenceObj = self.document.sentences[self.sentence] |
|
148
|
|
|
self.text = " ".join(self.sentenceObj.words[self.start:self.end]) |
|
149
|
|
|
|
|
150
|
|
|
def __str__(self): |
|
151
|
|
|
return self.text |
|
152
|
|
|
|
|
153
|
|
|
def to_JSON_dict(self): |
|
154
|
|
|
m = dict() |
|
155
|
|
|
m["label"] = self.label |
|
156
|
|
|
m["labels"] = self.labels |
|
157
|
|
|
m["start"] = self.start |
|
158
|
|
|
m["end"] = self.label |
|
159
|
|
|
m["sentence"] = self.sentence |
|
160
|
|
|
m["document"] = self.document.to_JSON_dict() |
|
161
|
|
|
# do we have a trigger? |
|
162
|
|
|
if self.trigger: |
|
163
|
|
|
m["trigger"] = self.trigger |
|
164
|
|
|
m["arguments"] = self.arguments_to_JSON_dict() |
|
165
|
|
|
m["keep"] = self.keep |
|
166
|
|
|
m["foundBy"] = self.foundBy |
|
167
|
|
|
return m |
|
168
|
|
|
|
|
169
|
|
|
def to_JSON(self): |
|
170
|
|
|
return json.dumps(self.to_JSON_dict(), sort_keys=True, indent=4) |
|
171
|
|
|
|
|
172
|
|
|
def arguments_to_JSON_dict(self): |
|
173
|
|
|
return dict((role, [a.to_JSON_dict() for a in args]) for (role, args) in self.arguments) |
|
174
|
|
|
|
|
175
|
|
|
@staticmethod |
|
176
|
|
|
def load_from_JSON(jdict): |
|
177
|
|
|
sentences = [] |
|
178
|
|
|
kwargs = { |
|
179
|
|
|
"label": jdict["label"], |
|
180
|
|
|
"labels": jdict["labels"], |
|
181
|
|
|
"start": jdict["start"], |
|
182
|
|
|
"end": jdict["end"], |
|
183
|
|
|
"sentence": jdict["sentence"], |
|
184
|
|
|
"document": Document.load_from_JSON(jdict["document"]), |
|
185
|
|
|
"trigger": jdict.get("trigger", None), |
|
186
|
|
|
"arguments": jdict.get("arguments", dict()), |
|
187
|
|
|
"keep": jdict.get("keep", True), |
|
188
|
|
|
"foundBy": jdict["foundBy"] |
|
189
|
|
|
} |
|
190
|
|
|
return Mention(**kwargs) |
|
191
|
|
|
|