-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
325 lines (317 loc) · 18.5 KB
/
main.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
import streamlit as st
from utils import *
def search():
# display header
col1, col2 = st.columns([2, 8])
with col2:
st.header('Brand New Multilingual Pragmaticon')
with col1:
st.image('https://github.com/vantral/pragmaticon/raw/master/static/img/logo.png')
# connect to db
cur, conn = connect_to_db()
# realisation search
realisation = st.text_input(
label='Word',
#placeholder='Use the Latin script'
)
# display search options if realisation search is not active
if not realisation:
col1, col2 = st.columns([5, 5])
with col1:
options = get_options(cur, 'primary_semantics')
primary_sem = st.multiselect(
label='Pragmatics',
options=sorted(options.values())
)
with col2:
options = get_options(cur, 'additional_semantics')
add_sem = st.multiselect(
label='Additional semantics',
options=sorted(options.values())
)
options = get_options(cur, 'lemmas')
lemmas = st.multiselect(
label='Lemma',
options=sorted(options.values())
)
col1, col2 = st.columns([5, 5])
with col1:
options = get_options(cur, 'glosses')
glosses = st.multiselect(
label='Glosses',
options=sorted(options.values())
)
with col2:
options = get_options(cur, 'languages')
languages = st.multiselect(
label='Language',
options=sorted(options.values())
)
constr_syntax = st.text_input(
label='Syntactic Structure',
#placeholder='VERB'
)
col1, col2 = st.columns([5, 5])
with col1:
options = get_options(cur, 'inner_structure_types')
inner_structure_type = st.multiselect(
label='Inner Structure',
options=sorted(options.values())
)
options = get_options(cur, 'structures')
structure = st.multiselect(
label='Structure',
options=sorted(options.values())
)
with col2:
options = get_options(cur, 'inner_structure_subtypes')
inner_structure_subtype = st.multiselect(
label='Inner Structure Subtype',
options=sorted(options.values())
)
options = get_options(cur, 'speech_acts')
speech_act = st.multiselect(
label='Speech Act',
options=sorted(options.values())
)
options = get_options(cur, 'intonations')
intonations = st.multiselect(
label='Intonation',
options=sorted(options.values())
)
sort_by = st.radio('Sort by', ['language', 'variants'])
result_number = st.slider('Number of results', 1, 100)
button = st.button('Search', key='1')
button2 = None
if not realisation:
placeholder_sql = """SELECT * FROM realisations
JOIN intonations using(intonation_id)
WHERE intonations.intonation='statement'"""
own_query = st.text_area(label='Custom SQL query:')
#placeholder=placeholder_sql)
button2 = st.button('Execute', key='2')
if button == 1:
st.subheader('Search Results:')
# exact formula search
if realisation:
cur.execute("""
WITH found_formulas AS (
SELECT formula_id, formula, language
FROM formulas
LEFT JOIN languages
ON formulas.language_id = languages.language_id
WHERE formula = %s
), inner_structs AS (
SELECT realisation_id, CONCAT(inner_structure_type, ': ', inner_structure_subtype) AS type
FROM realisation2inner_structure
LEFT JOIN inner_structure_types
ON realisation2inner_structure.inner_structure_type_id = inner_structure_types.inner_structure_type_id
LEFT JOIN inner_structure_subtypes
ON realisation2inner_structure.inner_structure_subtype_id = inner_structure_subtypes.inner_structure_subtype_id
GROUP BY realisation_id, type
), full_lemmas AS (
SELECT realisation_id, string_agg(lemma, ' ') AS lemmatized
FROM realisation2lemma
LEFT JOIN lemmas
ON realisation2lemma.lemma_id = lemmas.lemma_id
GROUP BY realisation_id
), full_sem AS (
SELECT realisation_id, primary_sem, string_agg(additional_sem, ' | ') AS add_sem
FROM semantics
LEFT JOIN primary_semantics
ON semantics.primary_sem_id = primary_semantics.primary_sem_id
LEFT JOIN additional_semantics
ON semantics.additional_sem_id = additional_semantics.additional_sem_id
GROUP BY realisation_id, primary_sem
), source_constr AS (
SELECT сonstruction_id, construction, construction_syntax, intonation AS sc_intonation
FROM source_constructions
LEFT JOIN intonations
ON source_constructions.intonation_id = intonations.intonation_id
), sa AS (
SELECT realisation_id, string_agg(speech_acts.speech_act, ' | ') AS speech_act,
string_agg(speech_acts_1.speech_act, ' | ') AS speech_act_1
FROM (SELECT * FROM speech_acts) AS speech_acts_1
RIGHT JOIN realisation2speech_acts
ON realisation2speech_acts.speech_act_1_id = speech_acts_1.speech_act_id
LEFT JOIN speech_acts
ON realisation2speech_acts.speech_act_id = speech_acts.speech_act_id
GROUP BY realisation_id
)
SELECT formula, language, string_agg(CONCAT(realisation, '\n', full_gloss, '\n',
lemmatized, '\n\n', examples), '+'), type, primary_sem, string_agg(add_sem, ' | '), intonation,
construction, construction_syntax, sc_intonation, structure, speech_act, speech_act_1
FROM realisations
JOIN found_formulas
ON realisations.formula_id = found_formulas.formula_id
LEFT JOIN inner_structs ON realisations.realisation_id = inner_structs.realisation_id
LEFT JOIN full_lemmas ON realisations.realisation_id = full_lemmas.realisation_id
LEFT JOIN full_sem ON realisations.realisation_id = full_sem.realisation_id
LEFT JOIN intonations ON realisations.intonation_id = intonations.intonation_id
LEFT JOIN source_constr ON realisations.source_constr_id = source_constr.сonstruction_id
LEFT JOIN sa ON realisations.realisation_id = sa.realisation_id
LEFT JOIN structures ON realisations.structure_id = structures.structure_id
GROUP BY language, formula, type, primary_sem, intonation, construction,
construction_syntax, sc_intonation, structure, speech_act, speech_act_1
""", (realisation,))
elif sort_by=='language':
cur.execute("""WITH glosses
AS(
SELECT realisation_id, gloss
FROM realisation2gloss
LEFT JOIN glosses using(gloss_id)),
inner_structure AS(SELECT realisation_id, inner_structure_type, inner_structure_subtype
FROM realisation2inner_structure
LEFT JOIN inner_structure_types using(inner_structure_type_id)
LEFT JOIN inner_structure_subtypes using(inner_structure_subtype_id)),
semantics AS(SELECT realisation_id, primary_sem, additional_sem
FROM semantics
LEFT JOIN primary_semantics using(primary_sem_id)
LEFT JOIN additional_semantics using(additional_sem_id)),
speech_acts
AS(
SELECT realisation_id, speech_acts.speech_act
AS speech_act, string_agg(speech_acts_1.speech_act, '|') AS speech_act_1
FROM(SELECT * FROM speech_acts) AS speech_acts_1
RIGHT JOIN realisation2speech_acts
ON speech_acts_1.speech_act_id = realisation2speech_acts.speech_act_1_id
LEFT JOIN speech_acts
ON realisation2speech_acts.speech_act_id = speech_acts.speech_act_id
GROUP BY realisation_id, speech_acts.speech_act),
lemmas AS(
SELECT realisation_id, lemma
FROM realisation2lemma
LEFT JOIN lemmas using(lemma_id)),
formulas AS(
SELECT formula_id, formula, language
FROM formulas
LEFT JOIN languages using(language_id)),
source_constr AS(
SELECT сonstruction_id, construction, construction_syntax, intonation
FROM source_constructions
LEFT JOIN intonations using(intonation_id))
SELECT formula, language, string_agg(CONCAT(realisation, '\n', full_gloss, '\n', lemma, '\n\n', examples),
'+') AS variant, inner_structure_type, primary_sem, string_agg(additional_sem, ' | '), intonation,
construction, construction_syntax, structure, speech_act, speech_act_1
FROM realisations
LEFT JOIN glosses using(realisation_id)
LEFT JOIN inner_structure using(realisation_id)
LEFT JOIN semantics using(realisation_id)
LEFT JOIN speech_acts using(realisation_id)
LEFT JOIN lemmas using(realisation_id)
LEFT JOIN formulas using(formula_id)
LEFT JOIN source_constr
ON realisations.source_constr_id = source_constr.сonstruction_id
LEFT JOIN structures using(structure_id)
WHERE (%(glosses)s='{}' OR gloss=ANY(%(glosses)s))
AND (%(lemmas)s='{}' OR lemma=ANY(%(lemmas)s))
AND (%(inner_structure_type)s='{}' OR inner_structure_type=ANY(%(inner_structure_type)s))
AND (%(inner_structure_subtype)s='{}' OR inner_structure_subtype=ANY(%(inner_structure_subtype)s))
AND (%(primary_sem)s='{}' OR primary_sem=ANY(%(primary_sem)s))
AND (%(add_sem)s='{}' OR additional_sem=ANY(%(add_sem)s))
AND (%(languages)s='{}' OR language=ANY(%(languages)s))
AND (%(constr_syntax)s='' OR construction_syntax=%(constr_syntax)s)
AND (%(speech_act)s='{}' OR speech_act=ANY(%(speech_act)s))
AND (%(structure)s='{}' OR structure=ANY(%(structure)s))
AND (%(intonations)s='{}' OR intonation=ANY(%(intonations)s))
GROUP BY language, formula, inner_structure_type, primary_sem, additional_sem, intonation, construction,
construction_syntax, structure, speech_act, speech_act_1
ORDER BY language""", {'glosses':glosses, 'lemmas':lemmas,
'inner_structure_type':inner_structure_type,
'inner_structure_subtype':inner_structure_subtype,
'primary_sem':primary_sem,
'add_sem':add_sem, 'languages':languages,
'constr_syntax':constr_syntax, 'speech_act':speech_act,
'structure':structure, 'intonations':intonations,})
else:
cur.execute("""WITH glosses
AS(
SELECT realisation_id, gloss
FROM realisation2gloss
LEFT JOIN glosses using(gloss_id)),
inner_structure AS(SELECT realisation_id, inner_structure_type, inner_structure_subtype
FROM realisation2inner_structure
LEFT JOIN inner_structure_types using(inner_structure_type_id)
LEFT JOIN inner_structure_subtypes using(inner_structure_subtype_id)),
semantics AS(SELECT realisation_id, primary_sem, additional_sem
FROM semantics
LEFT JOIN primary_semantics using(primary_sem_id)
LEFT JOIN additional_semantics using(additional_sem_id)),
speech_acts
AS(
SELECT realisation_id, speech_acts.speech_act
AS speech_act, string_agg(speech_acts_1.speech_act, '|') AS speech_act_1
FROM(SELECT * FROM speech_acts) AS speech_acts_1
RIGHT JOIN realisation2speech_acts
ON speech_acts_1.speech_act_id = realisation2speech_acts.speech_act_1_id
LEFT JOIN speech_acts
ON realisation2speech_acts.speech_act_id = speech_acts.speech_act_id
GROUP BY realisation_id, speech_acts.speech_act),
lemmas AS(
SELECT realisation_id, lemma
FROM realisation2lemma
LEFT JOIN lemmas using(lemma_id)),
formulas AS(
SELECT formula_id, formula, language
FROM formulas
LEFT JOIN languages using(language_id)),
source_constr AS(
SELECT сonstruction_id, construction, construction_syntax, intonation
FROM source_constructions
LEFT JOIN intonations using(intonation_id))
SELECT formula, language, string_agg(CONCAT(realisation, '\n', full_gloss, '\n', lemma, '\n\n', examples),
'+') AS variant, inner_structure_type, primary_sem, string_agg(additional_sem, ' | '), intonation,
construction, construction_syntax, structure, speech_act, speech_act_1
FROM realisations
LEFT JOIN glosses using(realisation_id)
LEFT JOIN inner_structure using(realisation_id)
LEFT JOIN semantics using(realisation_id)
LEFT JOIN speech_acts using(realisation_id)
LEFT JOIN lemmas using(realisation_id)
LEFT JOIN formulas using(formula_id)
LEFT JOIN source_constr
ON realisations.source_constr_id = source_constr.сonstruction_id
LEFT JOIN structures using(structure_id)
WHERE (%(glosses)s='{}' OR gloss=ANY(%(glosses)s))
AND (%(lemmas)s='{}' OR lemma=ANY(%(lemmas)s))
AND (%(inner_structure_type)s='{}' OR inner_structure_type=ANY(%(inner_structure_type)s))
AND (%(inner_structure_subtype)s='{}' OR inner_structure_subtype=ANY(%(inner_structure_subtype)s))
AND (%(primary_sem)s='{}' OR primary_sem=ANY(%(primary_sem)s))
AND (%(add_sem)s='{}' OR additional_sem=ANY(%(add_sem)s))
AND (%(languages)s='{}' OR language=ANY(%(languages)s))
AND (%(constr_syntax)s='' OR construction_syntax=%(constr_syntax)s)
AND (%(speech_act)s='{}' OR speech_act=ANY(%(speech_act)s))
AND (%(structure)s='{}' OR structure=ANY(%(structure)s))
AND (%(intonations)s='{}' OR intonation=ANY(%(intonations)s))
GROUP BY language, formula, inner_structure_type, primary_sem, additional_sem, intonation, construction,
construction_syntax, structure, speech_act, speech_act_1
ORDER BY COUNT(realisation) DESC""", {'glosses': glosses, 'lemmas': lemmas,
'inner_structure_type': inner_structure_type,
'inner_structure_subtype': inner_structure_subtype,
'primary_sem': primary_sem,
'add_sem': add_sem, 'languages': languages,
'constr_syntax': constr_syntax, 'speech_act': speech_act,
'structure': structure, 'intonations': intonations,})
results = cur.fetchall()
conn.close()
if results:
print_results(results[:result_number])
else:
st.text('Nothing found :(')
else:
if button2 == 1:
if 'select' in own_query.lower():
cur.execute(own_query)
results = cur.fetchall()
st.dataframe(data=own_query_results(results))
else:
try:
cur.execute(own_query)
st.text("Query Succeeded")
except:
st.text("Query Failed :(")
conn.commit()
def main():
search()
if __name__ == '__main__':
main()