-
Notifications
You must be signed in to change notification settings - Fork 3
/
helpers.py
150 lines (128 loc) · 4.82 KB
/
helpers.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import ast
import re
def label_values(input_string,map):
values = input_string.split(',')
if isinstance(map,str):
map = ast.literal_eval(map)
labeled_values = [f"{v}: {map[v]}" for v in values]
return ', '.join(labeled_values)
def first_non_empty_line(string):
"""Extracts the first non-empty line from a string.
Args:
string: The string to extract the first non-empty line from.
Returns:
The first non-empty line from the string.
"""
lines = string.splitlines()
for line in lines:
if line:
return line
return None
def extract_propositional_symbols(logical_form):
symbols = re.findall(r'\b[a-z]\b', logical_form)
return set(symbols)
def split_string_except_in_brackets(string, delimiter):
result = []
stack = []
current = ''
for char in string:
if char == '(':
stack.append('(')
elif char == ')':
if stack:
stack.pop()
if char == delimiter and not stack:
result.append(current)
current = ''
else:
current += char
if current:
result.append(current)
return result
def remove_text_after_last_parenthesis(input_string):
last_paren_index = input_string.rfind(')') # Find the index of the last ')'
if last_paren_index != -1: # If ')' is found
result = input_string[:last_paren_index+1] # Get the substring before the last ')'
return result.strip() # Return the result after removing any extra spaces
else:
return input_string # Return the original string if no ')' is found
def fix_inconsistent_arities(clauses1, clauses2):
all_clauses = clauses1 + clauses2
predicates = {}
# Extract predicates and their arities
for clause in all_clauses:
predicate = clause.split('(')[0] # Extract predicate
arity = len(clause.split(',')) # Calculate arity
if predicate in predicates:
# Update predicate arity if different
if predicates[predicate] != arity:
predicates[predicate] = min(predicates[predicate], arity)
else:
predicates[predicate] = arity
# Fix inconsistency by dropping extra arguments
fixed_clauses1 = []
fixed_clauses2 = []
for clause in clauses1:
predicate = clause.split('(')[0]
args = clause.split('(')[1].split(')')[0].split(',')
arity = len(args)
if arity > predicates[predicate]:
# Keep only necessary arguments based on predicate arity
new_clause = f"{predicate}({', '.join(args[:predicates[predicate]])})"
fixed_clauses1.append(new_clause)
else:
fixed_clauses1.append(clause)
for clause in clauses2:
predicate = clause.split('(')[0]
args = clause.split('(')[1].split(')')[0].split(',')
arity = len(args)
if arity > predicates[predicate]:
# Keep only necessary arguments based on predicate arity
new_clause = f"{predicate}({', '.join(args[:predicates[predicate]])})"
fixed_clauses2.append(new_clause)
else:
fixed_clauses2.append(clause)
return ', '.join(fixed_clauses1), ', '.join(fixed_clauses2)
def replace_variables(mapping, clause):
# Reverse the mapping for character to string replacement
reversed_mapping = {v: k for k, v in mapping.items()}
# Replace characters with corresponding strings from the reversed mapping
replaced_clause = clause
predicate = clause.split('(')[0]
args = clause.split('(')[1][:-1].split(',')
replaced_args=[]
for arg in args:
if arg in reversed_mapping:
replaced_args.append(reversed_mapping[arg])
else:
replaced_args.append(arg)
args=','.join(replaced_args)
replaced_clause=predicate+'('+args+')'
return replaced_clause
def substitute_variables(clause1,clause2,start_char):
variables={}
replaced_clause1 = clause1
predicate = clause1.split('(')[0]
args = clause1.split('(')[1][:-1].split(',')
current_char = start_char
replaced_args = []
for arg in args:
if arg not in variables:
variables[arg]=current_char
current_char = chr(ord(current_char) + 1)
replaced_args.append(variables[arg])
args=','.join(replaced_args)
replaced_clause1=predicate+'('+args+')'
###
replaced_clause2 = clause2
predicate = clause2.split('(')[0]
args = clause2.split('(')[1][:-1].split(',')
replaced_args = []
for arg in args:
if arg not in variables:
variables[arg]=current_char
current_char = chr(ord(current_char) + 1)
replaced_args.append(variables[arg])
args=','.join(replaced_args)
replaced_clause2=predicate+'('+args+')'
return replaced_clause1, replaced_clause2, current_char