def _init_anserini(): global anserini_monkey if anserini_monkey: return # jnius monkypatching import jnius_config anserini_found = False for j in jnius_config.get_classpath(): if "anserini" in j: anserini_found = True break assert anserini_found, 'Anserini jar not found: you should start PyTerrier, e.g. with '\ + 'pt.init(boot_packages=["io.anserini:anserini:0.9.2:fatjar"])' jnius_config.set_classpath = lambda x: x anserini_monkey = True #this is the Anserini early rank cutoff rule from matchpy import Wildcard, ReplacementRule, Pattern from .transformer import RankCutoffTransformer, rewrite_rules x = Wildcard.dot('x') _brAnserini = Wildcard.symbol('_brAnserini', AnseriniBatchRetrieve) def set_k(_brAnserini, x): _brAnserini.k = int(x.value) return _brAnserini rewrite_rules.append(ReplacementRule( Pattern(RankCutoffTransformer(_brAnserini, x) ), set_k ))
def setup_rewrites(): from .batchretrieve import BatchRetrieve, FeaturesBatchRetrieve #three arbitrary "things". x = Wildcard.dot('x') xs = Wildcard.plus('xs') y = Wildcard.dot('y') z = Wildcard.dot('z') # two different match retrives _br1 = Wildcard.symbol('_br1', BatchRetrieve) _br2 = Wildcard.symbol('_br2', BatchRetrieve) _fbr = Wildcard.symbol('_fbr', FeaturesBatchRetrieve) # batch retrieves for the same index BR_index_matches = CustomConstraint(lambda _br1, _br2: _br1.indexref == _br2.indexref) BR_FBR_index_matches = CustomConstraint(lambda _br1, _fbr: _br1.indexref == _fbr.indexref) # rewrite nested binary feature unions into one single polyadic feature union rewrite_rules.append(ReplacementRule( Pattern(FeatureUnionPipeline(x, FeatureUnionPipeline(y,z)) ), lambda x, y, z: FeatureUnionPipeline(x,y,z) )) rewrite_rules.append(ReplacementRule( Pattern(FeatureUnionPipeline(FeatureUnionPipeline(x,y), z) ), lambda x, y, z: FeatureUnionPipeline(x,y,z) )) rewrite_rules.append(ReplacementRule( Pattern(FeatureUnionPipeline(FeatureUnionPipeline(x,y), xs) ), lambda x, y, xs: FeatureUnionPipeline(*[x,y]+list(xs)) )) # rewrite nested binary compose into one single polyadic compose rewrite_rules.append(ReplacementRule( Pattern(ComposedPipeline(x, ComposedPipeline(y,z)) ), lambda x, y, z: ComposedPipeline(x,y,z) )) rewrite_rules.append(ReplacementRule( Pattern(ComposedPipeline(ComposedPipeline(x,y), z) ), lambda x, y, z: ComposedPipeline(x,y,z) )) rewrite_rules.append(ReplacementRule( Pattern(ComposedPipeline(ComposedPipeline(x,y), xs) ), lambda x, y, xs: ComposedPipeline(*[x,y]+list(xs)) )) # rewrite batch a feature union of BRs into an FBR rewrite_rules.append(ReplacementRule( Pattern(FeatureUnionPipeline(_br1, _br2), BR_index_matches), lambda _br1, _br2: FeaturesBatchRetrieve(_br1.indexref, ["WMODEL:" + _br1.controls["wmodel"], "WMODEL:" + _br2.controls["wmodel"]]) )) def push_fbr_earlier(_br1, _fbr): #TODO copy more attributes _fbr.controls["wmodel"] = _br1.controls["wmodel"] return _fbr # rewrite a BR followed by a FBR into a FBR rewrite_rules.append(ReplacementRule( Pattern(ComposedPipeline(_br1, _fbr), BR_FBR_index_matches), push_fbr_earlier )) global rewrites_setup rewrites_setup = True
CustomConstraint, Pattern, ReplacementRule, Wildcard, replace_all, ) logger = logging.getLogger(__name__) # a_true = Wildcard.dot('a_true', BoolTrue) # a_false = Wildcard.dot('a_false', BoolFalse) a_num = Wildcard.symbol('a_num', Value) b_num = Wildcard.symbol('b_num', Value) a = Wildcard.dot('a') b = Wildcard.dot('b') c = Wildcard.dot('c') d = Wildcard.dot('d') e = Wildcard.dot('e') f = Wildcard.dot('f') star = Wildcard.star('star') star_a = Wildcard.star('star_a') star_b = Wildcard.star('star_b') plus = Wildcard.plus('plus') plus_a = Wildcard.plus('plus_a') θ = Theta