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

Distances summary optimization -- https://github.com/ispras/lingvodoc-react/issues/1142 #1522

Merged
merged 2 commits into from
Oct 8, 2024
Merged
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
17 changes: 11 additions & 6 deletions lingvodoc/schema/gql_cognate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5669,15 +5669,20 @@ def get_complex_matrix(result_pool):
language_list = list(pers_by_lang.keys())
l_num = len(language_list)
distance_matrix = numpy.full((l_num, l_num), max_distance, dtype='float')
percent_matrix = numpy.full((l_num, l_num), "n/a", dtype='object')

for (l1_id, l2_id), relation in relation_result.items():

i = language_list.index(l1_id)
j = language_list.index(l2_id)

distance_matrix[i, j] = distance_matrix[j, i] = (
math.sqrt(math.log(relation) / -0.1 / math.sqrt(relation)) if relation > 0 else max_distance)
distance_matrix[i, i] = distance_matrix[j, j] = 0

return distance_matrix, pers_by_lang
percent_matrix[i, j] = percent_matrix[j, i] = f"{round(distance_matrix[i, j], 2)} ({int(relation * 100)}%)"

return distance_matrix, percent_matrix, pers_by_lang

@staticmethod
def mutate(
Expand Down Expand Up @@ -5717,22 +5722,22 @@ def get_perspective_str(perspective_id):
return f'{dictionary_name} - {perspective_name}'

try:
distance_matrix, pers_by_lang = (
distance_matrix, percent_matrix, pers_by_lang = (
ComplexDistance.get_complex_matrix(result_pool))

language_header = [f' {i+1}. {get_language_str(lang_id)}' for i, lang_id in enumerate(pers_by_lang)]

def export_html():

distance_frame = pd.DataFrame(distance_matrix, columns=language_header)
percent_frame = pd.DataFrame(percent_matrix, columns=language_header)
# Start index for distances from 1 to match with dictionaries numbers
distance_frame.index += 1
percent_frame.index += 1

html_result = build_table(distance_frame, 'orange_light', width="300px", index=True)
html_result = build_table(percent_frame, 'orange_light', width="300px", index=True)

for i1, (lang, pers) in enumerate(pers_by_lang.items()):
html_result += (
f"<pre key='{i1}'>\n{' ' * 2}{i1 + 1}. {get_language_str(lang)}</pre>")
f"<pre key='{i1}'>\n\n{' ' * 2}{i1 + 1}. {get_language_str(lang)}</pre>")
for i2, per in enumerate(pers):
html_result += (
f"<pre key='{i1}.{i2}'>{' ' * 6}{i1 + 1}.{i2 + 1} {get_perspective_str(per)}</pre>")
Expand Down