-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanswer_parser.py
More file actions
85 lines (73 loc) · 2.21 KB
/
answer_parser.py
File metadata and controls
85 lines (73 loc) · 2.21 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
import numpy as np
class Parser:
def __init__(self, answer):
self.answer = answer+" "
self.progress = 0
self.separators = " ,:;?!\n\r"
def read_word(self):
word = ""
end = False
while not end:
if self.progress >= len(self.answer):
return None
c = self.answer[self.progress]
if c not in self.separators:
word += c
elif word:
end = True
self.progress += 1
return word
def parse_pct(self, word):
if word is None:
return None
res = ""
pct = False
for c in word:
if c == '%':
pct = True
break
elif c.isdigit():
res += c
if not pct:
return ""
return res
def crop_info(self):
grammar = ["width", "pct", "pct", "height", "pct", "pct"]
nums = self.grammar_nums(grammar)
rgs = []
for i in range(0, len(nums), 4):
rg = [nums[j] for j in range(i, i+4)]
rgs.append(rg)
return rgs
def grammar_nums(self, grammar):
i = 0
res = []
while True:
word = self.read_word()
if word is None:
break
proceed = False
if grammar[i] == "num":
num = True
for c in word:
if not c.isdigit() and c != '.':
num = False
break
if num:
res.append(float(word))
proceed = True
elif grammar[i] == "pct":
pct = self.parse_pct(word)
if pct:
res.append(float(pct)/100)
proceed = True
elif word.lower() == grammar[i].lower():
proceed = True
if proceed:
if i == len(grammar)-1:
i = 0
else:
i += 1
return res
# pa = Parser("1. Wyoming: Number 3. South Dakota: Number 3")
# print(pa.grammar_nums(["Number", "num"]))