Skip to content

Commit

Permalink
removed query dependency in SearchResults constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
fbanados committed Nov 25, 2024
1 parent 6e23958 commit 8708113
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/morphodict/paradigm/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ class DisplayMode(Preference):
# nêhiyawêwin labels; e.g., niya → kiya, mêkwâc
"source_language": str(settings.MORPHODICT_LANGUAGE_ENDONYM) + " labels",
}
default = "english"
default = "english"
1 change: 1 addition & 0 deletions src/morphodict/search/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .runner import search
from .core import SearchResults


def search_with_affixes(
query: str, include_auto_definitions=False, inflect_english_phrases=False
) -> SearchResults:
Expand Down
8 changes: 6 additions & 2 deletions src/morphodict/search/affix.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ def do_affix_search(query: InternalForm, affixes: AffixSearcher) -> Iterable[Wor
return Wordform.objects.filter(id__in=matched_ids)


def do_target_language_affix_search(query: core.Query, search_results: core.SearchResults):
def do_target_language_affix_search(
query: core.Query, search_results: core.SearchResults
):
matching_words = do_affix_search(
query.query_string,
cache.target_language_affix_searcher,
Expand All @@ -106,7 +108,9 @@ def do_target_language_affix_search(query: core.Query, search_results: core.Sear
search_results.add_result(Result(word, target_language_affix_match=True))


def do_source_language_affix_search(query: core.Query, search_results: core.SearchResults):
def do_source_language_affix_search(
query: core.Query, search_results: core.SearchResults
):
matching_words = do_affix_search(
query.query_string,
cache.source_language_affix_searcher,
Expand Down
7 changes: 3 additions & 4 deletions src/morphodict/search/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ class SearchResults:
and to add results to the result collection for future ranking.
"""

def __init__(self, query: Query, include_auto_definitions=None):
def __init__(self, auto=None, verbose=False, include_auto_definitions=None):
self.include_auto_definitions = first_non_none_value(
query.auto, include_auto_definitions, default=False
auto, include_auto_definitions, default=False
)
self.verbose = query.verbose
self.verbose = verbose
self._results = {}
self._verbose_messages = []

Expand Down Expand Up @@ -150,7 +150,6 @@ def add_verbose_message(self, message=None, **messages):
def verbose_messages(self):
return self._verbose_messages


def __repr__(self):
return f"SearchResults<query={self.query!r}>"

Expand Down
4 changes: 3 additions & 1 deletion src/morphodict/search/cvd_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,6 @@ def do_cvd_search(query: Query, search_results: SearchResults):
else:
for wf in wordforms_for_query:
if wordform_query_matches(wordform_query, wf):
search_results.add_result(Result(wf, cosine_vector_distance=distance))
search_results.add_result(
Result(wf, cosine_vector_distance=distance)
)
8 changes: 6 additions & 2 deletions src/morphodict/search/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ def best_lemma_matches(analysis, possible_lemmas) -> list[Wordform]:
]


def fetch_results_from_target_language_keywords(query: core.Query,search_results: core.SearchResults):
def fetch_results_from_target_language_keywords(
query: core.Query, search_results: core.SearchResults
):
for stemmed_keyword in stem_keywords(query.query_string):
for wordform in Wordform.objects.filter(
target_language_keyword__text__iexact=stemmed_keyword
Expand All @@ -146,7 +148,9 @@ def fetch_results_from_target_language_keywords(query: core.Query,search_results
)


def fetch_results_from_source_language_keywords(query: core.Query, search_results: core.SearchResults):
def fetch_results_from_source_language_keywords(
query: core.Query, search_results: core.SearchResults
):
res = SourceLanguageKeyword.objects.filter(
Q(text=to_source_language_keyword(query.query_string))
)
Expand Down
20 changes: 12 additions & 8 deletions src/morphodict/search/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ def search(

search_query = Query(query)
search_results = SearchResults(
search_query,
include_auto_definitions=include_auto_definitions
auto=search_query.auto,
verbose=search_query.verbose,
include_auto_definitions=include_auto_definitions,
)

initial_query_terms = search_query.query_terms[:]
Expand All @@ -49,9 +50,9 @@ def search(

if settings.MORPHODICT_ENABLE_CVD:
cvd_search_type = first_non_none_value(
search_query.cvd,
default=CvdSearchType.DEFAULT)
search_query.cvd, default=CvdSearchType.DEFAULT
)

# For when you type 'cvd:exclusive' in a query to debug ONLY CVD results!
if cvd_search_type == CvdSearchType.EXCLUSIVE:

Expand All @@ -74,8 +75,7 @@ def sort_by_cvd(r: Result):

if settings.MORPHODICT_ENABLE_CVD:
if cvd_search_type.should_do_search() and not is_almost_certainly_cree(
search_query,
search_results
search_query, search_results
):
do_cvd_search(search_query, search_results)

Expand All @@ -90,8 +90,10 @@ def sort_by_cvd(r: Result):

return search_results


CREE_LONG_VOWEL = re.compile("[êîôâēīōā]")


def is_almost_certainly_cree(query: Query, search_results: SearchResults) -> bool:
"""
Heuristics intended to AVOID doing an English search.
Expand All @@ -105,7 +107,9 @@ def is_almost_certainly_cree(query: Query, search_results: SearchResults) -> boo
return True

if CREE_LONG_VOWEL.search(query.query_string):
search_results.add_verbose_message("Skipping CVD because query has Cree diacritics")
search_results.add_verbose_message(
"Skipping CVD because query has Cree diacritics"
)
return True

return False
8 changes: 4 additions & 4 deletions src/morphodict/tests/espt/test_espt_crk.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ def test_search_with_tags(query, has_tags, tags, filtered_query):
)
def test_espt_search(db, search, params):
search_query = Query(search)
search_results = SearchResults(search_query)
espt_search = EsptSearch(search_query,search_results)
search_results = SearchResults()
espt_search = EsptSearch(search_query, search_results)
espt_search.convert_search_query_to_espt()
assert search_query.query_terms == params["expected_query_terms"]
assert search_query.query_string == " ".join(params["expected_query_terms"])
Expand All @@ -118,8 +118,8 @@ def test_espt_search(db, search, params):

def test_espt_search_doesnt_crash_when_no_analysis(db):
search_query = Query("my little bears")
search_results = SearchResults(search_query)
espt_search = EsptSearch(search_query,search_results)
search_results = SearchResults()
espt_search = EsptSearch(search_query, search_results)
espt_search.convert_search_query_to_espt()

wordform = Wordform(text="pê-")
Expand Down

0 comments on commit 8708113

Please sign in to comment.