-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.py
124 lines (82 loc) · 2.55 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
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 5 17:10:01 2017
@author: tom
"""
import polyglot
from polyglot.text import Text, Word
from polyglot.downloader import downloader
#print(downloader.supported_languages_table("pos2"))
#Correspondances :
#ADJ: adjective
#ADP: adposition
#ADV: adverb
#AUX: auxiliary verb
#CONJ: coordinating conjunction
#DET: determiner
#INTJ: interjection
#NOUN: noun
#NUM: numeral
#PART: particle
#PRON: pronoun
#PROPN: proper noun
#PUNCT: punctuation
#SCONJ: subordinating conjunction
#SYM: symbol
#VERB: verb
#X: other
blob = """Je veux la moyenne d'âge des agents en fonction de leur salaire."""
#blob2 = """ Quel est la moyenne d'âge des personnes travaillant en mairie et qui gagnent plus 3000 par mois?"""
#text = Text(blob, hint_language_code='fr')
#print(text.pos_tags)
def text_parser(text):
"""Parses the text into sub groups of words
Returns a list of the subgroups"""
agregation = []
metric = []
dimension= [[]]
filters = [[]]
text = Text(text, hint_language_code='fr')
pos_tags = text.pos_tags
#Finding the agregation
verb_counter = 0
noun = None
i = 0
while noun == None and i < len(pos_tags):
if pos_tags[i][1] == 'VERB':
verb_counter += 1
if verb_counter > 0:
if pos_tags[i][1] == 'NOUN':
noun = pos_tags[i][0]
agregation_pos = i
i += 1
agregation.append(noun)
#Finding the metric
i = agregation_pos + 1
while i < len(pos_tags) and pos_tags[i][1] != 'ADP':
metric.append(pos_tags[i][0])
i += 1
end_metric_pos = i - 1
#Finding the dimensions
i = end_metric_pos + 1
dimension_counter = 0
while i < len(pos_tags) and pos_tags[i][1] != 'CONJ':
print(pos_tags[i][1])
if pos_tags[i][0] == 'et':
dimension_counter += 1
dimension.append([])
else :
dimension[dimension_counter].append(pos_tags[i][0])
i += 1
end_dimension_pos = i-1
#Finding the filters
i = end_dimension_pos + 1
filter_counter = 0
while i < len(pos_tags) :
if pos_tags[i][0] == 'et':
filter_counter += 1
filters.append([])
else :
filters[filter_counter].append(pos_tags[i][0])
i += 1
return([agregation,metric,dimension,filters])