2018-12-02 06:26:26 +03:00
|
|
|
"""Train for CONLL 2017 UD treebank evaluation. Takes .conllu files, writes
|
2018-03-27 20:23:02 +03:00
|
|
|
.conllu format for development data, allowing the official scorer to be used.
|
2018-12-02 06:26:26 +03:00
|
|
|
"""
|
2018-03-27 20:23:02 +03:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
import plac
|
|
|
|
import attr
|
|
|
|
from pathlib import Path
|
|
|
|
import re
|
|
|
|
import json
|
2019-12-16 15:12:19 +03:00
|
|
|
import tqdm
|
2018-03-27 20:23:02 +03:00
|
|
|
|
|
|
|
import spacy
|
|
|
|
import spacy.util
|
|
|
|
from spacy.tokens import Token, Doc
|
2019-11-11 19:35:27 +03:00
|
|
|
from spacy.gold import GoldParse, Example
|
2018-03-27 20:23:02 +03:00
|
|
|
from spacy.syntax.nonproj import projectivize
|
2019-11-11 19:35:27 +03:00
|
|
|
from collections import defaultdict
|
2018-03-27 20:23:02 +03:00
|
|
|
from spacy.matcher import Matcher
|
|
|
|
|
|
|
|
import itertools
|
|
|
|
import random
|
|
|
|
import numpy.random
|
|
|
|
|
2019-09-09 17:32:11 +03:00
|
|
|
from bin.ud import conll17_ud_eval
|
2018-03-27 20:23:02 +03:00
|
|
|
|
|
|
|
import spacy.lang.zh
|
|
|
|
import spacy.lang.ja
|
|
|
|
|
|
|
|
spacy.lang.zh.Chinese.Defaults.use_jieba = False
|
|
|
|
spacy.lang.ja.Japanese.Defaults.use_janome = False
|
|
|
|
|
|
|
|
random.seed(0)
|
|
|
|
numpy.random.seed(0)
|
|
|
|
|
2018-12-02 06:26:26 +03:00
|
|
|
|
2019-11-11 19:35:27 +03:00
|
|
|
def minibatch_by_words(examples, size=5000):
|
|
|
|
random.shuffle(examples)
|
2018-03-27 20:23:02 +03:00
|
|
|
if isinstance(size, int):
|
|
|
|
size_ = itertools.repeat(size)
|
|
|
|
else:
|
|
|
|
size_ = size
|
2019-11-11 19:35:27 +03:00
|
|
|
examples = iter(examples)
|
2018-03-27 20:23:02 +03:00
|
|
|
while True:
|
|
|
|
batch_size = next(size_)
|
|
|
|
batch = []
|
|
|
|
while batch_size >= 0:
|
|
|
|
try:
|
2019-11-11 19:35:27 +03:00
|
|
|
example = next(examples)
|
2018-03-27 20:23:02 +03:00
|
|
|
except StopIteration:
|
|
|
|
if batch:
|
|
|
|
yield batch
|
|
|
|
return
|
2019-11-11 19:35:27 +03:00
|
|
|
batch_size -= len(example.doc)
|
|
|
|
batch.append(example)
|
2018-03-27 20:23:02 +03:00
|
|
|
if batch:
|
|
|
|
yield batch
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
2018-12-02 06:26:26 +03:00
|
|
|
|
2018-03-27 20:23:02 +03:00
|
|
|
################
|
|
|
|
# Data reading #
|
|
|
|
################
|
|
|
|
|
2018-12-02 06:26:26 +03:00
|
|
|
space_re = re.compile("\s+")
|
|
|
|
|
2018-03-27 20:23:02 +03:00
|
|
|
|
2018-12-02 06:26:26 +03:00
|
|
|
def split_text(text):
|
|
|
|
return [space_re.sub(" ", par.strip()) for par in text.split("\n\n")]
|
|
|
|
|
|
|
|
|
|
|
|
def read_data(
|
|
|
|
nlp,
|
|
|
|
conllu_file,
|
|
|
|
text_file,
|
|
|
|
raw_text=True,
|
|
|
|
oracle_segments=False,
|
|
|
|
max_doc_length=None,
|
|
|
|
limit=None,
|
|
|
|
):
|
2019-11-11 19:35:27 +03:00
|
|
|
"""Read the CONLLU format into Example objects. If raw_text=True,
|
2018-03-27 20:23:02 +03:00
|
|
|
include Doc objects created using nlp.make_doc and then aligned against
|
|
|
|
the gold-standard sequences. If oracle_segments=True, include Doc objects
|
2018-12-02 06:26:26 +03:00
|
|
|
created from the gold-standard segments. At least one must be True."""
|
2018-03-27 20:23:02 +03:00
|
|
|
if not raw_text and not oracle_segments:
|
|
|
|
raise ValueError("At least one of raw_text or oracle_segments must be True")
|
|
|
|
paragraphs = split_text(text_file.read())
|
|
|
|
conllu = read_conllu(conllu_file)
|
|
|
|
# sd is spacy doc; cd is conllu doc
|
|
|
|
# cs is conllu sent, ct is conllu token
|
|
|
|
docs = []
|
|
|
|
golds = []
|
|
|
|
for doc_id, (text, cd) in enumerate(zip(paragraphs, conllu)):
|
|
|
|
sent_annots = []
|
|
|
|
for cs in cd:
|
|
|
|
sent = defaultdict(list)
|
|
|
|
for id_, word, lemma, pos, tag, morph, head, dep, _, space_after in cs:
|
2018-12-02 06:26:26 +03:00
|
|
|
if "." in id_:
|
2018-03-27 20:23:02 +03:00
|
|
|
continue
|
2018-12-02 06:26:26 +03:00
|
|
|
if "-" in id_:
|
2018-03-27 20:23:02 +03:00
|
|
|
continue
|
2018-12-02 06:26:26 +03:00
|
|
|
id_ = int(id_) - 1
|
|
|
|
head = int(head) - 1 if head != "0" else id_
|
|
|
|
sent["words"].append(word)
|
|
|
|
sent["tags"].append(tag)
|
|
|
|
sent["heads"].append(head)
|
|
|
|
sent["deps"].append("ROOT" if dep == "root" else dep)
|
|
|
|
sent["spaces"].append(space_after == "_")
|
|
|
|
sent["entities"] = ["-"] * len(sent["words"])
|
|
|
|
sent["heads"], sent["deps"] = projectivize(sent["heads"], sent["deps"])
|
2018-03-27 20:23:02 +03:00
|
|
|
if oracle_segments:
|
2018-12-02 06:26:26 +03:00
|
|
|
docs.append(Doc(nlp.vocab, words=sent["words"], spaces=sent["spaces"]))
|
2018-03-27 20:23:02 +03:00
|
|
|
golds.append(GoldParse(docs[-1], **sent))
|
|
|
|
|
|
|
|
sent_annots.append(sent)
|
|
|
|
if raw_text and max_doc_length and len(sent_annots) >= max_doc_length:
|
|
|
|
doc, gold = _make_gold(nlp, None, sent_annots)
|
|
|
|
sent_annots = []
|
|
|
|
docs.append(doc)
|
|
|
|
golds.append(gold)
|
|
|
|
if limit and len(docs) >= limit:
|
2019-11-11 19:35:27 +03:00
|
|
|
return golds_to_gold_data(docs, golds)
|
2018-03-27 20:23:02 +03:00
|
|
|
|
|
|
|
if raw_text and sent_annots:
|
|
|
|
doc, gold = _make_gold(nlp, None, sent_annots)
|
|
|
|
docs.append(doc)
|
|
|
|
golds.append(gold)
|
|
|
|
if limit and len(docs) >= limit:
|
2019-11-11 19:35:27 +03:00
|
|
|
return golds_to_gold_data(docs, golds)
|
|
|
|
return golds_to_gold_data(docs, golds)
|
2018-03-27 20:23:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
def read_conllu(file_):
|
|
|
|
docs = []
|
|
|
|
sent = []
|
|
|
|
doc = []
|
|
|
|
for line in file_:
|
2018-12-02 06:26:26 +03:00
|
|
|
if line.startswith("# newdoc"):
|
2018-03-27 20:23:02 +03:00
|
|
|
if doc:
|
|
|
|
docs.append(doc)
|
|
|
|
doc = []
|
2018-12-02 06:26:26 +03:00
|
|
|
elif line.startswith("#"):
|
2018-03-27 20:23:02 +03:00
|
|
|
continue
|
|
|
|
elif not line.strip():
|
|
|
|
if sent:
|
|
|
|
doc.append(sent)
|
|
|
|
sent = []
|
|
|
|
else:
|
2018-12-02 06:26:26 +03:00
|
|
|
sent.append(list(line.strip().split("\t")))
|
2018-03-27 20:23:02 +03:00
|
|
|
if len(sent[-1]) != 10:
|
|
|
|
print(repr(line))
|
|
|
|
raise ValueError
|
|
|
|
if sent:
|
|
|
|
doc.append(sent)
|
|
|
|
if doc:
|
|
|
|
docs.append(doc)
|
|
|
|
return docs
|
|
|
|
|
|
|
|
|
|
|
|
def _make_gold(nlp, text, sent_annots):
|
|
|
|
# Flatten the conll annotations, and adjust the head indices
|
|
|
|
flat = defaultdict(list)
|
|
|
|
for sent in sent_annots:
|
2018-12-02 06:26:26 +03:00
|
|
|
flat["heads"].extend(len(flat["words"]) + head for head in sent["heads"])
|
|
|
|
for field in ["words", "tags", "deps", "entities", "spaces"]:
|
2018-03-27 20:23:02 +03:00
|
|
|
flat[field].extend(sent[field])
|
|
|
|
# Construct text if necessary
|
2018-12-02 06:26:26 +03:00
|
|
|
assert len(flat["words"]) == len(flat["spaces"])
|
2018-03-27 20:23:02 +03:00
|
|
|
if text is None:
|
2018-12-02 06:26:26 +03:00
|
|
|
text = "".join(
|
|
|
|
word + " " * space for word, space in zip(flat["words"], flat["spaces"])
|
|
|
|
)
|
2018-03-27 20:23:02 +03:00
|
|
|
doc = nlp.make_doc(text)
|
2018-12-02 06:26:26 +03:00
|
|
|
flat.pop("spaces")
|
2018-03-27 20:23:02 +03:00
|
|
|
gold = GoldParse(doc, **flat)
|
|
|
|
return doc, gold
|
|
|
|
|
2018-12-02 06:26:26 +03:00
|
|
|
|
2018-03-27 20:23:02 +03:00
|
|
|
#############################
|
|
|
|
# Data transforms for spaCy #
|
|
|
|
#############################
|
|
|
|
|
2018-12-02 06:26:26 +03:00
|
|
|
|
2019-11-11 19:35:27 +03:00
|
|
|
def golds_to_gold_data(docs, golds):
|
|
|
|
"""Get out the training data format used by begin_training, given the
|
2018-12-02 06:26:26 +03:00
|
|
|
GoldParse objects."""
|
2019-11-11 19:35:27 +03:00
|
|
|
data = []
|
2018-03-27 20:23:02 +03:00
|
|
|
for doc, gold in zip(docs, golds):
|
2019-11-11 19:35:27 +03:00
|
|
|
example = Example(doc=doc)
|
|
|
|
example.add_doc_annotation(cats=gold.cats)
|
|
|
|
token_annotation_dict = gold.orig.to_dict()
|
|
|
|
example.add_token_annotation(**token_annotation_dict)
|
|
|
|
example.goldparse = gold
|
|
|
|
data.append(example)
|
|
|
|
return data
|
2018-03-27 20:23:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
##############
|
|
|
|
# Evaluation #
|
|
|
|
##############
|
|
|
|
|
2018-12-02 06:26:26 +03:00
|
|
|
|
2018-03-27 20:23:02 +03:00
|
|
|
def evaluate(nlp, text_loc, gold_loc, sys_loc, limit=None):
|
2018-12-02 06:26:26 +03:00
|
|
|
with text_loc.open("r", encoding="utf8") as text_file:
|
2018-03-27 20:23:02 +03:00
|
|
|
texts = split_text(text_file.read())
|
|
|
|
docs = list(nlp.pipe(texts))
|
2018-12-02 06:26:26 +03:00
|
|
|
with sys_loc.open("w", encoding="utf8") as out_file:
|
2018-03-27 20:23:02 +03:00
|
|
|
write_conllu(docs, out_file)
|
2018-12-02 06:26:26 +03:00
|
|
|
with gold_loc.open("r", encoding="utf8") as gold_file:
|
2018-03-27 20:23:02 +03:00
|
|
|
gold_ud = conll17_ud_eval.load_conllu(gold_file)
|
2018-12-02 06:26:26 +03:00
|
|
|
with sys_loc.open("r", encoding="utf8") as sys_file:
|
2018-03-27 20:23:02 +03:00
|
|
|
sys_ud = conll17_ud_eval.load_conllu(sys_file)
|
|
|
|
scores = conll17_ud_eval.evaluate(gold_ud, sys_ud)
|
|
|
|
return scores
|
|
|
|
|
|
|
|
|
|
|
|
def write_conllu(docs, file_):
|
|
|
|
merger = Matcher(docs[0].vocab)
|
2018-12-02 06:26:26 +03:00
|
|
|
merger.add("SUBTOK", None, [{"DEP": "subtok", "op": "+"}])
|
2018-03-27 20:23:02 +03:00
|
|
|
for i, doc in enumerate(docs):
|
|
|
|
matches = merger(doc)
|
2018-12-02 06:26:26 +03:00
|
|
|
spans = [doc[start : end + 1] for _, start, end in matches]
|
2018-03-27 20:23:02 +03:00
|
|
|
offsets = [(span.start_char, span.end_char) for span in spans]
|
|
|
|
for start_char, end_char in offsets:
|
|
|
|
doc.merge(start_char, end_char)
|
|
|
|
file_.write("# newdoc id = {i}\n".format(i=i))
|
|
|
|
for j, sent in enumerate(doc.sents):
|
|
|
|
file_.write("# sent_id = {i}.{j}\n".format(i=i, j=j))
|
|
|
|
file_.write("# text = {text}\n".format(text=sent.text))
|
|
|
|
for k, token in enumerate(sent):
|
2018-12-02 06:26:26 +03:00
|
|
|
file_.write(token._.get_conllu_lines(k) + "\n")
|
|
|
|
file_.write("\n")
|
2018-03-27 20:23:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
def print_progress(itn, losses, ud_scores):
|
|
|
|
fields = {
|
2018-12-02 06:26:26 +03:00
|
|
|
"dep_loss": losses.get("parser", 0.0),
|
|
|
|
"tag_loss": losses.get("tagger", 0.0),
|
|
|
|
"words": ud_scores["Words"].f1 * 100,
|
|
|
|
"sents": ud_scores["Sentences"].f1 * 100,
|
|
|
|
"tags": ud_scores["XPOS"].f1 * 100,
|
|
|
|
"uas": ud_scores["UAS"].f1 * 100,
|
|
|
|
"las": ud_scores["LAS"].f1 * 100,
|
2018-03-27 20:23:02 +03:00
|
|
|
}
|
2018-12-02 06:26:26 +03:00
|
|
|
header = ["Epoch", "Loss", "LAS", "UAS", "TAG", "SENT", "WORD"]
|
2018-03-27 20:23:02 +03:00
|
|
|
if itn == 0:
|
2018-12-02 06:26:26 +03:00
|
|
|
print("\t".join(header))
|
|
|
|
tpl = "\t".join(
|
|
|
|
(
|
|
|
|
"{:d}",
|
|
|
|
"{dep_loss:.1f}",
|
|
|
|
"{las:.1f}",
|
|
|
|
"{uas:.1f}",
|
|
|
|
"{tags:.1f}",
|
|
|
|
"{sents:.1f}",
|
|
|
|
"{words:.1f}",
|
|
|
|
)
|
|
|
|
)
|
2018-03-27 20:23:02 +03:00
|
|
|
print(tpl.format(itn, **fields))
|
|
|
|
|
2018-12-02 06:26:26 +03:00
|
|
|
|
|
|
|
# def get_sent_conllu(sent, sent_id):
|
2018-03-27 20:23:02 +03:00
|
|
|
# lines = ["# sent_id = {sent_id}".format(sent_id=sent_id)]
|
|
|
|
|
2018-12-02 06:26:26 +03:00
|
|
|
|
2018-03-27 20:23:02 +03:00
|
|
|
def get_token_conllu(token, i):
|
|
|
|
if token._.begins_fused:
|
|
|
|
n = 1
|
|
|
|
while token.nbor(n)._.inside_fused:
|
|
|
|
n += 1
|
2018-12-02 06:26:26 +03:00
|
|
|
id_ = "%d-%d" % (i, i + n)
|
|
|
|
lines = [id_, token.text, "_", "_", "_", "_", "_", "_", "_", "_"]
|
2018-03-27 20:23:02 +03:00
|
|
|
else:
|
|
|
|
lines = []
|
|
|
|
if token.head.i == token.i:
|
|
|
|
head = 0
|
|
|
|
else:
|
|
|
|
head = i + (token.head.i - token.i) + 1
|
2018-12-02 06:26:26 +03:00
|
|
|
fields = [
|
|
|
|
str(i + 1),
|
|
|
|
token.text,
|
|
|
|
token.lemma_,
|
|
|
|
token.pos_,
|
|
|
|
token.tag_,
|
|
|
|
"_",
|
|
|
|
str(head),
|
|
|
|
token.dep_.lower(),
|
|
|
|
"_",
|
|
|
|
"_",
|
|
|
|
]
|
|
|
|
lines.append("\t".join(fields))
|
|
|
|
return "\n".join(lines)
|
|
|
|
|
|
|
|
|
2018-03-27 20:23:02 +03:00
|
|
|
##################
|
|
|
|
# Initialization #
|
|
|
|
##################
|
|
|
|
|
|
|
|
|
|
|
|
def load_nlp(corpus, config):
|
2018-12-02 06:26:26 +03:00
|
|
|
lang = corpus.split("_")[0]
|
2018-03-27 20:23:02 +03:00
|
|
|
nlp = spacy.blank(lang)
|
|
|
|
if config.vectors:
|
2018-12-02 06:26:26 +03:00
|
|
|
nlp.vocab.from_disk(config.vectors / "vocab")
|
2018-03-27 20:23:02 +03:00
|
|
|
return nlp
|
|
|
|
|
2018-12-02 06:26:26 +03:00
|
|
|
|
2019-11-11 19:35:27 +03:00
|
|
|
def initialize_pipeline(nlp, examples, config):
|
2018-12-02 06:26:26 +03:00
|
|
|
nlp.add_pipe(nlp.create_pipe("parser"))
|
2018-03-27 20:23:02 +03:00
|
|
|
if config.multitask_tag:
|
2018-12-02 06:26:26 +03:00
|
|
|
nlp.parser.add_multitask_objective("tag")
|
2018-03-27 20:23:02 +03:00
|
|
|
if config.multitask_sent:
|
2018-12-02 06:26:26 +03:00
|
|
|
nlp.parser.add_multitask_objective("sent_start")
|
|
|
|
nlp.parser.moves.add_action(2, "subtok")
|
|
|
|
nlp.add_pipe(nlp.create_pipe("tagger"))
|
2019-11-11 19:35:27 +03:00
|
|
|
for ex in examples:
|
|
|
|
for tag in ex.gold.tags:
|
2018-03-27 20:23:02 +03:00
|
|
|
if tag is not None:
|
|
|
|
nlp.tagger.add_label(tag)
|
|
|
|
# Replace labels that didn't make the frequency cutoff
|
|
|
|
actions = set(nlp.parser.labels)
|
2018-12-02 06:26:26 +03:00
|
|
|
label_set = set([act.split("-")[1] for act in actions if "-" in act])
|
2019-11-11 19:35:27 +03:00
|
|
|
for ex in examples:
|
|
|
|
gold = ex.gold
|
2018-03-27 20:23:02 +03:00
|
|
|
for i, label in enumerate(gold.labels):
|
|
|
|
if label is not None and label not in label_set:
|
2018-12-02 06:26:26 +03:00
|
|
|
gold.labels[i] = label.split("||")[0]
|
2019-11-11 19:35:27 +03:00
|
|
|
return nlp.begin_training(lambda: examples)
|
2018-03-27 20:23:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
########################
|
|
|
|
# Command line helpers #
|
|
|
|
########################
|
|
|
|
|
2018-12-02 06:26:26 +03:00
|
|
|
|
2018-03-27 20:23:02 +03:00
|
|
|
@attr.s
|
|
|
|
class Config(object):
|
|
|
|
vectors = attr.ib(default=None)
|
|
|
|
max_doc_length = attr.ib(default=10)
|
|
|
|
multitask_tag = attr.ib(default=True)
|
|
|
|
multitask_sent = attr.ib(default=True)
|
|
|
|
nr_epoch = attr.ib(default=30)
|
|
|
|
batch_size = attr.ib(default=1000)
|
|
|
|
dropout = attr.ib(default=0.2)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def load(cls, loc):
|
2018-12-02 06:26:26 +03:00
|
|
|
with Path(loc).open("r", encoding="utf8") as file_:
|
2018-03-27 20:23:02 +03:00
|
|
|
cfg = json.load(file_)
|
|
|
|
return cls(**cfg)
|
|
|
|
|
|
|
|
|
|
|
|
class Dataset(object):
|
|
|
|
def __init__(self, path, section):
|
|
|
|
self.path = path
|
|
|
|
self.section = section
|
|
|
|
self.conllu = None
|
|
|
|
self.text = None
|
|
|
|
for file_path in self.path.iterdir():
|
|
|
|
name = file_path.parts[-1]
|
2018-12-02 06:26:26 +03:00
|
|
|
if section in name and name.endswith("conllu"):
|
2018-03-27 20:23:02 +03:00
|
|
|
self.conllu = file_path
|
2018-12-02 06:26:26 +03:00
|
|
|
elif section in name and name.endswith("txt"):
|
2018-03-27 20:23:02 +03:00
|
|
|
self.text = file_path
|
|
|
|
if self.conllu is None:
|
|
|
|
msg = "Could not find .txt file in {path} for {section}"
|
|
|
|
raise IOError(msg.format(section=section, path=path))
|
|
|
|
if self.text is None:
|
|
|
|
msg = "Could not find .txt file in {path} for {section}"
|
2018-12-02 06:26:26 +03:00
|
|
|
self.lang = self.conllu.parts[-1].split("-")[0].split("_")[0]
|
2018-03-27 20:23:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
class TreebankPaths(object):
|
|
|
|
def __init__(self, ud_path, treebank, **cfg):
|
2018-12-02 06:26:26 +03:00
|
|
|
self.train = Dataset(ud_path / treebank, "train")
|
|
|
|
self.dev = Dataset(ud_path / treebank, "dev")
|
2018-03-27 20:23:02 +03:00
|
|
|
self.lang = self.train.lang
|
|
|
|
|
|
|
|
|
|
|
|
@plac.annotations(
|
|
|
|
ud_dir=("Path to Universal Dependencies corpus", "positional", None, Path),
|
Generalize handling of tokenizer special cases (#4259)
* Generalize handling of tokenizer special cases
Handle tokenizer special cases more generally by using the Matcher
internally to match special cases after the affix/token_match
tokenization is complete.
Instead of only matching special cases while processing balanced or
nearly balanced prefixes and suffixes, this recognizes special cases in
a wider range of contexts:
* Allows arbitrary numbers of prefixes/affixes around special cases
* Allows special cases separated by infixes
Existing tests/settings that couldn't be preserved as before:
* The emoticon '")' is no longer a supported special case
* The emoticon ':)' in "example:)" is a false positive again
When merged with #4258 (or the relevant cache bugfix), the affix and
token_match properties should be modified to flush and reload all
special cases to use the updated internal tokenization with the Matcher.
* Remove accidentally added test case
* Really remove accidentally added test
* Reload special cases when necessary
Reload special cases when affixes or token_match are modified. Skip
reloading during initialization.
* Update error code number
* Fix offset and whitespace in Matcher special cases
* Fix offset bugs when merging and splitting tokens
* Set final whitespace on final token in inserted special case
* Improve cache flushing in tokenizer
* Separate cache and specials memory (temporarily)
* Flush cache when adding special cases
* Repeated `self._cache = PreshMap()` and `self._specials = PreshMap()`
are necessary due to this bug:
https://github.com/explosion/preshed/issues/21
* Remove reinitialized PreshMaps on cache flush
* Update UD bin scripts
* Update imports for `bin/`
* Add all currently supported languages
* Update subtok merger for new Matcher validation
* Modify blinded check to look at tokens instead of lemmas (for corpora
with tokens but not lemmas like Telugu)
* Use special Matcher only for cases with affixes
* Reinsert specials cache checks during normal tokenization for special
cases as much as possible
* Additionally include specials cache checks while splitting on infixes
* Since the special Matcher needs consistent affix-only tokenization
for the special cases themselves, introduce the argument
`with_special_cases` in order to do tokenization with or without
specials cache checks
* After normal tokenization, postprocess with special cases Matcher for
special cases containing affixes
* Replace PhraseMatcher with Aho-Corasick
Replace PhraseMatcher with the Aho-Corasick algorithm over numpy arrays
of the hash values for the relevant attribute. The implementation is
based on FlashText.
The speed should be similar to the previous PhraseMatcher. It is now
possible to easily remove match IDs and matches don't go missing with
large keyword lists / vocabularies.
Fixes #4308.
* Restore support for pickling
* Fix internal keyword add/remove for numpy arrays
* Add test for #4248, clean up test
* Improve efficiency of special cases handling
* Use PhraseMatcher instead of Matcher
* Improve efficiency of merging/splitting special cases in document
* Process merge/splits in one pass without repeated token shifting
* Merge in place if no splits
* Update error message number
* Remove UD script modifications
Only used for timing/testing, should be a separate PR
* Remove final traces of UD script modifications
* Update UD bin scripts
* Update imports for `bin/`
* Add all currently supported languages
* Update subtok merger for new Matcher validation
* Modify blinded check to look at tokens instead of lemmas (for corpora
with tokens but not lemmas like Telugu)
* Add missing loop for match ID set in search loop
* Remove cruft in matching loop for partial matches
There was a bit of unnecessary code left over from FlashText in the
matching loop to handle partial token matches, which we don't have with
PhraseMatcher.
* Replace dict trie with MapStruct trie
* Fix how match ID hash is stored/added
* Update fix for match ID vocab
* Switch from map_get_unless_missing to map_get
* Switch from numpy array to Token.get_struct_attr
Access token attributes directly in Doc instead of making a copy of the
relevant values in a numpy array.
Add unsatisfactory warning for hash collision with reserved terminal
hash key. (Ideally it would change the reserved terminal hash and redo
the whole trie, but for now, I'm hoping there won't be collisions.)
* Restructure imports to export find_matches
* Implement full remove()
Remove unnecessary trie paths and free unused maps.
Parallel to Matcher, raise KeyError when attempting to remove a match ID
that has not been added.
* Switch to PhraseMatcher.find_matches
* Switch to local cdef functions for span filtering
* Switch special case reload threshold to variable
Refer to variable instead of hard-coded threshold
* Move more of special case retokenize to cdef nogil
Move as much of the special case retokenization to nogil as possible.
* Rewrap sort as stdsort for OS X
* Rewrap stdsort with specific types
* Switch to qsort
* Fix merge
* Improve cmp functions
* Fix realloc
* Fix realloc again
* Initialize span struct while retokenizing
* Temporarily skip retokenizing
* Revert "Move more of special case retokenize to cdef nogil"
This reverts commit 0b7e52c797cd8ff1548f214bd4186ebb3a7ce8b1.
* Revert "Switch to qsort"
This reverts commit a98d71a942fc9bca531cf5eb05cf89fa88153b60.
* Fix specials check while caching
* Modify URL test with emoticons
The multiple suffix tests result in the emoticon `:>`, which is now
retokenized into one token as a special case after the suffixes are
split off.
* Refactor _apply_special_cases()
* Use cdef ints for span info used in multiple spots
* Modify _filter_special_spans() to prefer earlier
Parallel to #4414, modify _filter_special_spans() so that the earlier
span is preferred for overlapping spans of the same length.
* Replace MatchStruct with Entity
Replace MatchStruct with Entity since the existing Entity struct is
nearly identical.
* Replace Entity with more general SpanC
* Replace MatchStruct with SpanC
* Add error in debug-data if no dev docs are available (see #4575)
* Update azure-pipelines.yml
* Revert "Update azure-pipelines.yml"
This reverts commit ed1060cf59e5895b5fe92ad5b894fd1078ec4c49.
* Use latest wasabi
* Reorganise install_requires
* add dframcy to universe.json (#4580)
* Update universe.json [ci skip]
* Fix multiprocessing for as_tuples=True (#4582)
* Fix conllu script (#4579)
* force extensions to avoid clash between example scripts
* fix arg order and default file encoding
* add example config for conllu script
* newline
* move extension definitions to main function
* few more encodings fixes
* Add load_from_docbin example [ci skip]
TODO: upload the file somewhere
* Update README.md
* Add warnings about 3.8 (resolves #4593) [ci skip]
* Fixed typo: Added space between "recognize" and "various" (#4600)
* Fix DocBin.merge() example (#4599)
* Replace function registries with catalogue (#4584)
* Replace functions registries with catalogue
* Update __init__.py
* Fix test
* Revert unrelated flag [ci skip]
* Bugfix/dep matcher issue 4590 (#4601)
* add contributor agreement for prilopes
* add test for issue #4590
* fix on_match params for DependencyMacther (#4590)
* Minor updates to language example sentences (#4608)
* Add punctuation to Spanish example sentences
* Combine multilanguage examples for lang xx
* Add punctuation to nb examples
* Always realloc to a larger size
Avoid potential (unlikely) edge case and cymem error seen in #4604.
* Add error in debug-data if no dev docs are available (see #4575)
* Update debug-data for GoldCorpus / Example
* Ignore None label in misaligned NER data
2019-11-13 23:24:35 +03:00
|
|
|
parses_dir=("Directory to write the development parses", "positional", None, Path),
|
|
|
|
config=("Path to json formatted config file", "positional", None, Config.load),
|
2018-12-02 06:26:26 +03:00
|
|
|
corpus=(
|
Generalize handling of tokenizer special cases (#4259)
* Generalize handling of tokenizer special cases
Handle tokenizer special cases more generally by using the Matcher
internally to match special cases after the affix/token_match
tokenization is complete.
Instead of only matching special cases while processing balanced or
nearly balanced prefixes and suffixes, this recognizes special cases in
a wider range of contexts:
* Allows arbitrary numbers of prefixes/affixes around special cases
* Allows special cases separated by infixes
Existing tests/settings that couldn't be preserved as before:
* The emoticon '")' is no longer a supported special case
* The emoticon ':)' in "example:)" is a false positive again
When merged with #4258 (or the relevant cache bugfix), the affix and
token_match properties should be modified to flush and reload all
special cases to use the updated internal tokenization with the Matcher.
* Remove accidentally added test case
* Really remove accidentally added test
* Reload special cases when necessary
Reload special cases when affixes or token_match are modified. Skip
reloading during initialization.
* Update error code number
* Fix offset and whitespace in Matcher special cases
* Fix offset bugs when merging and splitting tokens
* Set final whitespace on final token in inserted special case
* Improve cache flushing in tokenizer
* Separate cache and specials memory (temporarily)
* Flush cache when adding special cases
* Repeated `self._cache = PreshMap()` and `self._specials = PreshMap()`
are necessary due to this bug:
https://github.com/explosion/preshed/issues/21
* Remove reinitialized PreshMaps on cache flush
* Update UD bin scripts
* Update imports for `bin/`
* Add all currently supported languages
* Update subtok merger for new Matcher validation
* Modify blinded check to look at tokens instead of lemmas (for corpora
with tokens but not lemmas like Telugu)
* Use special Matcher only for cases with affixes
* Reinsert specials cache checks during normal tokenization for special
cases as much as possible
* Additionally include specials cache checks while splitting on infixes
* Since the special Matcher needs consistent affix-only tokenization
for the special cases themselves, introduce the argument
`with_special_cases` in order to do tokenization with or without
specials cache checks
* After normal tokenization, postprocess with special cases Matcher for
special cases containing affixes
* Replace PhraseMatcher with Aho-Corasick
Replace PhraseMatcher with the Aho-Corasick algorithm over numpy arrays
of the hash values for the relevant attribute. The implementation is
based on FlashText.
The speed should be similar to the previous PhraseMatcher. It is now
possible to easily remove match IDs and matches don't go missing with
large keyword lists / vocabularies.
Fixes #4308.
* Restore support for pickling
* Fix internal keyword add/remove for numpy arrays
* Add test for #4248, clean up test
* Improve efficiency of special cases handling
* Use PhraseMatcher instead of Matcher
* Improve efficiency of merging/splitting special cases in document
* Process merge/splits in one pass without repeated token shifting
* Merge in place if no splits
* Update error message number
* Remove UD script modifications
Only used for timing/testing, should be a separate PR
* Remove final traces of UD script modifications
* Update UD bin scripts
* Update imports for `bin/`
* Add all currently supported languages
* Update subtok merger for new Matcher validation
* Modify blinded check to look at tokens instead of lemmas (for corpora
with tokens but not lemmas like Telugu)
* Add missing loop for match ID set in search loop
* Remove cruft in matching loop for partial matches
There was a bit of unnecessary code left over from FlashText in the
matching loop to handle partial token matches, which we don't have with
PhraseMatcher.
* Replace dict trie with MapStruct trie
* Fix how match ID hash is stored/added
* Update fix for match ID vocab
* Switch from map_get_unless_missing to map_get
* Switch from numpy array to Token.get_struct_attr
Access token attributes directly in Doc instead of making a copy of the
relevant values in a numpy array.
Add unsatisfactory warning for hash collision with reserved terminal
hash key. (Ideally it would change the reserved terminal hash and redo
the whole trie, but for now, I'm hoping there won't be collisions.)
* Restructure imports to export find_matches
* Implement full remove()
Remove unnecessary trie paths and free unused maps.
Parallel to Matcher, raise KeyError when attempting to remove a match ID
that has not been added.
* Switch to PhraseMatcher.find_matches
* Switch to local cdef functions for span filtering
* Switch special case reload threshold to variable
Refer to variable instead of hard-coded threshold
* Move more of special case retokenize to cdef nogil
Move as much of the special case retokenization to nogil as possible.
* Rewrap sort as stdsort for OS X
* Rewrap stdsort with specific types
* Switch to qsort
* Fix merge
* Improve cmp functions
* Fix realloc
* Fix realloc again
* Initialize span struct while retokenizing
* Temporarily skip retokenizing
* Revert "Move more of special case retokenize to cdef nogil"
This reverts commit 0b7e52c797cd8ff1548f214bd4186ebb3a7ce8b1.
* Revert "Switch to qsort"
This reverts commit a98d71a942fc9bca531cf5eb05cf89fa88153b60.
* Fix specials check while caching
* Modify URL test with emoticons
The multiple suffix tests result in the emoticon `:>`, which is now
retokenized into one token as a special case after the suffixes are
split off.
* Refactor _apply_special_cases()
* Use cdef ints for span info used in multiple spots
* Modify _filter_special_spans() to prefer earlier
Parallel to #4414, modify _filter_special_spans() so that the earlier
span is preferred for overlapping spans of the same length.
* Replace MatchStruct with Entity
Replace MatchStruct with Entity since the existing Entity struct is
nearly identical.
* Replace Entity with more general SpanC
* Replace MatchStruct with SpanC
* Add error in debug-data if no dev docs are available (see #4575)
* Update azure-pipelines.yml
* Revert "Update azure-pipelines.yml"
This reverts commit ed1060cf59e5895b5fe92ad5b894fd1078ec4c49.
* Use latest wasabi
* Reorganise install_requires
* add dframcy to universe.json (#4580)
* Update universe.json [ci skip]
* Fix multiprocessing for as_tuples=True (#4582)
* Fix conllu script (#4579)
* force extensions to avoid clash between example scripts
* fix arg order and default file encoding
* add example config for conllu script
* newline
* move extension definitions to main function
* few more encodings fixes
* Add load_from_docbin example [ci skip]
TODO: upload the file somewhere
* Update README.md
* Add warnings about 3.8 (resolves #4593) [ci skip]
* Fixed typo: Added space between "recognize" and "various" (#4600)
* Fix DocBin.merge() example (#4599)
* Replace function registries with catalogue (#4584)
* Replace functions registries with catalogue
* Update __init__.py
* Fix test
* Revert unrelated flag [ci skip]
* Bugfix/dep matcher issue 4590 (#4601)
* add contributor agreement for prilopes
* add test for issue #4590
* fix on_match params for DependencyMacther (#4590)
* Minor updates to language example sentences (#4608)
* Add punctuation to Spanish example sentences
* Combine multilanguage examples for lang xx
* Add punctuation to nb examples
* Always realloc to a larger size
Avoid potential (unlikely) edge case and cymem error seen in #4604.
* Add error in debug-data if no dev docs are available (see #4575)
* Update debug-data for GoldCorpus / Example
* Ignore None label in misaligned NER data
2019-11-13 23:24:35 +03:00
|
|
|
"UD corpus to train and evaluate on, e.g. UD_Spanish-AnCora",
|
2018-12-02 06:26:26 +03:00
|
|
|
"positional",
|
|
|
|
None,
|
|
|
|
str,
|
|
|
|
),
|
|
|
|
limit=("Size limit", "option", "n", int),
|
2018-03-27 20:23:02 +03:00
|
|
|
)
|
|
|
|
def main(ud_dir, parses_dir, config, corpus, limit=0):
|
2019-11-04 22:31:26 +03:00
|
|
|
Token.set_extension("get_conllu_lines", method=get_token_conllu)
|
|
|
|
Token.set_extension("begins_fused", default=False)
|
|
|
|
Token.set_extension("inside_fused", default=False)
|
2019-09-09 17:32:11 +03:00
|
|
|
|
Generalize handling of tokenizer special cases (#4259)
* Generalize handling of tokenizer special cases
Handle tokenizer special cases more generally by using the Matcher
internally to match special cases after the affix/token_match
tokenization is complete.
Instead of only matching special cases while processing balanced or
nearly balanced prefixes and suffixes, this recognizes special cases in
a wider range of contexts:
* Allows arbitrary numbers of prefixes/affixes around special cases
* Allows special cases separated by infixes
Existing tests/settings that couldn't be preserved as before:
* The emoticon '")' is no longer a supported special case
* The emoticon ':)' in "example:)" is a false positive again
When merged with #4258 (or the relevant cache bugfix), the affix and
token_match properties should be modified to flush and reload all
special cases to use the updated internal tokenization with the Matcher.
* Remove accidentally added test case
* Really remove accidentally added test
* Reload special cases when necessary
Reload special cases when affixes or token_match are modified. Skip
reloading during initialization.
* Update error code number
* Fix offset and whitespace in Matcher special cases
* Fix offset bugs when merging and splitting tokens
* Set final whitespace on final token in inserted special case
* Improve cache flushing in tokenizer
* Separate cache and specials memory (temporarily)
* Flush cache when adding special cases
* Repeated `self._cache = PreshMap()` and `self._specials = PreshMap()`
are necessary due to this bug:
https://github.com/explosion/preshed/issues/21
* Remove reinitialized PreshMaps on cache flush
* Update UD bin scripts
* Update imports for `bin/`
* Add all currently supported languages
* Update subtok merger for new Matcher validation
* Modify blinded check to look at tokens instead of lemmas (for corpora
with tokens but not lemmas like Telugu)
* Use special Matcher only for cases with affixes
* Reinsert specials cache checks during normal tokenization for special
cases as much as possible
* Additionally include specials cache checks while splitting on infixes
* Since the special Matcher needs consistent affix-only tokenization
for the special cases themselves, introduce the argument
`with_special_cases` in order to do tokenization with or without
specials cache checks
* After normal tokenization, postprocess with special cases Matcher for
special cases containing affixes
* Replace PhraseMatcher with Aho-Corasick
Replace PhraseMatcher with the Aho-Corasick algorithm over numpy arrays
of the hash values for the relevant attribute. The implementation is
based on FlashText.
The speed should be similar to the previous PhraseMatcher. It is now
possible to easily remove match IDs and matches don't go missing with
large keyword lists / vocabularies.
Fixes #4308.
* Restore support for pickling
* Fix internal keyword add/remove for numpy arrays
* Add test for #4248, clean up test
* Improve efficiency of special cases handling
* Use PhraseMatcher instead of Matcher
* Improve efficiency of merging/splitting special cases in document
* Process merge/splits in one pass without repeated token shifting
* Merge in place if no splits
* Update error message number
* Remove UD script modifications
Only used for timing/testing, should be a separate PR
* Remove final traces of UD script modifications
* Update UD bin scripts
* Update imports for `bin/`
* Add all currently supported languages
* Update subtok merger for new Matcher validation
* Modify blinded check to look at tokens instead of lemmas (for corpora
with tokens but not lemmas like Telugu)
* Add missing loop for match ID set in search loop
* Remove cruft in matching loop for partial matches
There was a bit of unnecessary code left over from FlashText in the
matching loop to handle partial token matches, which we don't have with
PhraseMatcher.
* Replace dict trie with MapStruct trie
* Fix how match ID hash is stored/added
* Update fix for match ID vocab
* Switch from map_get_unless_missing to map_get
* Switch from numpy array to Token.get_struct_attr
Access token attributes directly in Doc instead of making a copy of the
relevant values in a numpy array.
Add unsatisfactory warning for hash collision with reserved terminal
hash key. (Ideally it would change the reserved terminal hash and redo
the whole trie, but for now, I'm hoping there won't be collisions.)
* Restructure imports to export find_matches
* Implement full remove()
Remove unnecessary trie paths and free unused maps.
Parallel to Matcher, raise KeyError when attempting to remove a match ID
that has not been added.
* Switch to PhraseMatcher.find_matches
* Switch to local cdef functions for span filtering
* Switch special case reload threshold to variable
Refer to variable instead of hard-coded threshold
* Move more of special case retokenize to cdef nogil
Move as much of the special case retokenization to nogil as possible.
* Rewrap sort as stdsort for OS X
* Rewrap stdsort with specific types
* Switch to qsort
* Fix merge
* Improve cmp functions
* Fix realloc
* Fix realloc again
* Initialize span struct while retokenizing
* Temporarily skip retokenizing
* Revert "Move more of special case retokenize to cdef nogil"
This reverts commit 0b7e52c797cd8ff1548f214bd4186ebb3a7ce8b1.
* Revert "Switch to qsort"
This reverts commit a98d71a942fc9bca531cf5eb05cf89fa88153b60.
* Fix specials check while caching
* Modify URL test with emoticons
The multiple suffix tests result in the emoticon `:>`, which is now
retokenized into one token as a special case after the suffixes are
split off.
* Refactor _apply_special_cases()
* Use cdef ints for span info used in multiple spots
* Modify _filter_special_spans() to prefer earlier
Parallel to #4414, modify _filter_special_spans() so that the earlier
span is preferred for overlapping spans of the same length.
* Replace MatchStruct with Entity
Replace MatchStruct with Entity since the existing Entity struct is
nearly identical.
* Replace Entity with more general SpanC
* Replace MatchStruct with SpanC
* Add error in debug-data if no dev docs are available (see #4575)
* Update azure-pipelines.yml
* Revert "Update azure-pipelines.yml"
This reverts commit ed1060cf59e5895b5fe92ad5b894fd1078ec4c49.
* Use latest wasabi
* Reorganise install_requires
* add dframcy to universe.json (#4580)
* Update universe.json [ci skip]
* Fix multiprocessing for as_tuples=True (#4582)
* Fix conllu script (#4579)
* force extensions to avoid clash between example scripts
* fix arg order and default file encoding
* add example config for conllu script
* newline
* move extension definitions to main function
* few more encodings fixes
* Add load_from_docbin example [ci skip]
TODO: upload the file somewhere
* Update README.md
* Add warnings about 3.8 (resolves #4593) [ci skip]
* Fixed typo: Added space between "recognize" and "various" (#4600)
* Fix DocBin.merge() example (#4599)
* Replace function registries with catalogue (#4584)
* Replace functions registries with catalogue
* Update __init__.py
* Fix test
* Revert unrelated flag [ci skip]
* Bugfix/dep matcher issue 4590 (#4601)
* add contributor agreement for prilopes
* add test for issue #4590
* fix on_match params for DependencyMacther (#4590)
* Minor updates to language example sentences (#4608)
* Add punctuation to Spanish example sentences
* Combine multilanguage examples for lang xx
* Add punctuation to nb examples
* Always realloc to a larger size
Avoid potential (unlikely) edge case and cymem error seen in #4604.
* Add error in debug-data if no dev docs are available (see #4575)
* Update debug-data for GoldCorpus / Example
* Ignore None label in misaligned NER data
2019-11-13 23:24:35 +03:00
|
|
|
Token.set_extension("get_conllu_lines", method=get_token_conllu)
|
|
|
|
Token.set_extension("begins_fused", default=False)
|
|
|
|
Token.set_extension("inside_fused", default=False)
|
|
|
|
|
2018-03-27 20:23:02 +03:00
|
|
|
paths = TreebankPaths(ud_dir, corpus)
|
|
|
|
if not (parses_dir / corpus).exists():
|
|
|
|
(parses_dir / corpus).mkdir()
|
|
|
|
print("Train and evaluate", corpus, "using lang", paths.lang)
|
|
|
|
nlp = load_nlp(paths.lang, config)
|
|
|
|
|
2019-11-11 19:35:27 +03:00
|
|
|
examples = read_data(
|
2018-12-02 06:26:26 +03:00
|
|
|
nlp,
|
2019-11-11 19:35:27 +03:00
|
|
|
paths.train.conllu.open(encoding="utf8"),
|
|
|
|
paths.train.text.open(encoding="utf8"),
|
2018-12-02 06:26:26 +03:00
|
|
|
max_doc_length=config.max_doc_length,
|
|
|
|
limit=limit,
|
|
|
|
)
|
2018-03-27 20:23:02 +03:00
|
|
|
|
2019-11-11 19:35:27 +03:00
|
|
|
optimizer = initialize_pipeline(nlp, examples, config)
|
2018-03-27 20:23:02 +03:00
|
|
|
|
|
|
|
for i in range(config.nr_epoch):
|
2019-11-11 19:35:27 +03:00
|
|
|
docs = [nlp.make_doc(example.doc.text) for example in examples]
|
|
|
|
batches = minibatch_by_words(examples, size=config.batch_size)
|
2018-03-27 20:23:02 +03:00
|
|
|
losses = {}
|
|
|
|
n_train_words = sum(len(doc) for doc in docs)
|
|
|
|
with tqdm.tqdm(total=n_train_words, leave=False) as pbar:
|
|
|
|
for batch in batches:
|
2019-11-11 19:35:27 +03:00
|
|
|
pbar.update(sum(len(ex.doc) for ex in batch))
|
2018-12-02 06:26:26 +03:00
|
|
|
nlp.update(
|
2019-12-21 20:55:03 +03:00
|
|
|
examples=batch, sgd=optimizer, drop=config.dropout, losses=losses,
|
2018-12-02 06:26:26 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
out_path = parses_dir / corpus / "epoch-{i}.conllu".format(i=i)
|
2018-03-27 20:23:02 +03:00
|
|
|
with nlp.use_params(optimizer.averages):
|
|
|
|
scores = evaluate(nlp, paths.dev.text, paths.dev.conllu, out_path)
|
|
|
|
print_progress(i, losses, scores)
|
|
|
|
|
|
|
|
|
2018-12-02 06:26:26 +03:00
|
|
|
if __name__ == "__main__":
|
2018-03-27 20:23:02 +03:00
|
|
|
plac.call(main)
|