Skip to content

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