This repository was archived by the owner on Nov 13, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathner.py
More file actions
216 lines (185 loc) · 5.42 KB
/
Copy pathner.py
File metadata and controls
216 lines (185 loc) · 5.42 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""
Named Entity Recognition model.
author: Omar Barazanji
dataset:
https://raw.githubusercontent.com/Skuldur/virtual-assistant-tutorial/master/commands/play_commands.json
refs:
https://towardsdatascience.com/train-ner-with-custom-training-data-using-spacy-525ce748fab7
"""
from __future__ import unicode_literals, print_function
import json
import re
import random
from pathlib import Path
import spacy
from spacy.training.example import Example
from tqdm import tqdm
# which model to create
PLAY = 1
LIGHT = 0
TIMER = 0
NUM = 0
NAME = 0
EPOCHS = 100
# def create_load_data():
# load in and organize data
if PLAY:
with open("data/play_commands.json") as f:
json_data = json.load(f)
if LIGHT:
with open("data/light_commands.json") as f:
json_data = json.load(f)
elif TIMER:
with open("data/timer_commands.json") as f:
json_data = json.load(f)
elif NUM:
with open("data/numeric_commands.json") as f:
json_data = json.load(f)
elif NAME:
with open("data/name_commands.json") as f:
json_data = json.load(f)
sentences = []
entities = []
for ndx, data in enumerate(json_data["training_data"]):
if PLAY:
words = data["words"]
labels = data["labels"]
ent_ndx = []
for ndx, x in enumerate(labels):
if "song" in x or "artist" in x or "playlist" in x:
if "song" in x:
tag = "SONG"
ent_ndx.append([ndx, x])
elif "artist" in x:
tag = "ARTIST"
ent_ndx.append([ndx, x])
elif "playlist" in x:
tag = "PLAYLIST"
ent_ndx.append([ndx, x])
if LIGHT:
words = data["words"]
labels = data["labels"]
ent_ndx = []
for ndx, x in enumerate(labels):
if "lightname" in x:
tag = "LIGHTNAME"
ent_ndx.append([ndx, x])
if "brightness" in x:
tag = "BRIGHTNESS"
ent_ndx.append([ndx, x])
if "color" in x:
tag = "COLOR"
ent_ndx.append([ndx, x])
if "command" in x:
tag = "COMMAND"
ent_ndx.append([ndx, x])
elif TIMER:
words = data["words"]
labels = data["labels"]
ent_ndx = []
for ndx, x in enumerate(labels):
if "second" in x or "minute" in x:
if "second" in x:
tag = "SECOND"
ent_ndx.append([ndx, x])
elif "minute" in x:
tag = "MINUTE"
ent_ndx.append([ndx, x])
elif NUM:
words = data["words"]
labels = data["labels"]
ent_ndx = []
for ndx, x in enumerate(labels):
if "numeric" in x:
tag = "NUMERIC"
ent_ndx.append([ndx, x])
if "entity" in x:
tag = "ENTITY"
ent_ndx.append([ndx, x])
elif NAME:
words = data["words"]
labels = data["labels"]
ent_ndx = []
for ndx, x in enumerate(labels):
if "entity" in x:
tag = "ENTITY"
ent_ndx.append([ndx, x])
sentence = ""
for x in words:
sentence += x + " "
if (
"(" in sentence
or ")" in sentence
or "+" in sentence
or "?" in sentence
or "[" in sentence
):
continue
ent = []
curr_ndx = 0
exiting = False
for x in ent_ndx:
word = words[x[0]]
for match in re.finditer(word, sentence):
beg = match.start()
end = match.end()
if end <= curr_ndx:
exiting = True
curr_ndx = end
break
if exiting:
break
ent.append((beg, end, x[1]))
if exiting:
continue
sentences.append(sentence)
entities.append(ent)
TRAIN_DATA = []
for sent, ent in zip(sentences, entities):
ent_dict = dict()
ent_dict["entities"] = ent
TRAIN_DATA.append((sent, ent_dict))
# training time!
model = None
if PLAY:
output_dir = Path("models/ner/play")
elif TIMER:
output_dir = Path("models/ner/timer")
elif NUM:
output_dir = Path("models/ner/numeric")
elif LIGHT:
output_dir = Path("models/ner/light")
elif NAME:
output_dir = Path("models/ner/name")
if model is not None:
nlp = spacy.load(model)
print("Loaded model '%s'" % model)
else:
nlp = spacy.blank("en")
print("Created blank 'en' model")
# set up the pipeline
if "ner" not in nlp.pipe_names:
ner = nlp.add_pipe("ner")
# nlp.add_pipe(ner, last=True)
else:
ner = nlp.get_pipe("ner")
for _, annotations in TRAIN_DATA:
for ent in annotations.get("entities"):
ner.add_label(ent[2])
other_pipes = [pipe for pipe in nlp.pipe_names if pipe != "ner"]
with nlp.disable_pipes(*other_pipes): # only train NER
optimizer = nlp.begin_training()
for itn in range(EPOCHS):
random.shuffle(TRAIN_DATA)
losses = {}
for text, annotations in tqdm(TRAIN_DATA):
doc = nlp.make_doc(text)
example = Example.from_dict(doc, annotations)
nlp.update([example], drop=0.5, sgd=optimizer, losses=losses)
print(losses)
if output_dir is not None:
output_dir = Path(output_dir)
if not output_dir.exists():
output_dir.mkdir()
nlp.to_disk(output_dir)
print("Saved model to", output_dir)