1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
from __future__ import unicode_literals |
4
|
|
|
from termcolor import colored |
5
|
|
|
import requests |
6
|
|
|
import json |
7
|
|
|
import os |
8
|
|
|
|
9
|
|
|
def is_string(x): |
10
|
|
|
return isinstance(x, ("".__class__, u"".__class__)) |
11
|
|
|
|
12
|
|
|
def post_json(service, json_data): |
13
|
|
|
# POST json to the server API |
14
|
|
|
#response = requests.post(service, json={"text":"{}".format(text)}) |
15
|
|
|
# for older versions of requests, use the call below |
16
|
|
|
#print("SERVICE: {}".format(service)) |
17
|
|
|
response = requests.post(service, |
18
|
|
|
data=json_data, |
19
|
|
|
headers={'content-type': 'application/json; charset=utf-8'}, |
20
|
|
|
timeout=None |
21
|
|
|
) |
22
|
|
|
# response content should be utf-8 |
23
|
|
|
#response.encoding = "utf-8" |
24
|
|
|
content = response.content.decode("utf-8") |
25
|
|
|
#print("CONTENT: {}".format(content)) |
26
|
|
|
return json.loads(content) |
27
|
|
|
|
28
|
|
|
def full_path(p): |
29
|
|
|
""" |
30
|
|
|
Expand a path. Supports "~" shortcut. |
31
|
|
|
""" |
32
|
|
|
return os.path.abspath(os.path.normpath(os.path.expanduser(p))) |
33
|
|
|
|
34
|
|
|
class LabelManager(object): |
35
|
|
|
""" |
36
|
|
|
Keep track of common labels |
37
|
|
|
""" |
38
|
|
|
UNKNOWN = "UNKNOWN" |
39
|
|
|
# the O in IOB notation |
40
|
|
|
O = "O" |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
class OdinError(Exception): |
44
|
|
|
""" |
45
|
|
|
An error encountered while parsing an Odin rule. |
46
|
|
|
""" |
47
|
|
|
|
48
|
|
|
def __init__(self, rules, message): |
49
|
|
|
self.rules = rules |
50
|
|
|
self.message = message |
51
|
|
|
|
52
|
|
|
def __str__(self): |
53
|
|
|
cn = colored(self.__class__.__name__, color="red", attrs=["bold"]) |
54
|
|
|
return "{}: {}\n{}".format(cn, self.message, self.rules) |
55
|
|
|
|