-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_data.py
176 lines (145 loc) · 5.21 KB
/
test_data.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
import os
import re
import pytest
PROJECT_DIR = os.path.dirname(__file__)
MW_DIR = os.path.join(PROJECT_DIR, 'monier-williams')
SHS_DIR = os.path.join(PROJECT_DIR, 'sanskrit-heritage-site')
LSO_DIR = os.path.join(PROJECT_DIR, 'learnsanskrit.org')
def iter_csv_paths(dirs):
for d in dirs:
for filename in os.listdir(d):
if filename.endswith('.csv'):
yield os.path.join(d, filename)
def test_consistent_column_names():
"""Checks that all CSV column names are unique and well-formed."""
for path in iter_csv_paths([MW_DIR, SHS_DIR, LSO_DIR]):
with open(path, 'r') as f:
first_line = f.readline()
column_names = first_line.strip().split(',')
for c in column_names:
# 'L' is for greeklist.csv
assert re.match('^[a-z_L]+$', c), (filename, c)
assert len(set(column_names)) == len(column_names), column_names
@pytest.mark.parametrize('path', list(iter_csv_paths([MW_DIR])))
def test_consistent_mw_data(path):
"""Checks that the Monier-Williams data is well-typed."""
STEM_RE = r'([a-zA-Z\|]+|_)'
types = {
# TODO: '?', 'S1', '%26'
'betacode': r'([A-Z ()=/\-*+\'%0-9]+|\?|S1)',
'index': '[0-9]',
'L': '[0-9\.]+',
# Nominals
'stem': STEM_RE,
'stem_genders': '([mfn]+|none)',
# Prefixes and indeclinables
'name': STEM_RE,
'prefix_type': '(upasarga|cvi|DAc|other)',
# Roots
'class': r'(1|2|3|4|5|6|7|8|9|10|denom)',
'hom': r'[0-9]?',
'prefixed_root': STEM_RE,
'root': STEM_RE,
'unprefixed_root': STEM_RE,
'voice': '(para|atma)',
}
with open(path, 'r') as f:
first = True
regex = None
for line in f:
if first:
column_names = line.strip().split(',')
for c in column_names:
assert c in types, "Untyped column %s" % c
regex_body = ','.join(types[c] for c in column_names)
regex = '^' + regex_body + '$'
first = False
continue
line = line.strip()
assert re.match(regex, line), (regex, line)
@pytest.mark.parametrize('path', list(iter_csv_paths([SHS_DIR])))
def test_consistent_shs_data(path):
"""Checks that the Sanskrit Heritage Site data is well-typed."""
STEM_RE = r'([a-zA-Z\|]+|_)'
ROOT_RE = '[a-zA-Z]+(#[1-9])?'
types = {
'betacode': '[A-Z()/]+',
'index': '[0-9]',
'L': '[0-9]+',
'case': r'[12345678]',
'class': r'(1|2|3|4|5|6|7|8|9|10|denom|)',
'form': STEM_RE,
'form_gender': '[mfn]',
'pos': '(gerund|infinitive)',
'root': ROOT_RE,
'name': ROOT_RE,
'shs': ROOT_RE,
'mw': r'[a-zA-Z]+',
'stem': STEM_RE,
'stem_genders': '([mfn]+|none)',
'person': '[123]',
'number': r'[sdp]',
'mode': r'(pres|impv|past|ipft|opt|ben|inj|perf|fut|sfut|pfut|cond|aor)',
'modification': r'(|caus|denom|desid|intens)',
'voice': '(para|atma|pass|active)',
}
with open(path, 'r') as f:
first = True
regex = None
for line in f:
if first:
column_names = line.strip().split(',')
for c in column_names:
assert c in types, "Untyped column %s" % c
regex_body = ','.join(types[c] for c in column_names)
regex = '^' + regex_body + '$'
first = False
continue
line = line.strip()
assert re.match(regex, line), (regex, line)
@pytest.mark.parametrize('path', list(iter_csv_paths([LSO_DIR])))
def test_consistent_lso_data(path):
"""Checks that the learnsanskrit.org data is well-typed."""
STEM_RE = r'([a-zA-Z]+|_)'
types = {
# Upasargas
'name': STEM_RE,
# Enums
'enum_type': '[a-z_]+',
'human_readable_value': '.*',
'abbreviation': r'.*',
# Nominals and nominal endings
'ending': r'([a-zA-Z]+|)',
'case': r'[12345678]',
'form': STEM_RE,
'number': r'[sdp]',
'stem': STEM_RE,
'stem_type': STEM_RE,
'stem_genders': '([mfn]+|none)',
'form_gender': '([mfn]|none)',
# Verb endings
'category': '(both|simple|complex)',
'person': '[123]',
'number': '[sdp]',
'mode': r'(pres|impv|past|ipft|opt|ben|inj|perf|fut|sfut|pfut|cond|aor)',
'voice': '(para|atma)',
# Sandhi
'first': '[a-zA-Z]+',
'second': '[a-zA-Z]*',
'result': '[a-zA-Z ~\']+',
'type': '(common|internal|external)'
}
with open(path, 'r') as f:
first = True
regex = None
for line in f:
if first:
column_names = line.strip().split(',')
for c in column_names:
assert c in types, "Untyped column %s" % c
regex_body = ','.join(types[c] for c in column_names)
regex = '^' + regex_body + '$'
first = False
continue
line = line.strip()
assert re.match(regex, line), (regex, line)