#!/usr/local/bin/perl -w
use strict;
use CGI;
my $query = new CGI;
my $dbroot = "/var/www/html/lextypedb/db";
my $cssdir = "/lextypedb";
my $cgidir = "/cgi-bin/lextypedb_tools";
my $charset = "utf-8";
if(-e "params"){
    open(PARAM, "params");
    while(){
	chomp;
	(my $para, my $val) = split /=/;#/
	if($para eq "dbroot"){
	    $dbroot = $val;
	}elsif($para eq "charset"){
	    $charset = $val;
	}elsif($para eq "cssdir"){
	    $cssdir = $val;
	}elsif($para eq "cgidir"){
	    $cgidir = $val;
       }
    }
    open(PARAM, "params");
}
## only show $limit sentences
my $limit = 50;
#Receive params.
my $lextype = $query->param("lextype");
my $wid = $query->param("wid");
my $orth = $query->param("orth");
#Retrieve all sentences that contain a word
#whose lextype is $lextype and wid is $wid.
use DBI;
my $treebank_table = "treebank_tbl";
my $dbname = $dbroot."/"."lt.db";
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", "", "", {AutoCommit => 0});
#First, retrieve all the sentence ids.
my $sth = $dbh->prepare(
			"select sid from $treebank_table where lextype=? and wordid=? limit $limit"
			);
$sth->execute($lextype,$wid);
my @sid_a;
while(my @row = $sth->fetchrow_array){
    push(@sid_a, $row[0]);
}
#Next, retrieve the sentence itself for each sentence id.
$sth = $dbh->prepare(
		     "select wordid,orth from $treebank_table where sid=?"
		     );
my $total = 0;
my %all_sentences;
foreach my $sid (@sid_a){
    my @sentence = ();
    $sth->execute($sid);
     while(my @row = $sth->fetchrow_array){
 	my $orth = $row[1];
 	if ($row[0] eq $wid) {
 	    push  @sentence, "
| Sentences | ex | 
|---|---|
| $all_sentences{$sid}"; $out .= " | $sid | 
$mes