Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/anrouxel/MedicApp into Do…
Browse files Browse the repository at this point in the history
…wnloadFromApi
  • Loading branch information
marine committed Feb 28, 2024
2 parents 1d890dc + d912f93 commit 37c3965
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 73 deletions.
8 changes: 7 additions & 1 deletion .idea/sonarlint/issuestore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion .idea/sonarlint/securityhotspotstore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 3 additions & 71 deletions app/src/main/java/fr/medicapp/medicapp/ai/PrescriptionAI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import fr.medicapp.medicapp.ai.tokenization.Feature
import fr.medicapp.medicapp.ai.tokenization.FeatureConverter
import fr.medicapp.medicapp.database.repositories.medication.MedicationRepository
import fr.medicapp.medicapp.model.prescription.relationship.Prescription
import fr.medicapp.medicapp.utils.JaroWinkler
import org.pytorch.IValue
import org.pytorch.Module
import org.pytorch.Tensor
Expand Down Expand Up @@ -370,7 +371,7 @@ class PrescriptionAI(
label.startsWith("B-") -> {
if (label.removePrefix("B-") == "Drug") {
val medication = MedicationRepository(context).getAll().sortedByDescending {
jaroWinklerDistance(
JaroWinkler.jaroWinklerDistance(
it.medicationInformation.name,
query.trim()
)
Expand Down Expand Up @@ -401,7 +402,7 @@ class PrescriptionAI(
}
if (query.isNotEmpty()) {
val medication = MedicationRepository(context).getAll().sortedByDescending {
jaroWinklerDistance(
JaroWinkler.jaroWinklerDistance(
it.medicationInformation.name,
query.trim()
)
Expand All @@ -412,75 +413,6 @@ class PrescriptionAI(
return prescriptions
}

fun jaroWinklerDistance(st1: String, st2: String): Double {
val (s1, s2) = swapStringsIfRequired(st1, st2)
val len1 = s1.length
val len2 = s2.length
if (len2 == 0) {
return 0.0
}
val delta = max(0, len2 / 2 - 1)
val (matches, transpositions) = calculateMatchesAndTranspositions(s1, s2, delta)
if (matches == 0) {
return 1.0
}
val jaro = calculateJaroDistance(matches, len1, len2, transpositions)
val commonPrefix = calculateCommonPrefix(s1, s2)
return 1.0 - (jaro + commonPrefix * 0.1 * (1 - jaro))
}

private fun swapStringsIfRequired(s1: String, s2: String): Pair<String, String> {
return if (s1.length < s2.length) Pair(s2, s1) else Pair(s1, s2)
}

private fun calculateMatchesAndTranspositions(
s1: String,
s2: String,
delta: Int
): Pair<Int, Int> {
val flag = BooleanArray(s2.length)
val ch1Match = mutableListOf<Char>()
var transpositions = 0
var idx1 = 0
for ((idx1, ch1) in s1.withIndex()) {
for ((idx2, ch2) in s2.withIndex()) {
if (idx2 <= idx1 + delta && idx2 >= idx1 - delta && ch1 == ch2 && !flag[idx2]) {
flag[idx2] = true
ch1Match.add(ch1)
break
}
}
}
for ((idx2, ch2) in s2.withIndex()) {
if (flag[idx2]) {
if (ch2 != ch1Match[idx1]) {
transpositions++
}
idx1++
}
}
return Pair(ch1Match.size, transpositions)
}

private fun calculateJaroDistance(
matches: Int,
len1: Int,
len2: Int,
transpositions: Int
): Double {
return (matches.toDouble() / len1 + matches.toDouble() / len2 + (matches - transpositions / 2).toDouble() / matches) / 3.0
}

fun calculateCommonPrefix(s1: String, s2: String): Int {
var commonPrefix = 0
for (i in 0 until min(4, s2.length)) {
if (s1[i] == s2[i]) {
commonPrefix++
}
}
return commonPrefix
}

/**
* Récupère le chemin absolu du fichier modèle à partir des assets de l'application.
*
Expand Down
58 changes: 58 additions & 0 deletions app/src/main/java/fr/medicapp/medicapp/utils/JaroWinkler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package fr.medicapp.medicapp.utils

import java.util.Arrays

object JaroWinkler {
fun jaroWinklerDistance(string1: String, string2: String): Double {
var string1 = string1
var string2 = string2
var len1 = string1.length
var len2 = string2.length
if (len1 < len2) {
val s = string1
string1 = string2
string2 = s
val tmp = len1
len1 = len2
len2 = tmp
}
if (len2 == 0) return if (len1 == 0) 0.0 else 1.0
val delta = Math.max(1, len1 / 2) - 1
val flag = BooleanArray(len2)
Arrays.fill(flag, false)
val ch1Match = CharArray(len1)
var matches = 0
for (i in 0 until len1) {
val ch1 = string1[i]
for (j in 0 until len2) {
val ch2 = string2[j]
if ((j <= i + delta) && (j + delta >= i) && (ch1 == ch2) && !flag[j]) {
flag[j] = true
ch1Match[matches++] = ch1
break
}
}
}
if (matches == 0) return 1.0
var transpositions = 0
run {
var i: Int = 0
var j: Int = 0
while (j < len2) {
if (flag.get(j)) {
if (string2.get(j) != ch1Match.get(i)) ++transpositions
++i
}
++j
}
}
val m = matches.toDouble()
val jaro = ((m / len1) + (m / len2) + ((m - transpositions / 2.0) / m)) / 3.0
var commonPrefix = 0
len2 = Math.min(4, len2)
for (i in 0 until len2) {
if (string1[i] == string2[i]) ++commonPrefix
}
return 1.0 - (jaro + commonPrefix * 0.1 * (1.0 - jaro))
}
}

0 comments on commit 37c3965

Please sign in to comment.