added reqire_full_match to find_and_replace
This commit is contained in:
parent
a4758e42f3
commit
2909e6a958
@ -19,6 +19,7 @@ EXPECTED_OUTPUT_TABLE_SCHEMA.remove("Geschlecht")
|
|||||||
COL_INSTRUCTIONS = {
|
COL_INSTRUCTIONS = {
|
||||||
("Art",): {
|
("Art",): {
|
||||||
"command": "FIND_AND_REPLACE",
|
"command": "FIND_AND_REPLACE",
|
||||||
|
"reqire_full_match": True,
|
||||||
"map": SPICIES
|
"map": SPICIES
|
||||||
},
|
},
|
||||||
("Ort",): {
|
("Ort",): {
|
||||||
|
|||||||
@ -23,11 +23,13 @@ COL_INSTRUCTIONS = {
|
|||||||
("Geschlecht",): [
|
("Geschlecht",): [
|
||||||
{
|
{
|
||||||
"command": "FIND_AND_REPLACE",
|
"command": "FIND_AND_REPLACE",
|
||||||
"map": REPRODUCTION
|
"map": REPRODUCTION,
|
||||||
|
"reqire_full_match": True
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"command": "FIND_AND_REPLACE",
|
"command": "FIND_AND_REPLACE",
|
||||||
"map": AGE
|
"map": AGE,
|
||||||
|
"reqire_full_match": True
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"command": "REGEX_MOVE_TO_COL",
|
"command": "REGEX_MOVE_TO_COL",
|
||||||
|
|||||||
@ -2,13 +2,18 @@ import re
|
|||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
|
|
||||||
class FindAndReplace:
|
class FindAndReplace:
|
||||||
def __init__(self, map: dict[str, list[str]]):
|
def __init__(self, map: dict[str, list[str]], reqire_full_match: bool = False):
|
||||||
self.map = map
|
self.map = map
|
||||||
|
self.require_full_match = reqire_full_match
|
||||||
|
|
||||||
def execute_on_cell(self, entry) -> str:
|
def execute_on_cell(self, entry) -> str:
|
||||||
res = deepcopy(entry.value)
|
res = deepcopy(entry.value)
|
||||||
|
|
||||||
for species, alternatives in self.map.items():
|
for species, alternatives in self.map.items():
|
||||||
for old_version in alternatives:
|
for old_version in alternatives:
|
||||||
res = res.replace(old_version, species)
|
if self.require_full_match and old_version == res:
|
||||||
|
# Todo remove random leading/trailing spaces before if
|
||||||
|
res = species
|
||||||
|
else:
|
||||||
|
res = res.replace(old_version, species)
|
||||||
return res
|
return res
|
||||||
@ -109,7 +109,7 @@ class TableSerializer:
|
|||||||
res_row = {}
|
res_row = {}
|
||||||
match instruct.get("command"):
|
match instruct.get("command"):
|
||||||
case "FIND_AND_REPLACE":
|
case "FIND_AND_REPLACE":
|
||||||
res = FindAndReplace(instruct["map"]).execute_on_cell(row[source_cell])
|
res = FindAndReplace(instruct["map"], instruct.get("reqire_full_match", False)).execute_on_cell(row[source_cell])
|
||||||
case "REGEX_MOVE_TO_COL":
|
case "REGEX_MOVE_TO_COL":
|
||||||
res_row = RegexMoveToCol(instruct["match"], instruct["to"], instruct.get("with_input_iteration")) \
|
res_row = RegexMoveToCol(instruct["match"], instruct["to"], instruct.get("with_input_iteration")) \
|
||||||
.execute_on_row(row, source_cell)
|
.execute_on_row(row, source_cell)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user