File size: 9,665 Bytes
3f219b5 8488f43 3f219b5 0311a5a 3f219b5 cf5a59c 2a0e044 cf5a59c 8488f43 deecaf3 3f219b5 deecaf3 8488f43 5ea1261 8488f43 3a8280c 5964d8b 3a8280c 5964d8b 3a8280c 8488f43 a4b994e 5676fb5 a4b994e 8488f43 8548275 0311a5a 22ed69b 0311a5a c016b89 3f219b5 c016b89 d79a6a5 0311a5a d79a6a5 5c502c9 8488f43 a4b994e 7625517 2a0e044 71b9dff 2a0e044 71b9dff 2a0e044 71b9dff 5d44eb4 71b9dff 5d44eb4 71b9dff 2a0e044 5f7f244 8488f43 8f56351 8488f43 |
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
defmodule Medicode.Transcriptions do
@moduledoc """
The Transcriptions context.
"""
import Ecto.Query, warn: false
alias Medicode.Repo
alias Medicode.Transcriptions.{
Transcription,
TranscriptionChunk,
TranscriptionChunkKeyword,
TranscriptionChunkCodeVector
}
@doc """
Create transcription record and begin transcribing
## Examples
iex> create_and_transcribe_audio("my-audio.mp3")
%Transcription{audio_file: "my-audio.mp3"}
"""
def transcribe_audio(transcription) do
Medicode.TranscriptionSupervisor.start_transcription(transcription)
end
@doc """
Returns the list of transcriptions.
## Examples
iex> list_transcriptions()
[%Transcription{}, ...]
"""
def list_transcriptions(user) do
Transcription
|> where([t], t.user_id == ^user.id)
|> Repo.all()
end
@doc """
Gets a single transcription.
Returns `nil` if the Transcription does not exist.
## Examples
iex> get_transcription(123)
%Transcription{}
iex> get_transcription(456)
nil
"""
def get_transcription(id, preload_transcription_chunks \\ false) do
query =
if preload_transcription_chunks do
chunks_query =
TranscriptionChunk
|> preload([:keywords, :code_vectors])
|> order_by([c], asc: c.start_mark)
Transcription
|> preload(chunks: ^chunks_query)
else
Transcription
end
Repo.get(query, id)
end
@doc """
Gets a single transcription.
Raises `Ecto.NoResultsError` if the Transcription does not exist.
## Examples
iex> get_transcription!(123)
%Transcription{}
iex> get_transcription!(456)
** (Ecto.NoResultsError)
"""
def get_transcription!(id, preload_transcription_chunks \\ false) do
query =
if preload_transcription_chunks do
Transcription
|> preload(chunks: [:keywords, :code_vectors])
else
Transcription
end
Repo.get!(query, id)
end
@doc """
Gets a single transcription chunk.
Raises `Ecto.NoResultsError` if the TranscriptionChunk does not exist.
## Examples
iex> get_transcription_chunk!(123)
%TranscriptionChunk{}
iex> get_transcription_chunk!(456)
** (Ecto.NoResultsError)
"""
def get_transcription_chunk!(id, preload_transcription_chunk_associations \\ false) do
query =
if preload_transcription_chunk_associations do
TranscriptionChunk
|> preload([:keywords, :code_vectors])
else
TranscriptionChunk
end
Repo.get!(query, id)
end
@doc """
Lists code vectors for transcription chunk ID.
Returns `[]` if none are found.
## Examples
iex> list_transcription_chunk_code_vectors(123)
%CodeVector{}
iex> list_transcription_chunk_code_vectors(456)
[]
"""
def list_transcription_chunk_code_vectors(chunk_id) do
TranscriptionChunkCodeVector
|> join(:inner, [chunk_vector], code_vector in assoc(chunk_vector, :code_vector))
|> where([chunk_vector], chunk_vector.transcription_chunk_id == ^chunk_id)
|> order_by([chunk_vector], desc: chunk_vector.cosine_similarity)
|> preload([:assigned_by_user, :code_vector])
|> Repo.all()
end
def delete_code_vector_by_chunk_id_and_assigned_by_user_id(
chunk_id,
code_vector_id,
assigned_by_user_id
) do
query =
TranscriptionChunkCodeVector
|> where(
[v],
v.transcription_chunk_id == ^chunk_id and v.code_vector_id == ^code_vector_id and
v.assigned_by_user_id == ^assigned_by_user_id
)
with %TranscriptionChunkCodeVector{} = chunk_vector <- Medicode.Repo.one(query),
{:ok, _struct} <- Medicode.Repo.delete(chunk_vector) do
{:ok, chunk_vector}
else
res -> res
end
end
@doc """
List transcription chunks by transcription ID.
Returns [] if none are found.
## Examples
iex> list_transcription_chunks(123)
[%TranscriptionChunk{}]
iex> list_transcription_chunks(456)
[]
"""
def list_transcription_chunks(
transcription_chunk_ids,
preload_transcription_chunk_associations \\ false
) do
query =
if preload_transcription_chunk_associations do
TranscriptionChunk
|> preload([:keywords, :code_vectors])
else
TranscriptionChunk
end
query
|> where([c], c.id in ^transcription_chunk_ids)
|> select([c], {c.id, c})
|> Repo.all()
end
@doc """
Collect transcription keywords and order by score.
"""
def list_transcription_summary_keywords(transcription_id) do
query =
from(
k in TranscriptionChunkKeyword,
join: c in TranscriptionChunk, on: k.transcription_chunk_id == c.id,
where: c.transcription_id == ^transcription_id,
group_by: k.keyword,
select: %{keyword: k.keyword, score: sum(k.score)},
order_by: [desc: sum(k.score)]
)
Repo.all(query)
end
@doc """
Creates a transcription.
## Examples
iex> create_transcription(%{field: value})
{:ok, %Transcription{}}
iex> create_transcription(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_transcription(attrs \\ %{}) do
%Transcription{}
|> Transcription.changeset(attrs)
|> Repo.insert()
end
@doc """
Creates a transcription chunk.
## Examples
iex> create_chunk(%{field: value})
{:ok, %TranscriptionChunk{}}
iex> create_chunk(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_chunk(attrs \\ %{}) do
%TranscriptionChunk{}
|> TranscriptionChunk.changeset(attrs)
|> Repo.insert()
end
@doc """
Creates a transcription chunk keyword.
## Examples
iex> create_keyword_for_chunk(%{transcription_chunk_id: 1, keyword: "healthy"})
{:ok, %TranscriptionChunkKeyword{}}
iex> create_keyword_for_chunk(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_keyword_for_chunk(attrs \\ %{}) do
%TranscriptionChunkKeyword{}
|> TranscriptionChunkKeyword.changeset(attrs)
|> Repo.insert()
end
@doc """
Creates a transcription chunk code vector record.
## Examples
iex> replace_all_code_vectors_for_chunk(%TranscriptionChunk{}, [%{transcription_chunk_id: 1, code_vector_id: 1, cosine_similarity: 1, weight: "positive"}])
{:ok, [%{id: id}]}
iex> replace_all_code_vectors_for_chunk(%TranscriptionChunk{}, [%{field: bad_value}])
{:error, %Ecto.Changeset{}}
"""
def replace_all_code_vectors_for_chunk(%TranscriptionChunk{id: transcription_chunk_id}, attrs) do
timestamp =
DateTime.utc_now()
|> DateTime.truncate(:second)
{_count, chunk_code_vectors} =
TranscriptionChunkCodeVector
|> Repo.insert_all(
attrs,
placeholders: %{timestamp: timestamp},
on_conflict: {:replace, [:cosine_similarity, :weighting, :updated_at]},
returning: [:id],
conflict_target: [:transcription_chunk_id, :code_vector_id]
)
chunk_code_vector_ids = Enum.map(chunk_code_vectors, & &1.id)
# Clear previous code vectors except for those assigned by a user
from(v in TranscriptionChunkCodeVector,
where:
v.transcription_chunk_id == ^transcription_chunk_id and v.id not in ^chunk_code_vector_ids and
is_nil(v.assigned_by_user_id)
)
|> Repo.delete_all()
chunk_code_vectors
end
@doc """
Insert a transcription chunk code vector record.
## Examples
iex> upsert_code_vector_for_transcription_chunk(%{user_id: user.id, chunk_id: chunk.id})
{:ok, %TranscriptionChunkCodeVector{}}
iex> upsert_code_vector_for_transcription_chunk(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def upsert_code_vector_for_transcription_chunk(attrs) do
%TranscriptionChunkCodeVector{}
|> TranscriptionChunkCodeVector.changeset(attrs)
|> Repo.insert(
on_conflict: :nothing,
conflict_target: [:transcription_chunk_id, :code_vector_id]
)
end
@doc """
Updates a transcription.
## Examples
iex> update_transcription(transcription, %{field: new_value})
{:ok, %Transcription{}}
iex> update_transcription(transcription, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_transcription(%Transcription{} = transcription, attrs) do
transcription
|> Transcription.changeset(attrs)
|> Repo.update()
end
@doc """
Updates a transcription chunk.
## Examples
iex> update_transcription_chunk(transcription_chunk, %{field: new_value})
{:ok, %TranscriptionChunk{}}
iex> update_transcription_chunk(transcription_chunk, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_transcription_chunk(%TranscriptionChunk{} = chunk, attrs) do
chunk
|> TranscriptionChunk.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a transcription.
## Examples
iex> delete_transcription(transcription)
{:ok, %Transcription{}}
iex> delete_transcription(transcription)
{:error, %Ecto.Changeset{}}
"""
def delete_transcription(%Transcription{} = transcription) do
Repo.delete(transcription)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking transcription changes.
## Examples
iex> change_transcription(transcription)
%Ecto.Changeset{data: %Transcription{}}
"""
def change_transcription(%Transcription{} = transcription, attrs \\ %{}) do
Transcription.changeset(transcription, attrs)
end
end
|