diff options
| author | RncLsn <rnc.lsn@gmail.com> | 2015-06-05 16:37:55 +0100 |
|---|---|---|
| committer | RncLsn <rnc.lsn@gmail.com> | 2015-06-05 16:37:55 +0100 |
| commit | 939e7aa65abe18f57a129fee590f46b7cd32f688 (patch) | |
| tree | c0c56d0360f549807395bf087533e2269e16e81f /scripts/Violations_processor.py | |
| parent | 8c04e4d8003f33848ee84011f8427fe92d55001f (diff) | |
| download | ACQuA-939e7aa65abe18f57a129fee590f46b7cd32f688.tar.gz ACQuA-939e7aa65abe18f57a129fee590f46b7cd32f688.zip | |
Violations processor (Python script).
Diffstat (limited to 'scripts/Violations_processor.py')
| -rw-r--r-- | scripts/Violations_processor.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/scripts/Violations_processor.py b/scripts/Violations_processor.py new file mode 100644 index 0000000..62c1ade --- /dev/null +++ b/scripts/Violations_processor.py | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | import json | ||
| 2 | import argparse | ||
| 3 | from os.path import dirname, join | ||
| 4 | |||
| 5 | class Cleaner: | ||
| 6 | """Given a violation, it can make it very readable. | ||
| 7 | |||
| 8 | For example from: | ||
| 9 | atLeast(1 <RO_0002110> <FBbt_00003972>)(X) :- <auxiliary#NC36>(X) | ||
| 10 | to: | ||
| 11 | active(X) -> across(X,Y), act(Y) | ||
| 12 | """ | ||
| 13 | |||
| 14 | def __init__(self): | ||
| 15 | with open(join(dirname(__file__),'list_of_words.txt'), 'r') as f: | ||
| 16 | self._words = [x.strip() for x in f] | ||
| 17 | self._used_words = 0 | ||
| 18 | self._cache = {} | ||
| 19 | |||
| 20 | def clean(self, violation, prefix): | ||
| 21 | j = 0 | ||
| 22 | atoms = [] | ||
| 23 | for i in range(3): | ||
| 24 | i = violation.find('<', j) + 1 | ||
| 25 | j = violation.find('>', i) | ||
| 26 | atom = violation[i:j] | ||
| 27 | if atom not in self._cache: | ||
| 28 | self._cache[atom] = str(prefix) + self._words[self._used_words] | ||
| 29 | self._used_words += 1 | ||
| 30 | atoms.append(self._cache[atom]) | ||
| 31 | return '%10s(X) -> %10s(X,Y), %10s(Y)' % (atoms[2], atoms[0], atoms[1]) | ||
| 32 | |||
| 33 | |||
| 34 | if __name__ == '__main__': | ||
| 35 | parser = argparse.ArgumentParser(description='Transform violations (as output by PAGOdA) into a very readable form.') | ||
| 36 | parser.add_argument('input', help='json file containing violations') | ||
| 37 | args = parser.parse_args() | ||
| 38 | |||
| 39 | cleaner = Cleaner() | ||
| 40 | with open(args.input, 'r') as f: | ||
| 41 | violations_list_list = json.load(f)['violationClauses'] | ||
| 42 | clean_rules_list_list = [] | ||
| 43 | i = 0 | ||
| 44 | for violations_list in violations_list_list: | ||
| 45 | clean_rules_list_list.append(sorted(map(lambda x: cleaner.clean(x, i), violations_list))) | ||
| 46 | i += 1 | ||
| 47 | print json.dumps(clean_rules_list_list, indent=2) | ||
| 48 | |||
