2017-05-19 21:27:51 +03:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-05-26 19:32:34 +03:00
|
|
|
from ...gold import iob_to_biluo
|
2018-12-06 22:37:00 +03:00
|
|
|
from ...util import minibatch
|
2017-05-19 21:27:51 +03:00
|
|
|
|
2018-12-06 17:50:25 +03:00
|
|
|
import re
|
|
|
|
|
2017-05-19 21:27:51 +03:00
|
|
|
|
2018-11-30 22:16:14 +03:00
|
|
|
def iob2json(input_data, n_sents=10, *args, **kwargs):
|
2017-05-19 21:27:51 +03:00
|
|
|
"""
|
|
|
|
Convert IOB files into JSON format for use with train cli.
|
|
|
|
"""
|
2018-11-30 22:16:14 +03:00
|
|
|
docs = []
|
2018-12-06 22:37:00 +03:00
|
|
|
for group in minibatch(docs, n_sents):
|
2018-11-30 22:16:14 +03:00
|
|
|
group = list(group)
|
|
|
|
first = group.pop(0)
|
|
|
|
to_extend = first["paragraphs"][0]["sentences"]
|
|
|
|
for sent in group[1:]:
|
|
|
|
to_extend.extend(sent["paragraphs"][0]["sentences"])
|
|
|
|
docs.append(first)
|
|
|
|
return docs
|
2017-05-19 21:27:51 +03:00
|
|
|
|
|
|
|
|
2017-10-02 17:50:26 +03:00
|
|
|
def read_iob(raw_sents):
|
2017-05-19 21:27:51 +03:00
|
|
|
sentences = []
|
2017-10-02 17:50:26 +03:00
|
|
|
for line in raw_sents:
|
2017-05-19 21:27:51 +03:00
|
|
|
if not line.strip():
|
|
|
|
continue
|
2018-12-18 15:48:10 +03:00
|
|
|
# tokens = [t.split("|") for t in line.split()]
|
|
|
|
tokens = [re.split("[^\w\-]", line.strip())]
|
2017-05-28 16:11:39 +03:00
|
|
|
if len(tokens[0]) == 3:
|
|
|
|
words, pos, iob = zip(*tokens)
|
|
|
|
else:
|
|
|
|
words, iob = zip(*tokens)
|
2018-11-30 22:16:14 +03:00
|
|
|
pos = ["-"] * len(words)
|
2017-05-26 19:32:34 +03:00
|
|
|
biluo = iob_to_biluo(iob)
|
2018-11-30 22:16:14 +03:00
|
|
|
sentences.append(
|
|
|
|
[
|
|
|
|
{"orth": w, "tag": p, "ner": ent}
|
|
|
|
for (w, p, ent) in zip(words, pos, biluo)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
sentences = [{"tokens": sent} for sent in sentences]
|
|
|
|
paragraphs = [{"sentences": [sent]} for sent in sentences]
|
|
|
|
docs = [{"id": 0, "paragraphs": [para]} for para in paragraphs]
|
2017-05-19 21:27:51 +03:00
|
|
|
return docs
|