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

Add option to define matching groups to RegexpTagger #3598

Merged
merged 3 commits into from
Jan 17, 2025
Merged

Conversation

alanakbik
Copy link
Collaborator

Currently, the RegexpTagger only matches the full given regular expression in a sentence. Sometimes, you may want to match only a part of the given expression instead.

This PR extends the RegexpTagger such that you can optionally specify a matching group, i.e.:

from flair.data import Sentence
from flair.models import RegexpTagger

# Define a RegexpTagger with two patterns
tagger = RegexpTagger(
    mapping=[
        # The first pattern matches entire quotes (including quotation marks)
        (r'["„»]((?:(?=(\\?))\2.)*?)[”"“«]', "quote"),

        # The second pattern matches quotes, but without the quotation mark
        (r'["„»]((?:(?=(\\?))\2.)*?)[”"“«]', "quote_part", 1),
    ]
)

# tag example sentence
sentence = Sentence('He said: "this is interesting"')
tagger.predict(sentence)

# print the different matches
print(sentence.get_spans("quote_part"))
print(sentence.get_spans("quote"))

@alanakbik alanakbik merged commit 30974f2 into master Jan 17, 2025
2 checks passed
@alanakbik alanakbik deleted the regexp_tagger branch January 17, 2025 11:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant