|
3 | 3 | from copy import deepcopy
|
4 | 4 | import re
|
5 | 5 |
|
| 6 | + |
6 | 7 | re_whitespace = re.compile(r'\s+', flags=re.UNICODE)
|
7 | 8 |
|
8 | 9 |
|
9 | 10 | class Query:
|
10 | 11 |
|
11 | 12 | agg_ops = ['', 'MAX', 'MIN', 'COUNT', 'SUM', 'AVG']
|
12 | 13 | cond_ops = ['=', '>', '<', 'OP']
|
13 |
| - syms = ['SELECT', 'WHERE', 'AND', 'COL', 'TABLE', 'CAPTION', 'PAGE', 'SECTION', 'OP', 'COND', 'QUESTION', 'AGG', 'AGGOPS', 'CONDOPS'] |
| 14 | + syms = ['SELECT', 'WHERE', 'AND', 'COL', 'TABLE', 'CAPTION', 'PAGE', 'SECTION', 'OP', 'COND', 'QUESTION', 'AGG', 'AGGOPS', 'CONDOPS', 'END'] |
14 | 15 |
|
15 | 16 | def __init__(self, sel_index, agg_index, conditions=tuple()):
|
16 | 17 | self.sel_index = sel_index
|
@@ -68,3 +69,161 @@ def from_generated_dict(cls, d):
|
68 | 69 | end = len(val['words'])
|
69 | 70 | conds.append([col, op, detokenize(val)])
|
70 | 71 | return cls(d['sel'], d['agg'], conds)
|
| 72 | + |
| 73 | + @classmethod |
| 74 | + def from_sequence(cls, sequence, table, lowercase=True): |
| 75 | + sequence = deepcopy(sequence) |
| 76 | + if 'symend' in sequence['words']: |
| 77 | + end = sequence['words'].index('symend') |
| 78 | + for k, v in sequence.items(): |
| 79 | + sequence[k] = v[:end] |
| 80 | + terms = [{'gloss': g, 'word': w, 'after': a} for g, w, a in zip(sequence['gloss'], sequence['words'], sequence['after'])] |
| 81 | + headers = [detokenize(h) for h in table['header']] |
| 82 | + |
| 83 | + # lowercase everything and truncate sequence |
| 84 | + if lowercase: |
| 85 | + headers = [h.lower() for h in headers] |
| 86 | + for i, t in enumerate(terms): |
| 87 | + for k, v in t.items(): |
| 88 | + t[k] = v.lower() |
| 89 | + headers_no_whitespcae = [re.sub(re_whitespace, '', h) for h in headers] |
| 90 | + |
| 91 | + # get select |
| 92 | + if 'symselect' != terms.pop(0)['word']: |
| 93 | + raise Exception('Missing symselect operator') |
| 94 | + |
| 95 | + # get aggregation |
| 96 | + if 'symagg' != terms.pop(0)['word']: |
| 97 | + raise Exception('Missing symagg operator') |
| 98 | + agg_op = terms.pop(0)['word'] |
| 99 | + |
| 100 | + if agg_op == 'symcol': |
| 101 | + agg_op = '' |
| 102 | + else: |
| 103 | + if 'symcol' != terms.pop(0)['word']: |
| 104 | + raise Exception('Missing aggregation column') |
| 105 | + try: |
| 106 | + agg_op = cls.agg_ops.index(agg_op.upper()) |
| 107 | + except Exception as e: |
| 108 | + raise Exception('Invalid agg op {}'.format(agg_op)) |
| 109 | + |
| 110 | + def find_column(name): |
| 111 | + return headers_no_whitespcae.index(re.sub(re_whitespace, '', name)) |
| 112 | + |
| 113 | + def flatten(tokens): |
| 114 | + ret = {'words': [], 'after': [], 'gloss': []} |
| 115 | + for t in tokens: |
| 116 | + ret['words'].append(t['word']) |
| 117 | + ret['after'].append(t['after']) |
| 118 | + ret['gloss'].append(t['gloss']) |
| 119 | + return ret |
| 120 | + where_index = [i for i, t in enumerate(terms) if t['word'] == 'symwhere'] |
| 121 | + where_index = where_index[0] if where_index else len(terms) |
| 122 | + flat = flatten(terms[:where_index]) |
| 123 | + try: |
| 124 | + agg_col = find_column(detokenize(flat)) |
| 125 | + except Exception as e: |
| 126 | + raise Exception('Cannot find aggregation column {}'.format(flat['words'])) |
| 127 | + where_terms = terms[where_index+1:] |
| 128 | + |
| 129 | + # get conditions |
| 130 | + conditions = [] |
| 131 | + while where_terms: |
| 132 | + t = where_terms.pop(0) |
| 133 | + flat = flatten(where_terms) |
| 134 | + if t['word'] != 'symcol': |
| 135 | + raise Exception('Missing conditional column {}'.format(flat['words'])) |
| 136 | + try: |
| 137 | + op_index = flat['words'].index('symop') |
| 138 | + col_tokens = flatten(where_terms[:op_index]) |
| 139 | + except Exception as e: |
| 140 | + raise Exception('Missing conditional operator {}'.format(flat['words'])) |
| 141 | + cond_op = where_terms[op_index+1]['word'] |
| 142 | + try: |
| 143 | + cond_op = cls.cond_ops.index(cond_op.upper()) |
| 144 | + except Exception as e: |
| 145 | + raise Exception('Invalid cond op {}'.format(cond_op)) |
| 146 | + try: |
| 147 | + cond_col = find_column(detokenize(col_tokens)) |
| 148 | + except Exception as e: |
| 149 | + raise Exception('Cannot find conditional column {}'.format(col_tokens['words'])) |
| 150 | + try: |
| 151 | + val_index = flat['words'].index('symcond') |
| 152 | + except Exception as e: |
| 153 | + raise Exception('Cannot find conditional value {}'.format(flat['words'])) |
| 154 | + |
| 155 | + where_terms = where_terms[val_index+1:] |
| 156 | + flat = flatten(where_terms) |
| 157 | + val_end_index = flat['words'].index('symand') if 'symand' in flat['words'] else len(where_terms) |
| 158 | + cond_val = detokenize(flatten(where_terms[:val_end_index])) |
| 159 | + conditions.append([cond_col, cond_op, cond_val]) |
| 160 | + where_terms = where_terms[val_end_index+1:] |
| 161 | + q = cls(agg_col, agg_op, conditions) |
| 162 | + return q |
| 163 | + |
| 164 | + @classmethod |
| 165 | + def from_partial_sequence(cls, agg_col, agg_op, sequence, table, lowercase=True): |
| 166 | + sequence = deepcopy(sequence) |
| 167 | + if 'symend' in sequence['words']: |
| 168 | + end = sequence['words'].index('symend') |
| 169 | + for k, v in sequence.items(): |
| 170 | + sequence[k] = v[:end] |
| 171 | + terms = [{'gloss': g, 'word': w, 'after': a} for g, w, a in zip(sequence['gloss'], sequence['words'], sequence['after'])] |
| 172 | + headers = [detokenize(h) for h in table['header']] |
| 173 | + |
| 174 | + # lowercase everything and truncate sequence |
| 175 | + if lowercase: |
| 176 | + headers = [h.lower() for h in headers] |
| 177 | + for i, t in enumerate(terms): |
| 178 | + for k, v in t.items(): |
| 179 | + t[k] = v.lower() |
| 180 | + headers_no_whitespcae = [re.sub(re_whitespace, '', h) for h in headers] |
| 181 | + |
| 182 | + def find_column(name): |
| 183 | + return headers_no_whitespcae.index(re.sub(re_whitespace, '', name)) |
| 184 | + |
| 185 | + def flatten(tokens): |
| 186 | + ret = {'words': [], 'after': [], 'gloss': []} |
| 187 | + for t in tokens: |
| 188 | + ret['words'].append(t['word']) |
| 189 | + ret['after'].append(t['after']) |
| 190 | + ret['gloss'].append(t['gloss']) |
| 191 | + return ret |
| 192 | + where_index = [i for i, t in enumerate(terms) if t['word'] == 'symwhere'] |
| 193 | + where_index = where_index[0] if where_index else len(terms) |
| 194 | + where_terms = terms[where_index+1:] |
| 195 | + |
| 196 | + # get conditions |
| 197 | + conditions = [] |
| 198 | + while where_terms: |
| 199 | + t = where_terms.pop(0) |
| 200 | + flat = flatten(where_terms) |
| 201 | + if t['word'] != 'symcol': |
| 202 | + raise Exception('Missing conditional column {}'.format(flat['words'])) |
| 203 | + try: |
| 204 | + op_index = flat['words'].index('symop') |
| 205 | + col_tokens = flatten(where_terms[:op_index]) |
| 206 | + except Exception as e: |
| 207 | + raise Exception('Missing conditional operator {}'.format(flat['words'])) |
| 208 | + cond_op = where_terms[op_index+1]['word'] |
| 209 | + try: |
| 210 | + cond_op = cls.cond_ops.index(cond_op.upper()) |
| 211 | + except Exception as e: |
| 212 | + raise Exception('Invalid cond op {}'.format(cond_op)) |
| 213 | + try: |
| 214 | + cond_col = find_column(detokenize(col_tokens)) |
| 215 | + except Exception as e: |
| 216 | + raise Exception('Cannot find conditional column {}'.format(col_tokens['words'])) |
| 217 | + try: |
| 218 | + val_index = flat['words'].index('symcond') |
| 219 | + except Exception as e: |
| 220 | + raise Exception('Cannot find conditional value {}'.format(flat['words'])) |
| 221 | + |
| 222 | + where_terms = where_terms[val_index+1:] |
| 223 | + flat = flatten(where_terms) |
| 224 | + val_end_index = flat['words'].index('symand') if 'symand' in flat['words'] else len(where_terms) |
| 225 | + cond_val = detokenize(flatten(where_terms[:val_end_index])) |
| 226 | + conditions.append([cond_col, cond_op, cond_val]) |
| 227 | + where_terms = where_terms[val_end_index+1:] |
| 228 | + q = cls(agg_col, agg_op, conditions) |
| 229 | + return q |
0 commit comments