-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasicparser.py
More file actions
161 lines (126 loc) · 2.78 KB
/
basicparser.py
File metadata and controls
161 lines (126 loc) · 2.78 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
# minimal parsing
def get_matched_lines(content, expr):
lines = []
content = str_to_list(content)
for line in content:
if expr in line:
lines.append(line)
return lines
def check_type(content):
if type(content) == type(""):
return 'str'
return None
def str_to_list(content, separator='\\n'):
if check_type(content) == 'str':
if separator in content:
content = content.split(separator)
return content
return None
def minimize_white_space(line):
characters = []
keep = True
for c in line:
if not keep and ord(c) != 32:
keep = True
if keep:
characters.append(c)
if ord(c) == 32:
keep = False
return ''.join(characters)
def split_by_free_space(line):
line = minimize_white_space(line)
line = line.split(' ')
return line
def get_first_word(text):
return text.split(' ')[0]
def get_nth_word(text, n):
'''
Returns a word from a one-line sentence with n starting at 1 (not 0).
Example:
"This is a sentence."
n = 1: "This"
n = 2: "is"
...
n > 4: "<NULL_INDEX>"
Reverse Index:
n = -1: "sentence."
n = -2: "a"
...
n < -4: "<NULL_INDEX>"
Zero is not a number:
n = 0: "<NULL_INDEX>"
Args:
text (str): a one-line sentence is expected
n (int): index of word, starting at 1 (not 0)
Returns:
str: the word at index n with no spaces
'''
wordList = split_by_free_space(text)
length = len(wordList)
if n == 0 or n > length or n < length*(-1):
return '<NULL_INDEX>'
else:
if n > 0:
n = n - 1
# otherwise, n < 0 and it need not be modified
return split_by_free_space(text)[n]
def isLetter(c):
c = ord(c)
if c >= 65 and c <= 90:
return True
if c >= 90 and c <= 122:
return True
return False
def isDigit(c):
c = ord(c)
if c >= 48 and c <= 57:
return True
def isAcceptedCharacter(c, accepted=[]):
if len(accepted) > 0:
c = ord(c)
for character in accepted:
if c == ord(character):
return True
return False
def validate_input(input):
if not type(input) == type(''):
return None
# first char cannot be a number
if input and not isLetter(input[0]):
return None
for c in input[1:]:
c = ord(c)
# space, 0
if c != 45 and c < 48:
return None
# 9, A
if c > 57 and c < 65:
return None
# Z, a, -
if c > 90 and c < 97 and c != 95:
return None
# z
if c > 122:
return None
return input
def remove_list_item_by_element(item, items):
try:
del items[items.index(item)]
except:
pass
def crop_str(text, a, b):
u = len(a)
v = len(b)*(-1)
if text[:u] == a and text[v:] == b:
text = text[u:]
text = text[:v]
return text
def selective_search(line, column=None, splitAt=None, index=None):
final = None
if column:
final = get_nth_word(line, column)
if splitAt:
final = final.split(splitAt)
if index:
final = final[index]
return final