-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_reader.py
More file actions
41 lines (38 loc) · 1.66 KB
/
my_reader.py
File metadata and controls
41 lines (38 loc) · 1.66 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
import os
class Reader:
def __init__(self, grammar_file, sentences_file):
self.grammar_path = os.path.join(os.path.dirname(__file__), grammar_file)
self.sentences_path = os.path.join(os.path.dirname(__file__), sentences_file)
def read_bnf_grammar(self):
grammar = {}
last_lhs = None
try:
with open(self.grammar_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if not line: continue
if "::=" in line:
lhs, rhs = line.split("::=")
lhs = lhs.strip()
last_lhs = lhs
alternatives = [alt.strip().split() for alt in rhs.split("|") if alt.strip()]
alternatives.sort(key=len, reverse=True)
grammar[lhs] = alternatives
elif last_lhs:
extra_alts = [alt.strip().split() for alt in line.split("|") if alt.strip()]
grammar[last_lhs].extend(extra_alts)
return grammar
except FileNotFoundError: return {}
def read_sentences(self):
sentences = []
try:
with open(self.sentences_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if not line: continue
if " " not in line and line != "ε":
sentences.append(list(line))
else:
sentences.append(line.split())
return sentences
except FileNotFoundError: return []