From 1c6d77610c304eba1b5cb9016d041e00645f5c6d Mon Sep 17 00:00:00 2001 From: ines Date: Sat, 28 Apr 2018 23:33:09 +0200 Subject: [PATCH] Add remove_extension method on Doc, Token and Span (closes #2242) --- spacy/tests/test_underscore.py | 9 +++++++++ spacy/tokens/doc.pyx | 6 ++++++ spacy/tokens/span.pyx | 6 ++++++ spacy/tokens/token.pyx | 6 ++++++ 4 files changed, 27 insertions(+) diff --git a/spacy/tests/test_underscore.py b/spacy/tests/test_underscore.py index d2928d82f..15ad0e6f4 100644 --- a/spacy/tests/test_underscore.py +++ b/spacy/tests/test_underscore.py @@ -60,6 +60,15 @@ def test_token_underscore_method(): assert token._.hello() == 'cheese' +@pytest.mark.parametrize('obj', [Doc, Span, Token]) +def test_doc_underscore_remove_extension(obj): + ext_name = 'to_be_removed' + obj.set_extension(ext_name, default=False) + assert obj.has_extension(ext_name) + obj.remove_extension(ext_name) + assert not obj.has_extension(ext_name) + + @pytest.mark.parametrize('obj', [Doc, Span, Token]) def test_underscore_raises_for_dup(obj): obj.set_extension('test', default=None) diff --git a/spacy/tokens/doc.pyx b/spacy/tokens/doc.pyx index c9dcde905..1e919347c 100644 --- a/spacy/tokens/doc.pyx +++ b/spacy/tokens/doc.pyx @@ -108,6 +108,12 @@ cdef class Doc: def has_extension(cls, name): return name in Underscore.doc_extensions + @classmethod + def remove_extension(cls, name): + if not cls.has_extension(name): + raise ValueError(Errors.E046.format(name=name)) + return Underscore.doc_extensions.pop(name) + def __init__(self, Vocab vocab, words=None, spaces=None, user_data=None, orths_and_spaces=None): """Create a Doc object. diff --git a/spacy/tokens/span.pyx b/spacy/tokens/span.pyx index 1377655bc..ae57bc721 100644 --- a/spacy/tokens/span.pyx +++ b/spacy/tokens/span.pyx @@ -36,6 +36,12 @@ cdef class Span: def has_extension(cls, name): return name in Underscore.span_extensions + @classmethod + def remove_extension(cls, name): + if not cls.has_extension(name): + raise ValueError(Errors.E046.format(name=name)) + return Underscore.span_extensions.pop(name) + def __cinit__(self, Doc doc, int start, int end, attr_t label=0, vector=None, vector_norm=None): """Create a `Span` object from the slice `doc[start : end]`. diff --git a/spacy/tokens/token.pyx b/spacy/tokens/token.pyx index 631a0f447..5738f785e 100644 --- a/spacy/tokens/token.pyx +++ b/spacy/tokens/token.pyx @@ -41,6 +41,12 @@ cdef class Token: def has_extension(cls, name): return name in Underscore.token_extensions + @classmethod + def remove_extension(cls, name): + if not cls.has_extension(name): + raise ValueError(Errors.E046.format(name=name)) + return Underscore.token_extensions.pop(name) + def __cinit__(self, Vocab vocab, Doc doc, int offset): """Construct a `Token` object.