spaCy/spacy/cli/converters/iob2json.py

49 lines
1.6 KiB
Python
Raw Normal View History

2017-05-19 18:27:51 +00:00
# coding: utf8
from __future__ import unicode_literals
from cytoolz import partition_all, concat
2017-05-19 18:27:51 +00:00
from ...compat import json_dumps, path2str
from ...util import prints
2017-05-26 16:32:34 +00:00
from ...gold import iob_to_biluo
2017-05-19 18:27:51 +00:00
def iob2json(input_path, output_path, n_sents=10, *a, **k):
"""
Convert IOB files into JSON format for use with train cli.
"""
2017-05-28 13:11:39 +00:00
with input_path.open('r', encoding='utf8') as file_:
if n_sents:
lines = [' '.join(para) for para in partition_all(n_sents, file_)]
else:
lines = file_
sentences = read_iob(lines)
2017-05-19 18:27:51 +00:00
output_filename = input_path.parts[-1].replace(".iob", ".json")
output_file = output_path / output_filename
with output_file.open('w', encoding='utf-8') as f:
f.write(json_dumps(sentences))
prints("Created %d documents" % len(sentences),
2017-05-19 18:27:51 +00:00
title="Generated output file %s" % path2str(output_file))
def read_iob(raw_sents):
2017-05-19 18:27:51 +00:00
sentences = []
for line in raw_sents:
2017-05-19 18:27:51 +00:00
if not line.strip():
continue
2017-05-28 13:11:39 +00:00
tokens = [t.split('|') for t in line.split()]
if len(tokens[0]) == 3:
words, pos, iob = zip(*tokens)
else:
words, iob = zip(*tokens)
pos = ['-'] * len(words)
2017-05-26 16:32:34 +00:00
biluo = iob_to_biluo(iob)
2017-05-19 18:27:51 +00:00
sentences.append([
{'orth': w, 'tag': p, 'ner': ent}
2017-05-26 16:32:34 +00:00
for (w, p, ent) in zip(words, pos, biluo)
2017-05-19 18:27:51 +00:00
])
sentences = [{'tokens': sent} for sent in sentences]
paragraphs = [{'sentences': [sent]} for sent in sentences]
docs = [{'id': 0, 'paragraphs': [para]} for para in paragraphs]
return docs