[Artificial intelligence series] python Quepy library learning, pythonquepy

Source: Internet
Author: User
Tags nltk

[Artificial intelligence series] python Quepy library learning, pythonquepy

Article 1

What is Quepy?

Quepy is a Python framework to transform natural language problems in database query language queries. It can easily customize different types of problems for queries in natural languages and databases. Therefore, with very little code, you can build your own system and access your database in natural language.

Quepy currently supports SPARQL and MQL database query languages. We plan to extend it to other database query languages.

Note:

You need to install docopt and NumPy. In addition, you can only type:

Pip install quepy

Here you can get more Installation Details:

Http://quepy.readthedocs.org/en/latest/installation.html

To learn more, you can find the tutorial here:

Http://quepy.readthedocs.org/en/latest/tutorial.html

Complete documentation is provided here:

Http://quepy.readthedocs.org/

 

Article 2

Example

To illustrate what you can do with quepy, we include a sample application through theirSparqlThe endpoint accesses the content of DBpedia.

Here you can try an online example: Online demonstration

Alternatively, you can try this example in the following ways:

Python examples/dbpedia/main. py "Who is Tom Cruise? "

It will output something like this:
Select distinct? X1 WHERE {? X0 rdf: type foaf: Person. ? X0 rdfs: label "Tom Cruise" @ en. ? X0 rdfs: comment? X1 .}
# Output
Thomas Cruise Mapother IV, widely known as Tom Cruise, is...

The conversion from natural language to sparql is completed by using a special regular expression:

person_name = Group(Plus(Pos("NNP")), "person_name")regex = Lemma("who") + Lemma("be") + person_name + Question(Pos("."))

Then the semantic relationship is expressed in a convenient way:

person  =  IsPerson () +  HasKeyword (person_name )definition  =  DefinitionOf (person )

The rest of the conversion is automatically processed by the framework and the sparql is generated:

SELECT  DISTINCT  ?x1  WHERE  {     ?x0  rdf :type  foaf :Person 。    ?x0  rdfs :label  “Tom Cruise” @ en 。    ?x0  rdfs :comment  ?x1 。}

Using a very similar process, you can generate the same issue as the MQL query to obtain:

[{    "/common/topic/description": [{}],    "/type/object/name": "Tom Cruise",    "/type/object/type": "/people/person"}]

 

To demonstrate how to use quepy as the framework of the database's natural language interface, we will build (gradually) A sample application to access DBpedia.

The completed sample application can be tried online: Online demonstration

The completed sample code can be found here: Code

The first step is to select the questions we want to answer with the dbpedia database, and then we will develop a mechanism that can convert them into SPARQL queries.

Selected questions

In our sample application, we will seek to answer the following questions:

Who is<Someone>For example:

  • Who is Tom Cruise?
  • Who is President Obama?

What is<Something>For example:

  • What is a car?
  • What is a Python programming language?

List<Brand><Something>For example:

  • List Microsoft software
  • List Fiat cars
Start a quepy Project

To start a quepy project, you must create a quepy application. In our example, our application is calledDBpediaBy running the application:

$ Quepy. py startapp dbpedia

You will find a folder and some created files. It should be like this:
$ cd dbpedia$ tree ..├── dbpedia│   ├── __init__.py│   ├── parsing.py│   ├── dsl.py│   └── settings.py└── main.py1 directory, 4 files

 

This is the basic structure of each project.

  • Dbpedia/parsing. py: You will define a file that matches a natural language problem and converts it to a regular expression in an abstract semantic representation.
  • Dbpedia/dsl. py: The file in which you will define the database mode domain-specific language. In the case of SPARQL, you will specify the things that normally exist in the ontology, such as the link name.
  • Dbpedia/settings. py: Some configuration files installed.
  • Main. py: This file is an optional kickstart point in which you can have all the code required to interact with the application. If necessary, you can safely delete this file.
Configure the application

First, make sure that you have downloaded the necessary data of the nltk tag. If you do not checkInstallation section.

Edit nowDbpedia/settings. pyAnd add the nltk data pathNLTK_DATAVariable. This file has some other configuration options, but we do not need this example.

Also configureLANGUAGEIn this example, we will usesparql.

Define Regular Expressions

To process regular expressions, quepy uses refo, an awesome library that uses regular expressions as objects. Here you can read

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.