Closed linonetwo closed 4 years ago
I know, I can passing name=:
@labeling_function(name=rule)
def check(x):
return SPAM if ruleMatch(rule, x.text.lower()) else ABSTAIN
labelingFunctions.append(check)
If I do so, there will not be an error, but all things will be checked by the last labeling function, the previous one will be overwritten by the last function, result in all [True, True]
or [False, False]
, identical result.
Oh, it is due to closure, this can be solved by using IIFE:
def getLabelingFunctions(rules: List[str]):
labelingFunctions = []
for rule in rules:
# use iife to prevent classical closure stale value problem
def IIFE(rule: str):
@labeling_function(name=rule)
def checkRuleCoversText(row: Series):
# make sure pandas row contains value, it sometimes just being None
if len(row.values) == 0:
return False
text = row.values[0]
if text == None:
return False
# now use the rule to check text
return True if dfaFromRule(rule).execute(text) else False
labelingFunctions.append(checkRuleCoversText)
IIFE(rule)
return labelingFunctions
Issue description
I'm trying to load rule from config file, and generate labeling function on the fly, but I get:
ValueError: Operator names not unique: 2 operators with name check
Code example/repro steps
ruleMatch
is my function that parses rule and generates a parser.Expected behavior
We can use dynamic generated labeling functions
System info