Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matcher support for searching on a Span, as well as a Doc #5056

Closed
paoloq opened this issue Feb 24, 2020 · 3 comments · Fixed by #5113
Closed

Matcher support for searching on a Span, as well as a Doc #5056

paoloq opened this issue Feb 24, 2020 · 3 comments · Fixed by #5113
Labels
enhancement Feature requests and improvements feat / matcher Feature: Token, phrase and dependency matcher

Comments

@paoloq
Copy link
Contributor

paoloq commented Feb 24, 2020

Feature description

It would be nice to have support for running pattern matching on Span, as well as Doc.
Sometimes it could be not necessary to perform the pattern searching in the whole document, but in some sentences or, more generally, spans only.

I already implemented and partially tested a solution locally, any downside I'm not aware of in terms of policies or code design constraints for this kind of feature?

Here are some of my changes in the matcher.pyx:

def __call__(self, doc_or_span):
        """ ... """
        if isinstance(doc_or_span, Doc):
            doc = doc_or_span
        elif isinstance(doc_or_span, Span):
            doc = doc_or_span.doc
        else:
            raise ValueError()
        
        ...

        length = (
            (doc_or_span.end - doc_or_span.start)
            if isinstance(doc_or_span, Span) else len(doc)
        )
        matches = find_matches(&self.patterns[0], self.patterns.size(), doc_or_span, length, 
                                extensions=self._extensions, predicates=self._extra_predicates)

where find_matches changes accepting doc_or_span and length as new parameters:

cdef find_matches(TokenPatternC** patterns, int n, doc_or_span, int length, extensions=None, predicates=tuple()):
        ...
@adrianeboyd adrianeboyd added enhancement Feature requests and improvements feat / matcher Feature: Token, phrase and dependency matcher labels Feb 24, 2020
paoloq added a commit to erre-quadro/spaCy that referenced this issue Mar 5, 2020
honnibal pushed a commit that referenced this issue Apr 15, 2020
* Matcher support for Span, as well as Doc #5056

* Removes an import unused

* Signed contributors agreement

* Code optimization and better test

* Add error message for bad Matcher call argument

* Fix merging
@svlandeg
Copy link
Member

Thanks a lot for your contribution! It's now merged, and will be included in the next release of spaCy, probably 2.3 (or otherwise 3.0) 👍

@lock
Copy link

lock bot commented May 20, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

1 similar comment
@lock
Copy link

lock bot commented May 20, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators May 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Feature requests and improvements feat / matcher Feature: Token, phrase and dependency matcher
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants