stringOfTree()   B
last analyzed

Complexity

Conditions 7

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
c 1
b 0
f 0
dl 0
loc 14
rs 7.3333
1
from demo6 import get_answer
2
import json
3
import sys
4
import os
5
from ppp_questionparsing_grammatical.data.exceptions import GrammaticalError, QuotationError, QuestionWordError
6
7
INDENT_NUMBER=4
8
BASE_INDENT=1
9
10
class TripleError(Exception):
11
    """
12
        Raised when a triple contains connectors (e.g. AND, FIRST).
13
    """
14
    def __init__(self, expression, message):
15
        self.expression = expression
16
        self.message = message
17
18
symbol = {
19
    'missing': 'M',
20
    'resource': 'R',
21
    'triple': 'T',
22
    'list': 'List',
23
    'intersection': 'I',
24
    'union': 'U',
25
    'exists': 'E',
26
    'first': 'F',
27
    'last': 'L',
28
    'sort': 'S',
29
}
30
31
def smallDepth(t):
32
    allowedTypes = {'resource', 'missing'}
33
    return t.subject.type in allowedTypes and t.predicate.type in allowedTypes and t.object.type in allowedTypes
34
35
def stringOfTriple(t, indent):
36
    if smallDepth(t):
37
        i = 0
38
    else:
39
        i = indent+1
40
    _subject = stringOfTree(t.subject, i)
41
    _predicate = stringOfTree(t.predicate, i)
42
    _object = stringOfTree(t.object, i)
43
    if smallDepth(t):
44
        return '%s%s(%s, %s, %s)' % (' '*indent*INDENT_NUMBER, symbol[t.type], _subject, _predicate, _object)
45
    else:
46
        return '{0}{1}(\n{2},\n{3},\n{4}\n{0})'.format(' '*indent*INDENT_NUMBER, symbol[t.type], _subject, _predicate, _object)
47
48
def stringOfTree(t, indent=BASE_INDENT):
49
    if t.type == 'missing':
50
        return '%s%s()' % (' '*indent*INDENT_NUMBER, symbol[t.type])
51
    elif t.type == 'resource':
52
        return '%s%s("%s")' % (' '*indent*INDENT_NUMBER, symbol[t.type], t.value)
53
    elif t.type == 'triple':
54
        return stringOfTriple(t, indent)
55
    elif t.type in {'list', 'intersection', 'union'}:
56
        l = ',\n'.join(stringOfTree(x, indent+1) for x in t.list)
57
        return '{0}{1}([\n{2}\n{0}])'.format(' '*indent*INDENT_NUMBER, symbol[t.type], l)
58
    elif t.type in {'exists', 'nth', 'sort'}:
59
        l = stringOfTree(t.list, indent+1)
60
        return '{0}{1}(\n{2}\n{0})'.format(' '*indent*INDENT_NUMBER, symbol[t.type], l)
61
    raise TripleError(t,"Wrong triple (new datamodel connectors?).")
62
63
def process_string(s):
64
    return stringOfTree(get_answer(s))
65
66
if __name__ == "__main__":
67
    flag = False
68
    print('data = {')
69
    while True:
70
        try:
71
            s = input("")
72
        except EOFError:
73
            break
74
        try:
75
            result = process_string(s)
76
        except GrammaticalError:
77
            sys.stderr.write("#GrammaticalError:\t{0}\n".format(s))
78
            continue
79
        except QuotationError:
80
            sys.stderr.write("#QuotationError:\t{0}\n".format(s))
81
            continue
82
        except QuestionWordError:
83
            sys.stderr.write("#QuestionWordError:\t{0}\n".format(s))
84
            continue
85
        except RuntimeError:
86
            sys.stderr.write("#RuntimeError:\t{0}\n".format(s))
87
            continue
88
        except IndexError:
89
            sys.stderr.write("#IndexError:\t{0}\n".format(s))
90
            continue
91
        if flag:
92
            print('')
93
        else:
94
            flag=True
95
        print('%s\'%s\':' % (' '*BASE_INDENT*INDENT_NUMBER, s))
96
        print('%s,' % result)
97
    print('}')
98