Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proximity operator #6

Open
Guibod opened this issue Feb 9, 2017 · 1 comment
Open

Proximity operator #6

Guibod opened this issue Feb 9, 2017 · 1 comment

Comments

@Guibod
Copy link

Guibod commented Feb 9, 2017

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.

@sebastiandev
Copy link
Owner

sebastiandev commented May 4, 2017

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants