How I used Gemini API for my English wordbook app

Kei Fujikawa
5 min readSep 5, 2024

--

I updated the English vocabulary app I released this March and submitted it to the Gemini API Competition.

You can check out the submitted app and vote for it from the link below! Please take a look and support!

This competition provided a great opportunity to think about how to effectively use the Gemini API.

In this article, I’d like to introduce how I used the Gemini API in the English vocabulary app built with Flutter.

Features Using Gemini

I used Gemini for three main functions.

Word Suggestions

Originally, this app was designed to function like an English-English dictionary, allowing users to search for and save unknown words. For example, if you didn’t know the word “apple,” you would type “app,” and suggestions like “apple” or “appropriate” would appear.

This feature didn’t require Gemini and was handled using the Datamuse API.

However, when adding a Japanese-English dictionary feature, I realized I needed a Japanese-English dictionary API. I couldn’t find a suitable API, so I decided to use Gemini’s generative AI for this purpose.

As a result, it works as shown in the screenshot below:

Since I often ask generative AI like ChatGPT or Gemini for the meanings of English words, I implemented this feature.

Here’s the function I wrote for the prompt:

Future<String> showPossibleTranslations(String yourMotherLanguageWord) async {
final prompt = '''
Please provide multiple possible translations of the word "$yourMotherLanguageWord" into English, separated by commas, without any spaces after the commas.
For example, if the word you want to look up is "めんどくさい" in Japanese, the output should be as follows.
「tedious,bothersome,tiresome,annoying」
''';
return await _generateContent(prompt) ??
'No response because Gemini API went wrong.';
}

Did you notice the function parameter is `yourMotherLanguageWord`? This means it can handle any language available in language_picker.

Currently, it recognizes the language set when the app is first launched, based on the user’s mother tongue.

It might be difficult to build a feature that can translate from all languages to English using only publicly available dictionary APIs. But with generative AI, it’s easy to handle by simply passing the prompt in that language.

Creating Example Sentences and Multilingual Translations

Similarly, I implemented the feature to create example sentences and their translations (with multilingual support).

Here’s the code:

Stream<String> createExampleSentence(String word, {motherLanguage = 'ja'}) {
final languageName = Language.fromIsoCode(motherLanguage).name;
final prompt = '''
Please provide three example sentences in English using the following word or phrase.

- -
$word
- -

Leave one blank line between each example sentence. Do not include example numbers such as 1, 2, 3 before the sentences. Also, provide a $languageName translation below each example sentence.
If the word has multiple meanings or uses, show different meanings or uses in each example sentence. For instance, if the word can be used as both a verb and a noun, demonstrate each usage in separate sentences. Use English that is understandable at a high school level and create sentences that are commonly used in daily life.
For example, if the word you want to look up is "word," with Japanese translation, the output should be as follows.

- -
The word "hello" is used to greet someone.
「hello」という言葉は誰かに挨拶するために使われます。

The doctor used a medical word that I didn't understand.
医者は、私が理解できない医学用語を使いました。

Please choose your words carefully.
あなたの言葉は慎重に選んでください。
- -
''';
return _generateContentStream(prompt);
}

English Composition Review Feature

I added a review feature where users can practice writing sentences using the saved words. Gemini API helps with correcting these sentences.

Here’s the code:

Future<String> correct(ReviewItem item) async {
final word0 = item.words[0].text;
final word1 = item.words[1].text;
final prompt = '''
You're an English teacher.
Now you're checking the students' sentence.
The sentence conditions are two as follows:
1. Use both "$word0" and "$word1"
2. Grammatically correct

If the sentence meets these two conditions, respond with "Correct👍".
If the sentence doesn't meet the conditions, respond with "Incorrect❌", point out the problems, and provide a corrected version of the sentence.

So, please correct the following sentence.

- -
${item.sentence}
- -

Note that don't be too demanding. Only if the sentence is really incorrect, point out.
''';
final output = await _generateContent(prompt);
return output ?? 'No response because Gemini API went wrong.';
}

Conclusion

I’ve introduced how I used the Gemini API in the English vocabulary app. There might still be room for optimization.

https://play.google.com/store/apps/details?id=jp.kboy.tango&hl=en

Vote for me!

The user voting period lasts until September 30, so please vote for this app via the link below.

--

--