2018-07-25 23:21:31 +03:00
|
|
|
import re
|
2017-04-07 14:05:12 +03:00
|
|
|
|
2020-01-29 19:44:25 +03:00
|
|
|
from ...gold import Example
|
|
|
|
from ...gold import iob_to_biluo, spans_from_biluo_tags, biluo_tags_from_offsets
|
|
|
|
from ...language import Language
|
|
|
|
from ...tokens import Doc, Token
|
|
|
|
from .conll_ner2json import n_sents_info
|
|
|
|
from wasabi import Printer
|
2017-04-07 14:05:12 +03:00
|
|
|
|
2018-07-25 23:21:31 +03:00
|
|
|
|
2019-12-21 20:55:03 +03:00
|
|
|
def conllu2json(
|
2020-02-18 16:47:23 +03:00
|
|
|
input_data,
|
|
|
|
n_sents=10,
|
|
|
|
append_morphology=False,
|
|
|
|
lang=None,
|
|
|
|
ner_map=None,
|
|
|
|
merge_subtokens=False,
|
|
|
|
no_print=False,
|
|
|
|
**_
|
2019-12-21 20:55:03 +03:00
|
|
|
):
|
2017-04-15 12:59:21 +03:00
|
|
|
"""
|
|
|
|
Convert conllu files into JSON format for use with train cli.
|
2020-01-29 19:44:25 +03:00
|
|
|
append_morphology parameter enables appending morphology to tags, which is
|
2017-04-07 14:05:12 +03:00
|
|
|
useful for languages such as Spanish, where UD tags are not so rich.
|
|
|
|
|
2018-07-25 23:21:31 +03:00
|
|
|
Extract NER tags if available and convert them so that they follow
|
|
|
|
BILUO and the Wikipedia scheme
|
|
|
|
"""
|
2020-05-20 19:48:51 +03:00
|
|
|
MISC_NER_PATTERN = "^((?:name|NE)=)?([BILU])-([A-Z_]+)|O$"
|
2020-01-29 19:44:25 +03:00
|
|
|
msg = Printer(no_print=no_print)
|
|
|
|
n_sents_info(msg, n_sents)
|
2017-04-07 14:05:12 +03:00
|
|
|
docs = []
|
2019-11-26 18:10:08 +03:00
|
|
|
raw = ""
|
2017-04-07 14:05:12 +03:00
|
|
|
sentences = []
|
2020-02-18 16:47:23 +03:00
|
|
|
conll_data = read_conllx(
|
|
|
|
input_data,
|
|
|
|
append_morphology=append_morphology,
|
|
|
|
ner_tag_pattern=MISC_NER_PATTERN,
|
|
|
|
ner_map=ner_map,
|
|
|
|
merge_subtokens=merge_subtokens,
|
|
|
|
)
|
2020-05-20 19:48:51 +03:00
|
|
|
has_ner_tags = has_ner(input_data, MISC_NER_PATTERN)
|
2019-11-11 19:35:27 +03:00
|
|
|
for i, example in enumerate(conll_data):
|
2019-11-26 18:10:08 +03:00
|
|
|
raw += example.text
|
2019-12-21 20:55:03 +03:00
|
|
|
sentences.append(
|
|
|
|
generate_sentence(
|
|
|
|
example.token_annotation,
|
|
|
|
has_ner_tags,
|
|
|
|
MISC_NER_PATTERN,
|
|
|
|
ner_map=ner_map,
|
|
|
|
)
|
|
|
|
)
|
2019-11-25 18:03:28 +03:00
|
|
|
# Real-sized documents could be extracted using the comments on the
|
|
|
|
# conllu document
|
|
|
|
if len(sentences) % n_sents == 0:
|
2020-01-29 19:44:25 +03:00
|
|
|
doc = create_json_doc(raw, sentences, i)
|
2019-11-25 18:03:28 +03:00
|
|
|
docs.append(doc)
|
2019-11-26 18:10:08 +03:00
|
|
|
raw = ""
|
2019-11-25 18:03:28 +03:00
|
|
|
sentences = []
|
2019-11-26 18:05:17 +03:00
|
|
|
if sentences:
|
2020-01-29 19:44:25 +03:00
|
|
|
doc = create_json_doc(raw, sentences, i)
|
2019-11-26 18:05:17 +03:00
|
|
|
docs.append(doc)
|
2018-11-30 22:16:14 +03:00
|
|
|
return docs
|
2017-04-07 14:05:12 +03:00
|
|
|
|
|
|
|
|
2020-01-29 19:44:25 +03:00
|
|
|
def has_ner(input_data, ner_tag_pattern):
|
2018-11-30 22:16:14 +03:00
|
|
|
"""
|
2020-05-20 19:48:51 +03:00
|
|
|
Check the MISC column for NER tags.
|
2018-07-25 23:21:31 +03:00
|
|
|
"""
|
2020-01-29 19:44:25 +03:00
|
|
|
for sent in input_data.strip().split("\n\n"):
|
|
|
|
lines = sent.strip().split("\n")
|
|
|
|
if lines:
|
|
|
|
while lines[0].startswith("#"):
|
|
|
|
lines.pop(0)
|
2020-05-20 19:48:51 +03:00
|
|
|
for line in lines:
|
|
|
|
parts = line.split("\t")
|
2020-01-29 19:44:25 +03:00
|
|
|
id_, word, lemma, pos, tag, morph, head, dep, _1, misc = parts
|
2020-05-20 19:48:51 +03:00
|
|
|
for misc_part in misc.split("|"):
|
|
|
|
if re.match(ner_tag_pattern, misc_part):
|
|
|
|
return True
|
|
|
|
return False
|
2018-07-25 23:21:31 +03:00
|
|
|
|
2018-11-30 22:16:14 +03:00
|
|
|
|
2020-02-18 16:47:23 +03:00
|
|
|
def read_conllx(
|
|
|
|
input_data,
|
|
|
|
append_morphology=False,
|
|
|
|
merge_subtokens=False,
|
|
|
|
ner_tag_pattern="",
|
|
|
|
ner_map=None,
|
|
|
|
):
|
2020-01-29 19:44:25 +03:00
|
|
|
""" Yield examples, one for each sentence """
|
2020-02-18 16:47:23 +03:00
|
|
|
vocab = Language.Defaults.create_vocab() # need vocab to make a minimal Doc
|
2018-11-30 22:16:14 +03:00
|
|
|
for sent in input_data.strip().split("\n\n"):
|
|
|
|
lines = sent.strip().split("\n")
|
2017-04-07 14:05:12 +03:00
|
|
|
if lines:
|
2018-11-30 22:16:14 +03:00
|
|
|
while lines[0].startswith("#"):
|
2017-04-07 14:05:12 +03:00
|
|
|
lines.pop(0)
|
2020-02-18 16:47:23 +03:00
|
|
|
example = example_from_conllu_sentence(
|
|
|
|
vocab,
|
|
|
|
lines,
|
|
|
|
ner_tag_pattern,
|
|
|
|
merge_subtokens=merge_subtokens,
|
|
|
|
append_morphology=append_morphology,
|
|
|
|
ner_map=ner_map,
|
|
|
|
)
|
2019-11-11 19:35:27 +03:00
|
|
|
yield example
|
2017-04-07 14:05:12 +03:00
|
|
|
|
2018-11-30 22:16:14 +03:00
|
|
|
|
2020-01-29 19:44:25 +03:00
|
|
|
def get_entities(lines, tag_pattern, ner_map=None):
|
|
|
|
"""Find entities in the MISC column according to the pattern and map to
|
|
|
|
final entity type with `ner_map` if mapping present. Entity tag is 'O' if
|
|
|
|
the pattern is not matched.
|
|
|
|
|
|
|
|
lines (unicode): CONLL-U lines for one sentences
|
|
|
|
tag_pattern (unicode): Regex pattern for entity tag
|
|
|
|
ner_map (dict): Map old NER tag names to new ones, '' maps to O.
|
|
|
|
RETURNS (list): List of BILUO entity tags
|
2018-07-25 23:21:31 +03:00
|
|
|
"""
|
2020-01-29 19:44:25 +03:00
|
|
|
miscs = []
|
|
|
|
for line in lines:
|
|
|
|
parts = line.split("\t")
|
|
|
|
id_, word, lemma, pos, tag, morph, head, dep, _1, misc = parts
|
|
|
|
if "-" in id_ or "." in id_:
|
|
|
|
continue
|
|
|
|
miscs.append(misc)
|
|
|
|
|
|
|
|
iob = []
|
|
|
|
for misc in miscs:
|
|
|
|
iob_tag = "O"
|
2020-05-20 19:48:51 +03:00
|
|
|
for misc_part in misc.split("|"):
|
|
|
|
tag_match = re.match(tag_pattern, misc_part)
|
|
|
|
if tag_match:
|
|
|
|
prefix = tag_match.group(2)
|
|
|
|
suffix = tag_match.group(3)
|
|
|
|
if prefix and suffix:
|
|
|
|
iob_tag = prefix + "-" + suffix
|
|
|
|
if ner_map:
|
|
|
|
suffix = ner_map.get(suffix, suffix)
|
|
|
|
if suffix == "":
|
|
|
|
iob_tag = "O"
|
|
|
|
else:
|
|
|
|
iob_tag = prefix + "-" + suffix
|
|
|
|
break
|
2020-01-29 19:44:25 +03:00
|
|
|
iob.append(iob_tag)
|
|
|
|
return iob_to_biluo(iob)
|
2017-04-07 14:05:12 +03:00
|
|
|
|
2018-11-30 22:16:14 +03:00
|
|
|
|
2019-12-21 20:55:03 +03:00
|
|
|
def generate_sentence(token_annotation, has_ner_tags, tag_pattern, ner_map=None):
|
2017-04-07 14:05:12 +03:00
|
|
|
sentence = {}
|
|
|
|
tokens = []
|
2020-01-29 19:44:25 +03:00
|
|
|
for i, id_ in enumerate(token_annotation.ids):
|
2017-04-07 14:05:12 +03:00
|
|
|
token = {}
|
2020-01-29 19:44:25 +03:00
|
|
|
token["id"] = id_
|
|
|
|
token["orth"] = token_annotation.get_word(i)
|
|
|
|
token["tag"] = token_annotation.get_tag(i)
|
|
|
|
token["pos"] = token_annotation.get_pos(i)
|
|
|
|
token["lemma"] = token_annotation.get_lemma(i)
|
|
|
|
token["morph"] = token_annotation.get_morph(i)
|
|
|
|
token["head"] = token_annotation.get_head(i) - id_
|
|
|
|
token["dep"] = token_annotation.get_dep(i)
|
2018-07-25 23:21:31 +03:00
|
|
|
if has_ner_tags:
|
2020-01-29 19:44:25 +03:00
|
|
|
token["ner"] = token_annotation.get_entity(i)
|
2017-04-07 14:05:12 +03:00
|
|
|
tokens.append(token)
|
|
|
|
sentence["tokens"] = tokens
|
|
|
|
return sentence
|
|
|
|
|
|
|
|
|
2020-01-29 19:44:25 +03:00
|
|
|
def create_json_doc(raw, sentences, id_):
|
2017-04-07 14:05:12 +03:00
|
|
|
doc = {}
|
|
|
|
paragraph = {}
|
2020-01-29 19:44:25 +03:00
|
|
|
doc["id"] = id_
|
2017-04-07 14:05:12 +03:00
|
|
|
doc["paragraphs"] = []
|
2019-11-26 18:10:08 +03:00
|
|
|
paragraph["raw"] = raw.strip()
|
2017-04-07 14:05:12 +03:00
|
|
|
paragraph["sentences"] = sentences
|
|
|
|
doc["paragraphs"].append(paragraph)
|
|
|
|
return doc
|
2020-01-29 19:44:25 +03:00
|
|
|
|
|
|
|
|
2020-02-18 16:47:23 +03:00
|
|
|
def example_from_conllu_sentence(
|
|
|
|
vocab,
|
|
|
|
lines,
|
|
|
|
ner_tag_pattern,
|
|
|
|
merge_subtokens=False,
|
|
|
|
append_morphology=False,
|
|
|
|
ner_map=None,
|
|
|
|
):
|
2020-01-29 19:44:25 +03:00
|
|
|
"""Create an Example from the lines for one CoNLL-U sentence, merging
|
|
|
|
subtokens and appending morphology to tags if required.
|
|
|
|
|
|
|
|
lines (unicode): The non-comment lines for a CoNLL-U sentence
|
|
|
|
ner_tag_pattern (unicode): The regex pattern for matching NER in MISC col
|
|
|
|
RETURNS (Example): An example containing the annotation
|
|
|
|
"""
|
|
|
|
# create a Doc with each subtoken as its own token
|
|
|
|
# if merging subtokens, each subtoken orth is the merged subtoken form
|
|
|
|
if not Token.has_extension("merged_orth"):
|
|
|
|
Token.set_extension("merged_orth", default="")
|
|
|
|
if not Token.has_extension("merged_lemma"):
|
|
|
|
Token.set_extension("merged_lemma", default="")
|
|
|
|
if not Token.has_extension("merged_morph"):
|
|
|
|
Token.set_extension("merged_morph", default="")
|
|
|
|
if not Token.has_extension("merged_spaceafter"):
|
|
|
|
Token.set_extension("merged_spaceafter", default="")
|
|
|
|
words, spaces, tags, poses, morphs, lemmas = [], [], [], [], [], []
|
|
|
|
heads, deps = [], []
|
|
|
|
subtok_word = ""
|
|
|
|
in_subtok = False
|
|
|
|
for i in range(len(lines)):
|
|
|
|
line = lines[i]
|
|
|
|
parts = line.split("\t")
|
|
|
|
id_, word, lemma, pos, tag, morph, head, dep, _1, misc = parts
|
|
|
|
if "." in id_:
|
|
|
|
continue
|
|
|
|
if "-" in id_:
|
|
|
|
in_subtok = True
|
|
|
|
if "-" in id_:
|
|
|
|
in_subtok = True
|
|
|
|
subtok_word = word
|
|
|
|
subtok_start, subtok_end = id_.split("-")
|
|
|
|
subtok_spaceafter = "SpaceAfter=No" not in misc
|
|
|
|
continue
|
|
|
|
if merge_subtokens and in_subtok:
|
|
|
|
words.append(subtok_word)
|
|
|
|
else:
|
|
|
|
words.append(word)
|
|
|
|
if in_subtok:
|
|
|
|
if id_ == subtok_end:
|
|
|
|
spaces.append(subtok_spaceafter)
|
|
|
|
else:
|
|
|
|
spaces.append(False)
|
|
|
|
elif "SpaceAfter=No" in misc:
|
|
|
|
spaces.append(False)
|
|
|
|
else:
|
|
|
|
spaces.append(True)
|
|
|
|
if in_subtok and id_ == subtok_end:
|
|
|
|
subtok_word = ""
|
|
|
|
in_subtok = False
|
|
|
|
id_ = int(id_) - 1
|
2020-02-18 17:17:03 +03:00
|
|
|
head = (int(head) - 1) if head not in ("0", "_") else id_
|
2020-01-29 19:44:25 +03:00
|
|
|
tag = pos if tag == "_" else tag
|
|
|
|
morph = morph if morph != "_" else ""
|
|
|
|
dep = "ROOT" if dep == "root" else dep
|
|
|
|
lemmas.append(lemma)
|
|
|
|
poses.append(pos)
|
|
|
|
tags.append(tag)
|
|
|
|
morphs.append(morph)
|
|
|
|
heads.append(head)
|
|
|
|
deps.append(dep)
|
|
|
|
|
|
|
|
doc = Doc(vocab, words=words, spaces=spaces)
|
|
|
|
for i in range(len(doc)):
|
|
|
|
doc[i].tag_ = tags[i]
|
|
|
|
doc[i].pos_ = poses[i]
|
|
|
|
doc[i].dep_ = deps[i]
|
|
|
|
doc[i].lemma_ = lemmas[i]
|
|
|
|
doc[i].head = doc[heads[i]]
|
|
|
|
doc[i]._.merged_orth = words[i]
|
|
|
|
doc[i]._.merged_morph = morphs[i]
|
|
|
|
doc[i]._.merged_lemma = lemmas[i]
|
|
|
|
doc[i]._.merged_spaceafter = spaces[i]
|
|
|
|
ents = get_entities(lines, ner_tag_pattern, ner_map)
|
|
|
|
doc.ents = spans_from_biluo_tags(doc, ents)
|
|
|
|
doc.is_parsed = True
|
|
|
|
doc.is_tagged = True
|
|
|
|
|
|
|
|
if merge_subtokens:
|
|
|
|
doc = merge_conllu_subtokens(lines, doc)
|
|
|
|
|
|
|
|
# create Example from custom Doc annotation
|
|
|
|
ids, words, tags, heads, deps = [], [], [], [], []
|
|
|
|
pos, lemmas, morphs, spaces = [], [], [], []
|
|
|
|
for i, t in enumerate(doc):
|
|
|
|
ids.append(i)
|
|
|
|
words.append(t._.merged_orth)
|
|
|
|
if append_morphology and t._.merged_morph:
|
|
|
|
tags.append(t.tag_ + "__" + t._.merged_morph)
|
|
|
|
else:
|
|
|
|
tags.append(t.tag_)
|
|
|
|
pos.append(t.pos_)
|
|
|
|
morphs.append(t._.merged_morph)
|
|
|
|
lemmas.append(t._.merged_lemma)
|
|
|
|
heads.append(t.head.i)
|
|
|
|
deps.append(t.dep_)
|
|
|
|
spaces.append(t._.merged_spaceafter)
|
|
|
|
ent_offsets = [(e.start_char, e.end_char, e.label_) for e in doc.ents]
|
|
|
|
ents = biluo_tags_from_offsets(doc, ent_offsets)
|
|
|
|
raw = ""
|
|
|
|
for word, space in zip(words, spaces):
|
|
|
|
raw += word
|
|
|
|
if space:
|
|
|
|
raw += " "
|
|
|
|
example = Example(doc=raw)
|
2020-02-18 16:47:23 +03:00
|
|
|
example.set_token_annotation(
|
|
|
|
ids=ids,
|
|
|
|
words=words,
|
|
|
|
tags=tags,
|
|
|
|
pos=pos,
|
|
|
|
morphs=morphs,
|
|
|
|
lemmas=lemmas,
|
|
|
|
heads=heads,
|
|
|
|
deps=deps,
|
|
|
|
entities=ents,
|
|
|
|
)
|
2020-01-29 19:44:25 +03:00
|
|
|
return example
|
|
|
|
|
|
|
|
|
|
|
|
def merge_conllu_subtokens(lines, doc):
|
|
|
|
# identify and process all subtoken spans to prepare attrs for merging
|
|
|
|
subtok_spans = []
|
|
|
|
for line in lines:
|
|
|
|
parts = line.split("\t")
|
|
|
|
id_, word, lemma, pos, tag, morph, head, dep, _1, misc = parts
|
|
|
|
if "-" in id_:
|
|
|
|
subtok_start, subtok_end = id_.split("-")
|
2020-02-18 16:47:23 +03:00
|
|
|
subtok_span = doc[int(subtok_start) - 1 : int(subtok_end)]
|
2020-01-29 19:44:25 +03:00
|
|
|
subtok_spans.append(subtok_span)
|
|
|
|
# create merged tag, morph, and lemma values
|
|
|
|
tags = []
|
|
|
|
morphs = {}
|
|
|
|
lemmas = []
|
|
|
|
for token in subtok_span:
|
|
|
|
tags.append(token.tag_)
|
|
|
|
lemmas.append(token.lemma_)
|
|
|
|
if token._.merged_morph:
|
|
|
|
for feature in token._.merged_morph.split("|"):
|
|
|
|
field, values = feature.split("=", 1)
|
2020-02-18 16:47:23 +03:00
|
|
|
if field not in morphs:
|
2020-01-29 19:44:25 +03:00
|
|
|
morphs[field] = set()
|
|
|
|
for value in values.split(","):
|
|
|
|
morphs[field].add(value)
|
|
|
|
# create merged features for each morph field
|
|
|
|
for field, values in morphs.items():
|
|
|
|
morphs[field] = field + "=" + ",".join(sorted(values))
|
|
|
|
# set the same attrs on all subtok tokens so that whatever head the
|
|
|
|
# retokenizer chooses, the final attrs are available on that token
|
|
|
|
for token in subtok_span:
|
|
|
|
token._.merged_orth = token.orth_
|
|
|
|
token._.merged_lemma = " ".join(lemmas)
|
|
|
|
token.tag_ = "_".join(tags)
|
|
|
|
token._.merged_morph = "|".join(sorted(morphs.values()))
|
2020-02-18 16:47:23 +03:00
|
|
|
token._.merged_spaceafter = (
|
|
|
|
True if subtok_span[-1].whitespace_ else False
|
|
|
|
)
|
2020-01-29 19:44:25 +03:00
|
|
|
|
|
|
|
with doc.retokenize() as retokenizer:
|
|
|
|
for span in subtok_spans:
|
|
|
|
retokenizer.merge(span)
|
|
|
|
|
|
|
|
return doc
|