Skip to content

Commit

Permalink
search_run -> search_results
Browse files Browse the repository at this point in the history
  • Loading branch information
fbanados committed Nov 25, 2024
1 parent 79cb726 commit 491f681
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 105 deletions.
14 changes: 7 additions & 7 deletions src/morphodict/frontend/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,17 @@ def index(request): # pragma: no cover

user_query = request.GET.get("q", None)
dict_source = get_dict_source(request)
search_run = None
search_results = None

if user_query:
include_auto_definitions = should_include_auto_definitions(request)
inflect_english_phrases = should_inflect_phrases(request)
search_run = search_with_affixes(
search_results = search_with_affixes(
user_query,
include_auto_definitions=include_auto_definitions,
inflect_english_phrases=inflect_english_phrases,
)
search_results = search_run.serialized_presentation_results(
search_results_presentation = search_results.serialized_presentation_results(
display_mode=DisplayMode.current_value_from_request(request),
animate_emoji=AnimateEmoji.current_value_from_request(request),
show_emoji=ShowEmoji.current_value_from_request(request),
Expand All @@ -110,7 +110,7 @@ def index(request): # pragma: no cover
did_search = True

else:
search_results = []
search_results_presentation = []
did_search = False

if did_search:
Expand All @@ -123,15 +123,15 @@ def index(request): # pragma: no cover
word_search_form=WordSearchForm(),
# when we have initial query word to search and display
query_string=user_query,
search_results=search_results,
search_results=search_results_presentation,
did_search=did_search,
)
context["show_dict_source_setting"] = settings.SHOW_DICT_SOURCE_SETTING
context["show_morphemes"] = request.COOKIES.get("show_morphemes")
context["show_ic"] = request.COOKIES.get("show_inflectional_category")
if search_run and search_run.verbose_messages and search_run.query.verbose:
if search_results and search_results.verbose_messages and search_results.query.verbose:
context["verbose_messages"] = json.dumps(
search_run.verbose_messages, indent=2, ensure_ascii=False
search_results.verbose_messages, indent=2, ensure_ascii=False
)
return render(request, "morphodict/index.html", context)

Expand Down
14 changes: 7 additions & 7 deletions src/morphodict/search/affix.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,27 @@ def do_affix_search(query: InternalForm, affixes: AffixSearcher) -> Iterable[Wor
return Wordform.objects.filter(id__in=matched_ids)


def do_target_language_affix_search(search_run: core.SearchResults):
def do_target_language_affix_search(search_results: core.SearchResults):
matching_words = do_affix_search(
search_run.internal_query,
search_results.internal_query,
cache.target_language_affix_searcher,
)
for word in matching_words:
search_run.add_result(Result(word, target_language_affix_match=True))
search_results.add_result(Result(word, target_language_affix_match=True))


def do_source_language_affix_search(search_run: core.SearchResults):
def do_source_language_affix_search(search_results: core.SearchResults):
matching_words = do_affix_search(
search_run.internal_query,
search_results.internal_query,
cache.source_language_affix_searcher,
)
for word in matching_words:
search_run.add_result(
search_results.add_result(
Result(
word,
source_language_affix_match=True,
query_wordform_edit_distance=get_modified_distance(
word.text, search_run.internal_query
word.text, search_results.internal_query
),
)
)
Expand Down
4 changes: 2 additions & 2 deletions src/morphodict/search/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def presentation_results(
return [
presentation.PresentationResult(
r,
search_run=self,
search_results=self,
display_mode=display_mode,
animate_emoji=animate_emoji,
show_emoji=show_emoji,
Expand Down Expand Up @@ -129,7 +129,7 @@ def add_verbose_message(self, message=None, **messages):
Protip! Use keyword arguments as syntactic sugar for adding a dictionary, e.g.,
search_run.add_verbose_message(foo="bar")
search_results.add_verbose_message(foo="bar")
Will appear as:
Expand Down
8 changes: 4 additions & 4 deletions src/morphodict/search/cvd_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
logger = logging.getLogger(__name__)


def do_cvd_search(search_run: SearchResults):
def do_cvd_search(search_results: SearchResults):
"""Use cosine vector distance to add results to the search run.
Keywords from the query string are turned into vectors from Google News,
added together, and then compared against pre-computed definition vectors.
"""
keys = extract_keyed_words(search_run.query.query_string, google_news_vectors())
keys = extract_keyed_words(search_results.query.query_string, google_news_vectors())
if not keys:
return

search_run.add_verbose_message(cvd_extracted_keys=keys)
search_results.add_verbose_message(cvd_extracted_keys=keys)
query_vector = vector_for_keys(google_news_vectors(), keys)

try:
Expand Down Expand Up @@ -71,4 +71,4 @@ def do_cvd_search(search_run: SearchResults):
else:
for wf in wordforms_for_query:
if wordform_query_matches(wordform_query, wf):
search_run.add_result(Result(wf, cosine_vector_distance=distance))
search_results.add_result(Result(wf, cosine_vector_distance=distance))
26 changes: 13 additions & 13 deletions src/morphodict/search/espt.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ class EsptSearch:
other methods.
"""

def __init__(self, search_run):
self.search_run = search_run
def __init__(self, search_results):
self.search_results = search_results
self.query_analyzed_ok = False

def analyze_query(self):
"""Analyze this search’s search_run query, possibly updating it.
def convert_search_query_to_espt(self):
"""Analyze this search’s search_results query, possibly updating it.
If the phrase-parsing FST returns an analysis, e.g., “ crawls
+V+AI+Prt+3Pl” for “they crawled”, then the tags are saved for
Expand All @@ -53,8 +53,8 @@ def analyze_query(self):
"""
self.new_tags = []
analyzed_query = PhraseAnalyzedQuery(
self.search_run.internal_query,
add_verbose_message=self.search_run.add_verbose_message,
self.search_results.internal_query,
add_verbose_message=self.search_results.add_verbose_message,
)
if analyzed_query.has_tags:
if "+N" in analyzed_query.tags:
Expand All @@ -68,13 +68,13 @@ def analyze_query(self):
self.new_tags = tag_map.map_tags(analyzed_query.tags)
except UnknownTagError as e:
logger.error(f"Unable to map tags for {analyzed_query}", exc_info=True)
self.search_run.add_verbose_message(espt_analysis_error=repr(e))
self.search_results.add_verbose_message(espt_analysis_error=repr(e))
return

self.search_run.query.replace_query(analyzed_query.filtered_query)
self.search_results.query.replace_query(analyzed_query.filtered_query)
self.query_analyzed_ok = True

self.search_run.add_verbose_message(
self.search_results.add_verbose_message(
filtered_query=analyzed_query.filtered_query,
tags=analyzed_query.tags,
new_tags=self.new_tags,
Expand Down Expand Up @@ -116,10 +116,10 @@ def inflect_search_results(self):

# if there are multiple inflections for the same original result, we
# may already have removed it
if self.search_run.has_result(result.original_result):
self.search_run.remove_result(result.original_result)
if self.search_results.has_result(result.original_result):
self.search_results.remove_result(result.original_result)

self.search_run.add_result(
self.search_results.add_result(
result.original_result.create_related_result(
wordform,
is_espt_result=True,
Expand All @@ -128,7 +128,7 @@ def inflect_search_results(self):

def _collect_non_inflected_results(self) -> list[Result]:
words = []
for r in self.search_run.unsorted_results():
for r in self.search_results.unsorted_results():
if not r.is_lemma:
continue
analysis = r.wordform.analysis
Expand Down
4 changes: 2 additions & 2 deletions src/morphodict/search/glossary_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
DOCUMENT_FREQUENCY = {}


def get_glossary_count(search_run):
def get_glossary_count(search_results):
prep_freqs()
[find_glossary_count(result) for result in search_run.unsorted_results()]
[find_glossary_count(result) for result in search_results.unsorted_results()]


def prep_freqs():
Expand Down
4 changes: 2 additions & 2 deletions src/morphodict/search/lemma_freq.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def load_lemma_data():
LEMMA_FREQUENCY[l] = int(l_freq) / max


def get_lemma_freq(search_run):
def get_lemma_freq(search_results):
load_lemma_data()
[find_lemma_freq(result) for result in search_run.unsorted_results()]
[find_lemma_freq(result) for result in search_results.unsorted_results()]


def find_lemma_freq(result):
Expand Down
34 changes: 17 additions & 17 deletions src/morphodict/search/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@
logger = logging.getLogger(__name__)


def fetch_results(search_run: core.SearchResults):
fetch_results_from_target_language_keywords(search_run)
fetch_results_from_source_language_keywords(search_run)
def fetch_results(search_results: core.SearchResults):
fetch_results_from_target_language_keywords(search_results)
fetch_results_from_source_language_keywords(search_results)

# Use the spelling relaxation to try to decipher the query
# e.g., "atchakosuk" becomes "acâhkos+N+A+Pl" --
# thus, we can match "acâhkos" in the dictionary!
fst_analyses = set(rich_analyze_relaxed(search_run.internal_query))
fst_analyses = set(rich_analyze_relaxed(search_results.internal_query))
# print([a.tuple for a in fst_analyses])

db_matches = list(
Wordform.objects.filter(raw_analysis__in=[a.tuple for a in fst_analyses])
)

for wf in db_matches:
search_run.add_result(
search_results.add_result(
Result(
wf,
source_language_match=wf.text,
query_wordform_edit_distance=get_modified_distance(
wf.text, search_run.internal_query
wf.text, search_results.internal_query
),
)
)
Expand All @@ -61,15 +61,15 @@ def fetch_results(search_run: core.SearchResults):
logger.error(
"Cannot generate normative form for analysis: %s (query: %s)",
analysis,
search_run.internal_query,
search_results.internal_query,
)
continue

# If there are multiple forms for this analysis, use the one that is
# closest to what the user typed.
normatized_user_query = min(
normatized_form_for_analysis,
key=lambda f: get_modified_distance(f, search_run.internal_query),
key=lambda f: get_modified_distance(f, search_results.internal_query),
)

possible_lemma_wordforms = best_lemma_matches(
Expand All @@ -82,12 +82,12 @@ def fetch_results(search_run: core.SearchResults):
raw_analysis=analysis.tuple,
lemma=lemma_wordform,
)
search_run.add_result(
search_results.add_result(
Result(
synthetic_wordform,
analyzable_inflection_match=True,
query_wordform_edit_distance=get_modified_distance(
search_run.internal_query,
search_results.internal_query,
normatized_user_query,
),
)
Expand Down Expand Up @@ -136,27 +136,27 @@ def best_lemma_matches(analysis, possible_lemmas) -> list[Wordform]:
]


def fetch_results_from_target_language_keywords(search_run):
for stemmed_keyword in stem_keywords(search_run.internal_query):
def fetch_results_from_target_language_keywords(search_results):
for stemmed_keyword in stem_keywords(search_results.internal_query):
for wordform in Wordform.objects.filter(
target_language_keyword__text__iexact=stemmed_keyword
):
search_run.add_result(
search_results.add_result(
Result(wordform, target_language_keyword_match=[stemmed_keyword])
)


def fetch_results_from_source_language_keywords(search_run):
def fetch_results_from_source_language_keywords(search_results):
res = SourceLanguageKeyword.objects.filter(
Q(text=to_source_language_keyword(search_run.internal_query))
Q(text=to_source_language_keyword(search_results.internal_query))
)
for kw in res:
search_run.add_result(
search_results.add_result(
Result(
kw.wordform,
source_language_keyword_match=[kw.text],
query_wordform_edit_distance=get_modified_distance(
search_run.internal_query, kw.wordform.text
search_results.internal_query, kw.wordform.text
),
)
)
12 changes: 6 additions & 6 deletions src/morphodict/search/pos_matches.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
from morphodict.analysis import rich_analyze_relaxed


def find_pos_matches(search_run: SearchResults) -> None:
analyzed_query = AnalyzedQuery(search_run.internal_query)
# print(search_run.verbose_messages["new_tags"])
def find_pos_matches(search_results: SearchResults) -> None:
analyzed_query = AnalyzedQuery(search_results.internal_query)
# print(search_results.verbose_messages["new_tags"])

if len(search_run.verbose_messages) <= 1:
if len(search_results.verbose_messages) <= 1:
return
tags = search_run.verbose_messages[1].get("tags")
[pos_match(result, tags) for result in search_run.unsorted_results()]
tags = search_results.verbose_messages[1].get("tags")
[pos_match(result, tags) for result in search_results.unsorted_results()]


def pos_match(result, tags):
Expand Down
11 changes: 6 additions & 5 deletions src/morphodict/search/presentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ def __init__(
self,
result: types.Result,
*,
search_run: core.SearchResults,
search_results: core.SearchResults,
display_mode="community",
animate_emoji=AnimateEmoji.default,
show_emoji=ShowEmoji.default,
dict_source=None,
):
self._result = result
self._search_run = search_run
self._search_results = search_results
self._relabeller = {
"english": read_labels().english,
"linguistic": read_labels().linguistic_long,
Expand Down Expand Up @@ -216,7 +216,7 @@ def serialize(self) -> SerializedPresentationResult:
# This is the only place include_auto_definitions is used,
# because we only auto-translate non-lemmas, and this is the
# only place where a non-lemma search result appears.
include_auto_definitions=self._search_run.include_auto_definitions,
include_auto_definitions=self._search_results.include_auto_definitions,
dict_source=self.dict_source,
),
"lexical_info": self.lexical_info,
Expand All @@ -229,13 +229,13 @@ def serialize(self) -> SerializedPresentationResult:
self.is_lemma,
self.lemma_wordform,
self.dict_source,
self._search_run.include_auto_definitions,
self._search_results.include_auto_definitions,
),
"relevant_tags": tuple(t.serialize() for t in self.relevant_tags),
"morphemes": self.morphemes,
"lemma_morphemes": self.lemma_morphemes,
}
if self._search_run.query.verbose:
if self._search_results.query.verbose:
cast(Any, ret)["verbose_info"] = self._result

return ret
Expand Down Expand Up @@ -319,6 +319,7 @@ def should_show_form_of(
return True
if is_lemma:
return True
# TODO Refactor inner for-loop using instead a search via django .values
for definition in lemma_wordform.definitions.all():
for source in definition.source_ids:
if source in dict_source:
Expand Down
Loading

0 comments on commit 491f681

Please sign in to comment.