-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern_transformer.py
More file actions
54 lines (39 loc) · 1.74 KB
/
pattern_transformer.py
File metadata and controls
54 lines (39 loc) · 1.74 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
from pydantic import BaseModel, Field
from typing import Pattern
from novel_tools.framework import NovelData, Processor
class Substitution(BaseModel):
pattern: Pattern
new: str
class Unit(BaseModel):
filter: dict[str, str]
subs: list[Substitution]
def replace(self, data: NovelData):
data_dict = data.flat_dict()
if not all(data_dict.get(key) != val for key, val in self.filter.items()):
return data
content = data.content
for sub in self.subs:
pattern = sub.pattern
new = sub.new
while match := pattern.search(content):
begin, end = match.span()
content = content[:begin] + new.format(*match.groups()) + content[end:]
data.content = content
return data
class Options(BaseModel):
units: list[Unit] = Field(description='The list of processing units. `filter` is a dictionary with the fields as '
'the key, and `subs` lists the operations to be performed if the data is '
'not filtered. `pattern` is a regex describing the pattern, and `new` is '
'the string to replace the pattern.')
class PatternTransformer(Processor):
"""
Alters the content of the data by changing some patterns (usually in chapter contents). You can use this to format
punctuation symbols in the novel, or change from custom dividers to Markdown-style ones.
"""
def __init__(self, args):
options = Options(**args)
self.units = options.units
def process(self, data: NovelData) -> NovelData:
for unit in self.units:
data = unit.replace(data)
return data