Skip to content

Commit e3d04b1

Browse files
committed
modify conversion scripts
1 parent 2493840 commit e3d04b1

File tree

4 files changed

+48
-22
lines changed

4 files changed

+48
-22
lines changed

scripts/convert_formulas.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,24 @@
99

1010
from nltk.sem.drt import *
1111
from nltk2drs import convert_to_drs
12-
from nltk2normal import remove_true
12+
from nltk2normal import remove_true, rename
1313
from nltk2tptp import convert_to_tptp_proof
1414
from logic_parser import lexpr
1515

1616
ARGS=None
1717
DOCS=None
1818

1919
def get_formulas_from_xml(doc):
20-
formulas = [s.get('sem', None) for s in doc.xpath(
21-
'./sentences/sentence/semantics[1]/span[1]')]
22-
formulas = [f for f in formulas if f is not None]
20+
formulas = []
21+
for f in doc.xpath('./sentences/sentence'):
22+
if f.xpath('semantics'):
23+
s = f.xpath('semantics')[0]
24+
if s.get('status') == 'success':
25+
formulas += s.xpath('span[1]/@sem')
26+
else:
27+
formulas.append('semantics_error')
28+
else:
29+
formulas.append('syntax_error')
2330
return formulas
2431

2532
def main(args = None):
@@ -38,14 +45,14 @@ def main(args = None):
3845
parser.add_argument("--format", nargs='?', type=str, default="drs",
3946
choices=["fol", "drs", "notrue", "drsbox", "tptp"],
4047
help="Output format (default: drs).")
41-
48+
4249
ARGS = parser.parse_args()
4350

4451
if not os.path.exists(ARGS.sem):
4552
print('File does not exist: {0}'.format(ARGS.sem), file=sys.stderr)
4653
parser.print_help(file=sys.stderr)
4754
sys.exit(1)
48-
55+
4956
parser = etree.XMLParser(remove_blank_text=True)
5057
root = etree.parse(ARGS.sem, parser)
5158

@@ -58,7 +65,7 @@ def main(args = None):
5865
if ARGS.format == "fol":
5966
results = [convert_to_drs(lexpr(formula)).fol() for formula in formulas]
6067
if ARGS.format == "notrue":
61-
results = [remove_true(lexpr(formula)) for formula in formulas]
68+
results = [rename(remove_true(lexpr(formula))) for formula in formulas]
6269
if ARGS.format == "tptp":
6370
inference = [lexpr(f) for f in formulas]
6471
results = convert_to_tptp_proof(inference)

scripts/nltk2drs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
dp = nltk.sem.DrtExpression.fromstring
1010

1111
def convert_to_drs(expression):
12-
expression = rename_variable(expression)
1312
expression = remove_true(expression)
13+
expression = rename(expression)
1414
drs_expr = convert_drs(DRS([],[]), expression)
1515
return drs_expr
1616

scripts/nltk2normal.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
from nltk.internals import Counter
77
from logic_parser import lexpr
88

9-
_counter = Counter()
9+
class NCounter(Counter):
10+
def reset(self):
11+
self._value = 0
12+
return self._value
13+
14+
_counter = NCounter()
1015

1116
# Term t ::=
1217
# x,y,z <IndividualVariableExpression>
@@ -58,13 +63,16 @@ def new_variable(var):
5863
var = VariableExpression(var)
5964
# isinstance(var,EventVariableExpression) must come first
6065
if isinstance(var, EventVariableExpression):
61-
prefix = 'e0'
66+
prefix = 'e'
6267
elif isinstance(var, IndividualVariableExpression):
63-
prefix = 'x0'
68+
if str(var)[0] == 'd':
69+
prefix = 'd'
70+
else:
71+
prefix = 'x'
6472
elif isinstance(var, FunctionVariableExpression):
65-
prefix = 'F0'
73+
prefix = 'F'
6674
else:
67-
prefix = 'z0'
75+
prefix = 'z'
6876
v = Variable("%s%s" % (prefix, _counter.get()))
6977
return v
7078

@@ -291,6 +299,11 @@ def rename_variable(expression):
291299
expr = expression
292300
return expr
293301

302+
def rename(f):
303+
res = rename_variable(f)
304+
_counter.reset()
305+
return res
306+
294307
def convert_to_prenex(expression):
295308
# Convert a formula to one where all existential quantifers come first.
296309
expression = remove_true(expression)
@@ -492,7 +505,6 @@ def normalize_symbols(expression):
492505
comp12 = lexpr(r'(exists x.(_boy(x) & True & exists e.(_walk(e) & (Subj(e) = x) & _slowly(e) & exists x.(_park(x) & True & _in(e,x) & True))) & exists x.(_girl(x) & True & exists e.(_walk(e) & (Subj(e) = x) & _slowly(e) & exists x.(_park(x) & True & _in(e,x) & True))))')
493506
comp13 = lexpr(r'exists x.(_walking(x) & _man(x) & True & exists e.((Subj(e) = Subj(e)) & True))')
494507

495-
496508
test = [v1,v2,v3,
497509
atom1,atom2,atom3,atom4,atom5,
498510
nonatom1,nonatom2,

scripts/nltk2tptp.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,28 @@
22

33
from nltk.sem.logic import *
44
from logic_parser import lexpr
5-
from nltk2normal import rename_variable, remove_true
5+
from nltk2normal import rename_variable, remove_true, rename
66

77
def convert_to_tptp_proof(formulas):
88
if len(formulas) == 1:
99
conjecture = formulas[0]
10-
tptp_script = ['fof(h,conjecture,{0}).'.format(conjecture)]
10+
tptp_script = ['tff(h,conjecture,{0}).'.format(conjecture)]
1111
else:
1212
premises, conjecture = formulas[:-1], formulas[-1]
1313
tptp_script = []
1414
num = 1
1515
for formula in premises:
1616
formula = convert_to_tptp(formula)
17-
tptp_script.append('fof(t{0},axiom,{1}).'.format(num,formula))
17+
tptp_script.append('tff(t{0},axiom,{1}).'.format(num,formula))
1818
num += 1
1919
conjecture = convert_to_tptp(conjecture)
20-
tptp_script.append('fof(h,conjecture,{0}).'.format(conjecture))
21-
return tptp_script
20+
tptp_script.append('tff(h,conjecture,{0}).'.format(conjecture))
21+
return tptp_script
2222

2323
def convert_to_tptp(expression):
24-
expression = rename_variable(expression)
24+
# expression = rename_variable(expression)
2525
expression = remove_true(expression)
26+
expression = rename(expression)
2627
tptp_str = convert_tptp(expression)
2728
return tptp_str
2829

@@ -106,13 +107,19 @@ def convert_tptp_not(expression):
106107
def convert_tptp_exists(expression):
107108
variable = convert_tptp(expression.variable).upper()
108109
term = convert_tptp(expression.term)
109-
tptp_str = '?[' + variable + ']: ' + Tokens.OPEN + term + Tokens.CLOSE
110+
if variable[0] == 'D':
111+
tptp_str = '?[' + variable + ':$int]: ' + Tokens.OPEN + term + Tokens.CLOSE
112+
else:
113+
tptp_str = '?[' + variable + ']: ' + Tokens.OPEN + term + Tokens.CLOSE
110114
return tptp_str
111115

112116
def convert_tptp_all(expression):
113117
variable = convert_tptp(expression.variable).upper()
114118
term = convert_tptp(expression.term)
115-
tptp_str = '![' + variable + ']: ' + Tokens.OPEN + term + Tokens.CLOSE
119+
if variable[0] == 'D':
120+
tptp_str = '![' + variable + ':$int]: ' + Tokens.OPEN + term + Tokens.CLOSE
121+
else:
122+
tptp_str = '![' + variable + ']: ' + Tokens.OPEN + term + Tokens.CLOSE
116123
return tptp_str
117124

118125
def convert_tptp_lambda(expression):

0 commit comments

Comments
 (0)