Closed vaishakhRaveendran closed 1 month ago
The error you're encountering, "global flags not at the start of expression," indicates that the regular expression flags are not correctly positioned in the pattern. In Python, regular expression flags should be placed at the start of the pattern or passed as separate arguments to the re.compile
function.
Here's how you can fix the issue:
Place Flags at the Start of the Pattern:
Ensure that any flags (like re.IGNORECASE
, re.MULTILINE
, etc.) are placed at the beginning of the regular expression pattern using the (?i)
, (?m)
, etc., syntax.
Pass Flags as Separate Arguments:
Alternatively, you can pass the flags as separate arguments to the re.compile
function.
Here's an example of both approaches:
import re
pattern = r"(?i)your_pattern_here"
compiled_pattern = re.compile(pattern)
import re
pattern = r"your_pattern_here"
compiled_pattern = re.compile(pattern, re.IGNORECASE)
To apply this to your extract_text
method in regex_based.py
, locate the part of the code where the regular expression is defined and ensure the flags are correctly positioned.
If you provide the specific regular expression pattern from your extract_text
method, I can give more precise guidance on how to adjust it.
To continue talking to Dosu, mention @dosu.
@dosu The global flags are placed in the middle when the merge_extractor() method tries to combine the extractors based on common attributes or on whether they are multiline or not.
The issue with global flags being placed in the middle when the merge_extractor()
method combines extractors can be resolved by ensuring that the flags are correctly positioned at the start of the pattern or passed as separate arguments during the combination process.
To address this, you can modify the merge_extractor()
method to handle the flags appropriately. Here are the steps you can follow:
Extract Flags and Patterns Separately: When combining patterns, extract the flags and patterns separately.
Combine Patterns and Apply Flags: Combine the patterns and then apply the flags either at the start of the combined pattern or as separate arguments.
Here's a conceptual example of how you might adjust the merge_extractor()
method:
merge_extractor()
import re
def merge_extractor(patterns, flags):
combined_pattern = "|".join(patterns)
combined_flags = 0
for flag in flags:
combined_flags |= flag
# Option 1: Apply flags at the start of the pattern
combined_pattern_with_flags = f"(?{combined_flags}){combined_pattern}"
compiled_pattern = re.compile(combined_pattern_with_flags)
# Option 2: Pass flags as separate arguments
compiled_pattern = re.compile(combined_pattern, combined_flags)
return compiled_pattern
# Example usage
patterns = [r"pattern1", r"pattern2"]
flags = [re.IGNORECASE, re.MULTILINE]
compiled_pattern = merge_extractor(patterns, flags)
In this example, patterns
is a list of regular expression patterns, and flags
is a list of flags. The method combines the patterns and applies the flags correctly.
Ensure that the merge_extractor()
method in your regex_based.py
file follows a similar approach to handle the flags properly. This should resolve the issue of global flags being incorrectly positioned.
To continue talking to Dosu, mention @dosu.
@jjmachan this is the bug I hit
@yasyf are you using the experimental testset generation also? @vaishakhRaveendran thanks for bringing this up, will get this merged right now 🙂
I have checked the documentation and related resources and couldn't resolve my bug.
Describe the bug Error in regex_based.py, extract_text method:"global flags not at the start" Issue with regular expression pattern in ragas_experimental/testset/extractors/regex_based.py. Global flags in regex are incorrectly positioned. Need to move flags to start of pattern or use as separate arguments.
Ragas version: v0.1.10 Python version: 3.11.4
Code to Reproduce
Error trace![image](https://github.com/user-attachments/assets/4a485e1c-35c6-46cc-ad59-6afe31ce641a)
Expected behavior Expected to extract links, emails and markdown heading and add to the metadata
Additional context I believe the error is due to an issue with combining different rule-based extractions.