-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpreprocess.py
33 lines (28 loc) · 868 Bytes
/
preprocess.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
import spacy
from spacy.tokens import DocBin
import pickle
nlp = spacy.blank("en")
# load data
training_data = pickle.load(open("data/trainData.pickle", "rb"))
testing_data = pickle.load(open("data/testData.pickle", "rb"))
# the DocBin will store the example documents
db = DocBin()
for text, annotations in training_data:
doc = nlp(text)
ents = []
for start, end, label in annotations["entities"]:
span = doc.char_span(start, end, label=label)
ents.append(span)
doc.ents = ents
db.add(doc)
db.to_disk("./data/train.spacy")
db_test = DocBin()
for text, annotations in testing_data:
doc = nlp(text)
ents = []
for start, end, label in annotations["entities"]:
span = doc.char_span(start, end, label=label)
ents.append(span)
doc.ents = ents
db_test.add(doc)
db_test.to_disk("./data/test.spacy")