~ubuntu-branches/ubuntu/trusty/horae/trusty

« back to all changes in this revision

Viewing changes to 0CPAN/Parse-RecDescent-1.94/demo/demo_NL2SQL

  • Committer: Bazaar Package Importer
  • Author(s): Carlo Segre
  • Date: 2008-02-23 23:13:02 UTC
  • mfrom: (2.1.2 hardy)
  • Revision ID: james.westby@ubuntu.com-20080223231302-mnyyxs3icvrus4ke
Tags: 066-3
Apply patch to athena_parts/misc.pl for compatibility with 
perl-tk 804.28.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/local/bin/perl -ws
2
 
 
3
 
use Parse::RecDescent;
4
 
 
5
 
 
6
 
my $grammar = <<'EOGRAMMAR';
7
 
 
8
 
translate:      select
9
 
         |      sum
10
 
         |      identify
11
 
         |                      { "Could you rephase that?\n" }
12
 
 
13
 
select:         ask_select qualifier(?) field /of/ qualifier(?) table
14
 
                                { "SELECT DISTINCT $item[3]\nFROM $item[6]\n" }
15
 
              | ask_select qualifier(?) table /'?/ qualifier(?) field
16
 
                                { "SELECT DISTINCT $item[6]\nFROM $item[3]\n" }
17
 
              | ask_select qualifier(?) table 
18
 
                                { "SELECT *\nFROM $item[3]\n" }
19
 
 
20
 
sum:            ask_count table prep qualifier field prep(?) value
21
 
                                { "SELECT COUNT(*)\nFROM $item[2]\n" .
22
 
                                  "WHERE $item[5] = $item[7]\n" }
23
 
   |            ask_count table 
24
 
                                { "SELECT COUNT(*)\nFROM $item[2]\n" }
25
 
 
26
 
identify:       ask_select(?) /who supplies/ qualifier value
27
 
                                { "SELECT supplier\nFROM merchandise\n" .
28
 
                                  "WHERE name = $item[4]\n" }
29
 
 
30
 
        |       whats qualifier field /of/ qualifier /suppliers? of/ value
31
 
                                { "SELECT suppliers.$item[3]\n" .
32
 
                                  "FROM suppliers, products\n" .
33
 
                                  "WHERE products.name = $item[-1]\n" .
34
 
                                  "AND suppliers.name = products.supplier \n" }
35
 
 
36
 
 
37
 
 
38
 
 
39
 
field:          /(name)s?/                      { $1 }
40
 
     |          /(product)s?/                   { $1 }
41
 
     |          /(id)(entit(y|ies))?/           { $1 }
42
 
     |          /(quantit(y|ies))/              { $1 }
43
 
     |          /(received)s?/                  { $1 }
44
 
     |          /(supplier)s?/                  { $1 }
45
 
     |          /(cost)s?/                      { $1 }
46
 
     |          /(address)(es)?/                { $1 }
47
 
 
48
 
table:          /suppliers?/                    { 'suppliers' }
49
 
     |          'merchandise'
50
 
     |          /orders?/                       { 'orders' }
51
 
 
52
 
qualifier:      /the|every|all( the)?|any|our/
53
 
 
54
 
ask_select:     reply to_me
55
 
 
56
 
ask_count:      ask_select(?) /how (many|much)/
57
 
 
58
 
reply:          /tell|show|list/
59
 
 
60
 
value:          /\w+/                           { qq{'$item[1]'} }
61
 
 
62
 
to_me:          /((to )?(me|us))?/
63
 
 
64
 
whats:          /what's|what (is|are)/
65
 
 
66
 
prep:           /for|of|with|by/
67
 
 
68
 
EOGRAMMAR
69
 
 
70
 
my $parser = Parse::RecDescent->new($grammar) 
71
 
        or die "Bad grammar";
72
 
 
73
 
$| = 1;
74
 
while (<DATA>)
75
 
{
76
 
        print "> ";
77
 
        sleep 1;
78
 
        print; <>;
79
 
        my $SQL = $parser->translate($_);
80
 
        print $SQL, "\n";
81
 
}
82
 
 
83
 
__DATA__
84
 
how many orders for the product spam are there?
85
 
tell me how many suppliers by the name of Jones we have
86
 
what are the names of our suppliers of trinitrotoluene?
87
 
tell me our suppliers' names
88
 
list our merchandise
89
 
show us the suppliers of the mechanise
90
 
list all supplier names
91
 
how many orders are there?
92
 
how much merchandise do we carry?
93
 
who supplies our nitrocelluose?
94
 
tell me who supplies our ethylacetate?
95
 
what are the addresses of our suppliers of trinitrotoluene?