Create regex_checker.py
Browse files- regex_checker.py +86 -0
regex_checker.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# regex_checker.py
|
| 2 |
+
import re
|
| 3 |
+
import traceback
|
| 4 |
+
from typing import List, Dict, Any
|
| 5 |
+
|
| 6 |
+
def perform_regex_checks(plain_text_from_original_pdf: str) -> Dict[str, Any]:
|
| 7 |
+
"""
|
| 8 |
+
Performs custom regex checks on plain text from the original PDF.
|
| 9 |
+
Filters issues to only include those between "abstract" and "references/bibliography"
|
| 10 |
+
found within this specific text.
|
| 11 |
+
Assumes plain_text_from_original_pdf is already space-normalized.
|
| 12 |
+
"""
|
| 13 |
+
text_for_regex_analysis = plain_text_from_original_pdf
|
| 14 |
+
|
| 15 |
+
if not text_for_regex_analysis or not text_for_regex_analysis.strip():
|
| 16 |
+
print("RegexChecker: Input plain text is empty.")
|
| 17 |
+
return {"total_issues": 0, "issues_list": [], "text_used_for_analysis": ""}
|
| 18 |
+
|
| 19 |
+
text_for_regex_analysis_lower = text_for_regex_analysis.lower()
|
| 20 |
+
|
| 21 |
+
abstract_match = re.search(r'\babstract\b', text_for_regex_analysis_lower)
|
| 22 |
+
content_start_index = abstract_match.start() if abstract_match else 0
|
| 23 |
+
if abstract_match:
|
| 24 |
+
print(f"RegexChecker: Found 'abstract' at index {content_start_index} in its text.")
|
| 25 |
+
else:
|
| 26 |
+
print(f"RegexChecker: Did not find 'abstract', Regex analysis from index 0 of its text.")
|
| 27 |
+
|
| 28 |
+
references_match = re.search(r'\breferences\b', text_for_regex_analysis_lower)
|
| 29 |
+
bibliography_match = re.search(r'\bbibliography\b', text_for_regex_analysis_lower)
|
| 30 |
+
content_end_index = len(text_for_regex_analysis)
|
| 31 |
+
|
| 32 |
+
if references_match and bibliography_match:
|
| 33 |
+
content_end_index = min(references_match.start(), bibliography_match.start())
|
| 34 |
+
print(f"RegexChecker: Found 'references' at {references_match.start()} and 'bibliography' at {bibliography_match.start()}. Using {content_end_index} as end boundary.")
|
| 35 |
+
elif references_match:
|
| 36 |
+
content_end_index = references_match.start()
|
| 37 |
+
print(f"RegexChecker: Found 'references' at {content_end_index}. Using it as end boundary.")
|
| 38 |
+
elif bibliography_match:
|
| 39 |
+
content_end_index = bibliography_match.start()
|
| 40 |
+
print(f"RegexChecker: Found 'bibliography' at {content_end_index}. Using it as end boundary.")
|
| 41 |
+
else:
|
| 42 |
+
print(f"RegexChecker: Did not find 'references' or 'bibliography'. Regex analysis up to end of its text (index {content_end_index}).")
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
if content_start_index >= content_end_index:
|
| 46 |
+
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.")
|
| 47 |
+
|
| 48 |
+
processed_regex_issues: List[Dict[str, Any]] = []
|
| 49 |
+
try:
|
| 50 |
+
# Regex: Missing space before bracketed citation, e.g., "word[1]"
|
| 51 |
+
regex_pattern = r'\b(\w+)\[(\d+)\]'
|
| 52 |
+
regex_matches = list(re.finditer(regex_pattern, text_for_regex_analysis))
|
| 53 |
+
|
| 54 |
+
regex_issues_in_range = 0
|
| 55 |
+
for reg_idx, match in enumerate(regex_matches):
|
| 56 |
+
if not (content_start_index <= match.start() < content_end_index):
|
| 57 |
+
continue
|
| 58 |
+
regex_issues_in_range += 1
|
| 59 |
+
|
| 60 |
+
word = match.group(1)
|
| 61 |
+
number = match.group(2)
|
| 62 |
+
context_str = text_for_regex_analysis[match.start():match.end()]
|
| 63 |
+
processed_regex_issues.append({
|
| 64 |
+
'_internal_id': f"regex_{reg_idx}",
|
| 65 |
+
'ruleId': "SPACE_BEFORE_BRACKET",
|
| 66 |
+
'message': f"Missing space before '[' in '{context_str}'. Should be '{word} [{number}]'.",
|
| 67 |
+
'context_text': context_str,
|
| 68 |
+
'offset_in_text': match.start(),
|
| 69 |
+
'error_length': match.end() - match.start(),
|
| 70 |
+
'replacements_suggestion': [f"{word} [{number}]"],
|
| 71 |
+
'category_name': "Formatting",
|
| 72 |
+
'source_check_type': 'CustomRegex',
|
| 73 |
+
'is_mapped_to_pdf': False,
|
| 74 |
+
'pdf_coordinates_list': [],
|
| 75 |
+
'mapped_page_number': -1
|
| 76 |
+
})
|
| 77 |
+
print(f"RegexChecker: Regex check found {len(regex_matches)} raw matches, {regex_issues_in_range} issues within defined content range of its text.")
|
| 78 |
+
|
| 79 |
+
return {
|
| 80 |
+
"total_issues": len(processed_regex_issues),
|
| 81 |
+
"issues_list": processed_regex_issues,
|
| 82 |
+
"text_used_for_analysis": text_for_regex_analysis
|
| 83 |
+
}
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"Error in perform_regex_checks: {e}\n{traceback.format_exc()}")
|
| 86 |
+
return {"error": str(e), "total_issues": 0, "issues_list": [], "text_used_for_analysis": text_for_regex_analysis}
|