Added string replacement function for future use
This commit is contained in:
@ -1,9 +1,13 @@
|
||||
""" Common methods used in parsing """
|
||||
import re
|
||||
|
||||
|
||||
def flatten(arr: list) -> list:
|
||||
"""Flattens array"""
|
||||
return (
|
||||
flatten(arr[0]) + (flatten(arr[1:]) if len(arr) > 1 else [])
|
||||
if isinstance(arr, list) else [arr]
|
||||
if isinstance(arr, list)
|
||||
else [arr]
|
||||
)
|
||||
|
||||
|
||||
@ -17,4 +21,35 @@ def sum_dict(arr: list[dict]) -> dict:
|
||||
else:
|
||||
result[key] = element[key]
|
||||
return result
|
||||
|
||||
|
||||
|
||||
def string_rewrite(axiom: str, rules: dict):
|
||||
"""String rewrite / Lindemeyer system for rule based text manipulation
|
||||
|
||||
Args:
|
||||
axiom (str): Input string
|
||||
rules (dict): String manipulation rules in dict:
|
||||
|
||||
Example:
|
||||
rules = {
|
||||
"1": "2",
|
||||
"[2-9]": "45",
|
||||
"4": lambda: str(randint(1, 7)),
|
||||
"([1-9])(5)": lambda a, b: str(int(a)*int(b))
|
||||
}
|
||||
|
||||
for i range(10):
|
||||
print(string_rewrite("1", rules))
|
||||
"""
|
||||
|
||||
def _apply_rules(match):
|
||||
for key, value in rules.items():
|
||||
if re.match(key, match.group(0)):
|
||||
if callable(value):
|
||||
yield value(
|
||||
*match.groups()
|
||||
) if value.__code__.co_argcount > 0 else value()
|
||||
yield value
|
||||
|
||||
pattern = re.compile("|".join(rules.keys()))
|
||||
return pattern.sub(lambda m: next(_apply_rules(m)), axiom)
|
||||
|
||||
Reference in New Issue
Block a user