timgremore commited on
Commit
fad254c
1 Parent(s): 50d1bef

fix: Calculate cosine_similarity for selected code

Browse files
lib/medical_transcription/coding.ex CHANGED
@@ -169,6 +169,17 @@ defmodule Medicode.Coding do
169
  )
170
  end
171
 
 
 
 
 
 
 
 
 
 
 
 
172
  # Retrieves code vectors referenced in past feedback for a given search text. This allows including additional codes
173
  # for the weighting process outside of the closest 5 returned by `find_similar/2`.
174
  defp find_for_feedback(search_vector, past_feedbacks) do
 
169
  )
170
  end
171
 
172
+ def get_cosine_similarity(vector1, vector2) do
173
+ query =
174
+ from v in CodeVector,
175
+ select: %{cosine_similarity: 1 - cosine_distance(^vector1, ^vector2)},
176
+ limit: 1
177
+
178
+ query
179
+ |> Repo.one()
180
+ |> Map.get(:cosine_similarity)
181
+ end
182
+
183
  # Retrieves code vectors referenced in past feedback for a given search text. This allows including additional codes
184
  # for the weighting process outside of the closest 5 returned by `find_similar/2`.
185
  defp find_for_feedback(search_vector, past_feedbacks) do
lib/medical_transcription_web/components/code_select.ex CHANGED
@@ -115,7 +115,8 @@ defmodule MedicodeWeb.Components.CodeSelect do
115
  assigned_by_user_id: socket.assigns.current_user.id,
116
  code_vector_id: selected_code.id,
117
  transcription_chunk_id: socket.assigns.chunk.id,
118
- cosine_similarity: 1.0
 
119
  })
120
 
121
  send_update(MedicodeWeb.Components.TranscriptionTextComponent,
 
115
  assigned_by_user_id: socket.assigns.current_user.id,
116
  code_vector_id: selected_code.id,
117
  transcription_chunk_id: socket.assigns.chunk.id,
118
+ cosine_similarity:
119
+ Medicode.Coding.get_cosine_similarity(text_vector, selected_code.description_vector)
120
  })
121
 
122
  send_update(MedicodeWeb.Components.TranscriptionTextComponent,