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

feat: using fuzzy to match query #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion recent_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ def create_json(projects):
AlfredOutput([AlfredItem(project.name, project.path, project.path) for project in projects]))


def match_partial(chars, string):
index = 0
for char in chars:
Copy link
Owner

Choose a reason for hiding this comment

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

after having a second look at this, I believe this algorithm is too permissive. For example, it would find my-awesome-project by querying with ywp. I'd like to keep matching by first letters of each word in the name

Copy link
Owner

Choose a reason for hiding this comment

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

@alswl this comment is still not addressed I believe

Copy link
Owner

Choose a reason for hiding this comment

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

@alswl tagging you in the pending comment

Copy link
Author

Choose a reason for hiding this comment

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

Yes, the search algorithm is by design. Maybe I can make it more strict: only match the left letters of eatch word. For eg: my-awesome-projct will matching the patterns:

  • map -> yes
  • myawp -> yes
  • mawpr -> yes
  • awp -> yes
  • mwr -> no
  • ywp -> no

What's your opinion?

Copy link
Owner

Choose a reason for hiding this comment

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

that's what I had in mind, yes! Please make sure to extend the unit tests to cover these new cases

if char not in string[index:]:
return False
index = string.index(char, index) + 1
return True


class Project:
def __init__(self, path):
self.path = os.path.expanduser(path)
Expand Down Expand Up @@ -60,7 +69,8 @@ def abbreviate(self):
return abbreviation

def matches_query(self, query):
return query in self.path.lower() or query in self.abbreviation.lower() or query in self.name.lower()
chars = filter(lambda x: x.strip() != '', query)
return match_partial(chars, self.path.lower())

def sort_on_match_type(self, query):
if query == self.abbreviation:
Expand Down
23 changes: 22 additions & 1 deletion recent_projects_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from unittest import mock

from recent_projects import create_json, Project, find_app_data, find_recentprojects_file, read_projects_from_file, \
filter_and_sort_projects
filter_and_sort_projects, match_partial


class Unittests(unittest.TestCase):
Expand Down Expand Up @@ -115,6 +115,27 @@ def test_project_sort_on_match_type(self):
self.assertEqual(project.sort_on_match_type("spring-petclinic"), 1)
self.assertEqual(project.sort_on_match_type("foobar"), 2)

def test_match_partial(self):
self.assertEqual(match_partial("y", "your-foo-bar"), True)
self.assertEqual(match_partial("yr", "your-foo-bar"), True)
self.assertEqual(match_partial("yrb", "your-foo-bar"), True)
self.assertEqual(match_partial("your-foo-bar", "your-foo-bar"), True)
self.assertEqual(match_partial("your-fb", "your-foo-bar"), True)
self.assertEqual(match_partial("", "your-foo-bar"), True)

self.assertEqual(match_partial("uyfb", "/home/username/your-foo-bar"), True)
self.assertEqual(match_partial("uyr", "/home/username/your-foo-bar"), True)
self.assertEqual(match_partial("uyrb", "/home/username/your-foo-bar"), True)
self.assertEqual(match_partial("uyour-foo-bar", "/home/username/your-foo-bar"), True)
self.assertEqual(match_partial("uyour-fb", "/home/username/your-foo-bar"), True)
self.assertEqual(match_partial("h/u/your-fb", "/home/username/your-foo-bar"), True)

self.assertEqual(match_partial("not-exist", "your-foo-bar"), False)
self.assertEqual(match_partial("yr0", "your-foo-bar"), False)
self.assertEqual(match_partial("ybf", "your-foo-bar"), False)
self.assertEqual(match_partial(" ", "your-foo-bar"), False)
self.assertEqual(match_partial("oooo", "ooo"), False)


if __name__ == '__main__': # pragma: nocover
unittest.main()