-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
30 lines (23 loc) · 1.03 KB
/
Copy pathparser.py
File metadata and controls
30 lines (23 loc) · 1.03 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
import re
# This class will provide useful utilities for parsing inputting strings, such as removing the need to write np.cos.
class TextParse:
def __init__(self, text):
self.text = text
self.keywords = ["cos", "sin", "exp", "log", "tan"]
# Some explanation of the functionning of these regex patterns
#
# r is necessary because if not, the string wont be in raw mode and \b will be interperted
# as a backspace and not a "word boundary" for regex
#
# \b means word boundary, example \bcat\b matches "here is a cat" but not "catalog"
#
# \d matches any digit
#
# | is concatenation in regex
#
# \1, \2, ect, refers to the content matched in the first capturing group
# the part of the pattern in the first set of parentheses. It is useful to saveguard the context.
def parse(self):
self.text = re.sub(r'\b(' + '|'.join(map(re.escape, self.keywords)) + r')\b', r'np.\1', self.text)
self.text = re.sub(r'(\d)x', r'\1*x', self.text)
return self.text