-
Notifications
You must be signed in to change notification settings - Fork 3
/
annotator.py
87 lines (77 loc) · 2.65 KB
/
annotator.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
import requests
import os
import io
import traceback
def annotate_subject(source_url, ann_id, source_dir, subject_col_id, alpha, top_k=3, logger=None):
"""
:param source_dir: the directory of the source file
:param subject_col_id: the index of the subject column
:param top_k: the number of suggested classes to return
:param alpha: the alpha parameter to balance converage and specificity
:return: list of string (classes)
"""
data = {
'ann_source': ann_id,
'col_id': subject_col_id,
'alpha': alpha,
'k': top_k
}
files = [
('source', (
source_dir.split(os.sep)[-1],
open(source_dir, encoding='utf-8'),
'text/plain'))
]
response = requests.request("POST", source_url+'/subject', data=data, files=files)
if response.status_code == 200:
print("-- entities: ")
print(response.json())
logger.debug("annotate_subject> entities ")
logger.debug(str(response.json()))
entities = response.json()['entities']
else:
print("-- ERROR: status code: "+str(response.status_code))
entities = []
try:
print(response.json())
logger.debug(str(response.json()))
except:
print("No JSON")
logger.debug("annotate_subject> No JSON")
traceback.print_exc()
return entities
def annotate_property(source_url, ann_id, source_dir, subject_col_id, class_uri=None, top_k=3, logger=None):
"""
:param source_dir: the directory of the source file
:param subject_col_id: the index of the subject column
:param top_k: the number of suggested classes to return
:return: list of string (classes)
"""
data = {
'ann_source': ann_id,
'subject_col_id': subject_col_id,
'k': top_k,
}
files = {
'source': (source_dir.split(os.sep)[-1], open(source_dir, encoding='utf-8'), 'text/plain')
}
print("Sending the request")
response = requests.post(source_url+'/property', data=data, files=files)
if response.status_code == 200:
print("properties: ")
print(response.json())
logger.debug("annotate_property> properties ")
logger.debug(str(response.json()))
pairs = response.json()['cols_properties']
else:
pairs = []
try:
print(response.json())
logger.debug(str(response.json()))
except Exception as e:
print("No JSON")
print("Exception: "+str(e))
logger.debug("annotate_property> No JSON")
traceback.print_exc()
print("after the request")
return pairs