NUROP – CONERTING OF SENTENCES TO SQL
NUROP CONGRESS PAPER
CONVERTING OF SENTENCES TO SQL
NG CHEW YEN AND LUA KIM TENG1
School of Computing, National University of Singapore
3 Science Drive 2, Singapore 117543
ABSTRACT
This report surveys the subject of converting Sentences to SQL. A
prototype system has been developed to convert sentences to SQL. The
system includes a program that first converts the natural language
sentences to templates, and then to SQL, and based on the templates, it
queries the database. Due to time and resources constraints, we only
focus on queries related to insurance in this research. The design criteria
and the implementation details are presented in this report.
1
INTRODUCTION
The objective of this convert SQL to Sentences report is to demonstrate the application of
human language translation to computer language. We also aim to introduce the fundamentals of
Natural Language Processing, focusing on developing a simple Sentence to SQL protocol to
illustrate our stand.
2
PROJECT OVERVIEW
For this project, I will be using templates to classify the different phrases of a sentence. This
classification will be done by the Parser. It processes the user input, and by looking at each
phrase, according to the Most Number of Words Get Process First Logic2, it allocates each
phrase, existing in the database with a word class. After which, a template with all the word
classes will be generated by the Parser. Next, I use the Query Processor, which will retrieve the
correct SQL template, and query the database based on the SQL template. Upon getting the
result from the database, my Response Manager will then retrieve the appropriate response
template, and use it to generate an output for the user.
1
2
Student and Supervisor
Most Number of Words Get Process First Logic is whereby the system will class phrases with the most number of words in it
first, followed by lesser number of words. This is to ensure that words have a higher chance of being taken as a complete
phrase. For example, “insurance” and “insurance plan”, “insurance plan” will be classed first.
1
NUROP – CONERTING OF SENTENCES TO SQL
What insurance
have I bought?
USER
User input
Query
System
identifies
keywords and
allocates word
class to the
sentence
Parser
Query
Processor
System retrieves
SQL Template
based on word
class
Output to
Query
Retrieves response Template and
reply the user
Response
Manager
Figure 1. General system architecture of the project.
3
CONTRIBUTION OF MY PROJECT
My project’s main contribution is to be able to convert sentences to SQL using templates. Why
templates? Templates are actually a collection of facts and rules. It is more organized than rules.
However, it is not easy to implement my system using templates, because I would need a lot of
data in order to get many templates, so that the system can handle many types of queries. In fact,
I make use of templates for the entire design of my system. There are word class templates, SQL
templates, and even response templates, which are designed specially for the queries.
Secondly, as mentioned in section 1.4, my parser would process the sentence using a Most
Number of Words Get Process First Logic; this ensures that in the allocation of word classes,
priority is given to phrases of keywords with more number of words, rather than just single
keywords. Higher accuracy is achieved when the parser process by phrases, rather than by
single words because there are just too many words that can make up a phrase. Another
advantage of phrase is that there is more meaning when words are formed in a phrase. A word
by itself can have too many meanings, and will only serve to complicate matters. For my system
solution, I aim to attain the result using an algorithm as simple as possible. To look at my
algorithm, please see section 3.3.1.
Another of my contribution is that I only focus on the keywords of the sentences. All noisy
words are ignored. And the keywords can be in any order, as long as they have the same word
classes, the results will be the same. For more information of this, please see section 3.3 for the
Program Design.
2
NUROP – CONERTING OF SENTENCES TO SQL
4
SYSTEM DESIGN
4.1
DESIGN OBJECTIVES
My objective in this project is as below:
• Using Natural Language Processing & Template Based concept to design
“Convert Sentence to SQL” architecture.
• To demonstrate parsing of a sentence into its word class templates.
• To demonstrate the generation of SQL Query based on the word class template.
• To demonstrate the answering of the query asked by the user, based on the
template.
• To demonstrate querying of sample database information using simple English
sentence, with questions related to Insurance.
4.2
PROGRAM DESIGN
The system I designed is basically divided into 3 parts: Parser, Query Processor and the
Response Manager. We will discuss these 3 parts individually. Please refer to Figure 1 for the
overall system architecture.
4.2.1 PARSER
My Parser’s main role is to classify the sentence query into a template. The main tasks
performed by the Parser are to:
• Retrieve the Dictionary Table from Database
• Determine the meaning of the input text
• Form a word class template
The pseudo code designed for Parser is as follow:
For (all words in the sentence)
{
if (word phrase exists in Dictionary Table)
map the word class in the Dictionary Table to the word phrase
if (word phrase with different word class exist in Dictionary Table)
if (prob * usagecount of different word class > current word class)
replace word class with the current word class
}
return word class, arranged in same order as its word phrase.
4.2.2 QUERY PROCESSOR
The Query Processor continues the task from the Parser. Its main role is to query the database.
The tasks that the Query Processor performs are to:
• Get SQL Template
• Form Correct SQL Query
3
NUROP – CONERTING OF SENTENCES TO SQL
•
Query the Database
The pseudo code below is designed for the Query Processor:
if word class templates exist in SQL Template Table
{
get the correct SQL Template
replace SQL Template variables with the phrases from the sentence
query the database
}
else return error.
4.2.3 RESPONSE MANAGER
Response Manager’s role is to reply appropriately to the user’s query. I have designed a
response template table in the database, such that for each type of query, there is a response
template that matches it.
The following pseudo code is designed for the Response Manager:
if SQL templates exist in Response Template Table
{
get the correct Response Template
replace Response Template variables with the phrases from the sentence
Sends the result to the user
}
else return error.
5
CONCLUSION
My system to convert sentences to SQL is definitely not the first one available, and would not
be the last. There are continuous research efforts in this field, to attain better, faster methods of
converting sentences to SQL. For mine, I choose to use templates. It makes things more
organized, all types of sentences are structured into a template form. However, one disadvantage
is it does not have the learning ability to learn new templates. If given more time, I would
definitely like to pursue further research in this field, adding a learning ability in my system.
6
REFERENCE
[1]
[2]
[3]
[4]
[5]
AIA www.aia.com.sg
NTUC Income Insurance www.income.com.sg
Androut Sopolos, Natural Language Interface to Databases – An introduction
Edgar Jasper, Query Translation in Heterogeneous Database Environments
Susan W. McRoy, Songsak Channarukul, & Syed S. Ali – YAG: A Template Based
Generator for Real-Time Systems
[6] Joseph Mayo, C# Unleashed
[7] Travis Vandersypen, ADO Net Unleashed
[8] MSDN Library www.msdn.microsoft.com
4
© Copyright 2026 Paperzz