Skip to content

Commit

Permalink
feat: support definitions, transliteration, synonyms for mozhi (closes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Oct 29, 2024
1 parent 0fcd505 commit 8a4b492
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .idea/compiler.xml

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

2 changes: 1 addition & 1 deletion .idea/gradle.xml

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

10 changes: 10 additions & 0 deletions .idea/migrations.xml

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

2 changes: 1 addition & 1 deletion .idea/misc.xml

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

4 changes: 2 additions & 2 deletions app/release/output-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"type": "SINGLE",
"filters": [],
"attributes": [],
"versionCode": 41,
"versionName": "10.0",
"versionCode": 42,
"versionName": "11.0",
"outputFile": "app-release.apk"
}
],
Expand Down
22 changes: 20 additions & 2 deletions app/src/main/java/com/bnyro/translate/api/mh/MhEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.bnyro.translate.api.mh

import com.bnyro.translate.const.ApiKeyState
import com.bnyro.translate.db.obj.Language
import com.bnyro.translate.obj.Definition
import com.bnyro.translate.obj.Translation
import com.bnyro.translate.util.RetrofitHelper
import com.bnyro.translate.util.TranslationEngine
Expand All @@ -45,7 +46,8 @@ class MhEngine : TranslationEngine(
supportsAudio = true
) {
lateinit var api: Mozhi

private val transliterationFailedRegex = Regex("Direction \'\\w{2}\' is not supported")
private val bracketRegex = Regex("[<>]")

override fun createOrRecreate(): TranslationEngine = apply {
api = RetrofitHelper.createApi(this)
Expand All @@ -65,7 +67,23 @@ class MhEngine : TranslationEngine(
)
return Translation(
translatedText = response.translatedText,
detectedLanguage = response.detectedLanguage
transliterations = listOf(
response.sourceTransliteration,
response.targetTransliteration
).filter {
it.isNotBlank() && !it.matches(transliterationFailedRegex)
},
detectedLanguage = response.detectedLanguage.takeIf { !it.isNullOrBlank() },
definitions = response.wordChoices.orEmpty().map { definition ->
Definition(
definition = definition.definition.takeIf { it.isNotBlank() },
example = definition.example.takeIf { it.isNotBlank() }
)
},
similar = response.targetSynonyms,
examples = response.wordChoices.orEmpty()
.flatMap { it.examplesSource.orEmpty() + it.examplesTarget.orEmpty() }
.map { it.replace(bracketRegex, "") }
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@

package com.bnyro.translate.api.mh.obj

import com.bnyro.translate.api.st.obj.STDefinition
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class MhTranslationResponse (
@SerialName("definitions")
val definitions: STDefinition? = null,
data class MhTranslationResponse(
@SerialName("pronunciation")
val pronunciation: String? = null,
@SerialName("detected")
val detectedLanguage: String? = null,
@SerialName("translated-text")
val translatedText: String = ""
)
val translatedText: String = "",
@SerialName("source_synonyms") val sourceSynonyms: List<String>?,
@SerialName("source_transliteration") val sourceTransliteration: String,
@SerialName("target_synonyms") val targetSynonyms: List<String>?,
@SerialName("target_transliteration") val targetTransliteration: String,
@SerialName("word_choices") val wordChoices: List<WordChoice>? = null
)
30 changes: 30 additions & 0 deletions app/src/main/java/com/bnyro/translate/api/mh/obj/WordChoice.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2024 You Apps
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.bnyro.translate.api.mh.obj

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class WordChoice(
val definition: String,
val example: String,
val word: String,
@SerialName("examples_source") val examplesSource: List<String>? = null,
@SerialName("examples_target") val examplesTarget: List<String>? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fun AdditionalInfoComponent(
AdditionalInfo(
title = stringResource(R.string.definition),
text = listOfNotNull(it.type, it.definition, it.example, it.synonym).joinToString(
", "
" "
)
)
}
Expand Down

0 comments on commit 8a4b492

Please sign in to comment.