-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
77 lines (48 loc) · 1.88 KB
/
utilities.py
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
import re
from re import Match
from more_itertools import chunked
def pipeline(item, *steps):
for step in steps:
item = step(item)
return item
def splitAndStrip(line: str, delimiter):
# if x means x is not falsy
return [x.strip() for x in line.split(delimiter) if x]
def savePairs(words: list[str]) -> list[str]:
# can't delete based on index because index would change after deletion
if len(words) < 2:
return words
pair = ("(", ")")
# () for later
unmatched = []
for i, word in enumerate(words):
begin, end = pair
if word.count(begin) != word.count(end):
unmatched.append(i)
if not unmatched:
return words
for begin, end in chunked(unmatched, 2):
for i in range(begin + 1, end + 1):
words[begin] += ", " + words[i]
words[i] = ""
return [x for x in words if x]
def wrappedList(x: list):
if not x:
return ""
return "\n".join([item.__str__() for item in x])
def startingIndices(source: str, toIndex: str) -> list[int]:
return [match.start() for match in re.finditer(toIndex, source)]
def isMarkedWithinPairedSymbol(text: str, result: Match, symbol: tuple[str, str]) -> bool:
indices = sorted(startingIndices(text, symbol[0]) + startingIndices(text, symbol[1]))
for start, end in chunked(indices, 2):
if start < result.start() and result.end() < end:
return True
return False
def isMarkedWithinParenthesis(text: str, result: Match) -> bool:
return isMarkedWithinPairedSymbol(text, result, (r'\(', r'\)'))
def isMarkedWithinApostrophe(text: str, result: Match) -> bool:
return isMarkedWithinPairedSymbol(text, result, ('“', '”'))
def removeOutmostParenthesis(text: str):
if text.startswith("(") and text.endswith(")"):
return text.removeprefix("(").removesuffix(")")
return text