diff --git a/spacy/pipeline/attributeruler.py b/spacy/pipeline/attributeruler.py index 8e48dce57..4d1816238 100644 --- a/spacy/pipeline/attributeruler.py +++ b/spacy/pipeline/attributeruler.py @@ -78,7 +78,7 @@ class AttributeRuler(Pipe): DOCS: https://spacy.io/api/attributeruler#call """ - matches = self.matcher(doc) + matches = sorted(self.matcher(doc)) for match_id, start, end in matches: span = Span(doc, start, end, label=match_id) diff --git a/spacy/tests/pipeline/test_attributeruler.py b/spacy/tests/pipeline/test_attributeruler.py index 4c7cdb4e7..5fc6faaf0 100644 --- a/spacy/tests/pipeline/test_attributeruler.py +++ b/spacy/tests/pipeline/test_attributeruler.py @@ -112,6 +112,28 @@ def test_attributeruler_score(nlp, pattern_dicts): assert scores["morph_acc"] == pytest.approx(0.6) +def test_attributeruler_rule_order(nlp): + a = AttributeRuler(nlp.vocab) + patterns = [ + { + "patterns": [[{"TAG": "VBZ"}]], + "attrs": {"POS": "VERB"}, + }, + { + "patterns": [[{"TAG": "VBZ"}]], + "attrs": {"POS": "NOUN"}, + }, + ] + a.add_patterns(patterns) + doc = get_doc( + nlp.vocab, + words=["This", "is", "a", "test", "."], + tags=["DT", "VBZ", "DT", "NN", "."] + ) + doc = a(doc) + assert doc[1].pos_ == "NOUN" + + def test_attributeruler_tag_map(nlp, tag_map): a = AttributeRuler(nlp.vocab) a.load_from_tag_map(tag_map)