2017-05-19 18:27:51 +00:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2019-08-29 10:04:01 +00:00
|
|
|
from wasabi import Printer
|
2018-12-18 12:58:30 +00:00
|
|
|
|
2017-05-26 16:32:34 +00:00
|
|
|
from ...gold import iob_to_biluo
|
2019-05-11 19:12:09 +00:00
|
|
|
from ...util import minibatch
|
2019-08-29 10:04:01 +00:00
|
|
|
from .conll_ner2json import n_sents_info
|
2017-05-19 18:27:51 +00:00
|
|
|
|
|
|
|
|
2019-10-18 16:12:59 +00:00
|
|
|
def iob2json(input_data, n_sents=10, no_print=False, *args, **kwargs):
|
2017-05-19 18:27:51 +00:00
|
|
|
"""
|
2019-08-29 10:04:01 +00:00
|
|
|
Convert IOB files with one sentence per line and tags separated with '|'
|
|
|
|
into JSON format for use with train cli. IOB and IOB2 are accepted.
|
|
|
|
|
|
|
|
Sample formats:
|
|
|
|
|
|
|
|
I|O like|O London|I-GPE and|O New|B-GPE York|I-GPE City|I-GPE .|O
|
|
|
|
I|O like|O London|B-GPE and|O New|B-GPE York|I-GPE City|I-GPE .|O
|
|
|
|
I|PRP|O like|VBP|O London|NNP|I-GPE and|CC|O New|NNP|B-GPE York|NNP|I-GPE City|NNP|I-GPE .|.|O
|
|
|
|
I|PRP|O like|VBP|O London|NNP|B-GPE and|CC|O New|NNP|B-GPE York|NNP|I-GPE City|NNP|I-GPE .|.|O
|
2017-05-19 18:27:51 +00:00
|
|
|
"""
|
2019-10-18 16:12:59 +00:00
|
|
|
msg = Printer(no_print=no_print)
|
2019-08-29 10:04:01 +00:00
|
|
|
docs = read_iob(input_data.split("\n"))
|
|
|
|
if n_sents > 0:
|
|
|
|
n_sents_info(msg, n_sents)
|
|
|
|
docs = merge_sentences(docs, n_sents)
|
2018-11-30 19:16:14 +00:00
|
|
|
return docs
|
2017-05-19 18:27:51 +00:00
|
|
|
|
|
|
|
|
2017-10-02 14:50:26 +00:00
|
|
|
def read_iob(raw_sents):
|
2017-05-19 18:27:51 +00:00
|
|
|
sentences = []
|
2017-10-02 14:50:26 +00:00
|
|
|
for line in raw_sents:
|
2017-05-19 18:27:51 +00:00
|
|
|
if not line.strip():
|
|
|
|
continue
|
2019-08-31 11:39:06 +00:00
|
|
|
tokens = [t.split("|") for t in line.split()]
|
2017-05-28 13:11:39 +00:00
|
|
|
if len(tokens[0]) == 3:
|
|
|
|
words, pos, iob = zip(*tokens)
|
2019-01-16 12:44:16 +00:00
|
|
|
elif len(tokens[0]) == 2:
|
2017-05-28 13:11:39 +00:00
|
|
|
words, iob = zip(*tokens)
|
2018-11-30 19:16:14 +00:00
|
|
|
pos = ["-"] * len(words)
|
2019-01-16 12:44:16 +00:00
|
|
|
else:
|
2019-02-07 19:54:07 +00:00
|
|
|
raise ValueError(
|
2019-08-29 10:04:01 +00:00
|
|
|
"The sentence-per-line IOB/IOB2 file is not formatted correctly. Try checking whitespace and delimiters. See https://spacy.io/api/cli#convert"
|
2019-02-07 19:54:07 +00:00
|
|
|
)
|
2017-05-26 16:32:34 +00:00
|
|
|
biluo = iob_to_biluo(iob)
|
2018-11-30 19:16:14 +00: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]
|
2019-08-29 10:04:01 +00:00
|
|
|
docs = [{"id": i, "paragraphs": [para]} for i, para in enumerate(paragraphs)]
|
2017-05-19 18:27:51 +00:00
|
|
|
return docs
|
2019-05-11 17:15:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def merge_sentences(docs, n_sents):
|
|
|
|
merged = []
|
2019-05-11 19:12:09 +00:00
|
|
|
for group in minibatch(docs, size=n_sents):
|
2019-05-11 17:15:26 +00:00
|
|
|
group = list(group)
|
|
|
|
first = group.pop(0)
|
|
|
|
to_extend = first["paragraphs"][0]["sentences"]
|
2019-08-29 10:04:01 +00:00
|
|
|
for sent in group:
|
2019-05-11 17:15:26 +00:00
|
|
|
to_extend.extend(sent["paragraphs"][0]["sentences"])
|
|
|
|
merged.append(first)
|
|
|
|
return merged
|