-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
71 lines (54 loc) · 1.7 KB
/
app.py
File metadata and controls
71 lines (54 loc) · 1.7 KB
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
import hug
import spacy
model = spacy.load('en_core_web_sm')
@hug.get("/")
def hello():
return "ok"
@hug.post("/parseTree")
def parse_tree(text: str, merge: bool=False):
doc = model(text)
if merge:
for np in list(doc.noun_chunks):
np.merge(tag=np.root.tag_, lemma=np.lemma_,
ent_type=np.root.ent_type_)
return [parse_token_(sent.root) for sent in doc.sents]
def parse_token_(token):
obj = {
'word': token.text,
'lemma': token.lemma_,
# 'norm': token.norm_,
# 'shape': token.shape_,
# 'lower': token.lower_,
'entityType': token.ent_type_ or None,
'POS': token.pos_,
'tag': token.tag_,
'relation': token.dep_,
'index': token.i,
'isStopWord': token.is_stop,
# 'isOOV': token.is_oov,
'children': [parse_token_(child) for child in token.children],
}
if token.is_punct:
obj['punct'] = {}
if token.is_quote:
obj['punct']['type'] = 'quote'
elif token.is_bracket:
obj['punct']['type'] = 'bracket'
else:
obj['punct']['type'] = None
if token.is_left_punct:
if token.is_right_punct:
obj['punct']["direction"] = "both"
else:
obj['punct']["direction"] = "left"
elif token.is_right_punct:
obj['punct']["direction"] = "right"
else:
obj['punct']["direction"] = None
return obj
if __name__ == '__main__':
import waitress
from hug_middleware_cors import CORSMiddleware
app = hug.API(__name__)
app.http.add_middleware(CORSMiddleware(app))
waitress.serve(__hug_wsgi__, port=8080)