Saturday, 3 June 2023

A simple NLP query system for Car_Database

Introduction

The primary aim for this post is to present a simple NLP query system for Car_Database. Users can type in simple questions such as those shown below to consult the database:

  • help
  • what do you know?
  • show me all dealers.
  • list all brands.
  • what models do you know?
  • how many customers are there?
  • get all first names, last names and phone numbers.

Database Source: https://github.com/dtaivpp/car_company_database

After getting input from the user, the system will process the sentence in the following ways:

  1. Transfer the sentence into tokens of words, some punctuation marks are skipped.
  2. Use simplification rules to convert the list of tokens into a simple sentence, which is also in form of a list of words.
  3. Apply translation rules to convert the simple sentence into a command consisting of keywords.
  4. If the command is a SQL command, further translate it into a formal SQL statement.
  5. Execute the command, call Sqlite if necessary to retrieve data from Car database with the SQL statement built before.

In this post the engine can only generate simple SELECT and SELECT COUNT statements. Furthermore, only single table query can be preformed, that is queries involving relations between multiple tables cannot be handled yet.


Output

Source Code

There are two prolog files for the system: nlp01.pl holds the simplification and translation rules, while buildsql01.pl responsible for building and executing the SQL statements from the commands.

nlp01.pl



buildsql01.pl



The NLP system has a batch mode which accepts a list of questions from an external file and answer them one by one. You may activate the batch mode as shown below:

# swipl nlp01.pl
Enter a query: 
|: quit
[quit]
[quit]
---
Bye.
---

?- go2.
Enter a file name: 
|: carQuery.txt

The file named carQuery.txt is the external file which contains a list of queries to be sent to the system, its content can be found below.



The result of running the batch mode with the above file is given below:

No comments:

Post a Comment