0% found this document useful (0 votes)
131 views

Smart English-Chinese Dictionary With Python

This document summarizes a smart English-Chinese dictionary created using Python. The dictionary contains English words and their Chinese meanings extracted from a text file. It has functions like searching for words by English or Chinese input, selecting a word to display its details, and plotting a bar graph of letter frequencies in the dictionary. The dictionary's GUI was built using Tkinter. Key functions and algorithms are explained, including importing libraries, opening the text file, creating dictionaries and lists, and defining functions for searching, selecting words, plotting graphs, and restarting the program.

Uploaded by

Yonatan Yakob
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views

Smart English-Chinese Dictionary With Python

This document summarizes a smart English-Chinese dictionary created using Python. The dictionary contains English words and their Chinese meanings extracted from a text file. It has functions like searching for words by English or Chinese input, selecting a word to display its details, and plotting a bar graph of letter frequencies in the dictionary. The dictionary's GUI was built using Tkinter. Key functions and algorithms are explained, including importing libraries, opening the text file, creating dictionaries and lists, and defining functions for searching, selecting words, plotting graphs, and restarting the program.

Uploaded by

Yonatan Yakob
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Department of mechanical and electrical engineering, university of electronic science and

technology of china, china, E-mail: [email protected].

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.

Fig 1: imported modules


matplotlib.pyplot is a collection of command style functions that make matplotlib work
like MATLAB. Each pyplot function makes some change to a figure: e.g., creates a figure,
creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot
with labels, etc. In matplotlib.pyplot various states are preserved across function calls, so
that it keeps track of things like the current figure and plotting area, and the plotting
functions are directed to the current axes. This package was used to plot the frequency
bar graph for the 26 English letters by counting them from the string which only contains
English words (words). It is used along with NumPy to provide an environment that is an
effective open source alternative for MATLAB. NumPy, or Numeric Python, is a basic
Python package that provides an alternative to a regular Python list, a Numpy n-
dimensional homogeneous array. A list is a very useful tool offered by Python, as it lets
you store values of different types at once, and perform numerous operations on it. But
it has certain limitations, and some operations on list are not efficient. NumPy was
imported to use NumPy arange(). It is one of the array creation routines based on
numerical ranges. It creates an instance of ndarray with evenly spaced values and returns
the reference to it. It is possible to define the interval of the values contained in an array,
space between them, and their type with four parameters of arange(). So, it was used in
order to label the 26 alphabets next to the bar graph.
The Tkinter module (“Tk interface”) is the standard Python interface to the Tk GUI toolkit
from Scriptics. Tkinter consists of a number of modules. The Tk interface is provided by a
binary extension module named Tkinter. This module contains the low-level interface to
Tk, and should never be used directly by application programmers. It is usually a shared
library (or DLL), but might in some cases be statically linked with the Python interpreter.
The module was imported for the purpose of making the project to be interactive and
graphical. The created window includes 4 buttons, 5 entry boxes and 1 list box with a
scrollbar.
Collections in Python are containers that are used to store collections of data, for
example, list, dictionary, set, tuple etc. These are built-in collections. Several modules
have been developed that provide additional data structures to store collections of data.
One such module is the Python collections module. Collection module was used to import
and use the defaultdict library. The purpose of importing the defaultdict is to count each
alphabet’s frequency.

2. Opening the txt file and creating dictionaries and lists


There is a txt file “word.txt”. the first thing to was opening it. After opening this file, it was
split into “eng” (English) and “other”, since there was space between them. And it was
achieved by using “line.split(' ',1)” function. After that the “other” was split into “wc ”
(word class) and chin (Chinese). This was achieved by using “other.split('.',1)” function in
a for loop because there was many words and it is a repetitive process. The “words.txt”
text file was edited and a dot (.) mark was putted between the word class and the Chinese
words After that three dictionaries were created. They are “d”, “e” and “f”. in the “d”
dictionary English words are key and Chinese words are values. In the “e” dictionary
English word is key and word class is a value. And in “f” dictionary Chinese words as a key
and English words as a value.

Fig 2: importing the word file


Then the English list (lenglish) and Chinese list (gchinese) was created as shown in the
above figure.

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 3: list- string convertor

Fig 4: the search () function


But, if the input word was Chinese, then by using re.search () function it finds a similar
word from the Chinese words lists (gchinese). Then it returns the list of English counter
parts from Chinese-English dictionary (“f” dictionary) in the list box. And also returns the
number of matched English words.
II. Select ()
This function is created to select a word among those which are listed in the list box and to display
the basic function of this project, which is displaying the selected English word, the word class and
the Chinese word in the entry boxes word2, word3 and word4 respectively. This function starts
by removing any existing element from those entry boxes. The reason is, to be able to do a
repetitive selection. We can select a word more than one time. It is possible to select and get the
meaning of all the listed words one after the other. This function is activated by touching the
select button on the window.
Fig 5: the select () function

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.

Fig 7: anotherword () function


IV. Graph ()

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.

Fig 8: the graph function


After getting the values of each letters occurrence, by using the functions form the
imported modules, matplotlib.pyplot and NumPy, it plots the bar graph.

4. Creating interactive GUI window


As mentioned before this graphical window was built by using the Tkinter module. It is
commonly used because, Tkinter module is relatively easy and it is very flexible to change,
can easily be adjusted. A window was created by using the codes in the fig 9.

Fig 9: creating the window


The created window includes 4 buttons, 5 entry boxes and 1 list box. Those buttons are
search, select, another word and graph as shown in figure below, fig 10.
Fig 10: the initial window that pops up

Each button has its own respective command.

• Search button is linked with the search () function.


• Select button is associated with the select () function.
• Clear button is associated with the anotherword () function.
• The graph button is associated with the graph button.
Scrollbar was also added to the window frame which contain the list box. The bellow
three-line code were added to the program to get the scrollbar.

Fig 10: scrollbar configuration


5. Important pictures of the project

Fig 11: basic function of the program


Fig 12: extended function of the project
Fig 13: the bar graph

The source code is attached below.

You might also like