-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuiToBackendConnector.py
73 lines (57 loc) · 2.07 KB
/
uiToBackendConnector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import re
import sqlite3
import snowballstemmer
from typing import Set
from backend.runner import search
from backend.preferences import DisplayMode, AnimateEmoji
def getSearchResultsFromQuery(query: str, linguistic_mode = "community"):
# user_query is the input word from the user
user_query = query[:]
dict_source = None
search_run = None
search_results = []
if user_query:
include_auto_definitions = False
search_run = search_with_affixes(
user_query,
include_auto_definitions=include_auto_definitions,
)
search_results = search_run.serialized_presentation_results(
dict_source=dict_source,
display_mode=linguistic_mode
)
did_search = True
search_results = should_show_form_of(
search_results, dict_source, include_auto_definitions
)
else:
search_results = []
did_search = False
return search_results
def search_with_affixes(query: str, include_auto_definitions=False):
"""
Search for wordforms matching:
- the wordform text
- the definition keyword text
- affixes of the wordform text
- affixes of the definition keyword text
"""
return search(query=query, include_auto_definitions=include_auto_definitions)
def should_show_form_of(search_results, dict_source, include_auto_definitions):
for r in search_results:
if dict_source:
if not r["is_lemma"]:
for d in r["lemma_wordform"]["definitions"]:
for s in d["source_ids"]:
if s in dict_source:
r["show_form_of"] = True
elif (
include_auto_definitions
and s.replace("🤖", "") in dict_source
):
r["show_form_of"] = True
if "show_form_of" not in r:
r["show_form_of"] = False
else:
r["show_form_of"] = True
return search_results