Repository navigation
Expand file tree
/
Copy pathquery.py
More file actions
118 lines (100 loc) · 3.09 KB
/
Copy pathquery.py
File metadata and controls
118 lines (100 loc) · 3.09 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
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
import spacy
from graphene import ObjectType, Field, String
from schema import NLP, Doc, Token, Span, Cat, Meta
MODELS = {}
def get_model(name: str):
if name not in MODELS:
MODELS[name] = spacy.load(name)
return MODELS[name]
def get_token(token: spacy.tokens.Token) -> Token:
return Token(
text=token.text,
text_with_ws=token.text_with_ws,
orth=token.orth,
i=token.i,
idx=token.idx,
head_i=token.head.i,
lower=token.lower,
lower_=token.lower_,
shape=token.shape,
shape_=token.shape,
lemma=token.lemma,
lemma_=token.lemma_,
norm=token.norm,
norm_=token.norm_,
pos=token.pos,
pos_=token.pos_,
tag=token.tag,
tag_=token.tag_,
dep=token.dep,
dep_=token.dep_,
ent_type=token.ent_type,
ent_type_=token.ent_type_,
ent_iob=token.ent_iob,
ent_iob_=token.ent_iob,
is_alpha=token.is_alpha,
is_ascii=token.is_ascii,
is_digit=token.is_digit,
is_lower=token.is_lower,
is_upper=token.is_upper,
is_title=token.is_title,
is_punct=token.is_punct,
is_left_punct=token.is_left_punct,
is_right_punct=token.is_right_punct,
is_space=token.is_space,
is_bracket=token.is_bracket,
is_quote=token.is_quote,
is_stop=token.is_stop,
like_num=token.like_num,
like_url=token.like_url,
like_email=token.like_email
)
def get_span(span: spacy.tokens.Span) -> Span:
return Span(
text=span.text,
text_with_ws=span.text_with_ws,
start=span.start,
end=span.end,
start_char=span.start_char,
end_char=span.end_char,
label=span.label,
label_=span.label_
)
def get_cat(label: str, score: float) -> Cat:
return Cat(label=label, score=score)
def get_meta(meta: dict) -> Meta:
return Meta(
lang=meta.get('lang'),
name=meta.get('name'),
license=meta.get('license'),
author=meta.get('author'),
url=meta.get('url'),
email=meta.get('email'),
description=meta.get('description'),
pipeline=meta.get('pipeline'),
sources=meta.get('sources')
)
def get_doc(doc):
tokens = [get_token(token) for token in doc]
ents = [get_span(ent) for ent in doc.ents]
sents = [get_span(sent) for sent in doc.sents]
cats = [get_cats(label, score) for label, score in doc.cats.items()]
return Doc(
text=doc.text,
text_with_ws=doc.text_with_ws,
tokens=tokens,
ents=ents,
sents=sents,
cats=cats
)
class Query(ObjectType):
nlp = Field(NLP,
text=String(required=True, description="The text to process"),
model=String(required=True, description="The name of the model to use"),
description="The nlp object used to process a text"
)
def resolve_nlp(self, info, text, model):
_nlp = get_model(model)
meta = get_meta(_nlp.meta)
doc = get_doc(_nlp(text))
return NLP(doc=doc, meta=meta)