From 2d8c6e1124ec91df5dc83e968f50ab7348e1943b Mon Sep 17 00:00:00 2001 From: adrianeboyd Date: Mon, 25 Nov 2019 13:06:36 +0100 Subject: [PATCH 01/12] Iterate over lr_edges until sents are correct (#4702) Iterate over lr_edges until all heads are within the current sentence. Instead of iterating over them for a fixed number of iterations, check whether the sentence boundaries are correct for the heads and stop when all are correct. Stop after a maximum of 10 iterations, providing a warning in this case since the sentence boundaries may not be correct. --- spacy/errors.py | 1 + spacy/tests/parser/test_parse.py | 17 +++++++ spacy/tokens/doc.pxd | 3 ++ spacy/tokens/doc.pyx | 80 +++++++++++++++++++++++--------- 4 files changed, 78 insertions(+), 23 deletions(-) diff --git a/spacy/errors.py b/spacy/errors.py index 3e62b5a3e..c44c72117 100644 --- a/spacy/errors.py +++ b/spacy/errors.py @@ -101,6 +101,7 @@ class Warnings(object): "the Knowledge Base.") W025 = ("'{name}' requires '{attr}' to be assigned, but none of the " "previous components in the pipeline declare that they assign it.") + W026 = ("Unable to set all sentence boundaries from dependency parses.") @add_codes diff --git a/spacy/tests/parser/test_parse.py b/spacy/tests/parser/test_parse.py index c140cb485..384f14dad 100644 --- a/spacy/tests/parser/test_parse.py +++ b/spacy/tests/parser/test_parse.py @@ -148,3 +148,20 @@ def test_parser_arc_eager_finalize_state(en_tokenizer, en_parser): assert tokens[4].left_edge.i == 0 assert tokens[4].right_edge.i == 4 assert tokens[4].head.i == 4 + + +def test_parser_set_sent_starts(en_vocab): + words = ['Ein', 'Satz', '.', 'Außerdem', 'ist', 'Zimmer', 'davon', 'überzeugt', ',', 'dass', 'auch', 'epige-', '\n', 'netische', 'Mechanismen', 'eine', 'Rolle', 'spielen', ',', 'also', 'Vorgänge', ',', 'die', '\n', 'sich', 'darauf', 'auswirken', ',', 'welche', 'Gene', 'abgelesen', 'werden', 'und', '\n', 'welche', 'nicht', '.', '\n'] + heads = [1, 0, -1, 27, 0, -1, 1, -3, -1, 8, 4, 3, -1, 1, 3, 1, 1, -11, -1, 1, -9, -1, 4, -1, 2, 1, -6, -1, 1, 2, 1, -6, -1, -1, -17, -31, -32, -1] + deps = ['nk', 'ROOT', 'punct', 'mo', 'ROOT', 'sb', 'op', 'pd', 'punct', 'cp', 'mo', 'nk', '', 'nk', 'sb', 'nk', 'oa', 're', 'punct', 'mo', 'app', 'punct', 'sb', '', 'oa', 'op', 'rc', 'punct', 'nk', 'sb', 'oc', 're', 'cd', '', 'oa', 'ng', 'punct', ''] + doc = get_doc( + en_vocab, words=words, deps=deps, heads=heads + ) + for i in range(len(words)): + if i == 0 or i == 3: + assert doc[i].is_sent_start == True + else: + assert doc[i].is_sent_start == None + for sent in doc.sents: + for token in sent: + assert token.head in sent diff --git a/spacy/tokens/doc.pxd b/spacy/tokens/doc.pxd index 62665fcc5..7f231887f 100644 --- a/spacy/tokens/doc.pxd +++ b/spacy/tokens/doc.pxd @@ -21,6 +21,9 @@ ctypedef fused LexemeOrToken: cdef int set_children_from_heads(TokenC* tokens, int length) except -1 +cdef int _set_lr_kids_and_edges(TokenC* tokens, int length, int loop_count) except -1 + + cdef int token_by_start(const TokenC* tokens, int length, int start_char) except -2 diff --git a/spacy/tokens/doc.pyx b/spacy/tokens/doc.pyx index 6afe89e05..d4ba3803b 100644 --- a/spacy/tokens/doc.pyx +++ b/spacy/tokens/doc.pyx @@ -1153,35 +1153,69 @@ cdef int set_children_from_heads(TokenC* tokens, int length) except -1: tokens[i].r_kids = 0 tokens[i].l_edge = i tokens[i].r_edge = i - # Three times, for non-projectivity. See issue #3170. This isn't a very - # satisfying fix, but I think it's sufficient. - for loop_count in range(3): - # Set left edges - for i in range(length): - child = &tokens[i] - head = &tokens[i + child.head] - if child < head and loop_count == 0: - head.l_kids += 1 - if child.l_edge < head.l_edge: - head.l_edge = child.l_edge - if child.r_edge > head.r_edge: - head.r_edge = child.r_edge - # Set right edges - same as above, but iterate in reverse - for i in range(length-1, -1, -1): - child = &tokens[i] - head = &tokens[i + child.head] - if child > head and loop_count == 0: - head.r_kids += 1 - if child.r_edge > head.r_edge: - head.r_edge = child.r_edge - if child.l_edge < head.l_edge: - head.l_edge = child.l_edge + cdef int loop_count = 0 + cdef bint heads_within_sents = False + # Try up to 10 iterations of adjusting lr_kids and lr_edges in order to + # handle non-projective dependency parses, stopping when all heads are + # within their respective sentence boundaries. We have documented cases + # that need at least 4 iterations, so this is to be on the safe side + # without risking getting stuck in an infinite loop if something is + # terribly malformed. + while not heads_within_sents: + heads_within_sents = _set_lr_kids_and_edges(tokens, length, loop_count) + if loop_count > 10: + user_warning(Warnings.W026) + loop_count += 1 # Set sentence starts for i in range(length): if tokens[i].head == 0 and tokens[i].dep != 0: tokens[tokens[i].l_edge].sent_start = True +cdef int _set_lr_kids_and_edges(TokenC* tokens, int length, int loop_count) except -1: + # May be called multiple times due to non-projectivity. See issues #3170 + # and #4688. + # Set left edges + cdef TokenC* head + cdef TokenC* child + cdef int i, j + for i in range(length): + child = &tokens[i] + head = &tokens[i + child.head] + if child < head and loop_count == 0: + head.l_kids += 1 + if child.l_edge < head.l_edge: + head.l_edge = child.l_edge + if child.r_edge > head.r_edge: + head.r_edge = child.r_edge + # Set right edges - same as above, but iterate in reverse + for i in range(length-1, -1, -1): + child = &tokens[i] + head = &tokens[i + child.head] + if child > head and loop_count == 0: + head.r_kids += 1 + if child.r_edge > head.r_edge: + head.r_edge = child.r_edge + if child.l_edge < head.l_edge: + head.l_edge = child.l_edge + # Get sentence start positions according to current state + sent_starts = set() + for i in range(length): + if tokens[i].head == 0 and tokens[i].dep != 0: + sent_starts.add(tokens[i].l_edge) + cdef int curr_sent_start = 0 + cdef int curr_sent_end = 0 + # Check whether any heads are not within the current sentence + for i in range(length): + if (i > 0 and i in sent_starts) or i == length - 1: + curr_sent_end = i + for j in range(curr_sent_start, curr_sent_end): + if tokens[j].head + j < curr_sent_start or tokens[j].head + j >= curr_sent_end + 1: + return False + curr_sent_start = i + return True + + cdef int _get_tokens_lca(Token token_j, Token token_k): """Given two tokens, returns the index of the lowest common ancestor (LCA) among the two. If they have no common ancestor, -1 is returned. From 2160ecfc92b5b1739d65defa969e4e9744dd8b94 Mon Sep 17 00:00:00 2001 From: Ines Montani Date: Mon, 25 Nov 2019 13:08:19 +0100 Subject: [PATCH 02/12] Fix typo [ci skip] --- spacy/errors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spacy/errors.py b/spacy/errors.py index c44c72117..3aeddec09 100644 --- a/spacy/errors.py +++ b/spacy/errors.py @@ -72,7 +72,7 @@ class Warnings(object): "instead.") W014 = ("As of v2.1.0, the `disable` keyword argument on the serialization " "methods is and should be replaced with `exclude`. This makes it " - "consistent with the other objects serializable.") + "consistent with the other serializable objects.") W015 = ("As of v2.1.0, the use of keyword arguments to exclude fields from " "being serialized or deserialized is deprecated. Please use the " "`exclude` argument instead. For example: exclude=['{arg}'].") From 5b36dec7eb505b31bbbfa4bb448fdc35eb738cd7 Mon Sep 17 00:00:00 2001 From: Ines Montani Date: Mon, 25 Nov 2019 16:01:22 +0100 Subject: [PATCH 03/12] Auto-exclude disabled when calling from_disk during load (#4708) --- spacy/tests/regression/test_issue4707.py | 23 +++++++++++++++++++++++ spacy/util.py | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 spacy/tests/regression/test_issue4707.py diff --git a/spacy/tests/regression/test_issue4707.py b/spacy/tests/regression/test_issue4707.py new file mode 100644 index 000000000..e710881d7 --- /dev/null +++ b/spacy/tests/regression/test_issue4707.py @@ -0,0 +1,23 @@ +# coding: utf8 +from __future__ import unicode_literals + +from spacy.util import load_model_from_path +from spacy.lang.en import English + +from ..util import make_tempdir + + +def test_issue4707(): + """Tests that disabled component names are also excluded from nlp.from_disk + by default when loading a model. + """ + nlp = English() + nlp.add_pipe(nlp.create_pipe("sentencizer")) + nlp.add_pipe(nlp.create_pipe("entity_ruler")) + assert nlp.pipe_names == ["sentencizer", "entity_ruler"] + exclude = ["tokenizer", "sentencizer"] + with make_tempdir() as tmpdir: + nlp.to_disk(tmpdir, exclude=exclude) + new_nlp = load_model_from_path(tmpdir, disable=exclude) + assert "sentencizer" not in new_nlp.pipe_names + assert "entity_ruler" in new_nlp.pipe_names diff --git a/spacy/util.py b/spacy/util.py index 57ed244f8..9b96b2f5e 100644 --- a/spacy/util.py +++ b/spacy/util.py @@ -208,7 +208,7 @@ def load_model_from_path(model_path, meta=False, **overrides): factory = factories.get(name, name) component = nlp.create_pipe(factory, config=config) nlp.add_pipe(component, name=name) - return nlp.from_disk(model_path) + return nlp.from_disk(model_path, exclude=disable) def load_model_from_init_py(init_file, **overrides): From 16cb19e960d614ddfcf04e35350f5c2fe4b6ba71 Mon Sep 17 00:00:00 2001 From: Jari Bakken Date: Mon, 25 Nov 2019 21:26:26 +0100 Subject: [PATCH 04/12] update nb tag_map (#4711) --- spacy/lang/nb/tag_map.py | 99 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 96 insertions(+), 3 deletions(-) diff --git a/spacy/lang/nb/tag_map.py b/spacy/lang/nb/tag_map.py index 183c40c97..cf4c95840 100644 --- a/spacy/lang/nb/tag_map.py +++ b/spacy/lang/nb/tag_map.py @@ -1,12 +1,12 @@ # coding: utf8 from __future__ import unicode_literals -from ...symbols import POS, PUNCT, ADJ, CONJ, SCONJ, SYM, NUM, DET, ADV, ADP, X +from ...symbols import POS, PUNCT, ADJ, CONJ, CCONJ, SCONJ, SYM, NUM, DET, ADV, ADP, X from ...symbols import VERB, NOUN, PROPN, PART, INTJ, PRON, AUX -# Tags are a combination of POS and morphological features from a yet -# unpublished dataset developed by Schibsted, Nasjonalbiblioteket and LTG. The +# Tags are a combination of POS and morphological features from a +# https://github.com/ltgoslo/norne developed by Schibsted, Nasjonalbiblioteket and LTG. The # data format is .conllu and follows the Universal Dependencies annotation. # (There are some annotation differences compared to this dataset: # https://github.com/UniversalDependencies/UD_Norwegian-Bokmaal @@ -467,4 +467,97 @@ TAG_MAP = { "VERB__VerbForm=Part": {"morph": "VerbForm=Part", POS: VERB}, "VERB___": {"morph": "_", POS: VERB}, "X___": {"morph": "_", POS: X}, + 'CCONJ___': {"morph": "_", POS: CCONJ}, + "ADJ__Abbr=Yes": {"morph": "Abbr=Yes", POS: ADJ}, + "ADJ__Abbr=Yes|Degree=Pos": {"morph": "Abbr=Yes|Degree=Pos", POS: ADJ}, + "ADJ__Case=Gen|Definite=Def|Number=Sing|VerbForm=Part": {"morph": "Case=Gen|Definite=Def|Number=Sing|VerbForm=Part", POS: ADJ}, + "ADJ__Definite=Def|Number=Sing|VerbForm=Part": {"morph": "Definite=Def|Number=Sing|VerbForm=Part", POS: ADJ}, + "ADJ__Definite=Ind|Gender=Masc|Number=Sing|VerbForm=Part": {"morph": "Definite=Ind|Gender=Masc|Number=Sing|VerbForm=Part", POS: ADJ}, + "ADJ__Definite=Ind|Gender=Neut|Number=Sing|VerbForm=Part": {"morph": "Definite=Ind|Gender=Neut|Number=Sing|VerbForm=Part", POS: ADJ}, + "ADJ__Definite=Ind|Number=Sing|VerbForm=Part": {"morph": "Definite=Ind|Number=Sing|VerbForm=Part", POS: ADJ}, + "ADJ__Number=Sing|VerbForm=Part": {"morph": "Number=Sing|VerbForm=Part", POS: ADJ}, + "ADJ__VerbForm=Part": {"morph": "VerbForm=Part", POS: ADJ}, + "ADP__Abbr=Yes": {"morph": "Abbr=Yes", POS: ADP}, + "ADV__Abbr=Yes": {"morph": "Abbr=Yes", POS: ADV}, + "DET__Case=Gen|Gender=Masc|Number=Sing|PronType=Art": {"morph": "Case=Gen|Gender=Masc|Number=Sing|PronType=Art", POS: DET}, + "DET__Case=Gen|Number=Plur|PronType=Tot": {"morph": "Case=Gen|Number=Plur|PronType=Tot", POS: DET}, + "DET__Definite=Def|PronType=Prs": {"morph": "Definite=Def|PronType=Prs", POS: DET}, + "DET__Definite=Ind|Gender=Fem|Number=Sing|PronType=Prs": {"morph": "Definite=Ind|Gender=Fem|Number=Sing|PronType=Prs", POS: DET}, + "DET__Definite=Ind|Gender=Masc|Number=Sing|PronType=Prs": {"morph": "Definite=Ind|Gender=Masc|Number=Sing|PronType=Prs", POS: DET}, + "DET__Definite=Ind|Gender=Neut|Number=Sing|PronType=Prs": {"morph": "Definite=Ind|Gender=Neut|Number=Sing|PronType=Prs", POS: DET}, + "DET__Gender=Fem|Number=Sing|PronType=Art": {"morph": "Gender=Fem|Number=Sing|PronType=Art", POS: DET}, + "DET__Gender=Fem|Number=Sing|PronType=Ind": {"morph": "Gender=Fem|Number=Sing|PronType=Ind", POS: DET}, + "DET__Gender=Fem|Number=Sing|PronType=Prs": {"morph": "Gender=Fem|Number=Sing|PronType=Prs", POS: DET}, + "DET__Gender=Fem|Number=Sing|PronType=Tot": {"morph": "Gender=Fem|Number=Sing|PronType=Tot", POS: DET}, + "DET__Gender=Masc|Number=Sing|Polarity=Neg|PronType=Neg": {"morph": "Gender=Masc|Number=Sing|Polarity=Neg|PronType=Neg", POS: DET}, + "DET__Gender=Masc|Number=Sing|PronType=Art": {"morph": "Gender=Masc|Number=Sing|PronType=Art", POS: DET}, + "DET__Gender=Masc|Number=Sing|PronType=Ind": {"morph": "Gender=Masc|Number=Sing|PronType=Ind", POS: DET}, + "DET__Gender=Masc|Number=Sing|PronType=Tot": {"morph": "Gender=Masc|Number=Sing|PronType=Tot", POS: DET}, + "DET__Gender=Neut|Number=Sing|Polarity=Neg|PronType=Neg": {"morph": "Gender=Neut|Number=Sing|Polarity=Neg|PronType=Neg", POS: DET}, + "DET__Gender=Neut|Number=Sing|PronType=Art": {"morph": "Gender=Neut|Number=Sing|PronType=Art", POS: DET}, + "DET__Gender=Neut|Number=Sing|PronType=Dem,Ind": {"morph": "Gender=Neut|Number=Sing|PronType=Dem,Ind", POS: DET}, + "DET__Gender=Neut|Number=Sing|PronType=Ind": {"morph": "Gender=Neut|Number=Sing|PronType=Ind", POS: DET}, + "DET__Gender=Neut|Number=Sing|PronType=Tot": {"morph": "Gender=Neut|Number=Sing|PronType=Tot", POS: DET}, + "DET__Number=Plur|Polarity=Neg|PronType=Neg": {"morph": "Number=Plur|Polarity=Neg|PronType=Neg", POS: DET}, + "DET__Number=Plur|PronType=Art": {"morph": "Number=Plur|PronType=Art", POS: DET}, + "DET__Number=Plur|PronType=Ind": {"morph": "Number=Plur|PronType=Ind", POS: DET}, + "DET__Number=Plur|PronType=Prs": {"morph": "Number=Plur|PronType=Prs", POS: DET}, + "DET__Number=Plur|PronType=Tot": {"morph": "Number=Plur|PronType=Tot", POS: DET}, + "DET__PronType=Ind": {"morph": "PronType=Ind", POS: DET}, + "DET__PronType=Prs": {"morph": "PronType=Prs", POS: DET}, + "NOUN__Abbr=Yes": {"morph": "Abbr=Yes", POS: NOUN}, + "NOUN__Abbr=Yes|Case=Gen": {"morph": "Abbr=Yes|Case=Gen", POS: NOUN}, + "NOUN__Abbr=Yes|Definite=Def,Ind|Gender=Masc|Number=Plur,Sing": {"morph": "Abbr=Yes|Definite=Def,Ind|Gender=Masc|Number=Plur,Sing", POS: NOUN}, + "NOUN__Abbr=Yes|Definite=Def,Ind|Gender=Masc|Number=Sing": {"morph": "Abbr=Yes|Definite=Def,Ind|Gender=Masc|Number=Sing", POS: NOUN}, + "NOUN__Abbr=Yes|Definite=Def,Ind|Gender=Neut|Number=Plur,Sing": {"morph": "Abbr=Yes|Definite=Def,Ind|Gender=Neut|Number=Plur,Sing", POS: NOUN}, + "NOUN__Abbr=Yes|Gender=Masc": {"morph": "Abbr=Yes|Gender=Masc", POS: NOUN}, + "NUM__Case=Gen|Number=Plur|NumType=Card": {"morph": "Case=Gen|Number=Plur|NumType=Card", POS: NUM}, + "NUM__Definite=Def|Number=Sing|NumType=Card": {"morph": "Definite=Def|Number=Sing|NumType=Card", POS: NUM}, + "NUM__Definite=Def|NumType=Card": {"morph": "Definite=Def|NumType=Card", POS: NUM}, + "NUM__Gender=Fem|Number=Sing|NumType=Card": {"morph": "Gender=Fem|Number=Sing|NumType=Card", POS: NUM}, + "NUM__Gender=Masc|Number=Sing|NumType=Card": {"morph": "Gender=Masc|Number=Sing|NumType=Card", POS: NUM}, + "NUM__Gender=Neut|Number=Sing|NumType=Card": {"morph": "Gender=Neut|Number=Sing|NumType=Card", POS: NUM}, + "NUM__Number=Plur|NumType=Card": {"morph": "Number=Plur|NumType=Card", POS: NUM}, + "NUM__Number=Sing|NumType=Card": {"morph": "Number=Sing|NumType=Card", POS: NUM}, + "NUM__NumType=Card": {"morph": "NumType=Card", POS: NUM}, + "PART__Polarity=Neg": {"morph": "Polarity=Neg", POS: PART}, + "PRON__Animacy=Hum|Case=Acc|Gender=Fem|Number=Sing|Person=3|PronType=Prs": { "morph": "Animacy=Hum|Case=Acc|Gender=Fem|Number=Sing|Person=3|PronType=Prs", POS: PRON}, + "PRON__Animacy=Hum|Case=Acc|Gender=Masc|Number=Sing|Person=3|PronType=Prs": { "morph": "Animacy=Hum|Case=Acc|Gender=Masc|Number=Sing|Person=3|PronType=Prs", POS: PRON}, + "PRON__Animacy=Hum|Case=Acc|Number=Plur|Person=1|PronType=Prs": {"morph": "Animacy=Hum|Case=Acc|Number=Plur|Person=1|PronType=Prs", POS: PRON}, + "PRON__Animacy=Hum|Case=Acc|Number=Plur|Person=2|PronType=Prs": {"morph": "Animacy=Hum|Case=Acc|Number=Plur|Person=2|PronType=Prs", POS: PRON}, + "PRON__Animacy=Hum|Case=Acc|Number=Sing|Person=1|PronType=Prs": {"morph": "Animacy=Hum|Case=Acc|Number=Sing|Person=1|PronType=Prs", POS: PRON}, + "PRON__Animacy=Hum|Case=Acc|Number=Sing|Person=2|PronType=Prs": {"morph": "Animacy=Hum|Case=Acc|Number=Sing|Person=2|PronType=Prs", POS: PRON}, + "PRON__Animacy=Hum|Case=Gen,Nom|Number=Sing|PronType=Art,Prs": {"morph": "Animacy=Hum|Case=Gen,Nom|Number=Sing|PronType=Art,Prs", POS: PRON}, + "PRON__Animacy=Hum|Case=Gen|Number=Sing|PronType=Art,Prs": {"morph": "Animacy=Hum|Case=Gen|Number=Sing|PronType=Art,Prs", POS: PRON}, + "PRON__Animacy=Hum|Case=Nom|Gender=Fem|Number=Sing|Person=3|PronType=Prs": { "morph": "Animacy=Hum|Case=Nom|Gender=Fem|Number=Sing|Person=3|PronType=Prs", POS: PRON}, + "PRON__Animacy=Hum|Case=Nom|Gender=Masc|Number=Sing|Person=3|PronType=Prs": { "morph": "Animacy=Hum|Case=Nom|Gender=Masc|Number=Sing|Person=3|PronType=Prs", POS: PRON}, + "PRON__Animacy=Hum|Case=Nom|Number=Plur|Person=1|PronType=Prs": {"morph": "Animacy=Hum|Case=Nom|Number=Plur|Person=1|PronType=Prs", POS: PRON}, + "PRON__Animacy=Hum|Case=Nom|Number=Plur|Person=2|PronType=Prs": {"morph": "Animacy=Hum|Case=Nom|Number=Plur|Person=2|PronType=Prs", POS: PRON}, + "PRON__Animacy=Hum|Case=Nom|Number=Sing|Person=1|PronType=Prs": {"morph": "Animacy=Hum|Case=Nom|Number=Sing|Person=1|PronType=Prs", POS: PRON}, + "PRON__Animacy=Hum|Case=Nom|Number=Sing|Person=2|PronType=Prs": {"morph": "Animacy=Hum|Case=Nom|Number=Sing|Person=2|PronType=Prs", POS: PRON}, + "PRON__Animacy=Hum|Case=Nom|Number=Sing|PronType=Prs": {"morph": "Animacy=Hum|Case=Nom|Number=Sing|PronType=Prs", POS: PRON}, + "PRON__Animacy=Hum|Number=Plur|PronType=Rcp": {"morph": "Animacy=Hum|Number=Plur|PronType=Rcp", POS: PRON}, + "PRON__Animacy=Hum|Number=Sing|PronType=Art,Prs": {"morph": "Animacy=Hum|Number=Sing|PronType=Art,Prs", POS: PRON}, + "PRON__Animacy=Hum|Poss=Yes|PronType=Int": {"morph": "Animacy=Hum|Poss=Yes|PronType=Int", POS: PRON}, + "PRON__Animacy=Hum|PronType=Int": {"morph": "Animacy=Hum|PronType=Int", POS: PRON}, + "PRON__Case=Acc|PronType=Prs|Reflex=Yes": {"morph": "Case=Acc|PronType=Prs|Reflex=Yes", POS: PRON}, + "PRON__Gender=Fem,Masc|Number=Sing|Person=3|Polarity=Neg|PronType=Neg,Prs": { "morph": "Gender=Fem,Masc|Number=Sing|Person=3|Polarity=Neg|PronType=Neg,Prs", POS: PRON}, + "PRON__Gender=Fem,Masc|Number=Sing|Person=3|PronType=Ind,Prs": {"morph": "Gender=Fem,Masc|Number=Sing|Person=3|PronType=Ind,Prs", POS: PRON}, + "PRON__Gender=Fem,Masc|Number=Sing|Person=3|PronType=Prs,Tot": {"morph": "Gender=Fem,Masc|Number=Sing|Person=3|PronType=Prs,Tot", POS: PRON}, + "PRON__Gender=Fem|Number=Sing|Poss=Yes|PronType=Prs": {"morph": "Gender=Fem|Number=Sing|Poss=Yes|PronType=Prs", POS: PRON}, + "PRON__Gender=Masc|Number=Sing|Poss=Yes|PronType=Prs": {"morph": "Gender=Masc|Number=Sing|Poss=Yes|PronType=Prs", POS: PRON}, + "PRON__Gender=Neut|Number=Sing|Person=3|PronType=Ind,Prs": {"morph": "Gender=Neut|Number=Sing|Person=3|PronType=Ind,Prs", POS: PRON}, + "PRON__Gender=Neut|Number=Sing|Poss=Yes|PronType=Prs": {"morph": "Gender=Neut|Number=Sing|Poss=Yes|PronType=Prs", POS: PRON}, + "PRON__Number=Plur|Person=3|Polarity=Neg|PronType=Neg,Prs": {"morph": "Number=Plur|Person=3|Polarity=Neg|PronType=Neg,Prs", POS: PRON}, + "PRON__Number=Plur|Person=3|PronType=Ind,Prs": {"morph": "Number=Plur|Person=3|PronType=Ind,Prs", POS: PRON}, + "PRON__Number=Plur|Person=3|PronType=Prs,Tot": {"morph": "Number=Plur|Person=3|PronType=Prs,Tot", POS: PRON}, + "PRON__Number=Plur|Poss=Yes|PronType=Prs": {"morph": "Number=Plur|Poss=Yes|PronType=Prs", POS: PRON}, + "PRON__Number=Plur|Poss=Yes|PronType=Rcp": {"morph": "Number=Plur|Poss=Yes|PronType=Rcp", POS: PRON}, + "PRON__Number=Sing|Polarity=Neg|PronType=Neg": {"morph": "Number=Sing|Polarity=Neg|PronType=Neg", POS: PRON}, + "PRON__PronType=Prs": {"morph": "PronType=Prs", POS: PRON}, + "PRON__PronType=Rel": {"morph": "PronType=Rel", POS: PRON}, + "PROPN__Abbr=Yes": {"morph": "Abbr=Yes", POS: PROPN}, + "PROPN__Abbr=Yes|Case=Gen": {"morph": "Abbr=Yes|Case=Gen", POS: PROPN}, + "VERB__Abbr=Yes|Mood=Ind|Tense=Pres|VerbForm=Fin": {"morph": "Abbr=Yes|Mood=Ind|Tense=Pres|VerbForm=Fin", POS: VERB}, + "VERB__Definite=Ind|Number=Sing|VerbForm=Part": {"morph": "Definite=Ind|Number=Sing|VerbForm=Part", POS: VERB}, } From 48ea2e8d0ff6e46f65761425c24369c5d4ac30e1 Mon Sep 17 00:00:00 2001 From: adrianeboyd Date: Wed, 27 Nov 2019 16:33:34 +0100 Subject: [PATCH 05/12] Restructure Sentencizer to follow Pipe API (#4721) * Restructure Sentencizer to follow Pipe API Restructure Sentencizer to follow Pipe API so that it can be scored with `nlp.evaluate()`. * Add Sentencizer pipe() test --- spacy/pipeline/pipes.pyx | 64 +++++++++++++++++++----- spacy/tests/pipeline/test_sentencizer.py | 12 +++++ 2 files changed, 63 insertions(+), 13 deletions(-) diff --git a/spacy/pipeline/pipes.pyx b/spacy/pipeline/pipes.pyx index 8bf36e9c2..97b27d7ce 100644 --- a/spacy/pipeline/pipes.pyx +++ b/spacy/pipeline/pipes.pyx @@ -1464,21 +1464,59 @@ class Sentencizer(object): DOCS: https://spacy.io/api/sentencizer#call """ - start = 0 - seen_period = False - for i, token in enumerate(doc): - is_in_punct_chars = token.text in self.punct_chars - token.is_sent_start = i == 0 - if seen_period and not token.is_punct and not is_in_punct_chars: - doc[start].is_sent_start = True - start = token.i - seen_period = False - elif is_in_punct_chars: - seen_period = True - if start < len(doc): - doc[start].is_sent_start = True + tags = self.predict([doc]) + self.set_annotations([doc], tags) return doc + def pipe(self, stream, batch_size=128, n_threads=-1): + for docs in util.minibatch(stream, size=batch_size): + docs = list(docs) + tag_ids = self.predict(docs) + self.set_annotations(docs, tag_ids) + yield from docs + + def predict(self, docs): + """Apply the pipeline's model to a batch of docs, without + modifying them. + """ + if not any(len(doc) for doc in docs): + # Handle cases where there are no tokens in any docs. + guesses = [[] for doc in docs] + return guesses + guesses = [] + for doc in docs: + start = 0 + seen_period = False + doc_guesses = [False] * len(doc) + doc_guesses[0] = True + for i, token in enumerate(doc): + is_in_punct_chars = token.text in self.punct_chars + if seen_period and not token.is_punct and not is_in_punct_chars: + doc_guesses[start] = True + start = token.i + seen_period = False + elif is_in_punct_chars: + seen_period = True + if start < len(doc): + doc_guesses[start] = True + guesses.append(doc_guesses) + return guesses + + def set_annotations(self, docs, batch_tag_ids, tensors=None): + if isinstance(docs, Doc): + docs = [docs] + cdef Doc doc + cdef int idx = 0 + for i, doc in enumerate(docs): + doc_tag_ids = batch_tag_ids[i] + for j, tag_id in enumerate(doc_tag_ids): + # Don't clobber existing sentence boundaries + if doc.c[j].sent_start == 0: + if tag_id: + doc.c[j].sent_start = 1 + else: + doc.c[j].sent_start = -1 + def to_bytes(self, **kwargs): """Serialize the sentencizer to a bytestring. diff --git a/spacy/tests/pipeline/test_sentencizer.py b/spacy/tests/pipeline/test_sentencizer.py index d91fdd198..359552c5b 100644 --- a/spacy/tests/pipeline/test_sentencizer.py +++ b/spacy/tests/pipeline/test_sentencizer.py @@ -5,6 +5,7 @@ import pytest import spacy from spacy.pipeline import Sentencizer from spacy.tokens import Doc +from spacy.lang.en import English def test_sentencizer(en_vocab): @@ -17,6 +18,17 @@ def test_sentencizer(en_vocab): assert len(list(doc.sents)) == 2 +def test_sentencizer_pipe(): + texts = ["Hello! This is a test.", "Hi! This is a test."] + nlp = English() + nlp.add_pipe(nlp.create_pipe("sentencizer")) + for doc in nlp.pipe(texts): + assert doc.is_sentenced + sent_starts = [t.is_sent_start for t in doc] + assert sent_starts == [True, False, True, False, False, False, False] + assert len(list(doc.sents)) == 2 + + @pytest.mark.parametrize( "words,sent_starts,n_sents", [ From a7ee4b6f17b5b66fc5a43d4ba94167ff9bcc16a6 Mon Sep 17 00:00:00 2001 From: Christoph Purschke Date: Sun, 1 Dec 2019 23:08:21 +0100 Subject: [PATCH 06/12] new tests & tokenization fixes (#4734) - added some tests for tokenization issues - fixed some issues with tokenization of words with hyphen infix - rewrote the "tokenizer_exceptions.py" file (stemming from the German version) --- spacy/lang/lb/norm_exceptions.py | 2 +- spacy/lang/lb/punctuation.py | 23 +++++++++++++++-------- spacy/lang/lb/tokenizer_exceptions.py | 6 ++++-- spacy/tests/lang/lb/test_exceptions.py | 18 +++++++++++++++++- spacy/tests/lang/lb/test_text.py | 1 + 5 files changed, 38 insertions(+), 12 deletions(-) diff --git a/spacy/lang/lb/norm_exceptions.py b/spacy/lang/lb/norm_exceptions.py index 101102ca4..7063e6863 100644 --- a/spacy/lang/lb/norm_exceptions.py +++ b/spacy/lang/lb/norm_exceptions.py @@ -6,7 +6,7 @@ from __future__ import unicode_literals # variants (vläicht = vlaicht, vleicht, viläicht, viläischt, etc. etc.) # here one could include the most common spelling mistakes -_exc = {"datt": "dass", "wgl.": "weg.", "vläicht": "viläicht"} +_exc = {"dass": "datt", "viläicht": "vläicht"} NORM_EXCEPTIONS = {} diff --git a/spacy/lang/lb/punctuation.py b/spacy/lang/lb/punctuation.py index 53df6bcd9..68531d9d0 100644 --- a/spacy/lang/lb/punctuation.py +++ b/spacy/lang/lb/punctuation.py @@ -1,16 +1,23 @@ # coding: utf8 from __future__ import unicode_literals -from ..punctuation import TOKENIZER_INFIXES -from ..char_classes import ALPHA - +from ..char_classes import LIST_ELLIPSES, LIST_ICONS +from ..char_classes import CONCAT_QUOTES, ALPHA, ALPHA_LOWER, ALPHA_UPPER ELISION = " ' ’ ".strip().replace(" ", "") -HYPHENS = r"- – — ‐ ‑".strip().replace(" ", "") - -_infixes = TOKENIZER_INFIXES + [ - r"(?<=[{a}][{el}])(?=[{a}])".format(a=ALPHA, el=ELISION) -] +_infixes = ( + LIST_ELLIPSES + + LIST_ICONS + + [ + r"(?<=[{a}][{el}])(?=[{a}])".format(a=ALPHA, el=ELISION), + r"(?<=[{al}])\.(?=[{au}])".format(al=ALPHA_LOWER, au=ALPHA_UPPER), + r"(?<=[{a}])[,!?](?=[{a}])".format(a=ALPHA), + r"(?<=[{a}])[:<>=](?=[{a}])".format(a=ALPHA), + r"(?<=[{a}]),(?=[{a}])".format(a=ALPHA), + r"(?<=[{a}])--(?=[{a}])".format(a=ALPHA), + r"(?<=[0-9])-(?=[0-9])", + ] +) TOKENIZER_INFIXES = _infixes diff --git a/spacy/lang/lb/tokenizer_exceptions.py b/spacy/lang/lb/tokenizer_exceptions.py index d84372aef..8a35b6fb7 100644 --- a/spacy/lang/lb/tokenizer_exceptions.py +++ b/spacy/lang/lb/tokenizer_exceptions.py @@ -10,7 +10,9 @@ _exc = {} # translate / delete what is not necessary for exc_data in [ - {ORTH: "wgl.", LEMMA: "wann ech gelift", NORM: "wann ech gelieft"}, + {ORTH: "'t", LEMMA: "et", NORM: "et"}, + {ORTH: "'T", LEMMA: "et", NORM: "et"}, + {ORTH: "wgl.", LEMMA: "wannechgelift", NORM: "wannechgelift"}, {ORTH: "M.", LEMMA: "Monsieur", NORM: "Monsieur"}, {ORTH: "Mme.", LEMMA: "Madame", NORM: "Madame"}, {ORTH: "Dr.", LEMMA: "Dokter", NORM: "Dokter"}, @@ -18,7 +20,7 @@ for exc_data in [ {ORTH: "asw.", LEMMA: "an sou weider", NORM: "an sou weider"}, {ORTH: "etc.", LEMMA: "et cetera", NORM: "et cetera"}, {ORTH: "bzw.", LEMMA: "bezéiungsweis", NORM: "bezéiungsweis"}, - {ORTH: "Jan.", LEMMA: "Januar", NORM: "Januar"}, + {ORTH: "Jan.", LEMMA: "Januar", NORM: "Januar"} ]: _exc[exc_data[ORTH]] = [exc_data] diff --git a/spacy/tests/lang/lb/test_exceptions.py b/spacy/tests/lang/lb/test_exceptions.py index ca38c2c38..57541fc26 100644 --- a/spacy/tests/lang/lb/test_exceptions.py +++ b/spacy/tests/lang/lb/test_exceptions.py @@ -3,8 +3,24 @@ from __future__ import unicode_literals import pytest - @pytest.mark.parametrize("text", ["z.B.", "Jan."]) def test_lb_tokenizer_handles_abbr(lb_tokenizer, text): tokens = lb_tokenizer(text) assert len(tokens) == 1 + +@pytest.mark.parametrize("text", ["d'Saach", "d'Kanner", "d’Welt", "d’Suen"]) +def test_lb_tokenizer_splits_contractions(lb_tokenizer, text): + tokens = lb_tokenizer(text) + assert len(tokens) == 2 + +def test_lb_tokenizer_handles_exc_in_text(lb_tokenizer): + text = "Mee 't ass net evident, d'Liewen." + tokens = lb_tokenizer(text) + assert len(tokens) == 9 + assert tokens[1].text == "'t" + assert tokens[1].lemma_ == "et" + +@pytest.mark.parametrize("text,norm", [("dass", "datt"), ("viläicht", "vläicht")]) +def test_lb_norm_exceptions(lb_tokenizer, text, norm): + tokens = lb_tokenizer(text) + assert tokens[0].norm_ == norm diff --git a/spacy/tests/lang/lb/test_text.py b/spacy/tests/lang/lb/test_text.py index 387cf448d..2284ff794 100644 --- a/spacy/tests/lang/lb/test_text.py +++ b/spacy/tests/lang/lb/test_text.py @@ -16,6 +16,7 @@ def test_lb_tokenizer_handles_long_text(lb_tokenizer): [ ("»Wat ass mat mir geschitt?«, huet hie geduecht.", 13), ("“Dëst fréi Opstoen”, denkt hien, “mécht ee ganz duercherneen. ", 15), + ("Am Grand-Duché ass d'Liewen schéin, mee 't gëtt ze vill Autoen.", 14) ], ) def test_lb_tokenizer_handles_examples(lb_tokenizer, text, length): From e626a011cc94d77aa67a09a58f097d60cb1134c0 Mon Sep 17 00:00:00 2001 From: Antti Ajanki Date: Tue, 3 Dec 2019 13:55:28 +0200 Subject: [PATCH 07/12] Improvements to the Finnish language data (#4738) * Enable lex_attrs on Finnish * Copy the Danish tokenizer rules to Finnish Specifically, don't break hyphenated compound words * Contributor agreement * A new file for Finnish tokenizer rules instead of including the Danish ones --- .github/contributors/aajanki.md | 106 ++++++++++++++++++++++++++ spacy/lang/fi/__init__.py | 5 ++ spacy/lang/fi/lex_attrs.py | 3 +- spacy/lang/fi/punctuation.py | 33 ++++++++ spacy/tests/lang/fi/test_text.py | 27 +++++++ spacy/tests/lang/fi/test_tokenizer.py | 16 +++- 6 files changed, 188 insertions(+), 2 deletions(-) create mode 100644 .github/contributors/aajanki.md create mode 100644 spacy/lang/fi/punctuation.py create mode 100644 spacy/tests/lang/fi/test_text.py diff --git a/.github/contributors/aajanki.md b/.github/contributors/aajanki.md new file mode 100644 index 000000000..de7dc7fa2 --- /dev/null +++ b/.github/contributors/aajanki.md @@ -0,0 +1,106 @@ +# spaCy contributor agreement + +This spaCy Contributor Agreement (**"SCA"**) is based on the +[Oracle Contributor Agreement](http://www.oracle.com/technetwork/oca-405177.pdf). +The SCA applies to any contribution that you make to any product or project +managed by us (the **"project"**), and sets out the intellectual property rights +you grant to us in the contributed materials. The term **"us"** shall mean +[ExplosionAI GmbH](https://explosion.ai/legal). The term +**"you"** shall mean the person or entity identified below. + +If you agree to be bound by these terms, fill in the information requested +below and include the filled-in version with your first pull request, under the +folder [`.github/contributors/`](/.github/contributors/). The name of the file +should be your GitHub username, with the extension `.md`. For example, the user +example_user would create the file `.github/contributors/example_user.md`. + +Read this agreement carefully before signing. These terms and conditions +constitute a binding legal agreement. + +## Contributor Agreement + +1. The term "contribution" or "contributed materials" means any source code, +object code, patch, tool, sample, graphic, specification, manual, +documentation, or any other material posted or submitted by you to the project. + +2. With respect to any worldwide copyrights, or copyright applications and +registrations, in your contribution: + + * you hereby assign to us joint ownership, and to the extent that such + assignment is or becomes invalid, ineffective or unenforceable, you hereby + grant to us a perpetual, irrevocable, non-exclusive, worldwide, no-charge, + royalty-free, unrestricted license to exercise all rights under those + copyrights. This includes, at our option, the right to sublicense these same + rights to third parties through multiple levels of sublicensees or other + licensing arrangements; + + * you agree that each of us can do all things in relation to your + contribution as if each of us were the sole owners, and if one of us makes + a derivative work of your contribution, the one who makes the derivative + work (or has it made will be the sole owner of that derivative work; + + * you agree that you will not assert any moral rights in your contribution + against us, our licensees or transferees; + + * you agree that we may register a copyright in your contribution and + exercise all ownership rights associated with it; and + + * you agree that neither of us has any duty to consult with, obtain the + consent of, pay or render an accounting to the other for any use or + distribution of your contribution. + +3. With respect to any patents you own, or that you can license without payment +to any third party, you hereby grant to us a perpetual, irrevocable, +non-exclusive, worldwide, no-charge, royalty-free license to: + + * make, have made, use, sell, offer to sell, import, and otherwise transfer + your contribution in whole or in part, alone or in combination with or + included in any product, work or materials arising out of the project to + which your contribution was submitted, and + + * at our option, to sublicense these same rights to third parties through + multiple levels of sublicensees or other licensing arrangements. + +4. Except as set out above, you keep all right, title, and interest in your +contribution. The rights that you grant to us under these terms are effective +on the date you first submitted a contribution to us, even if your submission +took place before the date you sign these terms. + +5. You covenant, represent, warrant and agree that: + + * Each contribution that you submit is and shall be an original work of + authorship and you can legally grant the rights set out in this SCA; + + * to the best of your knowledge, each contribution will not violate any + third party's copyrights, trademarks, patents, or other intellectual + property rights; and + + * each contribution shall be in compliance with U.S. export control laws and + other applicable export and import laws. You agree to notify us if you + become aware of any circumstance which would make any of the foregoing + representations inaccurate in any respect. We may publicly disclose your + participation in the project, including the fact that you have signed the SCA. + +6. This SCA is governed by the laws of the State of California and applicable +U.S. Federal law. Any choice of law rules will not apply. + +7. Please place an “x” on one of the applicable statement below. Please do NOT +mark both statements: + + * [x] I am signing on behalf of myself as an individual and no other person + or entity, including my employer, has or will have rights with respect to my + contributions. + + * [ ] I am signing on behalf of my employer or a legal entity and I have the + actual authority to contractually bind that entity. + +## Contributor Details + +| Field | Entry | +|------------------------------- | -------------------- | +| Name | Antti Ajanki | +| Company name (if applicable) | | +| Title or role (if applicable) | | +| Date | 2019-11-30 | +| GitHub username | aajanki | +| Website (optional) | | diff --git a/spacy/lang/fi/__init__.py b/spacy/lang/fi/__init__.py index 6debe999c..45d2f886f 100644 --- a/spacy/lang/fi/__init__.py +++ b/spacy/lang/fi/__init__.py @@ -3,6 +3,8 @@ from __future__ import unicode_literals from .tokenizer_exceptions import TOKENIZER_EXCEPTIONS from .stop_words import STOP_WORDS +from .lex_attrs import LEX_ATTRS +from .punctuation import TOKENIZER_INFIXES, TOKENIZER_SUFFIXES from ..tokenizer_exceptions import BASE_EXCEPTIONS from ..norm_exceptions import BASE_NORMS @@ -13,10 +15,13 @@ from ...util import update_exc, add_lookups class FinnishDefaults(Language.Defaults): lex_attr_getters = dict(Language.Defaults.lex_attr_getters) + lex_attr_getters.update(LEX_ATTRS) lex_attr_getters[LANG] = lambda text: "fi" lex_attr_getters[NORM] = add_lookups( Language.Defaults.lex_attr_getters[NORM], BASE_NORMS ) + infixes = TOKENIZER_INFIXES + suffixes = TOKENIZER_SUFFIXES tokenizer_exceptions = update_exc(BASE_EXCEPTIONS, TOKENIZER_EXCEPTIONS) stop_words = STOP_WORDS diff --git a/spacy/lang/fi/lex_attrs.py b/spacy/lang/fi/lex_attrs.py index 97c876837..e960b55eb 100644 --- a/spacy/lang/fi/lex_attrs.py +++ b/spacy/lang/fi/lex_attrs.py @@ -18,7 +18,8 @@ _num_words = [ "kymmenen", "yksitoista", "kaksitoista", - "kolmetoista" "neljätoista", + "kolmetoista", + "neljätoista", "viisitoista", "kuusitoista", "seitsemäntoista", diff --git a/spacy/lang/fi/punctuation.py b/spacy/lang/fi/punctuation.py new file mode 100644 index 000000000..02eb1b200 --- /dev/null +++ b/spacy/lang/fi/punctuation.py @@ -0,0 +1,33 @@ +# coding: utf8 +from __future__ import unicode_literals + +from ..char_classes import LIST_ELLIPSES, LIST_ICONS +from ..char_classes import CONCAT_QUOTES, ALPHA, ALPHA_LOWER, ALPHA_UPPER +from ..punctuation import TOKENIZER_SUFFIXES + + +_quotes = CONCAT_QUOTES.replace("'", "") + +_infixes = ( + LIST_ELLIPSES + + LIST_ICONS + + [ + r"(?<=[{al}])\.(?=[{au}])".format(al=ALPHA_LOWER, au=ALPHA_UPPER), + r"(?<=[{a}])[,!?](?=[{a}])".format(a=ALPHA), + r"(?<=[{a}])[:<>=](?=[{a}])".format(a=ALPHA), + r"(?<=[{a}]),(?=[{a}])".format(a=ALPHA), + r"(?<=[{a}])([{q}\)\]\(\[])(?=[{a}])".format(a=ALPHA, q=_quotes), + r"(?<=[{a}])--(?=[{a}])".format(a=ALPHA), + r"(?<=[{a}0-9])[:<>=/](?=[{a}])".format(a=ALPHA), + ] +) + +_suffixes = [ + suffix + for suffix in TOKENIZER_SUFFIXES + if suffix not in ["'s", "'S", "’s", "’S", r"\'"] +] + + +TOKENIZER_INFIXES = _infixes +TOKENIZER_SUFFIXES = _suffixes diff --git a/spacy/tests/lang/fi/test_text.py b/spacy/tests/lang/fi/test_text.py new file mode 100644 index 000000000..2dd92597e --- /dev/null +++ b/spacy/tests/lang/fi/test_text.py @@ -0,0 +1,27 @@ +# coding: utf-8 +from __future__ import unicode_literals + +import pytest + + +@pytest.mark.parametrize( + "text,match", + [ + ("10", True), + ("1", True), + ("10000", True), + ("10,00", True), + ("-999,0", True), + ("yksi", True), + ("kolmetoista", True), + ("viisikymmentä", True), + ("tuhat", True), + ("1/2", True), + ("hevonen", False), + (",", False), + ], +) +def test_fi_lex_attrs_like_number(fi_tokenizer, text, match): + tokens = fi_tokenizer(text) + assert len(tokens) == 1 + assert tokens[0].like_num == match diff --git a/spacy/tests/lang/fi/test_tokenizer.py b/spacy/tests/lang/fi/test_tokenizer.py index 66be7bd46..cbbebcf28 100644 --- a/spacy/tests/lang/fi/test_tokenizer.py +++ b/spacy/tests/lang/fi/test_tokenizer.py @@ -12,9 +12,23 @@ ABBREVIATION_TESTS = [ ("Paino on n. 2.2 kg", ["Paino", "on", "n.", "2.2", "kg"]), ] +HYPHENATED_TESTS = [ + ( + "1700-luvulle sijoittuva taide-elokuva", + ["1700-luvulle", "sijoittuva", "taide-elokuva"] + ) +] + @pytest.mark.parametrize("text,expected_tokens", ABBREVIATION_TESTS) -def test_fi_tokenizer_handles_testcases(fi_tokenizer, text, expected_tokens): +def test_fi_tokenizer_abbreviations(fi_tokenizer, text, expected_tokens): + tokens = fi_tokenizer(text) + token_list = [token.text for token in tokens if not token.is_space] + assert expected_tokens == token_list + + +@pytest.mark.parametrize("text,expected_tokens", HYPHENATED_TESTS) +def test_fi_tokenizer_hyphenated_words(fi_tokenizer, text, expected_tokens): tokens = fi_tokenizer(text) token_list = [token.text for token in tokens if not token.is_space] assert expected_tokens == token_list From 676e75838f852246ac045837cd852b854b06d77e Mon Sep 17 00:00:00 2001 From: adrianeboyd Date: Fri, 6 Dec 2019 14:07:39 +0100 Subject: [PATCH 08/12] Include Doc.cats in serialization of Doc and DocBin (#4774) * Include Doc.cats in to_bytes() * Include Doc.cats in DocBin serialization * Add tests for serialization of cats Test serialization of cats for Doc and DocBin. --- spacy/tests/serialize/test_serialize_doc.py | 8 +++++++- spacy/tokens/_serialize.py | 6 ++++++ spacy/tokens/doc.pyx | 4 ++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/spacy/tests/serialize/test_serialize_doc.py b/spacy/tests/serialize/test_serialize_doc.py index 87b087760..ef2b1ee89 100644 --- a/spacy/tests/serialize/test_serialize_doc.py +++ b/spacy/tests/serialize/test_serialize_doc.py @@ -24,6 +24,7 @@ def test_serialize_empty_doc(en_vocab): def test_serialize_doc_roundtrip_bytes(en_vocab): doc = Doc(en_vocab, words=["hello", "world"]) + doc.cats = {"A": 0.5} doc_b = doc.to_bytes() new_doc = Doc(en_vocab).from_bytes(doc_b) assert new_doc.to_bytes() == doc_b @@ -66,12 +67,17 @@ def test_serialize_doc_exclude(en_vocab): def test_serialize_doc_bin(): doc_bin = DocBin(attrs=["LEMMA", "ENT_IOB", "ENT_TYPE"], store_user_data=True) texts = ["Some text", "Lots of texts...", "..."] + cats = {"A": 0.5} nlp = English() for doc in nlp.pipe(texts): + doc.cats = cats doc_bin.add(doc) bytes_data = doc_bin.to_bytes() # Deserialize later, e.g. in a new process nlp = spacy.blank("en") doc_bin = DocBin().from_bytes(bytes_data) - list(doc_bin.get_docs(nlp.vocab)) + reloaded_docs = list(doc_bin.get_docs(nlp.vocab)) + for i, doc in enumerate(reloaded_docs): + assert doc.text == texts[i] + assert doc.cats == cats diff --git a/spacy/tokens/_serialize.py b/spacy/tokens/_serialize.py index 18cb8a234..b60a6d7b3 100644 --- a/spacy/tokens/_serialize.py +++ b/spacy/tokens/_serialize.py @@ -58,6 +58,7 @@ class DocBin(object): self.attrs.insert(0, ORTH) # Ensure ORTH is always attrs[0] self.tokens = [] self.spaces = [] + self.cats = [] self.user_data = [] self.strings = set() self.store_user_data = store_user_data @@ -82,6 +83,7 @@ class DocBin(object): spaces = spaces.reshape((spaces.shape[0], 1)) self.spaces.append(numpy.asarray(spaces, dtype=bool)) self.strings.update(w.text for w in doc) + self.cats.append(doc.cats) if self.store_user_data: self.user_data.append(srsly.msgpack_dumps(doc.user_data)) @@ -102,6 +104,7 @@ class DocBin(object): words = [vocab.strings[orth] for orth in tokens[:, orth_col]] doc = Doc(vocab, words=words, spaces=spaces) doc = doc.from_array(self.attrs, tokens) + doc.cats = self.cats[i] if self.store_user_data: user_data = srsly.msgpack_loads(self.user_data[i], use_list=False) doc.user_data.update(user_data) @@ -121,6 +124,7 @@ class DocBin(object): self.tokens.extend(other.tokens) self.spaces.extend(other.spaces) self.strings.update(other.strings) + self.cats.extend(other.cats) if self.store_user_data: self.user_data.extend(other.user_data) @@ -140,6 +144,7 @@ class DocBin(object): "spaces": numpy.vstack(self.spaces).tobytes("C"), "lengths": numpy.asarray(lengths, dtype="int32").tobytes("C"), "strings": list(self.strings), + "cats": self.cats, } if self.store_user_data: msg["user_data"] = self.user_data @@ -164,6 +169,7 @@ class DocBin(object): flat_spaces = flat_spaces.reshape((flat_spaces.size, 1)) self.tokens = NumpyOps().unflatten(flat_tokens, lengths) self.spaces = NumpyOps().unflatten(flat_spaces, lengths) + self.cats = msg["cats"] if self.store_user_data and "user_data" in msg: self.user_data = list(msg["user_data"]) for tokens in self.tokens: diff --git a/spacy/tokens/doc.pyx b/spacy/tokens/doc.pyx index d4ba3803b..716df1087 100644 --- a/spacy/tokens/doc.pyx +++ b/spacy/tokens/doc.pyx @@ -887,6 +887,7 @@ cdef class Doc: "array_body": lambda: self.to_array(array_head), "sentiment": lambda: self.sentiment, "tensor": lambda: self.tensor, + "cats": lambda: self.cats, } for key in kwargs: if key in serializers or key in ("user_data", "user_data_keys", "user_data_values"): @@ -916,6 +917,7 @@ cdef class Doc: "array_body": lambda b: None, "sentiment": lambda b: None, "tensor": lambda b: None, + "cats": lambda b: None, "user_data_keys": lambda b: None, "user_data_values": lambda b: None, } @@ -937,6 +939,8 @@ cdef class Doc: self.sentiment = msg["sentiment"] if "tensor" not in exclude and "tensor" in msg: self.tensor = msg["tensor"] + if "cats" not in exclude and "cats" in msg: + self.cats = msg["cats"] start = 0 cdef const LexemeC* lex cdef unicode orth_ From de5453cdcb4b62eedf9006382edc6300345a8ba4 Mon Sep 17 00:00:00 2001 From: Nicolai Bjerre Pedersen Date: Fri, 6 Dec 2019 19:17:12 +0100 Subject: [PATCH 09/12] Fix link to user hooks in docs (#4778) * Fix link to user hooks in docs * Update mr_bjerre.md Mistake in contributor agreement * Apparently hard to get it right (wrong name of sca) --- .github/contributors/mr-bjerre.md | 87 ++++++++++++++++++++++++ website/docs/usage/vectors-similarity.md | 2 +- 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 .github/contributors/mr-bjerre.md diff --git a/.github/contributors/mr-bjerre.md b/.github/contributors/mr-bjerre.md new file mode 100644 index 000000000..ad3695bef --- /dev/null +++ b/.github/contributors/mr-bjerre.md @@ -0,0 +1,87 @@ +## Contributor Agreement + +1. The term "contribution" or "contributed materials" means any source code, +object code, patch, tool, sample, graphic, specification, manual, +documentation, or any other material posted or submitted by you to the project. + +2. With respect to any worldwide copyrights, or copyright applications and +registrations, in your contribution: + + * you hereby assign to us joint ownership, and to the extent that such + assignment is or becomes invalid, ineffective or unenforceable, you hereby + grant to us a perpetual, irrevocable, non-exclusive, worldwide, no-charge, + royalty-free, unrestricted license to exercise all rights under those + copyrights. This includes, at our option, the right to sublicense these same + rights to third parties through multiple levels of sublicensees or other + licensing arrangements; + + * you agree that each of us can do all things in relation to your + contribution as if each of us were the sole owners, and if one of us makes + a derivative work of your contribution, the one who makes the derivative + work (or has it made will be the sole owner of that derivative work; + + * you agree that you will not assert any moral rights in your contribution + against us, our licensees or transferees; + + * you agree that we may register a copyright in your contribution and + exercise all ownership rights associated with it; and + + * you agree that neither of us has any duty to consult with, obtain the + consent of, pay or render an accounting to the other for any use or + distribution of your contribution. + +3. With respect to any patents you own, or that you can license without payment +to any third party, you hereby grant to us a perpetual, irrevocable, +non-exclusive, worldwide, no-charge, royalty-free license to: + + * make, have made, use, sell, offer to sell, import, and otherwise transfer + your contribution in whole or in part, alone or in combination with or + included in any product, work or materials arising out of the project to + which your contribution was submitted, and + + * at our option, to sublicense these same rights to third parties through + multiple levels of sublicensees or other licensing arrangements. + +4. Except as set out above, you keep all right, title, and interest in your +contribution. The rights that you grant to us under these terms are effective +on the date you first submitted a contribution to us, even if your submission +took place before the date you sign these terms. + +5. You covenant, represent, warrant and agree that: + + * Each contribution that you submit is and shall be an original work of + authorship and you can legally grant the rights set out in this SCA; + + * to the best of your knowledge, each contribution will not violate any + third party's copyrights, trademarks, patents, or other intellectual + property rights; and + + * each contribution shall be in compliance with U.S. export control laws and + other applicable export and import laws. You agree to notify us if you + become aware of any circumstance which would make any of the foregoing + representations inaccurate in any respect. We may publicly disclose your + participation in the project, including the fact that you have signed the SCA. + +6. This SCA is governed by the laws of the State of California and applicable +U.S. Federal law. Any choice of law rules will not apply. + +7. Please place an “x” on one of the applicable statement below. Please do NOT +mark both statements: + + * [x] I am signing on behalf of myself as an individual and no other person + or entity, including my employer, has or will have rights with respect to my + contributions. + + * [ ] I am signing on behalf of my employer or a legal entity and I have the + actual authority to contractually bind that entity. + +## Contributor Details + +| Field | Entry | +|------------------------------- | -------------------- | +| Name | Nicolai Bjerre Pedersen | +| Company name (if applicable) | | +| Title or role (if applicable) | | +| Date | 2019-12-06 | +| GitHub username | mr_bjerre | +| Website (optional) | | diff --git a/website/docs/usage/vectors-similarity.md b/website/docs/usage/vectors-similarity.md index 53648f66e..aff797b84 100644 --- a/website/docs/usage/vectors-similarity.md +++ b/website/docs/usage/vectors-similarity.md @@ -219,7 +219,7 @@ tokens. You can customize these behaviors by modifying the `doc.user_hooks`, For more details on **adding hooks** and **overwriting** the built-in `Doc`, `Span` and `Token` methods, see the usage guide on -[user hooks](/usage/processing-pipelines#user-hooks). +[user hooks](/usage/processing-pipelines#custom-components-user-hooks). From bf611ebca7e7f436d94bfa6153f251ebaa41be73 Mon Sep 17 00:00:00 2001 From: Ines Montani Date: Fri, 6 Dec 2019 19:17:45 +0100 Subject: [PATCH 10/12] Document jsonl option on converter [ci skip] --- website/docs/api/cli.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/website/docs/api/cli.md b/website/docs/api/cli.md index a37921f3c..94bedd889 100644 --- a/website/docs/api/cli.md +++ b/website/docs/api/cli.md @@ -166,14 +166,13 @@ All output files generated by this command are compatible with ### Converter options - - | ID | Description | | ------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `auto` | Automatically pick converter based on file extension and file content (default). | | `conll`, `conllu`, `conllubio` | Universal Dependencies `.conllu` or `.conll` format. | | `ner` | NER with IOB/IOB2 tags, one token per line with columns separated by whitespace. The first column is the token and the final column is the IOB tag. Sentences are separated by blank lines and documents are separated by the line `-DOCSTART- -X- O O`. Supports CoNLL 2003 NER format. See [sample data](https://github.com/explosion/spaCy/tree/master/examples/training/ner_example_data). | | `iob` | NER with IOB/IOB2 tags, one sentence per line with tokens separated by whitespace and annotation separated by `|`, either `word|B-ENT` or `word|POS|B-ENT`. See [sample data](https://github.com/explosion/spaCy/tree/master/examples/training/ner_example_data). | +| `jsonl` | NER data formatted as JSONL with one dict per line and a `"text"` and `"spans"` key. This is also the format exported by the [Prodigy](https://prodi.gy) annotation tool. See [sample data](https://raw.githubusercontent.com/explosion/projects/master/ner-fashion-brands/fashion_brands_training.jsonl). | ## Debug data {#debug-data new="2.2"} From 780d43aac7ecceb177d27eb76424c516a869d014 Mon Sep 17 00:00:00 2001 From: Sofie Van Landeghem Date: Fri, 6 Dec 2019 19:18:14 +0100 Subject: [PATCH 11/12] fix bug in EL predict (#4779) --- spacy/pipeline/pipes.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spacy/pipeline/pipes.pyx b/spacy/pipeline/pipes.pyx index 97b27d7ce..82f9b665f 100644 --- a/spacy/pipeline/pipes.pyx +++ b/spacy/pipeline/pipes.pyx @@ -1302,7 +1302,7 @@ class EntityLinker(Pipe): if len(doc) > 0: # Looping through each sentence and each entity # This may go wrong if there are entities across sentences - because they might not get a KB ID - for sent in doc.ents: + for sent in doc.sents: sent_doc = sent.as_doc() # currently, the context is the same for each entity in a sentence (should be refined) sentence_encoding = self.model([sent_doc])[0] From ab8dc2732cc7d7caef41e0a6fab3da0e9d075b9d Mon Sep 17 00:00:00 2001 From: Tclack88 <39845330+Tclack88@users.noreply.github.com> Date: Fri, 6 Dec 2019 10:22:02 -0800 Subject: [PATCH 12/12] Update token.md (#4767) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update token.md documentation is confusing: A '?' is a right punct, but '¿' is a left punct * Update token.md add quotations around parentheses in `is_left_punct` and `is_right_punct` for clarrification, ensuring the question mark that follows is not percieved as an example of left and right punctuation * Move quotes into code block [ci skip] --- website/docs/api/token.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/api/token.md b/website/docs/api/token.md index 9d0c2371c..68402d1b4 100644 --- a/website/docs/api/token.md +++ b/website/docs/api/token.md @@ -450,8 +450,8 @@ The L2 norm of the token's vector representation. | `is_upper` | bool | Is the token in uppercase? Equivalent to `token.text.isupper()`. | | `is_title` | bool | Is the token in titlecase? Equivalent to `token.text.istitle()`. | | `is_punct` | bool | Is the token punctuation? | -| `is_left_punct` | bool | Is the token a left punctuation mark, e.g. `(`? | -| `is_right_punct` | bool | Is the token a right punctuation mark, e.g. `)`? | +| `is_left_punct` | bool | Is the token a left punctuation mark, e.g. `'('` ? | +| `is_right_punct` | bool | Is the token a right punctuation mark, e.g. `')'` ? | | `is_space` | bool | Does the token consist of whitespace characters? Equivalent to `token.text.isspace()`. | | `is_bracket` | bool | Is the token a bracket? | | `is_quote` | bool | Is the token a quotation mark? |