#!/usr/bin/perl
#
# BNCweb: A user-friendly interface to the British National Corpus - Version 4.3
# Copyright (C) 2005-10 Hoffmann/Evert/Lehmann
#
# Updates and information available at http://www.bncweb.info
#
# This file is part of BNCweb.
# 
# BNCweb is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
# 
# BNCweb is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

#
# based on BNCweb Version 2.0:
# © 1996/2004 Lehmann/Hoffmann/Schneider
#

use lib "../lib_files";
use bncLibCQPXML;
$username = $ENV{'REMOTE_USER'};

$thisProg='coll_options.pl';

$ref=$ENV{'HTTP_REFERER'};

use DBI;
# connect to mysql
my $bncHandle = DBI->connect("DBI:mysql:$bwMYSQLtable:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd") or bark("Can't connect to mysql on $bwMYSQLhost at port: $bwMysqlPort <BR>$bncHandle::errstr\n");

unless (getParams($ENV{'QUERY_STRING'})) {
	barkFull("incomplete transmition of URL<BR>This problem is likely to occur in combination with proxy servers<BR>turn off your web-proxy and try again");
}

$theData = $params{theData};

setParam("theData", "$theData");

delete $params{sentBound};

# use thMode to determine subcorpus
#$basis=1;
#$basis=2  if ($params{thMode}=~m/\AM\d+#\d+#\(spowri=1\)/);
#$basis=3  if ($params{thMode}=~m/\AM\d+#\d+#\(spowri=2\)/);

$hit=$params{thMode}=~m/\A(\d+)#T\d#(\d+)/;
if ($hit) {
	$full=$1;
	$thinned=$2;
	$thinning=($thinned / $full);
} else {
	$thinning=1;
}

# determine whether result set is based on metatextual restrictions or a user-defined subcorpus

$subcorpus = &subcorpusCheck($params{thMode});

$basis = "";
$basis = 2 if ($subcorpus eq "(spowri=1)");
$basis = 3 if ($subcorpus eq "(spowri=2)");

# determine whether frequency lists for this subcorpus have been cached.
if ($subcorpus) {
	# first, alphabetically order components to increase likelihood of match
	@subcorpParts = split (/ \& /, $subcorpus);
	$subcorpus=join(' & ', sort { lc($a) cmp lc($b) } @subcorpParts);
	
	$sqlQuery = "SELECT * FROM frequencyLists where subcorpus='$subcorpus'";

	$sth= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
	$sth->execute or bark("Can't execute $sqlQuery: " . $sth::errstr);
	$exists = $sth->rows;
	if ($exists) {
		@rowArray=$sth->fetchrow_array;
		$theName=$rowArray[0];
		setParam("fListCache", "$theName");
				
		
	} else {
		# determine size of subcorpus
		unless (($subcorpus eq "(spowri=1)") || ($subcorpus eq "(spowri=2)") || ($subcorpus eq "no_subcorpus")) {
			$spokenRes++ if ($subcorpus=~m/\((age|sex|class|firstLanguage|education|role|dialect)\=/);
			$spokenRes++ if ($subcorpus=~m/snum/);
			$writtenRes++ if ($subcorpus=~m/\((spowri|all(tim|typ)|wri(med|dom|ad|aag|ase|aty|aud|lev|tas|sta|sam)|sde(age|cla|sex)|spo(log|reg)|scgdom|genre|derivedtexttype)\=/);
			$writtenRes++ if ($subcorpus=~m/fnum/);
			
			if (($writtenRes) && (!$spokenRes)) {
				$sqlQuery = "SELECT sum(numwords) FROM headerInfo WHERE $subcorpus";
			} elsif ((!$writtenRes) && ($spokenRes)) {
				$sqlQuery = "SELECT sum(speakerWords) FROM speakers WHERE $subcorpus";
			} elsif (($writtenRes) && ($spokenRes)) {
				$sqlQuery = "SELECT sum(speakerWords) FROM speakers, headerInfo WHERE headerInfo.fname = speakers.filename and $subcorpus";
			}
			$sth = $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
			$sth->execute or bark("Can't execute $sqlQuery: " . $sth::errstr);
			@rowArray=$sth->fetchrow_array;
			$numTotalWords=$rowArray[0];
			$numTotalWordsDisplay=&makeDecimals($numTotalWords);
			if ($numTotalWords > 20000000) {
				## display something that gives users a choice whether they want to wait
				## This is disabled for the time being!!!
				$theDisplay = '
				<TR BGCOLOR="#FF9999">
					<TD>
						The current set of hits was retrieved from a large subpart of the corpus (' . $numTotalWordsDisplay . ' words). No cached frequency  data
						was found and frequency lists for the relevant texts/speakers will have to be compiled in order to provide accurate measures
						of collocational strength. Depending on the size of the subcorpus this may take several minutes and will use a lot of disk space. 
						Alternatively, you can work with one of the basic precompiled lists for the BNC.
						
					</TD>
					<TD WIDTH="31%" ALIGN="RIGHT">		
						<SELECT NAME="basis">
							<OPTION VALUE="1" SELECTED>whole BNC
							<OPTION VALUE="2"> written texts only
							<OPTION VALUE="3"> spoken texts only
							<OPTION VALUE="0"> compile accurate frequency lists
						</SELECT>	
					</TD>
					
					
				</TR>';

			} else {
				# do it on the fly - but display a warning that it will take a few seconds longer
				$displayWarning = 1;
				$theDisplay = '
				<TR>
					<TD COLSPAN="2" BGCOLOR="#FFCC99">
						<B>Note:</B> The current set of hits was retrieved from a subpart of the corpus (' . $numTotalWordsDisplay . ' words). No cached data
						was found and frequency lists 
						for the relevant texts/speakers will have to be compiled in order to provide accurate measures
						of collocational strength. This will increase the time needed for the calculation - please be patient.
					</TD>
					<INPUT TYPE="hidden" NAME="basis" VALUE="0">
				</TR>';
			}
		}	
	}
}


$variable="Collocation";
$expert = "";
$explanation = "(for concordance display of individual collocations)";
$theTitle="BNC Collocation Settings";

$theData2=&encodeForm($params{theData});

print "Content-type: text/html\n\n";

print <<END;

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>

<TITLE>$theTitle</TITLE>
<META NAME="generator" CONTENT="BBEdit 4.5">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=iso-8859-1">
</HEAD>
<BODY BGCOLOR="#FFFFFF">

<FORM ACTION="/$bwCGIalias/create_db.pl" METHOD=GET>
<INPUT TYPE="hidden" NAME="kind" VALUE="collocation">
<INPUT TYPE="hidden" NAME="theData" VALUE="$theData2">
<INPUT TYPE="hidden" NAME="qname" VALUE="$params{qname}">
<INPUT TYPE="hidden" NAME="queryID" VALUE="$params{queryID}">
<INPUT TYPE="hidden" NAME="fListCache" VALUE="$params{fListCache}">
<INPUT TYPE="hidden" NAME="delRare" VALUE="5">
<INPUT TYPE="hidden" NAME="theID" VALUE="$params{theID}">
<INPUT TYPE="hidden" NAME="excludePun" VALUE="0">
<TABLE BORDER="0" WIDTH="100%" CELLPADDING=3 CELLSPACING=2>
	<TR>
		<TD HEIGHT="27" COLSPAN="2" BGCOLOR="#CCCCFF"> <FONT SIZE="4"><B>BNC $variable Settings</B></FONT></TD>
	</TR>
	<TR>
		<TD HEIGHT="45" WIDTH="69%" BGCOLOR="#DDDDDD">Calculate over sentence boundaries:</TD>
		<TD WIDTH="31%" BGCOLOR="#DDDDDD" ALIGN="RIGHT">		
			<SELECT NAME="sentBound">
				<OPTION SELECTED VALUE=1> no
				<OPTION VALUE=0>yes
			</SELECT>
		<INPUT TYPE="hidden" NAME="statType" VALUE="1">
	
		</TD>
	</TR>
	<TR>
		<TD HEIGHT="45" WIDTH="69%" BGCOLOR="#DDDDDD">Include lemma information:</TD>
		<TD HEIGHT="45" WIDTH="31%" BGCOLOR="#DDDDDD" ALIGN="RIGHT">		
			<SELECT NAME="includeLemma">
				<OPTION SELECTED>no
				<OPTION>yes
			</SELECT>	
		</TD>
	</TR>
	<TR>
		<TD HEIGHT="45" WIDTH="69%" BGCOLOR="#DDDDDD">Maximum window span${expert}: </TD>
		<TD HEIGHT="45" WIDTH="31%" BGCOLOR="#DDDDDD" ALIGN="RIGHT">
			+/-		
			<SELECT NAME="totrange">
			<OPTION>4
			<OPTION SELECTED>5
			<OPTION>6
			<OPTION>7
			<OPTION>8
			<OPTION>9
			<OPTION>10
		</SELECT>
		<INPUT TYPE="hidden" NAME="basis" VALUE="$basis">
		<INPUT TYPE="hidden" NAME="numOfSolutions" VALUE="$params{numOfSolutions}">
		<INPUT TYPE="hidden" NAME="thinning" VALUE="$thinning">
		<INPUT TYPE="hidden" NAME="thMode" VALUE="$params{thMode}">
		</TD>
	</TR>
	<INPUT TYPE="hidden" NAME="inst" VALUE="50">
	$theDisplay
	<TR>
		<TD COLSPAN="2" BGCOLOR="#BBBBBB">
			<CENTER>
			<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
			<INPUT TYPE="SUBMIT" VALUE="Submit">
			</CENTER>


		</TD>
	</TR>
</TABLE>
</FORM>
END

&printFooter();
exit(0);
