forked from FerreroJeremy/ln2sql
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathParser.py
executable file
·903 lines (744 loc) · 38.2 KB
/
Parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
# -*- coding: utf-8 -*
import re
import sys
import unicodedata
import string
from threading import Thread
from ParsingException import ParsingException
from Query import *
import functools
reload(sys)
sys.setdefaultencoding("utf-8")
class SelectParser(Thread):
def __init__(self, columns_of_select, tables_of_from, phrase, count_keywords, sum_keywords, average_keywords, max_keywords, min_keywords, database_dico):
Thread.__init__(self)
self.select_objects = []
self.columns_of_select = columns_of_select
self.tables_of_from = tables_of_from
self.phrase = phrase
self.count_keywords = count_keywords
self.sum_keywords = sum_keywords
self.average_keywords = average_keywords
self.max_keywords = max_keywords
self.min_keywords = min_keywords
self.database_dico = database_dico
def get_tables_of_column(self, column):
tmp_table = []
for table in self.database_dico:
if column in self.database_dico[table]:
tmp_table.append(table)
return tmp_table
def get_column_name_with_alias_table(self, column, table_of_from):
one_table_of_column = self.get_tables_of_column(column)[0]
tables_of_column = self.get_tables_of_column(column)
if table_of_from in tables_of_column:
return str(table_of_from) + '.' + str(column)
else:
return str(one_table_of_column) + '.' + str(column)
def run(self):
for table_of_from in self.tables_of_from:
self.select_object = Select()
is_count = False
number_of_select_column = len(self.columns_of_select)
if number_of_select_column == 0:
for count_keyword in self.count_keywords:
# if count_keyword in (word.lower() for word in self.phrase):
# so that matches multiple words rather than just single word for COUNT
# (e.g. -> "how many city there are in which the employe name is aman ?" )
lower_self_phrase = ' '.join(word.lower() for word in self.phrase)
if count_keyword in lower_self_phrase:
is_count = True
if is_count:
self.select_object.add_column(None, 'COUNT')
else:
self.select_object.add_column(None, None)
else:
select_phrases = []
previous_index = 0
for i in range(0, len(self.phrase)):
if self.phrase[i] in self.columns_of_select:
select_phrases.append(
self.phrase[previous_index:i + 1])
previous_index = i + 1
select_phrases.append(self.phrase[previous_index:])
for i in range(0, len(select_phrases)):
select_type = None
# phrase = [word.lower() for word in select_phrases[i]]
# so that matches multiple words rather than just single word in select type of phrases
# (e.g. -> "how many name there are in emp in which the cityId is more than 3" )
lower_select_phrase = ' '.join(word.lower() for word in select_phrases[i])
for keyword in self.average_keywords:
if keyword in lower_select_phrase:
select_type = 'AVG'
for keyword in self.count_keywords:
if keyword in lower_select_phrase:
select_type = 'COUNT'
for keyword in self.max_keywords:
if keyword in lower_select_phrase:
select_type = 'MAX'
for keyword in self.min_keywords:
if keyword in lower_select_phrase:
select_type = 'MIN'
for keyword in self.sum_keywords:
if keyword in lower_select_phrase:
select_type = 'SUM'
if (i != len(select_phrases) - 1) or (select_type is not None):
if i >= len(self.columns_of_select):
column = None
else:
column = self.get_column_name_with_alias_table(
self.columns_of_select[i], table_of_from)
self.select_object.add_column(column, select_type)
self.select_objects.append(self.select_object)
def join(self):
Thread.join(self)
return self.select_objects
class FromParser(Thread):
def __init__(self, tables_of_from, columns_of_select, columns_of_where, database_object):
Thread.__init__(self)
self.queries = []
self.tables_of_from = tables_of_from
self.columns_of_select = columns_of_select
self.columns_of_where = columns_of_where
self.database_object = database_object
self.database_dico = self.database_object.get_tables_into_dictionnary()
def get_tables_of_column(self, column):
tmp_table = []
for table in self.database_dico:
if column in self.database_dico[table]:
tmp_table.append(table)
return tmp_table
def intersect(self, a, b):
return list(set(a) & set(b))
def difference(self, a, b):
differences = []
for _list in a:
if _list not in b:
differences.append(_list)
return differences
def is_direct_join_is_possible(self, table_src, table_trg):
fk_column_of_src_table = self.database_object.get_foreign_keys_of_table(table_src)
fk_column_of_trg_table = self.database_object.get_foreign_keys_of_table(table_trg)
for column in fk_column_of_src_table:
if column.is_foreign()['foreign_table'] == table_trg:
return [(table_src, column.get_name()), (table_trg, column.is_foreign()['foreign_column'])]
for column in fk_column_of_trg_table:
if column.is_foreign()['foreign_table'] == table_src:
return [(table_src, column.is_foreign()['foreign_column']), (table_trg, column.get_name())]
""" @todo Restore the following lines for implicit inner join on same id columns. """
# pk_table_src = self.database_object.get_primary_key_names_of_table(table_src)
# pk_table_trg = self.database_object.get_primary_key_names_of_table(table_trg)
# match_pk_table_src_with_table_trg = self.intersect(pk_table_src, self.database_dico[table_trg])
# match_pk_table_trg_with_table_src = self.intersect(pk_table_trg, self.database_dico[table_src])
# if len(match_pk_table_src_with_table_trg) >= 1:
# return [(table_trg, match_pk_table_src_with_table_trg[0]), (table_src, match_pk_table_src_with_table_trg[0])]
# elif len(match_pk_table_trg_with_table_src) >= 1:
# return [(table_trg, match_pk_table_trg_with_table_src[0]),
# (table_src, match_pk_table_trg_with_table_src[0])]
def get_all_direct_linked_tables_of_a_table(self, table_src):
links = []
for table_trg in self.database_dico:
if table_trg != table_src:
link = self.is_direct_join_is_possible(table_src, table_trg)
if link is not None:
links.append(link)
return links
def is_join(self, historic, table_src, table_trg):
historic = historic
links = self.get_all_direct_linked_tables_of_a_table(table_src)
differences = []
for join in links:
if join[0][0] not in historic:
differences.append(join)
links = differences
for join in links:
if join[1][0] == table_trg:
return [0, join]
path = []
historic.append(table_src)
for join in links:
result = [1, self.is_join(historic, join[1][0], table_trg)]
if result[1] != []:
if result[0] == 0:
path.append(result[1])
path.append(join)
else:
path = result[1]
path.append(join)
return path
def get_link(self, table_src, table_trg):
path = self.is_join([], table_src, table_trg)
if len(path) > 0:
path.pop(0)
path.reverse()
return path
def unique(self, _list):
return [list(x) for x in set(tuple(x) for x in _list)]
def unique_ordered(self, _list):
frequency = []
for element in _list:
if element not in frequency:
frequency.append(element)
return frequency
def run(self):
self.queries = []
for table_of_from in self.tables_of_from:
links = []
query = Query()
query.set_from(From(table_of_from))
join_object = Join()
for column in self.columns_of_select:
if column not in self.database_dico[table_of_from]:
foreign_table = self.get_tables_of_column(column)[0]
join_object.add_table(foreign_table)
link = self.get_link(table_of_from, foreign_table)
links.extend(link)
for column in self.columns_of_where:
if column not in self.database_dico[table_of_from]:
foreign_table = self.get_tables_of_column(column)[0]
join_object.add_table(foreign_table)
link = self.get_link(table_of_from, foreign_table)
links.extend(link)
join_object.set_links(self.unique_ordered(links))
query.set_join(join_object)
self.queries.append(query)
if len(join_object.get_tables()) > len(join_object.get_links()):
self.queries = None
def join(self):
Thread.join(self)
return self.queries
class WhereParser(Thread):
def __init__(self, phrases, tables_of_from, columns_of_values_of_where, count_keywords, sum_keywords, average_keywords, max_keywords, min_keywords, greater_keywords, less_keywords, between_keywords, negation_keywords, junction_keywords, disjunction_keywords, database_dico, like_keywords):
Thread.__init__(self)
self.where_objects = []
self.phrases = phrases
self.tables_of_from = tables_of_from
self.columns_of_values_of_where = columns_of_values_of_where
self.count_keywords = count_keywords
self.sum_keywords = sum_keywords
self.average_keywords = average_keywords
self.max_keywords = max_keywords
self.min_keywords = min_keywords
self.greater_keywords = greater_keywords
self.less_keywords = less_keywords
self.between_keywords = between_keywords
self.negation_keywords = negation_keywords
self.junction_keywords = junction_keywords
self.disjunction_keywords = disjunction_keywords
self.database_dico = database_dico
self.columns_of_values_of_where = columns_of_values_of_where
self.like_keywords = like_keywords
def get_tables_of_column(self, column):
tmp_table = []
for table in self.database_dico:
if column in self.database_dico[table]:
tmp_table.append(table)
return tmp_table
def get_column_name_with_alias_table(self, column, table_of_from):
one_table_of_column = self.get_tables_of_column(column)[0]
tables_of_column = self.get_tables_of_column(column)
if table_of_from in tables_of_column:
return str(table_of_from) + '.' + str(column)
else:
return str(one_table_of_column) + '.' + str(column)
def intersect(self, a, b):
return list(set(a) & set(b))
def predict_operation_type(self, previous_column_offset, current_column_offset):
interval_offset = range(previous_column_offset, current_column_offset)
if(len(self.intersect(interval_offset, self.count_keyword_offset)) >= 1):
return 'COUNT'
elif(len(self.intersect(interval_offset, self.sum_keyword_offset)) >= 1):
return 'SUM'
elif(len(self.intersect(interval_offset, self.average_keyword_offset)) >= 1):
return 'AVG'
elif(len(self.intersect(interval_offset, self.max_keyword_offset)) >= 1):
return 'MAX'
elif(len(self.intersect(interval_offset, self.min_keyword_offset)) >= 1):
return 'MIN'
else:
return None
def predict_operator(self, current_column_offset, next_column_offset):
interval_offset = range(current_column_offset, next_column_offset)
if(len(self.intersect(interval_offset, self.negation_keyword_offset)) >= 1) and (len(self.intersect(interval_offset, self.greater_keyword_offset)) >= 1):
return '<'
elif(len(self.intersect(interval_offset, self.negation_keyword_offset)) >= 1) and (len(self.intersect(interval_offset, self.less_keyword_offset)) >= 1):
return '>'
if(len(self.intersect(interval_offset, self.less_keyword_offset)) >= 1):
return '<'
elif(len(self.intersect(interval_offset, self.greater_keyword_offset)) >= 1):
return '>'
elif(len(self.intersect(interval_offset, self.between_keyword_offset)) >= 1):
return 'BETWEEN'
elif(len(self.intersect(interval_offset, self.negation_keyword_offset)) >= 1):
return '!='
elif(len(self.intersect(interval_offset, self.like_keyword_offset)) >= 1):
return 'LIKE'
else:
return '='
def predict_junction(self, previous_column_offset, current_column_offset):
interval_offset = range(previous_column_offset, current_column_offset)
junction = 'AND'
if(len(self.intersect(interval_offset, self.disjunction_keyword_offset)) >= 1):
return 'OR'
elif(len(self.intersect(interval_offset, self.junction_keyword_offset)) >= 1):
return 'AND'
first_encountered_junction_offset = -1
first_encountered_disjunction_offset = -1
for offset in self.junction_keyword_offset:
if offset >= current_column_offset:
first_encountered_junction_offset = offset
break
for offset in self.disjunction_keyword_offset:
if offset >= current_column_offset:
first_encountered_disjunction_offset = offset
break
if first_encountered_junction_offset >= first_encountered_disjunction_offset:
return 'AND'
else:
return 'OR'
def run(self):
number_of_where_columns = 0
columns_of_where = []
offset_of = {}
column_offset = []
self.count_keyword_offset = []
self.sum_keyword_offset = []
self.average_keyword_offset = []
self.max_keyword_offset = []
self.min_keyword_offset = []
self.greater_keyword_offset = []
self.less_keyword_offset = []
self.between_keyword_offset = []
self.junction_keyword_offset = []
self.disjunction_keyword_offset = []
self.negation_keyword_offset = []
self.like_keyword_offset = []
for phrase in self.phrases:
phrase_offset_string = ''
for i in range(0, len(phrase)):
for table in self.database_dico:
if phrase[i] in self.database_dico[table]:
number_of_where_columns += 1
columns_of_where.append(phrase[i])
offset_of[phrase[i]] = i
column_offset.append(i)
break
phrase_keyword = str(phrase[i]).lower() # for robust keyword matching
phrase_offset_string += phrase_keyword + " "
for keyword in self.count_keywords:
if keyword in phrase_offset_string :
if (phrase_offset_string.find(" " + keyword + " ") + len(keyword) + 2 == len(phrase_offset_string) ) :
self.count_keyword_offset.append(i)
for keyword in self.sum_keywords:
if keyword in phrase_offset_string :
if (phrase_offset_string.find(" " + keyword + " ") + len(keyword) + 2 == len(phrase_offset_string) ) :
self.sum_keyword_offset.append(i)
for keyword in self.average_keywords:
if keyword in phrase_offset_string :
if (phrase_offset_string.find(" " + keyword + " ") + len(keyword) + 2 == len(phrase_offset_string) ) :
self.average_keyword_offset.append(i)
for keyword in self.max_keywords:
if keyword in phrase_offset_string :
if (phrase_offset_string.find(" " + keyword + " ") + len(keyword) + 2 == len(phrase_offset_string) ) :
self.max_keyword_offset.append(i)
for keyword in self.min_keywords:
if keyword in phrase_offset_string :
if (phrase_offset_string.find(" " + keyword + " ") + len(keyword) + 2 == len(phrase_offset_string) ) :
self.min_keyword_offset.append(i)
for keyword in self.greater_keywords:
if keyword in phrase_offset_string :
if (phrase_offset_string.find(" " + keyword + " ") + len(keyword) + 2 == len(phrase_offset_string) ) :
self.greater_keyword_offset.append(i)
for keyword in self.less_keywords:
if keyword in phrase_offset_string :
if (phrase_offset_string.find(" " + keyword + " ") + len(keyword) + 2 == len(phrase_offset_string) ) :
self.less_keyword_offset.append(i)
for keyword in self.between_keywords:
if keyword in phrase_offset_string :
if (phrase_offset_string.find(" " + keyword + " ") + len(keyword) + 2 == len(phrase_offset_string) ) :
self.between_keyword_offset.append(i)
for keyword in self.junction_keywords:
if keyword in phrase_offset_string :
if (phrase_offset_string.find(" " + keyword + " ") + len(keyword) + 2 == len(phrase_offset_string) ) :
self.junction_keyword_offset.append(i)
for keyword in self.disjunction_keywords:
if keyword in phrase_offset_string :
if (phrase_offset_string.find(" " + keyword + " ") + len(keyword) + 2 == len(phrase_offset_string) ) :
self.disjunction_keyword_offset.append(i)
for keyword in self.negation_keywords:
if keyword in phrase_offset_string :
if (phrase_offset_string.find(" " + keyword + " ") + len(keyword) + 2 == len(phrase_offset_string) ) :
self.negation_keyword_offset.append(i)
for keyword in self.like_keywords:
if keyword in phrase_offset_string :
if (phrase_offset_string.find(" " + keyword + " ") + len(keyword) + 2 == len(phrase_offset_string) ) :
self.like_keyword_offset.append(i)
for table_of_from in self.tables_of_from:
where_object = Where()
for i in range(0, len(column_offset)):
current = column_offset[i]
if i == 0:
previous = 0
else:
previous = column_offset[i - 1]
if i == (len(column_offset) - 1):
_next = 999
else:
_next = column_offset[i + 1]
junction = self.predict_junction(previous, current)
column = self.get_column_name_with_alias_table(columns_of_where[i], table_of_from)
operation_type = self.predict_operation_type(previous, current)
if len(self.columns_of_values_of_where) > i:
value = self.columns_of_values_of_where[i]
else:
value = 'OOV' # Out Of Vocabulary: default value
operator = self.predict_operator(current, _next)
where_object.add_condition(junction, Condition(column, operation_type, operator, value))
self.where_objects.append(where_object)
def join(self):
Thread.join(self)
return self.where_objects
class GroupByParser(Thread):
def __init__(self, phrases, tables_of_from, database_dico):
Thread.__init__(self)
self.group_by_objects = []
self.phrases = phrases
self.tables_of_from = tables_of_from
self.database_dico = database_dico
def get_tables_of_column(self, column):
tmp_table = []
for table in self.database_dico:
if column in self.database_dico[table]:
tmp_table.append(table)
return tmp_table
def get_column_name_with_alias_table(self, column, table_of_from):
one_table_of_column = self.get_tables_of_column(column)[0]
tables_of_column = self.get_tables_of_column(column)
if table_of_from in tables_of_column:
return str(table_of_from) + '.' + str(column)
else:
return str(one_table_of_column) + '.' + str(column)
def run(self):
for table_of_from in self.tables_of_from:
group_by_object = GroupBy()
for phrase in self.phrases:
for i in range(0, len(phrase)):
for table in self.database_dico:
if phrase[i] in self.database_dico[table]:
column = self.get_column_name_with_alias_table(
phrase[i], table_of_from)
group_by_object.set_column(column)
self.group_by_objects.append(group_by_object)
def join(self):
Thread.join(self)
return self.group_by_objects
class OrderByParser(Thread):
def __init__(self, phrases, tables_of_from, asc_keywords, desc_keywords, database_dico):
Thread.__init__(self)
self.order_by_objects = []
self.phrases = phrases
self.tables_of_from = tables_of_from
self.asc_keywords = asc_keywords
self.desc_keywords = desc_keywords
self.database_dico = database_dico
def get_tables_of_column(self, column):
tmp_table = []
for table in self.database_dico:
if column in self.database_dico[table]:
tmp_table.append(table)
return tmp_table
def get_column_name_with_alias_table(self, column, table_of_from):
one_table_of_column = self.get_tables_of_column(column)[0]
tables_of_column = self.get_tables_of_column(column)
if table_of_from in tables_of_column:
return str(table_of_from) + '.' + str(column)
else:
return str(one_table_of_column) + '.' + str(column)
def intersect(self, a, b):
return list(set(a) & set(b))
def predict_order(self, phrase):
if(len(self.intersect(phrase, self.desc_keywords)) >= 1):
return 'DESC'
else:
return 'ASC'
def run(self):
for table_of_from in self.tables_of_from:
order_by_object = OrderBy()
for phrase in self.phrases:
for i in range(0, len(phrase)):
for table in self.database_dico:
if phrase[i] in self.database_dico[table]:
column = self.get_column_name_with_alias_table(phrase[i], table_of_from)
order_by_object.add_column(column, self.predict_order(phrase))
self.order_by_objects.append(order_by_object)
def join(self):
Thread.join(self)
return self.order_by_objects
# --------------------------------PART OF ALGORITHM FOR VALUE EXTRACTION STRARTS
def _myCmp(s1,s2):
# if len(s1.split()) == 1 and len(s2.split()) == 1:
if len(s1.split()) == len(s2.split()) :
if len(s1) >= len(s2) :
return 1
else:
return -1
else:
if len(s1.split()) >= len(s2.split()):
return 1
else:
return -1
def _transformationSortAlgo(transitionalList):
return sorted(transitionalList, key=functools.cmp_to_key(_myCmp),reverse=True)
# -----------------------------------PART OF ALGORITHM FOR VALUE EXTRACTION ENDS
class Parser:
database_object = None
database_dico = None
language = None
thesaurus_object = None
count_keywords = []
sum_keywords = []
average_keywords = []
max_keywords = []
min_keywords = []
junction_keywords = []
disjunction_keywords = []
greater_keywords = []
less_keywords = []
between_keywords = []
order_by_keywords = []
asc_keywords = []
desc_keywords = []
group_by_keywords = []
negation_keywords = []
equal_keywords = []
like_keywords = []
def __init__(self, database, config):
self.database_object = database
self.database_dico = self.database_object.get_tables_into_dictionnary()
self.count_keywords = config.get_count_keywords()
self.sum_keywords = config.get_sum_keywords()
self.average_keywords = config.get_avg_keywords()
self.max_keywords = config.get_max_keywords()
self.min_keywords = config.get_min_keywords()
self.junction_keywords = config.get_junction_keywords()
self.disjunction_keywords = config.get_disjunction_keywords()
self.greater_keywords = config.get_greater_keywords()
self.less_keywords = config.get_less_keywords()
self.between_keywords = config.get_between_keywords()
self.order_by_keywords = config.get_order_by_keywords()
self.asc_keywords = config.get_asc_keywords()
self.desc_keywords = config.get_desc_keywords()
self.group_by_keywords = config.get_group_by_keywords()
self.negation_keywords = config.get_negation_keywords()
self.equal_keywords = config.get_equal_keywords()
self.like_keywords = config.get_like_keywords()
# self.distinct_keywords = config.get_distinct_keywords()
# @todo DISTINCT functionality needs to be implemented
def set_thesaurus(self, thesaurus):
self.thesaurus_object = thesaurus
def remove_accents(self, string):
nkfd_form = unicodedata.normalize('NFKD', unicode(string))
return u"".join([c for c in nkfd_form if not unicodedata.combining(c)])
def parse_sentence(self, sentence):
number_of_table = 0
number_of_select_column = 0
number_of_where_column = 0
last_table_position = 0
columns_of_select = []
columns_of_where = []
input_for_finding_value = sentence.decode('utf-8').rstrip(string.punctuation.replace('"','').replace("'",""))
columns_of_values_of_where = []
filter_list = [",", "!"]
for filter_element in filter_list:
input_for_finding_value = input_for_finding_value.replace(filter_element, " ")
input_word_list = input_for_finding_value.split()
number_of_where_column_temp = 0
number_of_table_temp = 0
last_table_position_temp = 0
start_phrase = ''
med_phrase = ''
end_phrase = ''
for i in range(0, len(input_word_list)):
if input_word_list[i] in self.database_dico:
if number_of_table_temp == 0:
start_phrase = input_word_list[:i]
number_of_table_temp += 1
last_table_position_temp = i
for table in self.database_dico:
if input_word_list[i] in self.database_dico[table]:
if number_of_where_column_temp == 0:
med_phrase = input_word_list[
len(start_phrase):last_table_position_temp + 1]
number_of_where_column_temp += 1
break
else:
if (number_of_table_temp != 0) and (number_of_where_column_temp == 0) and (i == (len(input_word_list) - 1)):
med_phrase = input_word_list[len(start_phrase):]
end_phrase = input_word_list[len(start_phrase) + len(med_phrase):]
irext = ' '.join(end_phrase)
''' @todo set this part of the algorithm (detection of values of where) in the part of the phrases where parsing '''
if irext:
irext = irext.lower()
# .lower() is necessary to make our own irext case insensetive for proper value extraction and it will not even
# reflect any problems for Case Sensetive fields , it is just for improving logic for our extracting assigners.
# eg -> "show data for city where cityName is LIke Pune" A query like this would also work even if lang you dont write all the permutations of 'like'.
filter_list = [",", "!"]
for filter_element in filter_list:
irext = irext.replace(filter_element, " ")
assignment_list = self.equal_keywords + self.like_keywords + self.greater_keywords + self.less_keywords + self.negation_keywords
# As these words can also be part of assigners
assignment_list.append(':')
assignment_list.append('=')
# custom operators added as they can be possibilities
assignment_list = _transformationSortAlgo(assignment_list) # Algorithmic logic for best substitution for extraction of values with the help of assigners.
maverickjoy_general_assigner = "*res*@3#>>*"
maverickjoy_like_assigner = "*like*@3#>>*"
for idx,assigner in enumerate(assignment_list):
if assigner in self.like_keywords:
assigner = str(" " + assigner + " ")
irext = irext.replace(assigner, str(" "+maverickjoy_like_assigner+" "))
else:
assigner = str(" " + assigner + " ")
# Reason for adding " " these is according to the LOGIC implemented assigner operators help us extract the value,
# hence they should be independent entities not part of some other big entity else logic will fail.
# for eg -> "show data for city where cityName where I like to risk my life is Pune" will end up extacting ,
# 'k' and '1' both. I know its a lame sentence but something like this could be a problem.
irext = irext.replace(assigner, str(" "+maverickjoy_general_assigner+" "))
# replace all spaces from values to <_> for proper value assignment in SQL
# eg. (where name is 'abc def') -> (where name is abc<_>def)
for i in re.findall("(['\"].*?['\"])", irext):
irext = irext.replace(i, i.replace(' ', '<_>').replace("'", '').replace('"',''))
irext_list = irext.split()
for idx, x in enumerate(irext_list):
index = idx + 1
if x == maverickjoy_like_assigner:
if index < len(irext_list) and irext_list[index] != maverickjoy_like_assigner and irext_list[index] != maverickjoy_general_assigner:
# replace back <_> to spaces from the values assigned
columns_of_values_of_where.append(str("'%" + str(irext_list[index]).replace('<_>', ' ') + "%'"))
if x == maverickjoy_general_assigner:
if index < len(irext_list) and irext_list[index] != maverickjoy_like_assigner and irext_list[index] != maverickjoy_general_assigner:
# replace back <_> to spaces from the values assigned
columns_of_values_of_where.append(str("'" + str(irext_list[index]).replace('<_>', ' ') + "'"))
# print "columns_of_values_of_where : ",columns_of_values_of_where
tables_of_from = []
select_phrase = ''
from_phrase = ''
where_phrase = ''
words = re.findall(r"[\w]+", self.remove_accents(sentence.decode('utf-8')))
for i in range(0, len(words)):
if words[i] in self.database_dico:
if number_of_table == 0:
select_phrase = words[:i]
tables_of_from.append(words[i])
number_of_table += 1
last_table_position = i
for table in self.database_dico:
if words[i] in self.database_dico[table]:
if number_of_table == 0:
columns_of_select.append(words[i])
number_of_select_column += 1
else:
if number_of_where_column == 0:
from_phrase = words[
len(select_phrase):last_table_position + 1]
columns_of_where.append(words[i])
number_of_where_column += 1
break
else:
if (number_of_table != 0) and (number_of_where_column == 0) and (i == (len(words) - 1)):
from_phrase = words[len(select_phrase):]
where_phrase = words[len(select_phrase) + len(from_phrase):]
if (number_of_select_column + number_of_table + number_of_where_column) == 0:
raise ParsingException("No keyword found in sentence!")
if len(tables_of_from) > 0:
from_phrases = []
previous_index = 0
for i in range(0, len(from_phrase)):
if from_phrase[i] in tables_of_from:
from_phrases.append(from_phrase[previous_index:i + 1])
previous_index = i + 1
last_junction_word_index = -1
for i in range(0, len(from_phrases)):
number_of_junction_words = 0
number_of_disjunction_words = 0
for word in from_phrases[i]:
if word in self.junction_keywords:
number_of_junction_words += 1
if word in self.disjunction_keywords:
number_of_disjunction_words += 1
if (number_of_junction_words + number_of_disjunction_words) > 0:
last_junction_word_index = i
if last_junction_word_index == -1:
from_phrase = sum(from_phrases[:1], [])
where_phrase = sum(from_phrases[1:], []) + where_phrase
else:
from_phrase = sum(
from_phrases[:last_junction_word_index + 1], [])
where_phrase = sum(
from_phrases[last_junction_word_index + 1:], []) + where_phrase
real_tables_of_from = []
for word in from_phrase:
if word in tables_of_from:
real_tables_of_from.append(word)
tables_of_from = real_tables_of_from
if len(tables_of_from) == 0:
raise ParsingException("No table name found in sentence!")
group_by_phrase = []
order_by_phrase = []
new_where_phrase = []
previous_index = 0
previous_phrase_type = 0
yet_where = 0
for i in range(0, len(where_phrase)):
if where_phrase[i] in self.order_by_keywords:
if yet_where > 0:
if previous_phrase_type == 1:
order_by_phrase.append(where_phrase[previous_index:i])
elif previous_phrase_type == 2:
group_by_phrase.append(where_phrase[previous_index:i])
else:
new_where_phrase.append(where_phrase[previous_index:i])
previous_index = i
previous_phrase_type = 1
yet_where += 1
if where_phrase[i] in self.group_by_keywords:
if yet_where > 0:
if previous_phrase_type == 1:
order_by_phrase.append(where_phrase[previous_index:i])
elif previous_phrase_type == 2:
group_by_phrase.append(where_phrase[previous_index:i])
else:
new_where_phrase.append(where_phrase[previous_index:i])
previous_index = i
previous_phrase_type = 2
yet_where += 1
if previous_phrase_type == 1:
order_by_phrase.append(where_phrase[previous_index:])
elif previous_phrase_type == 2:
group_by_phrase.append(where_phrase[previous_index:])
else:
new_where_phrase.append(where_phrase)
select_parser = SelectParser(columns_of_select, tables_of_from, select_phrase, self.count_keywords, self.sum_keywords, self.average_keywords, self.max_keywords, self.min_keywords, self.database_dico)
from_parser = FromParser(tables_of_from, columns_of_select, columns_of_where, self.database_object)
where_parser = WhereParser(new_where_phrase, tables_of_from, columns_of_values_of_where, self.count_keywords, self.sum_keywords, self.average_keywords, self.max_keywords, self.min_keywords, self.greater_keywords, self.less_keywords, self.between_keywords, self.negation_keywords, self.junction_keywords, self.disjunction_keywords, self.database_dico, self.like_keywords)
group_by_parser = GroupByParser(group_by_phrase, tables_of_from, self.database_dico)
order_by_parser = OrderByParser(order_by_phrase, tables_of_from, self.asc_keywords, self.desc_keywords, self.database_dico)
select_parser.start()
from_parser.start()
where_parser.start()
group_by_parser.start()
order_by_parser.start()
queries = from_parser.join()
if queries is None:
raise ParsingException("There is at least one unattainable column from the table of FROM!")
select_objects = select_parser.join()
where_objects = where_parser.join()
group_by_objects = group_by_parser.join()
order_by_objects = order_by_parser.join()
for i in range(0, len(queries)):
query = queries[i]
query.set_select(select_objects[i])
query.set_where(where_objects[i])
query.set_group_by(group_by_objects[i])
query.set_order_by(order_by_objects[i])
return queries