-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_utils.py
More file actions
83 lines (59 loc) · 2.44 KB
/
Copy pathbench_utils.py
File metadata and controls
83 lines (59 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from functools import cache
import re
import random
import copy
def get_questions(x: dict, num_filters=None):
if 'questions' in x:
if num_filters is None:
return x['questions']
else:
questions = copy.copy(x['questions'])
random.seed(42)
random.shuffle(questions)
return questions[:num_filters]
elif 'question' in x:
if x['question'] is None:
return []
else:
return [x['question']]
raise ValueError(f'Unknown question format: {x}')
def calculate_latex_command_text_ratio(line_text: str) -> tuple[float, list, list]:
pattern = r'\\[a-zA-Z]+\*?(?:\[.*?\])?(?:{([^{}]*(?:{[^{}]*})*[^{}]*)})?'
matches = list(re.finditer(pattern, line_text))
full_commands = [match.group(0) for match in matches]
command_chars_length = sum(len(cmd) for cmd in full_commands)
total_length = len(line_text)
ratio = command_chars_length / total_length if total_length > 0 else 0.0
return ratio
def extract_target_spans(latex_content: str) -> list[tuple[int, int]]:
spans = []
pattern = r'\\begin{abstract}(.*?)\\end{abstract}'
matches = re.finditer(pattern, latex_content, re.DOTALL)
for match in matches:
span = match.span(0)
spans.append(span)
pattern = r'(\\section{[^}]*(?:Introduction|Evaluation|Experiment|Result|Discussion)[^}]*}.*?)(?:\\section|$)'
matches = re.finditer(pattern, latex_content, re.DOTALL)
for match in matches:
span = match.span(1)
spans.append(span)
pattern = r'(\\subsection{[^}]*(?:Result|Evaluation|Discussion)[^}]*}.*?)(?:\\subsection|$)'
matches = re.finditer(pattern, latex_content, re.DOTALL)
for match in matches:
span = match.span(1)
spans.append(span)
return spans
def extract_block_spans(latex_content: str) -> list[tuple[int, int]]:
figure_pattern = r'\\begin{(?:.+?)\*?}(.*?)\\end{(?:.+?)\*?}'
spans = []
figure_matches = re.finditer(figure_pattern, latex_content, re.DOTALL)
for match in figure_matches:
span = match.span(0)
spans.append(span)
return spans
def check_span_overlapping(spans: list[tuple[int, int]], targets: list[tuple[int, int]]) -> bool:
for span in spans:
for target in targets:
if span[0] < target[1] and span[1] > target[0]:
return True
return False