-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_automaton.py
More file actions
330 lines (250 loc) · 12.3 KB
/
test_automaton.py
File metadata and controls
330 lines (250 loc) · 12.3 KB
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
"""
Government Service Query Classifier - Test Cases
=================================================
Comprehensive test suite demonstrating DFA functionality
with test cases in English, Sinhala, and Tamil.
"""
from automaton import GovernmentServiceDFA
def print_header(title):
"""Print a formatted header."""
print("\n" + "=" * 70)
print(f" {title}")
print("=" * 70)
def print_result(query, result, lang):
"""Print formatted test result."""
status_icon = "✅" if result['is_accepted'] else "❌"
print(f"\n{status_icon} Query ({lang}): \"{query}\"")
print(f" State: {result['state']} ({result['state_name']})")
print(f" Status: {result['status']}")
print(f" Transition: {result['transition']}")
if result['is_accepted'] and 'service' in result:
print(f" Service: {result['service']['name']}")
print("-" * 50)
def test_english_queries(dfa):
"""Test English language queries."""
print_header("ENGLISH LANGUAGE TEST CASES")
test_cases = [
# NIC Services
("How to apply for NIC?", "en"),
("I need my national identity card", "en"),
# Passport
("Passport renewal process", "en"),
("How to get a travel document?", "en"),
# Birth Certificate
("Birth certificate application", "en"),
("Birth registration for newborn", "en"),
# Death Certificate
("Death certificate request", "en"),
# Marriage Certificate
("Marriage registration process", "en"),
# Driving License
("Driving license renewal", "en"),
("How to get a driver license?", "en"),
# Vehicle Registration
("Vehicle registration transfer", "en"),
("Car registration process", "en"),
# Tax Services
("Income tax filing", "en"),
("Tax payment methods", "en"),
# Pension
("EPF withdrawal process", "en"),
("Pension application", "en"),
# Samurdhi/Welfare
("Samurdhi benefits eligibility", "en"),
("Aswesuma registration", "en"),
# Education
("School admission process", "en"),
("University application", "en"),
("Scholarship application", "en"),
# Health Services
("Hospital services inquiry", "en"),
("Medical card registration", "en"),
# REJECT cases
("What is the weather today?", "en"),
("Random unrelated query", "en"),
]
accept_count = 0
reject_count = 0
for query, lang in test_cases:
result = dfa.process_query(query, lang)
print_result(query, result, lang)
if result['is_accepted']:
accept_count += 1
else:
reject_count += 1
print(f"\n📊 English Results: {accept_count} ACCEPT, {reject_count} REJECT")
return accept_count, reject_count
def test_sinhala_queries(dfa):
"""Test Sinhala language queries."""
print_header("SINHALA LANGUAGE TEST CASES (සිංහල)")
test_cases = [
# NIC - හැඳුනුම්පත
("ජාතික හැඳුනුම්පත අයදුම් කරන්නේ කෙසේද?", "si"),
("අනන්යතා පත්රය ගන්න ඕනේ", "si"),
# Passport - ගමන් බලපත්රය
("ගමන් බලපත්රය අලුත් කරන්න", "si"),
("පාස්පෝට් එක ගන්න කොහොමද?", "si"),
# Birth Certificate - උප්පැන්න සහතිකය
("උප්පැන්න සහතිකය ගන්න ඕනේ", "si"),
("උපත් සහතිකය අයදුම්පත", "si"),
# Death Certificate - මරණ සහතිකය
("මරණ සහතිකය ලබා ගන්නේ කෙසේද?", "si"),
# Marriage Certificate - විවාහ සහතිකය
("විවාහ සහතිකය ලියාපදිංචිය", "si"),
# Driving License - රියදුරු බලපත්රය
("රියදුරු බලපත්රය අලුත් කරන්න", "si"),
("ලයිසන් එක ගන්න ඕනේ", "si"),
# Vehicle - වාහන
("වාහන ලියාපදිංචිය මාරු කරන්න", "si"),
("රථවාහන බලපත්රය", "si"),
# Tax - බදු
("ආදායම් බදු ගෙවීම", "si"),
("බදු සේවා පිළිබඳ තොරතුරු", "si"),
# Pension - විශ්රාම වැටුප්
("විශ්රාම වැටුප් අයදුම්පත", "si"),
# Samurdhi - සමෘද්ධි
("සමෘද්ධි ප්රතිලාභ", "si"),
("අස්වැසුම ලියාපදිංචිය", "si"),
# Education - අධ්යාපනය
("පාසල් ඇතුළත් කිරීම", "si"),
("ශිෂ්යත්ව අයදුම්පත", "si"),
# Health - සෞඛ්ය
("රෝහල සේවා", "si"),
("වෛද්ය කාඩ්පත ලබාගැනීම", "si"),
# REJECT case
("අද කාලගුණය කොහොමද?", "si"),
]
accept_count = 0
reject_count = 0
for query, lang in test_cases:
result = dfa.process_query(query, lang)
print_result(query, result, lang)
if result['is_accepted']:
accept_count += 1
else:
reject_count += 1
print(f"\n📊 Sinhala Results: {accept_count} ACCEPT, {reject_count} REJECT")
return accept_count, reject_count
def test_tamil_queries(dfa):
"""Test Tamil language queries."""
print_header("TAMIL LANGUAGE TEST CASES (தமிழ்)")
test_cases = [
# NIC - அடையாள அட்டை
("தேசிய அடையாள அட்டை விண்ணப்பம்", "ta"),
("அடையாளம் பெற வேண்டும்", "ta"),
# Passport - கடவுச்சீட்டு
("கடவுச்சீட்டு புதுப்பித்தல்", "ta"),
("பாஸ்போர்ட் எடுக்க வேண்டும்", "ta"),
# Birth Certificate - பிறப்புச் சான்றிதழ்
("பிறப்புச் சான்றிதழ் வேண்டும்", "ta"),
("பிறப்பு பதிவு செய்ய", "ta"),
# Death Certificate - இறப்புச் சான்றிதழ்
("இறப்புச் சான்றிதழ் பெற", "ta"),
# Marriage Certificate - திருமணச் சான்றிதழ்
("திருமணம் பதிவு செய்ய", "ta"),
("திருமண சான்றிதழ்", "ta"),
# Driving License - ஓட்டுநர் உரிமம்
("ஓட்டுநர் உரிமம் புதுப்பிக்க", "ta"),
("உரிமம் எடுக்க வேண்டும்", "ta"),
# Vehicle - வாகனம்
("வாகன பதிவு மாற்றம்", "ta"),
("வாகனம் பதிவு செய்ய", "ta"),
# Tax - வரி
("வருமான வரி செலுத்த", "ta"),
("வரி சேவைகள்", "ta"),
# Pension - ஓய்வூதியம்
("ஓய்வூதியம் விண்ணப்பம்", "ta"),
("ஓய்வு பெற", "ta"),
# Samurdhi - சமுர்தி
("சமுர்தி உதவி", "ta"),
("நலன்புரி சேவை", "ta"),
# Education - கல்வி
("பள்ளி சேர்க்கை", "ta"),
("பல்கலைக்கழகம் விண்ணப்பம்", "ta"),
# Health - சுகாதாரம்
("மருத்துவமனை சேவைகள்", "ta"),
("மருத்துவர் சந்திப்பு", "ta"),
# REJECT case
("இன்று வானிலை எப்படி?", "ta"),
]
accept_count = 0
reject_count = 0
for query, lang in test_cases:
result = dfa.process_query(query, lang)
print_result(query, result, lang)
if result['is_accepted']:
accept_count += 1
else:
reject_count += 1
print(f"\n📊 Tamil Results: {accept_count} ACCEPT, {reject_count} REJECT")
return accept_count, reject_count
def test_edge_cases(dfa):
"""Test edge cases and boundary conditions."""
print_header("EDGE CASES AND BOUNDARY CONDITIONS")
test_cases = [
# Mixed language
("NIC එක ගන්න ඕනේ", "en"), # Mixed Sinhala-English
# Case insensitivity
("NIC", "en"),
("nic", "en"),
("Nic", "en"),
# Partial matches
("I need NIC information", "en"),
("What about passport fees?", "en"),
# Empty or whitespace
(" ", "en"),
# Multiple keywords (should match first)
("NIC and passport", "en"),
]
for query, lang in test_cases:
if query.strip():
result = dfa.process_query(query, lang)
print_result(query, result, lang)
def run_all_tests():
"""Run all test suites."""
dfa = GovernmentServiceDFA()
# Print DFA diagram
print(dfa.get_state_diagram())
# Print formal definition
print_header("FORMAL DFA DEFINITION")
formal = dfa.get_formal_definition()
for key, value in formal.items():
print(f" {key}: {value}")
# Run test suites
en_accept, en_reject = test_english_queries(dfa)
si_accept, si_reject = test_sinhala_queries(dfa)
ta_accept, ta_reject = test_tamil_queries(dfa)
# Edge cases
test_edge_cases(dfa)
# Final summary
print_header("FINAL TEST SUMMARY")
total_accept = en_accept + si_accept + ta_accept
total_reject = en_reject + si_reject + ta_reject
total = total_accept + total_reject
print(f"""
╔══════════════════════════════════════════════════════╗
║ TEST RESULTS SUMMARY ║
╠══════════════════════════════════════════════════════╣
║ Language │ ACCEPT │ REJECT │ Total ║
╠══════════════════════════════════════════════════════╣
║ English │ {en_accept:2d} │ {en_reject:2d} │ {en_accept + en_reject:2d} ║
║ Sinhala │ {si_accept:2d} │ {si_reject:2d} │ {si_accept + si_reject:2d} ║
║ Tamil │ {ta_accept:2d} │ {ta_reject:2d} │ {ta_accept + ta_reject:2d} ║
╠══════════════════════════════════════════════════════╣
║ TOTAL │ {total_accept:2d} │ {total_reject:2d} │ {total:2d} ║
╚══════════════════════════════════════════════════════╝
✅ DFA correctly classifies government service queries
✅ Trilingual support (English, Sinhala, Tamil) working
✅ ACCEPT/REJECT outcomes properly generated
✅ State transitions correctly logged
""")
if __name__ == "__main__":
# Set console encoding for Windows
import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
print("\n" + "=" * 60)
print("\n GOVERNMENT SERVICE QUERY CLASSIFIER - DFA TEST SUITE")
print(" Sri Lanka - Automata Theory Assignment\n")
print("=" * 60)
run_all_tests()