-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprepare_data.py
126 lines (96 loc) · 3.53 KB
/
prepare_data.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
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
119
120
121
122
123
124
125
126
import json
import os
import random
import re
from sre_parse import WHITESPACE
from tkinter.filedialog import test
import spacy
from pyvi import ViUtils
from sklearn.model_selection import train_test_split
PREPROCESS_CLASSES = [
"PERSON_NAME", "ADDRESS", "EDUCATION", "GPA", "SKILL", "EXPERIENCE_LEVEL",
"JOB_TITLE", "DATE_BIRTH", "MAJOR", "MARIAGE_STATUS", 'GENDER',
'ORGANIZATION', 'LOCATION'
]
CORPUS_PATH = 'data/resume_annotations'
SAVE_PATH = 'data/spacy_ner_resume_corpus'
#Token to check if their are blank text (span) in corpus
WHITESPACE_TOKEN = re.compile('\s+')
#Get list corpus name
list_corpus_files = os.listdir(CORPUS_PATH)
#Train test spilt
train_corpus_files, val_corpus_files = train_test_split(list_corpus_files,
test_size=0.20,
random_state=0)
print("Shuffle list corpus done")
#Read data, convert corpus dtype from json spacy format to spacy training format
docBin = spacy.tokens.DocBin()
nlp = spacy.blank('en')
print("Start loading and converting corpus set")
#Train corpus
for file_name in train_corpus_files:
file_path = f"{CORPUS_PATH}/{file_name}"
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
if len(data['annotations']) == 0:
continue
annotations = data['annotations'][0]
text = annotations[0]
doc = nlp(text)
entities = annotations[1]['entities']
ents_list = []
for start_idx, end_idx, label in entities:
span = doc.char_span(start_idx,
end_idx,
label=label,
alignment_mode='contract')
#Skip blank span
if WHITESPACE_TOKEN.match(str(span).strip()) or len(
str(span)) == 0:
print("skip")
continue
if span and label in PREPROCESS_CLASSES:
ents_list.append(span)
ent_list = spacy.util.filter_spans(ents_list)
doc.ents = ents_list
docBin.add(doc)
#Create saving folder
if not os.path.exists(SAVE_PATH):
os.makedirs(SAVE_PATH)
#Save train docBin
docBin.to_disk(f'{SAVE_PATH}/train.spacy')
print(
f"Loading and converting training set done. Save at {SAVE_PATH}/train.spacy"
)
#Test corpus
docBin = spacy.tokens.DocBin()
for file_name in val_corpus_files:
file_path = f"{CORPUS_PATH}/{file_name}"
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
if len(data['annotations']) == 0:
continue
annotations = data['annotations'][0]
text = annotations[0]
doc = nlp(text)
entities = annotations[1]['entities']
ents_list = []
for start_idx, end_idx, label in entities:
span = doc.char_span(start_idx,
end_idx,
label=label,
alignment_mode='contract')
#Skip blank span
if WHITESPACE_TOKEN.match(str(span).strip()) or len(
str(span)) == 0:
print("skip")
continue
if span and label in PREPROCESS_CLASSES:
ents_list.append(span)
ent_list = spacy.util.filter_spans(ents_list)
doc.ents = ents_list
docBin.add(doc)
#Save test set
docBin.to_disk(f'{SAVE_PATH}/val.spacy')
print(
f"Loading and converting training set done. Save at {SAVE_PATH}/val.spacy")