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

Feat/seperate text recog module #34

Merged
merged 3 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
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
13 changes: 13 additions & 0 deletions autotextcorrection/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
plugins {
id("java-library")
id("org.jetbrains.kotlin.jvm")
}

java {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}

dependencies {
implementation(UnitTest.JUNIT)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.konkuk.autotextcorrection

class AutoTextCorrector(
targetTextList: List<String>,
val errorCorrectionDistance: Int = 1,
) {

private val targetWordsMap = mutableMapOf<String, String>()
private val targetTextDestructedList = targetTextList.map { word ->
val destructedWord = destructWord(word)
targetWordsMap[destructedWord] = word
destructedWord
}

fun destructWord(word: String): String {
var result = StringBuilder()
for (element in word) {
var uniVal = element

if (uniVal.code >= 0xAC00) {
uniVal = (uniVal.code - 0xAC00).toChar()
val cho = (uniVal.code / 28 / 21).toChar()
val joong = (uniVal.code / 28 % 21).toChar()
val jong = (uniVal.code % 28).toChar()
result.append(INITIAL_CHARACTER[cho.code] + MID_CHARACTER[joong.code] + FINAL_CHARACTER[jong.code])
} else {
result.append(element)
}
}
return String(result)
}

fun correctWord(word: String): String {
if (word.isEmpty()) return word
val destructedWord = destructWord(word)
for (destructedChars in targetTextDestructedList) {
val dp = List(destructedChars.length + 1) { IntArray(destructedWord.length + 1) }
for (i in 1..destructedChars.length) {
for (j in 1..destructedWord.length) {
if (destructedChars[i - 1] == destructedWord[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1
} else {
dp[i][j] = maxOf(dp[i - 1][j], dp[i][j - 1])
}
}
}
if (abs(dp.last().last() - destructedChars.length) <= errorCorrectionDistance &&
abs(dp.last().last() - destructedWord.length) <= errorCorrectionDistance
) {
return targetWordsMap[destructedChars]!!
}
}
return word
}

fun correctText(text: String): String {
return text.split(" ").map { word -> correctWord(word) }.joinToString(" ")
}

private fun abs(n: Int) = if (n < 0) -n else n

companion object {
private val INITIAL_CHARACTER = listOf(
"ใ„ฑ", "ใ„ฒ", "ใ„ด", "ใ„ท", "ใ„ธ", "ใ„น", "ใ…", "ใ…‚", "ใ…ƒ",
"ใ……", "ใ…†", "ใ…‡", "ใ…ˆ", "ใ…‰", "ใ…Š", "ใ…‹", "ใ…Œ", "ใ…", "ใ…Ž",
)

private val MID_CHARACTER = listOf(
"ใ…", "ใ…", "ใ…‘", "ใ…’", "ใ…“", "ใ…”", "ใ…•", "ใ…–", "ใ…—", "ใ…˜",
"ใ…™", "ใ…š", "ใ…›", "ใ…œ", "ใ…", "ใ…ž", "ใ…Ÿ", "ใ… ", "ใ…ก", "ใ…ข", "ใ…ฃ",
)

private val FINAL_CHARACTER = listOf(
"", "ใ„ฑ", "ใ„ฒ", "ใ„ณ", "ใ„ด", "ใ„ต", "ใ„ถ", "ใ„ท", "ใ„น", "ใ„บ", "ใ„ป", "ใ„ผ",
"ใ„ฝ", "ใ„พ", "ใ„ฟ", "ใ…€", "ใ…", "ใ…‚", "ใ…„", "ใ……", "ใ…†", "ใ…‡", "ใ…ˆ", "ใ…Š", "ใ…‹", "ใ…Œ", "ใ…", "ใ…Ž",
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.konkuk.autotextcorrection

import org.junit.Assert
import org.junit.Test

class AutoTextCorrectorUnitTest {

private val targetTextList = listOf("์นผ๋กœ๋ฆฌ", "๋‚˜ํŠธ๋ฅจ", "๋‹จ๋ฐฑ์งˆ", "ํƒ„์ˆ˜ํ™”๋ฌผ")
private val corrector = AutoTextCorrector(
targetTextList = targetTextList,
errorCorrectionDistance = 2,
)

@Test
fun `destruct_corrector๊ฐ€ string์„ char๋กœ ๋ถ„ํ•ด`() {
Assert.assertEquals(
targetTextList.map { corrector.destructWord(it) },
listOf(
"ใ…‹ใ…ใ„นใ„นใ…—ใ„นใ…ฃ",
"ใ„ดใ…ใ…Œใ…กใ„นใ… ใ…",
"ใ„ทใ…ใ„ดใ…‚ใ…ใ„ฑใ…ˆใ…ฃใ„น",
"ใ…Œใ…ใ„ดใ……ใ…œใ…Žใ…˜ใ…ใ…œใ„น",
),
)
}

@Test
fun `correct_corrector๊ฐ€ word๋ฅผ ์ˆ˜์ •`() {
Assert.assertEquals(
"๋‚˜ํŠธ๋ฅจ",
corrector.correctWord("๋‹ˆํŠธ๋ฅจ"),
)

Assert.assertEquals(
"ํƒ„์ˆ˜ํ™”๋ฌผ",
corrector.correctWord("ํƒ์ˆ˜ํ™”๋ฏˆ"),
)
}

@Test
fun `correct_corrector๊ฐ€ ์˜ค๋ฅ˜๋ฅผ ์ˆ˜์ •`() {
Assert.assertEquals(
"๋‚˜ํŠธ๋ฅจ ์ •๋ง ๋ง›์žˆ์–ด,ใ…Žใ…Ž ๊ทธ๋ฆฌ๊ณ  ๋‚˜๋Š” ํƒ„์ˆ˜ํ™”๋ฌผ ๋งค์šฐ ์ข‹์•„ํ•˜์ง€!! ์นผ๋กœ๋ฆฌ ๋„ˆ๋ฌด ํ–‰๋ณต",
corrector.correctText("๋‹ˆํŠธ๋ฅจ ์ •๋ง ๋ง›์žˆ์–ด,ใ…Žใ…Ž ๊ทธ๋ฆฌ๊ณ  ๋‚˜๋Š” ํƒ์ˆ˜ํ™”๋ฏˆ ๋งค์šฐ ์ข‹์•„ํ•˜์ง€!! ์นผ๋กœใ… ๋„ˆ๋ฌด ํ–‰๋ณต"),
)
}
}
1 change: 1 addition & 0 deletions feat/capture/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ android {

dependencies {
implementation(project(":common"))
implementation(project(":autotextcorrection"))

// Android
implementation(AndroidX.CORE_KTX)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.graphics.Bitmap
import android.net.Uri
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import com.konkuk.autotextcorrection.AutoTextCorrector
import com.konkuk.common.data.FoodInfo
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -13,15 +14,15 @@ import javax.inject.Inject
class EnrollTextInputViewModel @Inject constructor(
savesStateHandle: SavedStateHandle,
) : ViewModel() {
private val sodiumRegex = Regex("๋‚˜ํŠธ[๋ฅจ๋ฃธ]([\\d.]+)\\D")
private val sodiumRegex = Regex("๋‚˜ํŠธ๋ฅจ([\\d.]+)\\D")
private val carbohydratesRegex = Regex("ํƒ„์ˆ˜ํ™”๋ฌผ([\\d.]+)\\D")
private val fatRegex = Regex("์ง€๋ฐฉ([\\d.]+)\\D")
private val cholesterolRegex = Regex("์ฝœ[๋ ˆ๋ž˜๋Ÿฌ]์Šค[ํƒœํ…Œ]๋กค([\\d.]+)\\D")
private val cholesterolRegex = Regex("์ฝœ๋ ˆ์Šคํ…Œ๋กค([\\d.]+)\\D")
private val proteinRegex = Regex("๋‹จ๋ฐฑ์งˆ([\\d.]+)\\D")
private val sugarRegex = Regex("๋‹น[๋ฅ˜๋ฃจ]([\\d.]+)\\D")
private val transFatRegex = Regex("ํŠธ[๋žœ๋ Œ]์Šค์ง€๋ฐฉ([\\d.]+)\\D")
private val transFatRegex = Regex("ํŠธ๋žœ์Šค์ง€๋ฐฉ([\\d.]+)\\D")
private val saturatedFatRegex = Regex("ํฌํ™”์ง€๋ฐฉ([\\d.]+)\\D")
private val totalGramsRegex = Regex("์ด๋‚ด์šฉ[๋Ÿ‰๋ž‘](\\d+)g")
private val totalGramsRegex = Regex("์ด๋‚ด์šฉ๋Ÿ‰(\\d+)g")
private val totalCaloriesRegex = Regex("(\\d+)kca")
private val perCaloriesRegex = Regex("(\\d+)g๋‹น(\\d+)kca")

Expand All @@ -40,11 +41,14 @@ class EnrollTextInputViewModel @Inject constructor(

init {
savesStateHandle.get<String>(OCR_RESULT_KEY)?.let { text ->
val result = text.replace(" ", "")
.replace(",", ".")
.replace(")", "")
val correctedText =
AutoTextCorrector(nutritionNameList, 2).correctText(text.replace("%", "% "))

setFoodInfo(result)
setFoodInfo(
correctedText.replace(" ", "")
.replace(",", ".")
.replace(")", ""),
)
}
savesStateHandle.get<FoodInfo?>(API_RESULT_KEY)?.let {
setFoodInfo(it)
Expand Down Expand Up @@ -123,6 +127,18 @@ class EnrollTextInputViewModel @Inject constructor(
const val API_RESULT_KEY = "API_RESULT_KEY"
const val BITMAP_PICTURE_KEY = "BITMAP_PICTURE_KEY"
const val URI_PICTURE_KEY = "URI_PICTURE_KEY"

val nutritionNameList = listOf(
"์นผ๋กœ๋ฆฌ",
"ํƒ„์ˆ˜ํ™”๋ฌผ",
"๋‹จ๋ฐฑ์งˆ",
"๋‹น๋ฅ˜",
"์ง€๋ฐฉ",
"ํฌํ™”์ง€๋น™",
"ํŠธ๋žœ์Šค์ง€๋ฐฉ",
"์ฝœ๋ ˆ์Šคํ…Œ๋กค",
"๋‚˜ํŠธ๋ฅจ",
)
}
}

Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ include ':feat:personal'
include ':feat:history'
include ':feat:capture'
include ':common'
include ':autotextcorrection'
9 changes: 9 additions & 0 deletions textautoconvert/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
id("java-library")
id("org.jetbrains.kotlin.jvm")
}

java {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}
Loading