Exemplo n.º 1
0
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
    ))
Exemplo n.º 2
0
from matchpy import (
    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
Exemplo n.º 3
0
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
Exemplo n.º 4
0
from matchpy import Wildcard, Pattern, ReplacementRule, is_match, replace_all, ManyToOneReplacer

from operation import Int, Mul, Add, Pow, Log
from symbol import VariableSymbol, ConstantSymbol
from constraint import FreeQ, NonzeroQ

a, b, c, d, e, f, g, h, x = map(VariableSymbol, 'abcdefghx')
n, m = map(VariableSymbol, 'nm')
a_, b_, c_, d_, e_, f_, g_, h_ = map(Wildcard.dot, 'abcdefgh')
n_, m_ = map(Wildcard.dot, 'nm')

x_ = Wildcard.symbol('x')
u_ = Wildcard.symbol('u')

one = ConstantSymbol(1)
m_one = ConstantSymbol(-1)

pattern1 = Pattern(Int(Mul(a_, Pow(x_, -1)), x), FreeQ((a, ), x))
rule1 = ReplacementRule(pattern1, lambda a, x: Mul(a, Log(x)))

pattern2 = Pattern(Int(Pow(x_, m_), x), FreeQ((m, ), x),
                   NonzeroQ(Add(m_, one), (m, )))
rule2 = ReplacementRule(
    pattern2, lambda m, x: Mul(Pow(x, Add(m, one)), Pow(Add(m, one), m_one)))

rubi = ManyToOneReplacer(rule1)
rubi.add(rule2)

test = [[
    Int(Pow(x, one), x),
    Mul(Pow(Add(one, one), m_one), Pow(x, Add(one, one)))