I transformed my Raspberry Pi into an enhanced English dictionary server.

Raspberry Pi is a mini-computer, which is sized like a credit card. It's pretty cheap compared to other computers. It's great to learn computing, practice programming and make beginner-level projects.

I have been seeking a project idea to build and improve my skills. Yesterday, an idea emerged. A dictionary, but it's different from the others. It is a telegram chatbot that contains all words, idioms, and phrases in English. A telegram chatbot like this is what I needed. I wanted two things in the response of my dictionary. Definitions of the word and sentences written using them.

I searched for Python English dictionary libraries that can be used in my project. I found the PyDictionary library useful. It was easy to use and free. Here is an example of how to use it:

from PyDictionary import PyDictionary

'There can be any number of words in the Instance'

print(dictionary.printMeanings()) '''This print the meanings of all the words'''
print(dictionary.getMeanings()) '''This will return meanings as dictionaries'''
print (dictionary.getSynonyms())

It easily looks up words and prints their definitions and sentences.

But I can't say, "Let's call it a day." This library does not contain all words, phrases, and idioms. In this library, there are not all words, phrases and idioms. I need all of them, so I researched on the internet to find a free advanced dictionary API. I found a website named Wiktionary, which is a Wikipedia-like website for languages. I also saw that it has a Python library called wiktionaryparser. I looked through the documentation.

JSON structure (in response)

[{ "pronunciations": { "text": ["pronunciation text"], "audio": ["pronunciation audio"] }, "definitions": [{ "relatedWords": [{ "relationshipType": "word relationship type", "words": ["list of related words"] }], "text": ["list of definitions"], "partOfSpeech": "part of speech", "examples": ["list of examples"] }], "etymology": "etymology text", }]

Here is a sample for you to use.

>>> from wiktionaryparser import WiktionaryParser 
>>> parser = WiktionaryParser()
>>> word = parser.fetch('attorney')
>>> parser.set_default_language('English')
>>> parser.exclude_part_of_speech('noun')
>>> parser.include_relation('alternative forms')

The wiktionaryparser library was great for my project, but there is a problem. The problem was that it almost had no example sentences. So, I thought, how do I create sentences from a word?

I decided to use OpenAI's GPT-3.5-Turbo API. I researched how to use it in Python. Then, I coded:

import openai
openai.api_key = "sk-xxxx"
response = openai.ChatCompletion.create(
        {"role": "system", "content": "You are a dictionary bot"},
        {"role": "user", "content": "Define attorney for English Learners"},
result = ""
for choice in response.choices:
    result += choice.message.content

It costs 0.0015 dollars for every 750 words, and it also provides an additional 5 dollars for free. I calculated how many definitions and sentences I can create using the free API. Definitions and example sentences amount to 75 words combined. I estimated that an API request costs me 0.00015 USD. Therefore, 5/0.00015 = 33333, which is quite sufficient.

To use this efficiently, I decided to utilize all the libraries I mentioned earlier. The function operates as follows:

It first attempts to use PyDictionary. If it finds definitions and example sentences, it returns them. If not, it tries using wiktionaryparser to accomplish the same task. If that fails as well, it generates definitions and example sentences using GPT-3.5-Turbo.

A mini database system

I have also conceived a mini text-based database system. If the user requests the definition and example sentences of a word, phrase or idiom that has been previously queried, the script will provide an instant response.

How the function works

At the start of the main function, it searches for the word in the database. If it is not found, it utilizes libraries and the ChatGPT API to generate the desired information. Subsequently, it adds the definitions and example sentences to the database.

Using Telegram API

There are plenty of Telegram chatbot APIs. I selected one of them, TelegramBotAPI, and utilized it.

In the end, I implemented my definitions and example sentences generator function into Telegram.