From 4204f59b524a447b49dc067d142600e347cc7d88 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Tue, 10 Feb 2015 00:10:12 +0100 Subject: Move normalize to prolog.util --- prolog/util.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'prolog') diff --git a/prolog/util.py b/prolog/util.py index e5a93e2..48e3345 100644 --- a/prolog/util.py +++ b/prolog/util.py @@ -156,6 +156,17 @@ def rename_vars(tokens, names=None): tokens[i] = Token('VARIABLE', names[cur_name]) return tokens +# Helper function to remove trailing punctuation from lines and rename +# variables to A1,A2,A3,… (potentially using [var_names]). Return a tuple. +def normalized(line, var_names=None): + # Remove trailing punctuation. + i = len(line) + while i > 0: + if line[i-1].type not in ('COMMA', 'PERIOD', 'SEMI'): + break + i -= 1 + return tuple(rename_vars(line[:i], var_names)) + # transformation = before → after; applied on line which is part of rule # return mapping from formal vars in before+after to actual vars in rule # line and rule should of course not be normalized -- cgit v1.2.1