-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery_processor.py
426 lines (351 loc) · 14 KB
/
query_processor.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
import sqlite3
import json
import io
import os
from subprocess import Popen, PIPE
from collections import defaultdict
from typing import Set
from unicodedata import normalize
from dataclasses import dataclass
from QueryParser import query_parser, QueryTransformer, ASTNode, OrNode, AndNode, NotNode
from QueryParser import EqFeature, EqPhoneme, EqFeatures
from helpers import get_all_language_ids # get_count_for_features, check_eq
#
# Globals
#
USAGE_short = """Usage:
python query_processor.py query [phylum=family-list|genus=group-list]
or
python query_processor.py help
"""
USAGE = '''
Usage
=====
python query_processor.py query [phylum=family-list|genus=group-list]
returns the list of languages satisfying the query.
python query_processor.py list-features
lists all consonant and vowel features that can be used in a query.
python query_processor.py list-phyla
lists all languages families found in the database.
python query_processor.py genus-tree
prints a tree of all language groups found in the database organised
by language family.
Query types
===========
1. Presence/absence for segments and feature bundles:
"+ /p/" (languages that have /p/), "- labio-dental fricative"
(languages without labio-dental fricatives), &c.
2. Count queries for segments and feature bundles:
"> 4 bilabial consonant" (languages with more than 4 bilabial consonants),
"= 3 plosive" (languages with exactly 3 plosives), &c.
Supported comparison operators: =, <, >, <=, >=.
3. Comparison queries for feature bundles:
"> bilabial consonant, dental consonant" (languages with more bilabial
consonants than dental consonants), &c. The same comparison operators
are supported as above. Note that elements of feature lists are separated
by spaces, while the two lists are separated by a comma.
Complex queries
===============
Queries can be negated using "not" and combined using "and" and "or".
Operator-precedence order: not > and > or.
Use parentheses for nested queries and clarity.
An example complex query:
not (
>= bilabial plosive, labio-dental fricative
or
<= bilabial plosive, labio-dental fricative
) or
+ /p/ and > 4 approximant
Restricting the search by phyla or genera
=========================================
It is possible to apply queries only to languages from particular
language families or groups. Examples:
python query_processor.py "- /p/" "phylum=Indo-European,Austronesian"
python query_processor.py "< nasal consonant, lateral consonant" "genus=Avar-Andi"
'''
@dataclass
class Language:
iso: str
name: str
phylum: str
genus: str
latitude: str
longitude: str
db_connection = sqlite3.connect(os.path.join('data', 'europhon.sqlite'))
meta = {
language_id: Language(iso, language_name, phylum,
genus, latitude, longitude)
for language_id, iso, language_name, phylum, genus, latitude, longitude
in db_connection.execute(
"""
SELECT
languages.id, languages.`iso_code`, languages.name,
phyla.name, genera.name, languages.latitude, languages.longitude
FROM languages
LEFT JOIN phyla ON
languages.phylum_id = phyla.id
LEFT JOIN genera ON
languages.genus_id = genera.id
""")
}
with open(f'phoible_meta.json', 'r', encoding='utf-8') as inp:
meta_phoible = json.load(inp)
query_transformer = QueryTransformer()
def apply_query(query: ASTNode, db_connection: sqlite3.Connection, query_phoible: bool = False) -> Set[int]:
"""
Recursively applies the query transformed into an ASTNode to the inventories
stored in the database and returns a set of inventory ids.
"""
if type(query) == OrNode:
return apply_query(query.lhs, db_connection, query_phoible) | apply_query(query.rhs, db_connection, query_phoible)
elif type(query) == AndNode:
return apply_query(query.lhs, db_connection, query_phoible) & apply_query(query.rhs, db_connection, query_phoible)
elif type(query) == NotNode:
return get_all_language_ids(db_connection, query_phoible) - apply_query(query.query, db_connection, query_phoible)
elif type(query) == EqPhoneme:
return apply_eq_phoneme_go(query, query_phoible)
elif type(query) == EqFeature:
return apply_eq_feature_go(query, query_phoible)
elif type(query) == EqFeatures:
return apply_eq_features_go(query, query_phoible)
else:
raise NotImplementedError(
f'The query type is not recognised: {type(query)}')
# It turned out to be much faster to iterate over inventories,
# both as IPA-encoded segments and as lists of lists of features,
# in separate Go binaries than do this in Python itself, even
# if they have to read all the data from disk first for each run!
# The main motivation for keeping the Python wrapper is to continue
# using Lark.
def apply_eq_phoneme_go(query: EqPhoneme, query_phoible: bool = False):
test_segment = normalize('NFD', query.phoneme)
input_string = '\n'.join([
'phoible' if query_phoible else 'eurphon',
query.op,
str(query.number),
test_segment
]) + '\n'
go_process = Popen(['./phonemequery'], stdin=PIPE, stdout=PIPE)
output = go_process.communicate(input=input_string.encode())[0].decode()
return set(json.loads(output))
def supply_defaults(input_set):
feature_set = set(el for el in input_set)
if ('+', 'approximant') in feature_set and ('+', 'lateral') not in feature_set:
feature_set.add(('-', 'lateral'))
if ('+', 'plosive') in feature_set and ('+', 'nasal') not in feature_set:
feature_set.add(('-', 'nasal'))
return feature_set
def apply_eq_feature_go(query: EqFeature, query_phoible: bool = False):
prefixed_features = [
f'{prefix}{feature}' for prefix, feature in supply_defaults(query.features)]
input_string = '\n'.join([
'phoible' if query_phoible else 'eurphon',
query.op,
str(query.number),
json.dumps(prefixed_features)
]) + '\n'
print(input_string)
go_process = Popen(['./countquery'], stdin=PIPE, stdout=PIPE)
output = go_process.communicate(input=input_string.encode())[0].decode()
return set(json.loads(output))
def apply_eq_features_go(query: EqFeatures, query_phoible: bool = False):
prefixed_features1 = [
f'{prefix}{feature}' for prefix, feature in supply_defaults(query.features_1)]
prefixed_features2 = [
f'{prefix}{feature}' for prefix, feature in supply_defaults(query.features_2)]
input_string = '\n'.join([
'phoible' if query_phoible else 'eurphon',
query.op,
json.dumps(prefixed_features1),
json.dumps(prefixed_features2)
]) + '\n'
go_process = Popen(['./comparisonquery'], stdin=PIPE, stdout=PIPE)
output = go_process.communicate(input=input_string.encode())[0].decode()
return set(json.loads(output))
#
# UI functions
#
def print_features():
with open(f'parses_cache.json', 'r', encoding='utf-8') as inp:
parses = json.load(inp)
consonant_features = set()
vowel_features = set()
for feature_list in parses.values():
if 'consonant' in feature_list:
consonant_features.update(feature_list)
else:
vowel_features.update(feature_list)
consonant_features.discard('consonant')
vowel_features.discard('vowel')
with io.StringIO() as out:
print('"vowel" and "consonant" can be used as features.', file=out)
print('', file=out)
print('Vowel features', file=out)
print('==============', file=out)
print(', '.join(sorted(vowel_features)), file=out)
print('', file=out)
print('Consonant features', file=out)
print('==================', file=out)
print(', '.join(sorted(consonant_features)), file=out)
return out.getvalue()
def print_phyla():
phyla = sorted(row[0] for row
in db_connection.execute("SELECT name FROM phyla"))
return '\n'.join(phyla)
def print_genus_tree():
tree = defaultdict(list)
for phylum_id, phylum in db_connection.execute(
"SELECT id, name FROM phyla"
):
tree[phylum] = sorted(row[0] for row in db_connection.execute(
"SELECT name FROM genera WHERE `phylum_id` = ?",
(phylum_id,)))
with io.StringIO() as out:
for phylum in sorted(tree):
print(phylum, file=out)
for genus in tree[phylum]:
print('\t' + genus, file=out)
return out.getvalue()
def parse_query(query_string):
"We separate this bit into a separate function to catch Lark exceptions."
return query_parser.parse(query_string)
def apply_query_and_filter(query_tree, restrictor_dict={}, query_phoible=False):
# Transforming the query after successfully parsing it should be safe.
query = query_transformer.transform(query_tree)
result = apply_query(query, db_connection, query_phoible)
if 'phylum' in restrictor_dict:
phyla = restrictor_dict['phylum']
result = list(
filter(lambda lang_id: meta[lang_id].phylum in phyla, result))
elif 'genus' in restrictor_dict:
genera = restrictor_dict['genus']
result = list(
filter(lambda lang_id: meta[lang_id].genus in genera, result))
if query_phoible:
result = {
lang_id: {
'name': meta_phoible[str(lang_id)]['name'],
'glottocode': meta_phoible[str(lang_id)]['glottocode'],
'phylum': meta_phoible[str(lang_id)]['phylum'],
'genus': meta_phoible[str(lang_id)]['genus'],
'latitude': meta_phoible[str(lang_id)]['latitude'],
'longitude': meta_phoible[str(lang_id)]['longitude']
} for lang_id in result
}
else:
result = {
lang_id: {
'name': meta[lang_id].name,
'iso': meta[lang_id].iso,
'phylum': meta[lang_id].phylum,
'genus': meta[lang_id].genus,
'latitude': meta[lang_id].latitude,
'longitude': meta[lang_id].longitude
} for lang_id in result
}
return result
#
# Legacy slow code; can be still useful for testing
#
# def apply_eq_phoneme(query: EqPhoneme, db_connection: sqlite3.Connection):
# result = set()
# test_segment = normalize('NFD', query.phoneme)
# for language_id in get_all_language_ids(db_connection):
# segment_count = 0
# for (segment,) in db_connection.execute(
# 'SELECT ipa FROM segments WHERE `language_id` = ?',
# (language_id,)
# ):
# if segment == test_segment:
# segment_count += 1
# # We expect to see each segment only once
# # in an inventory.
# break
# diff = segment_count - query.number
# if check_eq(diff, query.op):
# result.add(language_id)
# return result
# def apply_eq_feature(query: EqFeature, db_connection: sqlite3.Connection):
# result = set()
# # A search optimisation: only check the subset once for each segment.
# hit_tmp = {}
# for language_id in get_all_language_ids(db_connection):
# diff = get_count_for_features(
# language_id,
# query.features,
# db_connection,
# hit_tmp) - query.number
# if check_eq(diff, query.op):
# result.add(language_id)
# return result
# def apply_eq_features(query: EqFeatures, db_connection: sqlite3.Connection):
# result = set()
# # Two caches for two sets of features.
# hit_tmp1 = {}
# hit_tmp2 = {}
# for language_id in get_all_language_ids(db_connection):
# lhs_count = get_count_for_features(
# language_id, query.features_1, db_connection, hit_tmp1)
# rhs_count = get_count_for_features(
# language_id, query.features_2, db_connection, hit_tmp2)
# diff = lhs_count - rhs_count
# if check_eq(diff, query.op):
# result.add(language_id)
# return result
if __name__ == "__main__":
import sys
if len(sys.argv) < 2 or len(sys.argv) > 3:
print(USAGE_short)
sys.exit()
elif sys.argv[1] in {'-h', 'help', '--help', '-help'}:
print(USAGE)
sys.exit()
if len(sys.argv) > 1:
if sys.argv[1] == 'list-features':
print(print_features(), end='')
sys.exit()
elif sys.argv[1] == 'list-phyla':
print(print_phyla())
sys.exit()
elif sys.argv[1] == 'genus-tree':
print(print_genus_tree(), end='')
sys.exit()
try:
raw_query = query_parser.parse(sys.argv[1])
except Exception as e:
print(
f'Bad query: {sys.argv[1]}\n\nQuery-parser output: {e}', file=sys.stderr)
sys.exit(1)
query_transformer = QueryTransformer()
print(raw_query.pretty())
query = query_transformer.transform(raw_query)
print(query, '\n')
query_phoible = False
result = apply_query(query, db_connection, query_phoible)
# Filter by phylum or genus when applicable
if len(sys.argv) > 2:
phyla = None
genera = None
restrictor = sys.argv[2]
if not (
restrictor.startswith('phylum=')
or restrictor.startswith('genus=')
):
print(USAGE_short)
sys.exit(1)
if restrictor.startswith('phylum'):
phyla = restrictor.split('=')[1].split(',')
result = list(
filter(lambda lang_id: meta[lang_id].phylum in phyla, result))
else:
genera = restrictor.split('=')[1].split(',')
result = list(
filter(lambda lang_id: meta[lang_id].genus in genera, result))
# Format the output
if query_phoible:
result = list(
map(lambda lang_id: f'{meta_phoible[str(lang_id)]["name"]} ({meta_phoible[str(lang_id)]["glottocode"]})', result))
else:
result = list(
map(lambda lang_id: f'{meta[lang_id].name} ({meta[lang_id].iso})', result))
print(result)