forked from explosion/spacy-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exc_03_12.py
38 lines (28 loc) · 1.19 KB
/
exc_03_12.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
import json
import spacy
from spacy.language import Language
from spacy.tokens import Span
from spacy.matcher import PhraseMatcher
with open("exercises/en/countries.json", encoding="utf8") as f:
COUNTRIES = json.loads(f.read())
with open("exercises/en/capitals.json", encoding="utf8") as f:
CAPITALS = json.loads(f.read())
nlp = spacy.blank("en")
matcher = PhraseMatcher(nlp.vocab)
matcher.add("COUNTRY", list(nlp.pipe(COUNTRIES)))
@Language.component("countries_component")
def countries_component_function(doc):
# Create an entity Span with the label "GPE" for all matches
matches = matcher(doc)
doc.ents = [____(____, ____, ____, label=____) for match_id, start, end in matches]
return doc
# Add the component to the pipeline
____.____(____)
print(nlp.pipe_names)
# Getter that looks up the span text in the dictionary of country capitals
get_capital = lambda span: CAPITALS.get(span.text)
# Register the Span extension attribute "capital" with the getter get_capital
____.____(____, ____)
# Process the text and print the entity text, label and capital attributes
doc = nlp("Czech Republic may help Slovakia protect its airspace")
print([(____, ____, ____) for ent in doc.ents])