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

Prefix completions with custom python filter #287

Merged
merged 27 commits into from
Mar 6, 2024

Conversation

hendrikmuhs
Copy link
Contributor

@hendrikmuhs hendrikmuhs commented Feb 15, 2024

this PR combines #283 and #284 and puts custom python filtering on completions on top.

It exposes a set_min_weight method to influence/shortcut DFS traversal during matching. Example for a custom top-5 implementation:

heap=[]
def filter_top_5(completer):
    for m in completer:
        if len(heap) < 5:
            heapq.heappush(heap, m.score)
            yield m
        elif m.score > heap[0]:
            heapq.heappop(heap)
            heapq.heappush(heap, m.score)
            completer.set_min_weight(heap[0])
            yield m

[m.matched_string for m in filter_top_5(d.complete_prefix(key))]

Outdated:

After some discussion the following idea has been dropped.

It allows on the fly filtering and custom weight adjustment from python:

def filter(m):
    accept= True
    weight = 42
    ... # implement some filtering logic
    return accept, weight

d.complete_prefix(key, filter)

@hendrikmuhs hendrikmuhs marked this pull request as ready for review February 19, 2024 08:42
Copy link
Member

@narekgharibyan narekgharibyan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

/**
* Set the minimum weight states must be greater or equal to.
*
* Only available for WeightedTransition specialization.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this method is only available to WeightedTransition, should we prevent calls to it with something like throw std:: logic_error("Not implemented") instead of having a method that is no-op ?

Am good with this as well, just wanna see if this was intentional.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had it like this in an earlier version, but I reverted, because it broke completions on dictionaries without weights (test case should exist, so it is easy to find out).

I will try this once more and make a new PR if it doesn't break this case.

So many changes, at some point I lost track.

@hendrikmuhs hendrikmuhs merged commit a93fa71 into KeyviDev:master Mar 6, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants