Maximize Your Python Search Capabilities with Huntela

Learn how this module can help you get richer search results, more easily, than Python’s built-in “filter” method

Tomisin Abiodun
Geek Culture
4 min readMar 14, 2023

--

Originally published on my blog.

For the umpteenth time, you are having to implement a search functionality in your Python application. As a no-brainer, you turn to Python’s filter method.

Half the time, the filter method rightly serves you just fine.

But, as you increase the requirements of your “search” function, you realise that you need to put substantial effort into improving the robustness of the filter function’s results.

For instance, you’re looking to return more “interesting” (fuzzy) results, say your application gives users a list of songs matching their search input, and you want the result to include songs which closely match the input, permitting spelling errors, Huntela is made for you.

What is Huntela

Huntela is a module that contains several methods that could help you get robust search results. A simple illustration

>>> import huntela
>>>
>>> huntela.fuzzy_search(term='app', items=['app', 'apps', 'phone'])
[
{'confidence': 1, 'index': 0, 'value': 'app'},
{'confidence': 0.8, 'index': 1, 'value': 'apps'}
]

Right off the bat, we notice some…

Advantages of Huntela over Python’s filter Method

  1. Fuzzy Matching: The fuzzy_search function uses a character comparison method that includes close matches in its results, allowing for fuzzy matching and increased flexibility in searching for similar or related terms.
  2. Confidence Values: The fuzzy_search function returns results with a confidence score that indicates the level of similarity between the search term and the matched item, providing a more precise indication of how well the search term matches the desired results.
  3. Index Values: The fuzzy_search function method also returns the index of each matching item in the original list, which can be useful for further processing.
  4. Lots of Utility Search Functions: Now, guess what! The same juiciness of the result of the fuzzy_search function applies to several other search functions the module provides. These include binary_search, search_for_least_frequent_items, search_for_most_frequent_items.
  5. Consistent Interfaces: The fuzzy_search method returns a list of Result objects representing the search results, which provide additional information such as the confidence value and index.

Ready to give Huntela a try?

Open up your favourite command line, and type in:

pip install huntela

Practical Examples With the Huntela Library

Autocorrect & Text Suggestion Feature

You are tasked with returning words which closely match a user’s input. This would be a great use-case for Huntela.

>>> import huntela
>>>
>>> user_input = input("Type in a word: ") # hellu
>>>
>>> vocab = ["hello", "run", "jump", ...]
>>> results = huntela.fuzzy_search(user_input, vocab)
[
{"confidence": 0.8, "index": 0, "value": "hello"}
]
>>> print(f"Did you mean: '{results[0]['value']}'?")
Did you mean 'hello'?

Improving User Search Experience in a Shopping Site

Suppose you are assigned to fetch products from an e-commerce platform that closely resemble the user’s input in a search query. You can achieve this by using the fuzzy_search function of the Huntela module.

>>> import huntela
>>>
>>> user_input = input("What would you like to buy? ") # t-shirt
>>>
>>> store = [
{"name": "Headphones", "price": 59.99},
{"name": "Shirt", "price": 19.99}
]
>>> results = huntela.fuzzy_search(user_input, store, key='name')
[
{"confidence": 0.8, "index": 1, "value": "Shirt"}
]

Searching for a specific value in a sorted list

Suppose you have a sorted list of integers, and you want to find if a specific value exists in the list. You can use binary search to do this efficiently.

>>> import huntela
>>>
>>> results = huntela.binary_search(term=5, items=[1, 3, 4, 5, 7, 9, 11])
[
{"confidence": 1, "index": 3, "value": 5}
]

Analytics

You could also use this function to analyze large datasets and find the most and least frequent values. For example, you could use this function to find the most common search queries on a website or the most popular products purchased by customers.

>>> import huntela
>>>
>>> site_visitor_ids = [101, 102, 103, 101, 105, 103, 101, 102, 104]
>>>
>>> most_frequent = huntela.search_for_most_frequent_items(2, user_ids)
[
{'confidence': 1, 'index': [0, 3, 6], 'value': 101},
{'confidence': 1, 'index': [1, 7], 'value': 102},
{'confidence': 1, 'index': [2, 5], 'value': 103}
]

Conclusion

If you’re looking to get more robust and flexible search results than Python’s built-in filter method can provide, Huntela is definitely worth a try.

With its fuzzy matching, confidence values, and index values, Huntela can help you implement features like autocorrect and text suggestion, improve user search experiences in an e-commerce platform, and efficiently search sorted lists.

Additionally, Huntela’s other search functions like searching for most and least frequent items provide valuable utility for analysing large datasets. The module is easy to use and consistent in its interfaces, making it a powerful alternative to the filter method.

So why not give it a try and see how it can enhance your Python search capabilities?

Ready to give Huntela a try?

Open up your favourite command line, and type in:

pip install huntela

If you found this article helpful, clap, share and subscribe.

--

--

Tomisin Abiodun
Geek Culture

Senior Software Engineer · ex-Microsoft · I share interesting thoughts, and guides on software development.