Smart English-Chinese Dictionary With Python
Smart English-Chinese Dictionary With Python
Introduction:
This report proposes a smart and interactive English-Chinese dictionary by using python
programming language. As we all know language dictionary is a very useful tool for a
student. This dictionary was realized by a python source code and this source code have
three parts. The first part contains the code for importing different libraries, opening and
extracting the given text file, “words.txt”, which contains English words with their
respective Chinese meaning and mainly defining different functions. In this part there are
four main methods. They are the search functions, the select functions, the graph
functions and the tryanother functions. The second part is counting the English letters
from the list which only contains English words and plotting the frequency bar graph for
each English letter. The third part is the graphical unit interface (GUI) part of the
program. It was built by using the Tkinter graphical module of python.
Dictionary is a book or electronic resource that lists the words of a language (typically in
alphabetical order) and gives their meaning, or gives the equivalent words in a different
language, often also providing information about pronunciation, origin, and usage. So, this
dictionary operates according to this definition. The basic function of this project is to take
an Input letters from the user and return the matched words list in a list box and matched
words count in a given box. And then after the User select a word from the list box it returns
the word class and Chinese counterpart of the selected word. The first extended function is
that when a user enters Chinese word the dictionary returns all possible matched English
words list. And the second extended function is Counting the number of occurrences of 26
letters of English in the dictionary and draw the occurrences with bar graph. The algorithms
of the project and the methods of realizing this project’s function are explained bellow.
1. Importing libraries:
Four specific python modules were used in this project for different purposes. Those
imported modules are matplotlib.pyplot, NumPy, collections and Tkinter libraries. Tkinter
and collections libraries are built in libraries. But for the matplotlib.pyplot and NumPy
libraries we have install it into our system. They are shown below.
3. Defining functions
In Python, a function is a group of related statements that performs a specific task.
Functions help break our program into smaller and modular chunks. As our program
grows larger and larger, functions make it more organized and manageable. Furthermore,
it avoids repetition and makes the code reusable. In this project search (), select (), graph
() and anotherword () functions were defined. Each function have its own respective use
and they are explained one by one below.
I. Search ()
This function was created, as the name indicates, to find a match for an input word from
a user and to list the matched words in the list box (matches). This function also counts
the matched words and returns the counted number in the entry box (word5). The first
thing that happens when this function is called is that, all the elements in the list box and
in the entry box will be deleted in order to avoid the repetition and also it is because we
want an empty box in order to display a list and counted number of the new word in them.
We can search for any word repeatedly, once we run the program. The second thing that
happens is checking if the input word is Chinese or English word by if condition. So, if the
input letter or word is an element of an English string (words), then by using for loop and
if condition it compares the word with the English word list (lenglish) using re.match()
function. And then if there is a matched word, it returns the them in the list box and also
returns the number of matched words in the entry box (word5). The string (words) was
created by converting the English word list (lenglish) into a string with the code shown in
the figure below.
Fig 6: the result when there is no selected word and hitting the select button
The function gets a value from the button click by using curselection() function. curselection()
Return the indices of currently selected item. The select () function only works if there is a
highlighted word in the list box. But there is highlighted word in the list box, when the button is
clicked it displays “no words were selected” as shown in the fig 6. It was realized using the if
condition.
III. Anotherword ()
This function was created with only one aim. The purpose is to clear everything and making the
program as ready as the first time. The code is shown in fig 7.
This function was created to plot a bar graph for the frequency of the 26 English
alphabets. So, the first thing to do should be counting the value of each letters in the
English words string (words), which we created by converting the list “lenglish” into a
string for the counting and checking purpose which we used in the search () function.
Then the graph was realized by using the letters as y-axis label using arange() function
and the counting value as x-axis value. The code is shown below in fig 8.