# regex_checker.py import re import traceback from typing import List, Dict, Any def perform_regex_checks(plain_text_from_original_pdf: str) -> Dict[str, Any]: """ Performs custom regex checks on plain text from the original PDF. Filters issues to only include those between "abstract" and "references/bibliography" found within this specific text. Assumes plain_text_from_original_pdf is already space-normalized. """ text_for_regex_analysis = plain_text_from_original_pdf if not text_for_regex_analysis or not text_for_regex_analysis.strip(): print("RegexChecker: Input plain text is empty.") return {"total_issues": 0, "issues_list": [], "text_used_for_analysis": ""} text_for_regex_analysis_lower = text_for_regex_analysis.lower() abstract_match = re.search(r'\babstract\b', text_for_regex_analysis_lower) content_start_index = abstract_match.start() if abstract_match else 0 if abstract_match: print(f"RegexChecker: Found 'abstract' at index {content_start_index} in its text.") else: print(f"RegexChecker: Did not find 'abstract', Regex analysis from index 0 of its text.") references_match = re.search(r'\breferences\b', text_for_regex_analysis_lower) bibliography_match = re.search(r'\bbibliography\b', text_for_regex_analysis_lower) content_end_index = len(text_for_regex_analysis) if references_match and bibliography_match: content_end_index = min(references_match.start(), bibliography_match.start()) print(f"RegexChecker: Found 'references' at {references_match.start()} and 'bibliography' at {bibliography_match.start()}. Using {content_end_index} as end boundary.") elif references_match: content_end_index = references_match.start() print(f"RegexChecker: Found 'references' at {content_end_index}. Using it as end boundary.") elif bibliography_match: content_end_index = bibliography_match.start() print(f"RegexChecker: Found 'bibliography' at {content_end_index}. Using it as end boundary.") else: print(f"RegexChecker: Did not find 'references' or 'bibliography'. Regex analysis up to end of its text (index {content_end_index}).") if content_start_index >= content_end_index: print(f"RegexChecker: Warning: Content start index ({content_start_index}) is not before end index ({content_end_index}) in its text. No Regex issues will be reported from this range.") processed_regex_issues: List[Dict[str, Any]] = [] try: # Regex: Missing space before bracketed citation, e.g., "word[1]" regex_pattern = r'\b(\w+)\[(\d+)\]' regex_matches = list(re.finditer(regex_pattern, text_for_regex_analysis)) regex_issues_in_range = 0 for reg_idx, match in enumerate(regex_matches): if not (content_start_index <= match.start() < content_end_index): continue regex_issues_in_range += 1 word = match.group(1) number = match.group(2) context_str = text_for_regex_analysis[match.start():match.end()] processed_regex_issues.append({ '_internal_id': f"regex_{reg_idx}", 'ruleId': "SPACE_BEFORE_BRACKET", 'message': f"Missing space before '[' in '{context_str}'. Should be '{word} [{number}]'.", 'context_text': context_str, 'offset_in_text': match.start(), 'error_length': match.end() - match.start(), 'replacements_suggestion': [f"{word} [{number}]"], 'category_name': "Formatting", 'source_check_type': 'CustomRegex', 'is_mapped_to_pdf': False, 'pdf_coordinates_list': [], 'mapped_page_number': -1 }) print(f"RegexChecker: Regex check found {len(regex_matches)} raw matches, {regex_issues_in_range} issues within defined content range of its text.") return { "total_issues": len(processed_regex_issues), "issues_list": processed_regex_issues, "text_used_for_analysis": text_for_regex_analysis } except Exception as e: print(f"Error in perform_regex_checks: {e}\n{traceback.format_exc()}") return {"error": str(e), "total_issues": 0, "issues_list": [], "text_used_for_analysis": text_for_regex_analysis}