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

add clean_whitespace option to fuzzy retrieval function #900

Closed
wants to merge 1 commit into from
Closed
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
33 changes: 24 additions & 9 deletions regolith/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ def document_by_value(documents, address, value):
return g_doc


def fuzzy_retrieval(documents, sources, value, case_sensitive=True):
def fuzzy_retrieval(documents, sources, value, case_sensitive=True, clean_whitespace=True):
"""Retrieve a document from the documents where value is compared against
multiple potential sources

Expand All @@ -930,6 +930,8 @@ def fuzzy_retrieval(documents, sources, value, case_sensitive=True):
The value to compare against to find the document of interest
case_sensitive: Bool
When true will match case (Default = True)
clean_whitespace: Bool
When true will strip whitespaces off front and end of string (Default = True)

Returns
-------
Expand All @@ -938,7 +940,7 @@ def fuzzy_retrieval(documents, sources, value, case_sensitive=True):

Examples
--------
>>> fuzzy_retrieval(people, ['aka', 'name'], 'pi_name', case_sensitive = False)
>>> fuzzy_retrieval(people, ['aka', 'name'], 'pi_name', case_sensitive = False, clean_whitespace = True)

This would get the person entry for which either the alias or the name was
``pi_name``.
Expand All @@ -951,15 +953,28 @@ def fuzzy_retrieval(documents, sources, value, case_sensitive=True):
if not isinstance(ret, list):
ret = [ret]
returns.extend(ret)
if not case_sensitive:
returns = [reti.lower() for reti in returns if
isinstance(reti, str)]
if isinstance(value, str):
if value.lower() in frozenset(returns):
if not clean_whitespace:
if not case_sensitive:
returns = [reti.lower() for reti in returns if
isinstance(reti, str)]
if isinstance(value, str):
if value.lower() in frozenset(returns):
return doc
else:
if value in frozenset(returns):
return doc
else:
if value in frozenset(returns):
return doc
if not case_sensitive:
returns = [reti.lower().strip() for reti in returns if
isinstance(reti, str)]
if isinstance(value, str):
if value.lower().strip() in frozenset(returns):
return doc
else:
returns = [reti.strip() for reti in returns if isinstance(reti, str)]
if isinstance(value, str):
if value.strip() in frozenset(returns):
return doc


def number_suffix(number):
Expand Down