| 17 | return [x for x in toks if ('.' in x) and (not x.startswith('m.')) and (not '^^' in x)] |
| 18 | |
| 19 | class ASTNode: |
| 20 | UNARY = 'unary' |
| 21 | BINARY = 'binary' |
| 22 | def __init__(self, construction, val, data_type, fields): |
| 23 | self.construction = construction |
| 24 | self.val = val |
| 25 | # unary or binary |
| 26 | self.data_type = data_type |
| 27 | self.fields = fields |
| 28 | |
| 29 | # determined after construction |
| 30 | self.depth = -1 |
| 31 | self.level = -1 |
| 32 | |
| 33 | def assign_depth_and_level(self, level=0): |
| 34 | self.level = level |
| 35 | if self.fields: |
| 36 | max_depth = max([x.assign_depth_and_level(level + 1) for x in self.fields]) |
| 37 | self.depth = max_depth + 1 |
| 38 | else: |
| 39 | self.depth = 0 |
| 40 | return self.depth |
| 41 | |
| 42 | @classmethod |
| 43 | def build(cls, tok, data_type, fields): |
| 44 | if tok == 'AND': |
| 45 | return AndNode(data_type, fields) |
| 46 | elif tok == 'R': |
| 47 | return RNode(data_type, fields) |
| 48 | elif tok == 'COUNT': |
| 49 | return CountNode(data_type, fields) |
| 50 | elif tok == 'JOIN': |
| 51 | return JoinNode(data_type, fields) |
| 52 | elif tok in ['le', 'lt', 'ge', 'gt']: |
| 53 | return CompNode(tok, data_type, fields) |
| 54 | elif tok in ['ARGMIN', 'ARGMAX']: |
| 55 | return ArgNode(tok, data_type, fields) |
| 56 | elif tok.startswith('m.'): |
| 57 | return EntityNode(tok, data_type, fields) |
| 58 | elif '^^http://www.w3.org/2001/XMLSchema' in tok: |
| 59 | return ValNode(tok, data_type, fields) |
| 60 | else: |
| 61 | return SchemaNode(tok, data_type, fields) |
| 62 | |
| 63 | def logical_form(self): |
| 64 | if self.depth == 0: |
| 65 | return self.val |
| 66 | else: |
| 67 | fields_str = [x.logical_form() for x in self.fields] |
| 68 | return ' '.join(['(', self.val] + fields_str + [')']) |
| 69 | |
| 70 | # nothing special. just fit legacy code input syle |
| 71 | def compact_logical_form(self): |
| 72 | lf = self.logical_form() |
| 73 | return lf.replace('( ', '(').replace(' )', ')') |
| 74 | |
| 75 | def skeleton_form(self): |
| 76 | if self.depth == 0: |
nothing calls this directly
no outgoing calls
no test coverage detected