plyse icon indicating copy to clipboard operation
plyse copied to clipboard

Proximity operator

Open Guibod opened this issue 8 years ago • 1 comments

How can I achieve a Lucene proximity operator in Plyse ?

"happy birthday"~3

I use Plyse because i don't want to invest that much time in PyParsing, yet I really don't understand the Plyse object structures so that I am unable to create a proper parsing item for that kind of string. My goal is to obtain a term with a field_type = Term.PROXIMITY_STRING and a value that provide something like ['happy', 'birthday', 3]

See this pyparsing implementation for lucene for reference.

Guibod avatar Feb 09 '17 13:02 Guibod

@Guibod sorry for the delay. If you are still interested, you could do something like this:

You have to create a new primitive:

class StringProximity(And, BaseType):

    name = 'string_proximity'

    def __init__(self, parse_method=None, precedence=11):
        ANd.__init__(self, QuotedString() + Literal('~') + Integer())
        BaseType.__init__(self, precedence)

        if parse_method:
            self.addParseAction(parse_method)

for the parsing (usually a method in the TermParser):

def proximity_parse(string, location, tokens):
    if tokens:
        prox = int(tokens[-1])
        phrase = tokens[:-2]
        return {'value': phrase + [prox], 'value_type': 'string_proximity'}

To test it you can do:

sp = StringProximity(parse_method=proximity_parse)
sp.parseString(" 'hello world'~3")

# ([{'value_type': 'string_proximity', 'value': ['hello word', 2]}], {})

Of course the parsing would be a TermParser method that use later define in the config for the StringProximity.

sebastiandev avatar May 04 '17 17:05 sebastiandev