update DictionaryService to use englishName, refine ExampleSentenceRequest prompt, and fix example sentence generation logic in VocabularyViewModel

This commit is contained in:
jonasgaudian
2026-02-14 01:12:10 +01:00
parent fdce6ba500
commit b65e16000c
3 changed files with 28 additions and 19 deletions

View File

@@ -109,10 +109,19 @@ class ExampleSentenceRequest(
override val requiredFields = listOf("word", "sourceSentence", "targetSentence")
init {
promptBuilder.basePrompt = "Provide one short, simple and clear example sentence for the word '$word' in $languageFirst and fully translate the sentence to $languageSecond, using $wordTranslation as a translation."
addDetail("Structure: { 'word': string, 'sourceSentence': string, 'targetSentence': string }.")
addDetail("Only include the fields above. Keep sentences concise and clear. Do not include any explanations or additional text.")
withJsonResponse("a JSON object with 'word' (the original word), 'sourceSentence' (the example sentence in the source language), and 'targetSentence' (the translation in the target language). Ensure all values are properly quoted strings.")
promptBuilder.basePrompt = """
Task: Create a short, concise natural example sentence in $languageFirst for the word '$word'.
Rules:
1. The 'sourceSentence' must be entirely in $languageFirst.
2. Do NOT use the word '$wordTranslation' in the 'sourceSentence'.
3. The 'targetSentence' must be the $languageSecond translation of the 'sourceSentence'.
4. In the 'targetSentence', use the word '$wordTranslation'.
""".trimIndent()
addDetail("Constraint: Ensure 'sourceSentence' contains ONLY $languageFirst and 'targetSentence' contains ONLY $languageSecond.")
addDetail("Structure: { 'word': '$word', 'sourceSentence': string, 'targetSentence': string }.")
withJsonResponse("a JSON object. Ensure no mixed-language sentences occur.")
}
}

View File

@@ -90,14 +90,14 @@ class DictionaryService(context: Context) {
@OptIn(ExperimentalTime::class)
suspend fun searchDefinition(query: String, language: Language): Result<DictionaryEntry> = withContext(Dispatchers.IO) {
try {
Log.i("DictionaryService", "Searching definition for word: $query in language: ${language.name}")
Log.i("DictionaryService", "Searching definition for word: $query in language: ${language.englishName}")
val requestedParts = getActivatedDictionaryOptions()
Log.d("DictionaryService", "Requested dictionary parts: $requestedParts")
val template = DictionaryDefinitionRequest(
word = query,
language = language.name,
language = language.englishName,
requestedParts = requestedParts
)
@@ -110,7 +110,7 @@ class DictionaryService(context: Context) {
word = apiResponse.word,
definition = apiResponse.parts,
languageCode = language.nameResId,
languageName = language.name,
languageName = language.englishName,
createdAt = Clock.System.now()
)
}.onFailure { exception ->
@@ -127,13 +127,13 @@ class DictionaryService(context: Context) {
*/
suspend fun getExampleSentence(word: String, wordTranslation: String, languageFirst: Language, languageSecond: Language): Result<Pair<String, String>> = withContext(Dispatchers.IO) {
try {
Log.i("DictionaryService", "Getting example sentence for word: $word (${languageFirst.name} -> ${languageSecond.name})")
Log.i("DictionaryService", "Getting example sentence for word: $word (${languageFirst.englishName} -> ${languageSecond.englishName})")
val template = ExampleSentenceRequest(
word = word,
wordTranslation = wordTranslation,
languageFirst = languageFirst.name,
languageSecond = languageSecond.name
languageFirst = languageFirst.englishName,
languageSecond = languageSecond.englishName
)
val result = apiRequestHandler.executeRequest(template)
@@ -179,7 +179,7 @@ class DictionaryService(context: Context) {
}
}
Log.i("DictionaryService", "Generating new word of the day for: $todayString in language: ${language.name}")
Log.i("DictionaryService", "Generating new word of the day for: $todayString in language: ${language.englishName}")
val topics = listOf(
"science", "literature", "history", "technology", "nature",
@@ -192,7 +192,7 @@ class DictionaryService(context: Context) {
Log.d("DictionaryService", "Selected topic for word of the day: $randomTopic")
val template = WordOfTheDayRequest(
language = language.name,
language = language.englishName,
category = randomTopic
)
@@ -205,7 +205,7 @@ class DictionaryService(context: Context) {
word = apiResponse.word,
definition = apiResponse.parts,
languageCode = language.nameResId,
languageName = language.name,
languageName = language.englishName,
createdAt = today
)
dictionaryRepository.saveWordOfTheDay(newEntry)
@@ -225,7 +225,7 @@ class DictionaryService(context: Context) {
suspend fun getEtymology(query: String, language: Language): Result<EtymologyData> = withContext(Dispatchers.IO) {
try {
Log.i("DictionaryService", "Getting etymology for word: $query in language: ${language.name}")
Log.i("DictionaryService", "Getting etymology for word: $query in language: ${language.englishName}")
val template = EtymologyRequest(
word = query,

View File

@@ -986,15 +986,15 @@ class VocabularyViewModel @Inject constructor(
}
suspend fun getExampleForItem(itemId: Int, isFirstWord: Boolean, languageFirst: Language?, languageSecond: Language?): Pair<String, String>? {
Log.d(TAG, "Fetching example for item ID $itemId")
val item = getVocabularyItemById(itemId) ?: return null
val word = if (isFirstWord) item.wordFirst else item.wordSecond
val wordTranslation = if (!isFirstWord) item.wordFirst else item.wordSecond
if (languageFirst == null || languageSecond == null) return null
return dictionaryService.getExampleSentence(word, wordTranslation, languageFirst, languageSecond).getOrNull()
return if (isFirstWord) {
dictionaryService.getExampleSentence(item.wordFirst , item.wordSecond, languageFirst, languageSecond).getOrNull()
} else {
dictionaryService.getExampleSentence(item.wordSecond, item.wordFirst , languageSecond, languageFirst).getOrNull()
}
}
suspend fun fetchAndUpdateZipfFrequency(item: VocabularyItem): VocabularyItem {