Skip to content

Add Wiktionary word definition skill#379

Open
tylxr59 wants to merge 2 commits intoStypox:masterfrom
tylxr59:add-definition-skill
Open

Add Wiktionary word definition skill#379
tylxr59 wants to merge 2 commits intoStypox:masterfrom
tylxr59:add-definition-skill

Conversation

@tylxr59
Copy link
Contributor

@tylxr59 tylxr59 commented Dec 16, 2025

tmp-1765850672600

Another quick skill addition. Uses the Wiktionary API to get word definitions and supports using the user's language to return appropriate definitions. It only reads out the first definition of the word to keep any spoken interaction short.

Copy link
Owner

@Stypox Stypox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Looks good!

An improvement for a separate PR could be to somehow report the source of the data for all skills that rely on external services, right now it's very not transparent.

Comment on lines +3 to +4
- (what does|what is|what s|whats) .word. mean|means?
- (what is|what s|whats) the? (definition|meaning) (of|for) .word.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is likely not what you meant. | is not like in Regex, where it is the lowest precedence operator. "what is|what s" means either "what is s" or "what what s". See https://github.com/Stypox/dicio-sentences-compiler.

Comment on lines +25 to +26
// Get language code from locale (e.g., "en", "fr", "de")
val languageCode = ctx.locale.language.lowercase(Locale.getDefault())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +38 to +40
} catch (e: IOException) {
// Network error
DefinitionOutput.NetworkError(word = word)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just let this fall through and be handled by the skill evaluator (it will display a nice error message for the network error)

val definitionData = ConnectionUtils.getPageJson(apiUrl)
parseDefinitions(word, definitionData)
} catch (e: FileNotFoundException) {
// 404 - word not found in Wiktionary
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the $languageCode is not supported? Can the two errors be distinguished? Saying that the definition was not found on any 404 would be wrong.

val languageCode = ctx.locale.language.lowercase(Locale.getDefault())

// Build Wiktionary API URL based on user's locale
val apiUrl = "https://$languageCode.wiktionary.org/api/rest_v1/page/definition/" +
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know there aren't many other tests, but it might be worth writing a test that makes a request to this URL for every possible language supported by Dicio with some known word in that language (e.g. extracted from strings.xml), and checks whether the definition gets resolved correctly.

Comment on lines +104 to +106
} catch (e: JSONException) {
return DefinitionOutput.ParseError(word = word)
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmmh you have double try-catch. Remove this one and let it be handled in the other try. I'm also wondering whether the JSONException should just be let fall through to the skill evaluator so that it shows a Report button (it should not happen in any case and showing "Could not understand the definition for ..." would not help the user more than showing "An error occurred [throwable message] [report button]".

}
}

private fun parseDefinitions(word: String, data: JSONObject): SkillOutput {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might also be worth writing a test that loads "https://$languageCode.wiktionary.org/api/rest_v1/page/definition/..." and makes sure it is parsed as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants