Closed duongkstn closed 3 years ago
You can wrap all the perturbation functions into one. Here is an example of how to do that if you just want to run each of the perturbation functions in the original input (you can remove all of the meta
checking if you are not using that argument):
import checklist
from checklist.perturb import Perturb
import spacy
nlp = spacy.load('en_core_web_sm')
def wrap_multiple_perturbs(perturb_fns):
def rfn(example, *args, **kwargs):
use_meta = kwargs.get('meta', False)
ret = []
meta = []
for fn in perturb_fns:
r = fn(example, *args, **kwargs)
if not r:
continue
meta = []
if use_meta:
r, meta = r
ret.extend(r)
meta.extend(meta)
if use_meta:
ret = (ret, meta)
return ret
return rfn
fn = wrap_multiple_perturbs([Perturb.change_names, Perturb.change_location])
fn(nlp('John is a man who lives in Turkey.'), n=3)
['Dylan is a man who lives in Turkey.', 'Connor is a man who lives in Turkey.', 'Nathan is a man who lives in Turkey.', 'John is a man who lives in Egypt.', 'John is a man who lives in Mozambique.', 'John is a man who lives in Japan.']
Note that this is running each function independently. It's a little more complicated if you want to run them in sequence, because the order matters and the output of these functions are strings while the inputs are spacy.doc
. Anyway, here is a version you can tweak to your needs (removed meta just for conciseness):
def wrap_perturbs_in_sequence(perturb_fns):
def rfn(example, *args, **kwargs):
ret = []
examples_to_run = [example]
for fn in perturb_fns:
new = []
for example in examples_to_run:
r = fn(example, *args, **kwargs)
if not r:
continue
new.extend([nlp(x) for x in r])
examples_to_run.extend(new)
return [x.text for x in examples_to_run if x.text != example]
return rfn
fn = wrap_perturbs_in_sequence([Perturb.change_names, Perturb.change_location])
fn(nlp('John is a man who lives in Turkey.'), n=3)
[John is a man who lives in Turkey., Austin is a man who lives in Turkey., Patrick is a man who lives in Turkey., John is a man who lives in Angola., John is a man who lives in Nigeria., John is a man who lives in Germany., Austin is a man who lives in Mozambique., Austin is a man who lives in Vietnam., Austin is a man who lives in Colombia., Patrick is a man who lives in Ghana., Patrick is a man who lives in Bangladesh., Patrick is a man who lives in United Kingdom., Robert is a man who lives in Vietnam., Robert is a man who lives in India., Robert is a man who lives in Ghana.]
Thanks a lot for great implementations !
Hi, As in your tutorials,
Perturb.perturb
usually inputs 1 function (example:Perturb.change_names
). But in my scenario, I want 2 functions as parameters ofPerturb.perturb
, then it will return sentences which are mixture of func1 and func2 . Ex: func1:change_names
func2:change_city
I hope it returned a sentence which name is changed by func1 or/and city is changed by func2 (something like:Perturb.perturb(data, Perturb.change_names, Perturb.change_city )
). Otherwise, I still can usePerturb.perturb(data, Perturb.change_names)
andPerturb.perturb(data, Perturb.change_city)
independently. How can I do that ?