Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,29 @@ def convert_dataturks_to_spacy(dataturks_JSON_FilePath):
data = json.loads(line)
text = data['content']
entities = []
for annotation in data['annotation']:
#only a single point in text annotation.
point = annotation['points'][0]
labels = annotation['label']
# handle both list of labels or a single label.
if not isinstance(labels, list):
labels = [labels]

for label in labels:
#dataturks indices are both inclusive [start, end] but spacy is not [start, end)
entities.append((point['start'], point['end'] + 1 ,label))

data_annotations = data['annotation']
if data_annotations is not None:
for annotation in data_annotations:
#only a single point in text annotation.
point = annotation['points'][0]
labels = annotation['label']
# handle both list of labels or a single label.
if not isinstance(labels, list):
labels = [labels]

for label in labels:
point_start = point['start']
point_end = point['end']
point_text = point['text']

lstrip_diff = len(point_text) - len(point_text.lstrip())
rstrip_diff = len(point_text) - len(point_text.rstrip())
if lstrip_diff != 0:
point_start = point_start + lstrip_diff
if rstrip_diff != 0:
point_end = point_end - rstrip_diff
entities.append((point_start, point_end + 1 , label))
training_data.append((text, {"entities" : entities}))

return training_data
except Exception as e:
logging.exception("Unable to process " + dataturks_JSON_FilePath + "\n" + "error = " + str(e))
Expand Down