#!/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
#

$|=1;
use DBI;

use lib "../lib_files";
use bncLibCQPXML;

# connect to mysql
$bncHandle = DBI->connect("DBI:mysql:$bwMYSQLtable:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd") or barkFull("Can't connect to mysql on $bwMYSQLhost at port: $bwMysqlPort <BR>" . $userHandle::errstr);
$userHandle = DBI->connect("DBI:mysql:$bwMYSQLusertable:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd") or barkFull("Can't connect to mysql on $bwMYSQLhost at port: $bwMysqlPort <BR>" . $userHandle::errstr);
$frequencyHandle = DBI->connect("DBI:mysql:$bwMYSQLfrequency:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd") or bark("Can't connect to mysql on $bwMYSQLhost at port: $bwMysqlPort <BR>" . $frequencyHandle::errstr);

$username = $ENV{'REMOTE_USER'};

# get default view from settings database (for main.pl)
$sqlQuery = "SELECT view FROM settings where user='$username'";
$sth = $bncHandle->prepare($sqlQuery) or barkFull("Can't prepare $sqlQuery:" . $bncHandle::errstr);
$sth->execute or barkFull("Can't execute $sqlQuery: $sth::errstr");
@rowArray=$sth->fetchrow_array;
$view = $rowArray[0];

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");
}


$dbname = $params{dbname};
$numOfSolutions = $params{numOfSolutions};

setParam("tag", "all") unless ($params{tag});
setParam("inst", "50") unless ($params{inst});
setParam("first", "no") unless ($params{first});

$tagClean=$params{tag};

$word = $params{word};
$word=~s/ /~/g;
#$word=~s/-/_/g if ($params{coll} == 1);
$wordClean=$word;
setParam("word", "$word");

#setParam("theData", &decURL($params{theData}));

# remove trailing spaces and returns from the input
$theData = $params{theData};
$OriginaltheData = $params{theData};
$theData=~s/(\r|\s)+\Z//;

# Is this some kind of linewrap??? Check! (25.7.2005)
$theData=~s/%D%A//g;

$check = $theData=~m/ª/;
if ($check) {
	$theData = $`;
	# where does this odd character come from? Check!!! (18.4.2006)
	$theData=~s/\W\Z//;
	$theRestrictions = $';
	($writtenRes, $spokenRes) = split(/##/, $theRestrictions);
	
	if (($writtenRes) && (!$spokenRes)) {
		$metatextualRestr = "$writtenRes";
	} elsif ((!$writtenRes) && ($spokenRes)) {
		$metatextualRestr = "$spokenRes";
	} else {
		$metatextualRestr = "$writtenRes AND $spokenRes";
	}
	$metatextualQuote = $bncHandle->quote($metatextualRestr);
			
} else {
	#warn "huh?  $theData";
}

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


$theDataShow = $params{theData};
$theDataShow=~s/\</&lt\;/g;
$theDataShow=~s/\>/&gt\;/g;
		
$hitGroupTag=0;
$check=$tagClean=~m/(ADJ|ADV|ART|CONJ|INTERJ|PREP|PRON|SUBST|VERB|UNC|STOP)/;
$hitGroupTag=1 if ($check);


$timeTaken = $params{timeTaken};
setParam("timeTaken","");

if ($timeTaken) {
	$timeInfo = "<font color=\"#AAAAAA\" size=\"1\"> [$timeTaken seconds]</font>";
}
if ($timeTaken eq "cache") {
	$timeInfo = "<font color=\"#AAAAAA\" size=\"1\"> [data retrieved from cache]</font>";
}


# this checks for user-settings (if there are any) and changes the variables accordingly
# the user-defined range is only accepted if it is within the selected range for calculation
# otherwise, the selected range overrides the user-settings.
if ($params{first} eq "yes") {
	$originalRange=$params{plus};
	$originalMinusrange=$params{minus};
	$sqlQuery = "SELECT statType, delRare, totBNC, `range`, minusrange FROM settings where user='$username'";
	$sth= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
	$sth->execute or bark("Can't execute $sqlQuery: " . $sth::errstr);
	$rows = $sth->rows;
	if ($rows) {	
		@rowArray=$sth->fetchrow_array;
		$statType=$rowArray[0];
		$delRare=$rowArray[1];
		$totfreq=$rowArray[2];
		$range=$rowArray[3];
		$minusrange=$rowArray[4];
		setParam("delRare", "$delRare");
		setParam("minus", "$minusrange");
		setParam("plus", "$range");
		setParam("statType", "$statType");
		setParam("totfreq", "$totfreq");
		if (($originalRange < $params{plus}) || ($originalMinusrange > $params{minus})) {
			$range=$originalRange;
			setParam("minus", "$originalMinusrange");
			setParam("plus", "$range");
		}
	}
	$params{first} = "no";
}
$having = "HAVING sum >= $params{delRare}";

$windowSize=$params{plus} - $params{minus};
$windowSize++ if (($params{plus} < 0) || ($params{minus} > 0));

print "Content-type: text/html\n\n";
print <<END;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
	<HTML>
		<HEAD>
			<TITLE>Collocation Database</TITLE>
			<META NAME="generator" CONTENT="BBEdit 4.5">
			<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
		</HEAD>
		<BODY BGCOLOR="#FFFFFF">
END
$limitString = "LIMIT $params{begin}, 50";

# set variables for different calculations of collocation values depending on sub-corpus chosen
# the variable $totBasis should ideally not be hard-coded! 

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

if ((!$subcorpus) || ($params{basis} == 1)) {
	if ($params{coll} == 1) {
		$wordFreq="$bwMYSQLtable.tags_overall";
	} elsif ($params{coll} == 2) {
		$wordFreq="$bwMYSQLtable.allLemma";
	} else {
		$wordFreq="$bwMYSQLtable.allWords";
		if ($tagClean ne "all") {
			$wordFreq="$bwMYSQLtable.allWordsT";
			$wordFreq="$bwMYSQLtable.allWordsG" if ($hitGroupTag);
		}
	}
	$totBasis=112102325 * $params{thinning};
	$dispBasis="whole BNC";
} elsif (($subcorpus eq "(spowri=1") || ($params{basis} == 2)) {
	if ($params{coll} == 1) {
		$wordFreq="$bwMYSQLtable.tags_written";
	} elsif ($params{coll} == 2) {
		$wordFreq="$bwMYSQLtable.allLemmaW";
	} else {
		$wordFreq="$bwMYSQLtable.allWordsW";
		if ($tagClean ne "all") {
			$wordFreq="$bwMYSQLtable.allWordsTW";
			$wordFreq="$bwMYSQLtable.allWordsWG" if ($hitGroupTag);
		}
	}
	$dispBasis="written texts";
	$totBasis=100119205 * $params{thinning};
} elsif (($subcorpus eq "(spowri=2") || ($params{basis} == 3)) {
	if ($params{coll} == 1) {
		$wordFreq="$bwMYSQLtable.tags_spoken";
	} elsif ($params{coll} == 2) {
		$wordFreq="$bwMYSQLtable.allLemmaW";
	} else {
		$wordFreq="$bwMYSQLtable.allWordsS";
		if ($tagClean ne "all") {
			$wordFreq="$bwMYSQLtable.allWordsTS";
			$wordFreq="$bwMYSQLtable.allWordsSG" if ($hitGroupTag);
		}
	}
	$dispBasis="spoken texts";
	$totBasis=11983120 * $params{thinning};
} else {
	$cachedName = $params{fListCache};
	if ($params{coll} == 1) {
		#doesn't work for subcorpora - disable!
		$wordFreq="$bwMYSQLfrequency.tags_spoken";
	} elsif ($params{coll} == 2) {
		$wordFreq="$bwMYSQLfrequency.allLemma_$cachedName";
	} else {
		$wordFreq="$bwMYSQLfrequency.allWords_$cachedName";
		if ($tagClean ne "all") {
			$wordFreq="$bwMYSQLfrequency.allWordsT_$cachedName";
			$wordFreq="$bwMYSQLfrequency.allWordsG_$cachedName" if ($hitGroupTag);
		}
	}
	$dispBasis="current subcorpus";

	$sqlQuery = "SELECT sum(freq) FROM allWords_$cachedName";
	$sth = $frequencyHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $frequencyHandle::errstr);
	$sth->execute or bark("Can't execute $sqlQuery: " . $sth::errstr);
	@rowArray=$sth->fetchrow_array;
	$totBasis = $rowArray[0] * $params{thinning};

}
$N = $totBasis;

$colVal=0;
$colVal=1 unless ($params{statType} == 0);

if ($params{plus} < $params{minus}) {
	print  <<END;
	<H3 ALIGN=LEFT>Your position range does not make sense. </H3>
	Please use the 'back' button on your browser and change your input accordingly.<BR>
END
	&printFooter();
	exit(0);
}

if ($params{plus} ne $params{minus}) {
	$rangeString="dist BETWEEN $params{minus} AND $params{plus}";
} else {
	$rangeString="dist = $params{plus}";
}

$sqlQuery = "SELECT COUNT(*) from $dbname WHERE $rangeString";
$sth = $userHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $userHandle::errstr);
$sth->execute or bark("Can't execute $sqlQuery: " . $sth::errstr);
@rowArray=$sth->fetchrow_array;
$R1 = $rowArray[0];
$R2 = $N - $R1;

$sqlQuery = "SELECT COUNT(DISTINCT refnum) from $dbname WHERE $rangeString";
$sth = $userHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $userHandle::errstr);
$sth->execute or bark("Can't execute $sqlQuery: " . $sth::errstr);
@rowArray=$sth->fetchrow_array;
$DICE_NODE_F = $rowArray[0];
	
if ($params{coll} == 2) {
	$sqlQuery = "SELECT distinct(lemma) FROM $dbname where tag <> 'YYY'";
	$sth= $userHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $userHandle::errstr);
	$sth->execute or bark("Can't execute $sqlQuery: " . $sth::errstr);
	$typesTotal = $sth->rows;
	$blurb = "lemmas";
} else {
	$sqlQuery = "SELECT distinct(item) FROM $dbname where tag <> 'YYY'";
	$sth= $userHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $userHandle::errstr);
	$sth->execute or bark("Can't execute $sqlQuery: " . $sth::errstr);
	$typesTotal = $sth->rows;
	$blurb = "types";
}

if ($params{timeTaken}) {
	$timeTaken = $params{timeTaken};
	setParam("timeTaken","");

	unless ($timeTaken eq "cache") {
		$timeInfo= "<font color=\"#AAAAAA\" size=\"1\"> [$timeTaken seconds]</font>";
	} else {
		$timeInfo = "<font color=\"#AAAAAA\" size=\"1\"> [data retrieved from cache]</font>";
	}
}
$titleBar = &createTitle($params{thMode}, $theDataShow, $numOfSolutions, $params{numOfFiles}, $listFiles, $numTotalWords, $numTotalWordsOrig, $params{subcorpus}, $theRestrictions);
	
if ($params{word}) {
	# for some reason, if I remove the if-then brackets, there will be an error message. Metaphysics!
} else {
	print <<END
	<TABLE BORDER="0" CELLPADDING="3" WIDTH="100%" BGCOLOR="#FFFFFF">
		<TR>
			<TD COLSPAN="4" BGCOLOR="#CCCCFF" HEIGHT="25">
				<B>Collocation parameters:</B>
			 </TD>
		</TR>
END
}

$theMinus="&#45;";

$baseOfCalc="Frequency ranking" if ($params{statType} == 0);
$baseOfCalc="Mutual information" if ($params{statType} == 1);
$baseOfCalc="MI3" if ($params{statType} == 2);
$baseOfCalc="Z-score" if ($params{statType} == 3);
$baseOfCalc="T-score" if ($params{statType} == 4);
#	$baseOfCalc="Chi-squared with Yates' correction" if ($params{statType} == 5);
$baseOfCalc="Log-likelihood" if ($params{statType} == 6);
$baseOfCalc="Dice coefficient" if ($params{statType} == 7);

if (!$params{coll}) {
	if ($tagClean ne "all") {
		$check=$tagClean=~m/(ADJ|ADV|ART|CONJ|INTERJ|PREP|PRON|SUBST|VERB|UNC|STOP)/;
		if ($check) {
			$value=$1;
			$lemma_tag = 1;
			# for the display when downloading result
			$tagRestriction="any verb" if ($value eq "VERB");
			$tagRestriction="any noun" if ($value eq "SUBST");
			$tagRestriction="any adjective" if ($value eq "ADJ");
			$tagRestriction="any adverb" if ($value eq "ADV");
			$tagRestriction="any pronoun" if ($value eq "PRON");
			$tagRestriction="any preposition" if ($value eq "PREP");
			$tagRestriction="any conjunction" if ($value eq "CONJ");
			$tagRestriction="any article" if ($value eq "ART");
			$tagRestriction="any interjection" if ($value eq "INTERJ");
			$tagRestriction="any punctuation" if ($value eq "STOP");
			$tagRestriction="any unclear" if ($value eq "UNC");
			
			# for the SQL-query (allWordsT etc. have different tags...)
			
			$pos = class2cpos($value);
			$concatTag="_" . $tagClean;
			$tagRegexp = "and $bwMYSQLusertable.$dbname.tag regexp \'$pos\' ";
		} else {
			$concatTag="_" . $tagClean;
			$tagRestriction = $tagClean;
			$tagRegexp = "and $bwMYSQLusertable.$dbname.tag = \'$tagClean\' ";
		}
		$concatWord="concat($bwMYSQLusertable.$dbname.item, \"$concatTag\")";
	} else {
		$concatWord="$bwMYSQLusertable.$dbname.item";
		$tagRegexp = "";
		$tagRestriction="";
	}
	$itemOrTag="$bwMYSQLusertable.$dbname.item";
	$basisWordOrTag="$wordFreq.word";
	$displayWord ="Collocation";
} elsif ($params{coll} == 2) {
	$tagRestriction="";
	$tagRegexp = "";
	$concatWord="$bwMYSQLusertable.$dbname.lemma";
	$itemOrTag= "$bwMYSQLusertable.$dbname.lemma";
	if ($params{lemmaType}) {
		$tagRegexp = " AND $concatWord regexp '_$params{lemmaType}' ";
	}
	$basisWordOrTag="$wordFreq.lemma";
	$displayWord ="Lemma collocation";
} else {
	$tagRestriction="";
	$tagRegexp = "";
	$concatWord="$bwMYSQLusertable.$dbname.tag";
	$itemOrTag= "$bwMYSQLusertable.$dbname.tag";
	$basisWordOrTag="$wordFreq.tag";
	$limitString="";
	$displayWord ="Colligation";
}

&printForm() if (!$params{word});

if ($params{thinning} != 1) {
	$thinFactor = "* $params{thinning}";
} else {
	$thinFactor = "";
}	

my $C1 = "($wordFreq.freq)";
my $C2 = "($N - $C1)";
my $O11 = "COUNT($itemOrTag)";
my $O12 = "($R1 - $O11)";
my $O21 = "($C1 - $O11)";
my $O22 = "($R2 - $O21)";
my $E11 = "($R1 * $C1 / (1e0 * $N))";
my $E12 = "($R1 * $C2 / (1e0 * $N))";
my $E21 = "($R2 * $C1 / (1e0 * $N))";
my $E22 = "($R2 * $C2 / (1e0 * $N))";

my $P_COLL_NODE = "(COUNT(DISTINCT refnum) / (1e0 * $DICE_NODE_F))";
if ($thinFactor) {
	my $P_NODE_COLL = "(COUNT($itemOrTag) / (1e0 * $wordFreq.freq * $thinFactor))";
} else {
	$P_NODE_COLL = "(COUNT($itemOrTag) / (1e0 * $wordFreq.freq))";
}


if (!$params{word}) {
	if ($colVal) {
		# one of the collocation statistics (other than sort by frequency) were selected

		print <<END;
			<TABLE BORDER="0" CELLPADDING="2" WIDTH="100%" BGCOLOR="#FFFFFF">
				<TR>
				<TD COLSPAN="7" BGCOLOR="#CCCCFF" HEIGHT="25">
					<B>There are $typesTotal different $blurb in your collocation database
					for &quot;$theDataShow&quot;. ($titleBar)</B> $timeInfo
				 </TD>
			</TR>
END
		if (!$params{coll}) {
			print '<TR BGCOLOR="#FFCC66">' . "<TD VALIGN=TOP><B>&nbsp;No.</B></TD><TD VALIGN=TOP><B>&nbsp;Word</B></TD><TD VALIGN=TOP ALIGN=MIDDLE><B>Total No. in <BR>$dispBasis</B></TD><TD VALIGN=TOP ALIGN=MIDDLE width=\"17%\"><B>Expected collocate frequency</B></TD><TD VALIGN=TOP ALIGN=MIDDLE width=\"17%\"><B>Observed collocate frequency</B></TD><TD VALIGN=TOP ALIGN=MIDDLE><B>In No. of texts</B></TD><TD VALIGN=TOP ALIGN=MIDDLE><B>$baseOfCalc value</B></TD></TR><TR BGCOLOR=\"#DDDDDD\">\n";
		} elsif ($params{coll} == 2) {
			print '<TR BGCOLOR="#FFCC66">' . "<TD VALIGN=TOP><B>&nbsp;No.</B></TD><TD VALIGN=TOP><B>&nbsp;Lemma</B></TD><TD VALIGN=TOP ALIGN=MIDDLE><B>Total No. in <BR>$dispBasis</B></TD><TD VALIGN=TOP ALIGN=MIDDLE width=\"17%\"><B>Expected collocate frequency</B></TD><TD VALIGN=TOP ALIGN=MIDDLE width=\"17%\"><B>Observed collocate frequency</B></TD><TD VALIGN=TOP ALIGN=MIDDLE><B>In No. of texts</B></TD><TD VALIGN=TOP ALIGN=MIDDLE><B>$baseOfCalc value</B></TD></TR><TR BGCOLOR=\"#DDDDDD\">\n";
		} else {
			print '<TR BGCOLOR="#FFCC66">' . "<TD VALIGN=TOP><B>&nbsp;No.</B></TD><TD VALIGN=TOP><B>&nbsp;Tag</B></TD><TD VALIGN=TOP ALIGN=MIDDLE><B>Total No. in <BR>$dispBasis</B></TD><TD VALIGN=TOP ALIGN=MIDDLE width=\"17%\"><B>Expected collocate frequency</B></TD><TD VALIGN=TOP ALIGN=MIDDLE width=\"17%\"><B>Observed collocate frequency</B></TD><TD VALIGN=TOP ALIGN=MIDDLE><B>In No. of texts</B></TD><TD VALIGN=TOP ALIGN=MIDDLE><B>$baseOfCalc value</B></TD></TR><TR BGCOLOR=\"#DDDDDD\">\n";
		}
		
		# get SQL query from subroutine
		$sqlQuery = &make_SQL_query($params{statType});
		
		
		$sth= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $sth::errstr);
		$sth->execute or bark("Can't execute $sqlQuery: " . $sth::errstr);
		$resArrayResult=$sth->fetchall_arrayref or bark("Can't fetch $sqlQuery: " . $sth::errstr);
		for $k ( 0 .. ($#{$resArrayResult}) ) {
			
			$theWord=$resArrayResult->[$k][0];
			$theFrequency=$resArrayResult->[$k][1];
			$theColValue=$resArrayResult->[$k][2];
			$theOccurValue=$resArrayResult->[$k][3];
			$numOfFiles=$resArrayResult->[$k][4];
			$expectedValue=$resArrayResult->[$k][5];
			
			$theColValue = int(($theColValue * 10000) + 0.5)/ 10000;
			
			$theOccurValue=&makeDecimals($theOccurValue);
			
			# clean item for display
			$theShowWord=$theWord;
			$theShowWord=&output2($theShowWord);

			$wordCode=$theWord;
			$wordCode=$userHandle->quote($wordCode);
			
			$sqlQ="SELECT beginPosition, endPosition, dist, item INTO OUTFILE '/$bwMySQLTempPath/${dbname}_new' from $bwMYSQLusertable.$dbname where $itemOrTag=$wordCode AND $rangeString $tagRegexp";

			$history=$params{thMode};
			if ($params{coll} == 1) {
				$history .= "##Y$theShowWord#$theFrequency";
			} elsif ($params{coll} == 2) {
				$history .= "##Z$theShowWord#$theFrequency";
			} else {
				$history .= "##C$theShowWord#$theFrequency#$tagClean";
			}
			
			$history =~ s/(\W)/sprintf("%%%x", ord($1))/eg;
			$sqlQ=~s/(\W)/sprintf("%%%x", ord($1))/eg;	
			
			if ($params{coll} == 2) {
				$theShowWord=~m/_(ADJ|ADV|ART|CONJ|INTERJ|PREP|PRON|SUBST|VERB|UNC|STOP)/;
				$theShowWord = lc($`);
				$theShowWord = $theShowWord . $&;
			} else {
				$theShowWord=lc($theShowWord) unless ($params{coll} == 1);
			}
			$theShowWordLink = $theShowWord;
			# this is a dirty hack - clean up for tildes etc.
			$theShowWord=~s/_/-/ if ($params{coll} == 1);
			$theShowWordLink=~s/(\W)/sprintf("%%%x", ord($1))/eg;
							
			$bgColour = "BGCOLOR=\"#DDDDDD\"";
			
			$theLink  = '<A HREF="/' . $bwCGIalias . '/main.pl?theData=' . $paramsEnc{theData} . '&chunk=1&inst=' . $paramsEnc{inst} . '&max=INIT&qname=' . $paramsEnc{qname} . '&queryID=' . $paramsEnc{queryID} . '&SQL=' . $sqlQ . '&program=collocations&word=' . $theShowWordLink . '&dbname=' . $paramsEnc{dbname} . '&queryID=' . $paramsEnc{queryID} . '&theID=' . $paramsEnc{theID} . '&view=' . $view . '&thMode=' . $history . '&urlTest=yes">' . $theFrequency . '</A>';

			$theLink2 = '<A HREF="/' . $bwCGIalias . '/show_collocation.pl?qname=' . $paramsEnc{qname} . '&queryID=' . $paramsEnc{queryID} . '&dbname=' . $paramsEnc{dbname} . '&numOfSolutions=' . $numOfSolutions . '&tag=' . $paramsEnc{tag} . '&delRare=' . $paramsEnc{delRare} . '&begin=' . $paramsEnc{begin} . '&theData=' . $paramsEnc{theData} . '&theID=' . $paramsEnc{theID} . '&minus=' . $paramsEnc{minus} . '&plus=' . $paramsEnc{plus} . '&word=' . $theShowWordLink . '&statType=' . $paramsEnc{statType} . '&basis=' . $paramsEnc{basis} . "&excludePun=" . $paramsEnc{excludePun} . '&totrange=' . $paramsEnc{totrange} . '&totfreq=' . $paramsEnc{totfreq} . '&thinning=' . $paramsEnc{thinning} . '&coll=' . $paramsEnc{coll} . '&thMode=' . $history . '&fListCache=' . $paramsEnc{fListCache} . '&includeLemma=' . $params{includeLemma} . '&urlTest=yes">' . $theShowWord . '</A>';
			
			$indicator=$params{begin} + $k + 1;
							
			if ($theColValue) {
				print "<TR $bgColour><TD>&nbsp;$indicator</TD><TD>&nbsp;$theLink2</TD><TD ALIGN=CENTER>$theOccurValue</TD><TD ALIGN=CENTER>$expectedValue</TD><TD ALIGN=CENTER>&nbsp;$theLink</TD><TD ALIGN=CENTER>$numOfFiles</TD><TD ALIGN=CENTER>$theColValue</TD></TR>\n";
			} else {
				print "<TR BGCOLOR=\"#DDDDDD\"><TD>&nbsp;$indicator</TD><TD>&nbsp;$theLink2</TD><TD ALIGN=CENTER>$theOccurValue</TD><TD ALIGN=CENTER>$expectedValue</TD><TD ALIGN=CENTER>&nbsp;$theLink</TD><TD ALIGN=CENTER>$numOfFiles</TD><TD ALIGN=CENTER>n/a</TD></TR>\n";
			}

		}		
	} else {
		# sort by frequency
		if ($params{coll} == 1) {
			$showType="Tag";
		} elsif ($params{coll} == 2) {
			$showType="Lemma";
		} else {
			$showType="Word";
		}
		print <<END;
		<TABLE BORDER="0" CELLPADDING="2" WIDTH="100%" BGCOLOR="#FFFFFF">
			<TR>
				<TD COLSPAN="6" BGCOLOR="#CCCCFF" HEIGHT="25">
					<B>There are $typesTotal different $blurb in your collocation database
					for &quot;$theDataShow&quot;. ($titleBar)</B> $timeInfo
				 </TD>
			</TR>
			<TR BGCOLOR=\"#FFCC66\">
				<TD VALIGN=TOP><B>&nbsp;No.</B></TD>
				<TD VALIGN=TOP><B>&nbsp;$showType</B></TD>
				<TD ALIGN=MIDDLE><B>Total No. in <BR>$dispBasis</B></TD>
				<TD VALIGN=TOP ALIGN=MIDDLE width=\"20%\"><B>Expected collocate frequency</B></TD>
				<TD VALIGN=TOP ALIGN=MIDDLE width=\"20%\"><B>Observed collocate frequency</B></TD>
				<TD VALIGN=TOP ALIGN=MIDDLE><B>In No. of texts</B></TD>

			</TR>
			<TR BGCOLOR=\"#DDDDDD\">
END

		$sqlQuery="select $itemOrTag, $O11 as sum, $wordFreq.freq, count(distinct(fname)), format($E11, 3)
			from $bwMYSQLusertable.$dbname, $wordFreq 
			where $concatWord = $basisWordOrTag
			and $rangeString
			$tagRegexp
			and $wordFreq.freq >= $params{totfreq}
			group by $itemOrTag
			$having
			order by sum desc $limitString";
		$sth= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $sth::errstr);
		$sth->execute or bark("Can't execute $sqlQuery: " . $sth::errstr);
		$resArrayResult=$sth->fetchall_arrayref or bark("Can't fetch $sqlQuery: " . $sth::errstr);
		for $k ( 0 .. ($#{$resArrayResult}) ) {
			$theWord=$resArrayResult->[$k][0];
			$theFrequency=$resArrayResult->[$k][1];
			$theOccurValue=$resArrayResult->[$k][2];
			$numOfFiles=$resArrayResult->[$k][3];
			$expectedValue=$resArrayResult->[$k][4];
			
			
			$theOccurValue=&makeDecimals($theOccurValue);
			
			$theShowWord=$theWord;
			$theShowWord=&output2($theShowWord);
			if ($params{coll} == 2) {
				$theShowWord=~m/_(ADJ|ADV|ART|CONJ|INTERJ|PREP|PRON|SUBST|VERB|UNC|STOP)/;
				$theShowWord = lc($`);
				$theShowWord = $theShowWord . $&;
			} else {
				$theShowWord=lc($theShowWord) unless ($params{coll} == 1);
			}
			
			$theWordLink=$theWord;
			
			$wordCode=$theWord;
			$wordCode=$userHandle->quote($wordCode);
			
			$sqlQ="SELECT beginPosition, endPosition, dist, item INTO OUTFILE '/$bwMySQLTempPath/${dbname}_new' from $bwMYSQLusertable.$dbname where $itemOrTag=$wordCode AND $rangeString $tagRegexp";

			$history=$params{thMode};
			if ($params{coll} == 1) {
				$history .= "##Y$theShowWord#$theFrequency";
			} elsif ($params{coll} == 2) {
				$history .= "##Z$theShowWord#$theFrequency";
			} else {
				$history .= "##C$theShowWord#$theFrequency#$tagClean";
			}
			$history =~ s/(\W)/sprintf("%%%x", ord($1))/eg;
			
			$sqlQ=~s/(\W)/sprintf("%%%x", ord($1))/eg;
			$theShowWord=~s/_/-/ if ($params{coll} == 1);

			$theWordLink=~s/(\W)/sprintf("%%%x", ord($1))/eg;
			$theLink='<A HREF="/' . $bwCGIalias . '/main.pl?theData=' . $paramsEnc{theData} . '&chunk=1&inst=' . $paramsEnc{inst} . '&max=INIT&qname=' . $paramsEnc{qname} . '&queryID=' . $paramsEnc{queryID} . '&SQL=' . $sqlQ . '&program=collocations&word=' . $theWordLink . '&dbname=' . $params{dbname} . '&theID=' . $paramsEnc{theID} . '&view=' . $view . '&thMode=' . $history . '&urlTest=yes">' . $theFrequency . '</A>';
			$theLink2 = '<A HREF="/' . $bwCGIalias . '/show_collocation.pl?qname=' . $params{qname} . '&dbname=' . $paramsEnc{dbname} . '&numOfSolutions=' . $paramsEnc{numOfSolutions} . '&tag=' . $paramsEnc{tag} . '&delRare=' . $paramsEnc{delRare} . '&begin=' . $paramsEnc{begin} . '&theData=' . $paramsEnc{theData} . '&theID=' . $paramsEnc{theID} . '&minus=' . $paramsEnc{minus} . '&plus=' . $paramsEnc{plus} . '&word=' . $theWordLink . '&statType=' . $paramsEnc{statType} . '&basis=' . $paramsEnc{basis} . "&excludePun=" . $paramsEnc{excludePun} . '&totrange=' . $paramsEnc{totrange} . '&totfreq=' . $paramsEnc{totfreq} . '&thinning=' . $paramsEnc{thinning} . '&coll=' . $paramsEnc{coll} . '&thMode=' . $history . '&fListCache=' . $paramsEnc{fListCache} . '&includeLemma=' . $params{includeLemma} . '&urlTest=yes">' . $theShowWord . '</A>';
			$indicator=$params{begin} + $k + 1;
			print "<TR BGCOLOR=\"#DDDDDD\"><TD>&nbsp;$indicator</TD><TD>&nbsp;$theLink2</TD><TD ALIGN=CENTER>$theOccurValue</TD><TD ALIGN=CENTER>$expectedValue</TD><TD ALIGN=CENTER>&nbsp;$theLink</TD><TD ALIGN=CENTER>$numOfFiles</TD></TR>\n";
		}
	}
	print "</TABLE><HR>\n";
			
	# check whether there are more items to be displayed. Only print the link if there are...
	$something=$resArrayResult->[49][0];
	
	$beginplus=$params{begin} + 50;
	if (($something)  && ($params{coll} != 1)) {
		$indexBut = '<A HREF="/' . $bwCGIalias . '/show_collocation.pl?qname=' . $paramsEnc{qname} . '&queryID=' . $paramsEnc{queryID} . '&dbname=' . $paramsEnc{dbname} . '&numOfSolutions=' . $paramsEnc{numOfSolutions} . '&tag=' . $paramsEnc{tag} . '&lemmaType=' . $paramsEnc{lemmaType} . '&delRare=' . $paramsEnc{delRare} . '&begin=' . $beginplus . '&theData=' . $paramsEnc{theData} . '&theID=' . $paramsEnc{theID} . '&minus=' . $paramsEnc{minus} . '&sentBound=' . $paramsEnc{sentBound} . '&plus=' . $paramsEnc{plus} . '&word=&statType=' . $paramsEnc{statType} . '&basis=' . $paramsEnc{basis} . "&excludePun=" . $paramsEnc{excludePun} . '&totrange=' . $paramsEnc{totrange} . '&totfreq=' . $paramsEnc{totfreq} . '&thinning=' . $paramsEnc{thinning} . '&coll=' . $paramsEnc{coll} . '&thMode=' . $paramsEnc{thMode} . '&fListCache=' . $paramsEnc{fListCache} . '&includeLemma=' . $params{includeLemma} . '&urlTest=yes">Show next 50 values</A>';
		print "[" . $indexBut . "]";
	} else {
		print "[Show next 50 values]";
	}
	if ($params{begin} > 1) {
		$beginminus=$params{begin} - 50;
		$indexBut = '<A HREF="/' . $bwCGIalias . '/show_collocation.pl?qname=' . $paramsEnc{qname} . '&dbname=' . $paramsEnc{dbname} . '&queryID=' . $paramsEnc{queryID} . '&numOfSolutions=' . $paramsEnc{numOfSolutions} . '&tag=' . $paramsEnc{tag} . '&lemmaType=' . $paramsEnc{lemmaType} . '&delRare=' . $paramsEnc{delRare} . '&begin=' . $beginminus . '&theData=' . $paramsEnc{theData} . '&theID=' . $paramsEnc{theID} . '&minus=' . $paramsEnc{minus} . '&sentBound=' . $paramsEnc{sentBound} . '&plus=' . $paramsEnc{plus} . '&word=&statType=' . $paramsEnc{statType} . '&basis=' . $paramsEnc{basis} . "&excludePun=" . $paramsEnc{excludePun} . '&totrange=' . $paramsEnc{totrange} . '&totfreq=' . $paramsEnc{totfreq} . '&thinning=' . $paramsEnc{thinning} . '&coll=' . $paramsEnc{coll} . '&thMode=' . $paramsEnc{thMode} . '&fListCache=' . $paramsEnc{fListCache} . '&includeLemma=' . $params{includeLemma} . '&urlTest=yes">Show previous 50 values</A>';
		print "[" . $indexBut . "]";
		$indexBut = '<A HREF="/' . $bwCGIalias . '/show_collocation.pl?qname=' . $paramsEnc{qname} . '&dbname=' . $paramsEnc{dbname} . '&queryID=' . $paramsEnc{queryID} . '&numOfSolutions=' . $paramsEnc{numOfSolutions} . '&tag=' . $paramsEnc{tag} . '&lemmaType=' . $paramsEnc{lemmaType} . '&delRare=' . $paramsEnc{delRare} . '&begin=0&theData=' . $paramsEnc{theData} . '&theID=' . $paramsEnc{theID} . '&minus=' . $paramsEnc{minus} . '&plus=' . $paramsEnc{plus} . '&sentBound=' . $paramsEnc{sentBound} . '&word=&statType=' . $paramsEnc{statType} . '&basis=' . $paramsEnc{basis} . "&excludePun=" . $paramsEnc{excludePun} . '&totrange=' . $paramsEnc{totrange} . '&totfreq=' . $paramsEnc{totfreq} . '&thinning=' . $paramsEnc{thinning} . '&coll=' . $paramsEnc{coll} . '&thMode=' . $paramsEnc{thMode} . '&fListCache=' . $paramsEnc{fListCache} . '&includeLemma=' . $params{includeLemma} . '&urlTest=yes">Show first 50 values</A>';
		print "[" . $indexBut . "]";
	} else {
		print "[Show previous 50 values][Show first 50 values]";
	}		
} else {
	# user selected word information
	if (($params{lemmaType}) && ($params{coll} == 2)) {
		$wordClean = $wordClean . "_" . $params{lemmaType} unless ($wordClean=~m/_(ADJ|ADV|ART|CONJ|INTERJ|PREP|PRON|SUBST|VERB|UNC)/);
	}

	$searchWord=$userHandle->quote($wordClean);
	# calculate collocation value for selected word (based on the window size of the previous collocation list)
	$statTypeName[1]="Mutual information";
	$statTypeName[2]="MI3";
	$statTypeName[3]="Z-score";
	$statTypeName[4]="T-score";
	#$statTypeName[5]="Chi-squared with Yates' correction";
	$statTypeName[6]="Log-likelihood";
	$statTypeName[7]="Dice coefficient";
	
	for ($i = 1; $i <= 7; $i++) {
		next if ($i == 5);
		$sqlQuery = &make_SQL_query($i, $searchWord);
		$sth= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $sth::errstr);
		$sth->execute or bark("Can't execute $sqlQuery: " . $sth::errstr);
		@rowArray=$sth->fetchrow_array;
		
		$theFrequency=$rowArray[1];
		$theColValue[$i]=$rowArray[2];
		$theOccurValue=$rowArray[3];
		$numOfFiles=$rowArray[4];
		$theColValue[$i] = int(($theColValue[$i] * 10000) + 0.5)/ 10000;
		$expectedValue=$rowArray[5];
		
		$theFrequency="0" if (!$theFrequency);
		$numOfFiles="0" if (!$numOfFiles);
		$theColValue[$i] = "n/a" if (!$theColValue[$i]);
	
	}
	$theOccurValue=&makeDecimals($theOccurValue);
	
	unless (($params{tag} eq "all") || ($params{coll} == 1)) {
		$tagName=" with tag restriction <I>$tagRestriction</I>";
	} else {
		$tagName="";
	}
			
	$countTot=0;
	for ($i = $params{minus}; $i <= $params{plus}; $i++) {
		next if ($i == 0);
		if (($params{coll} == 2) && (!$params{lemmaType})) {
			$sqlQuery = "SELECT count($itemOrTag), count(distinct(fname)) FROM $dbname WHERE $itemOrTag regexp $searchWord AND dist=$i $tagRegexp";
		} else {
			$sqlQuery = "SELECT count($itemOrTag), count(distinct(fname)) FROM $dbname WHERE $itemOrTag=$searchWord AND dist=$i $tagRegexp";
		}

		$sth= $userHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $userHandle::errstr);
		$res = $sth->execute or bark("Can't execute $sqlQuery: " . $sth::errstr);
		
		@rowArray=$sth->fetchrow_array;
		$frequency{$i}=$rowArray[0];
		$fileNums{$i}=$rowArray[1];
	
		$countTot += $frequency{$i} if ($frequency{$i});
	}	
	
	$theShowWord=$wordClean;
	$theShowWord=&output2($theShowWord);
	$theShowWord=~s/_/-/ if ($params{coll} == 1);
	if (($params{coll} == 2) && (!$params{lemmaType})) {
		$theShowWord=~s/^\^//;
		$theShowWord=~s/_$//;
		
	}
	
	if ($countTot) {
		print <<END;
			<TABLE BORDER="0" CELLPADDING="2" WIDTH="100%">
			
			<TR BGCOLOR="#CCCCFF">
				<TD COLSPAN="4" HEIGHT="27">
					<B>$displayWord information for the node &quot;$theDataShow&quot; and &quot;${theShowWord}&quot;${tagName} ($theOccurValue occurrences in $dispBasis)</B>
				</TD>
			</TR>
			<TR BGCOLOR="#DDDDDD">
				<TD COLSPAN="2">
					<B>&nbsp;Type of Statistics</B>
				</TD>
				<TD COLSPAN="2">
					<B>&nbsp;Value</B> (for window span $params{minus} to $params{plus})
				</TD>

			</TR>
END

		for ($i = 1; $i <= 7; $i++) {
			next if ($i == 5); # we got rid of chi-squared
			print <<END;
				<TR BGCOLOR="#EEEEEE">
					<TD COLSPAN="2">
						&nbsp;$statTypeName[$i]
					</TD>
					<TD COLSPAN="2">
						&nbsp;$theColValue[$i]
					</TD>

				</TR>
			
END
		}
		print <<END;
			<TR BGCOLOR="#CCCCFF">
				<TD COLSPAN="4" HEIGHT="27">
					<B>&nbsp;Within the window $params{minus} to $params{plus}, <I>$theShowWord</I> $tagName
					occurs $theFrequency times in $numOfFiles different files (expected frequency: $expectedValue)</B>
				</TD>
			</TR>

			<TR BGCOLOR="#FFCC66">
				<TD>
					&nbsp;&nbsp;&nbsp;&nbsp;<B>Distance</B>
				</TD>
				<TD ALIGN = "MIDDLE">
					<B>No. of Occurrences</B>
				</TD>
				<TD ALIGN = "MIDDLE">
					<B>In No. of Files</B>
				</TD>					
				<TD ALIGN = "MIDDLE">
					<B>Percent</B>
				</TD>
			</TR>
			<TR BGCOLOR="#DDDDDD">

END
		
		for ($i = $params{minus}; $i <= $params{plus}; $i++) {
			if ($i == 0) {
				print "<TR><TD COLSPAN=\"4\"></TD></TR>\n";
				next;
			}
			$frequency = $frequency{$i};
			$numFiles = $fileNums{$i};
			if (!$frequency) {
				$theLink="$i";
				$frequency="0";
				$percent = "0";
			} else {
				if (($params{coll} == 2) && (!$params{lemmaType})) {
					$sqlQ="SELECT beginPosition, endPosition, dist, item INTO OUTFILE '/$bwMySQLTempPath/${dbname}_new' from $bwMYSQLusertable.$dbname where $itemOrTag regexp $searchWord AND dist=$i $tagRegexp";
				} else {
					$sqlQ="SELECT beginPosition, endPosition, dist, item INTO OUTFILE '/$bwMySQLTempPath/${dbname}_new' from $bwMYSQLusertable.$dbname where $itemOrTag=$searchWord AND dist=$i $tagRegexp";
				}
				$sqlQ=~s/(\W)/sprintf("%%%x", ord($1))/eg;

				$history=$params{thMode};
				if ($params{coll} == 1) {
					$history .= "##L$theShowWord#$frequency#$i";
				} elsif ($params{coll} == 2) {
					$history .= "##B$theShowWord#$frequency#$i";
				} else {
					$history .= "##R$theShowWord#$frequency#$i#$tagClean";
				}
				$history =~ s/(\W)/sprintf("%%%x", ord($1))/eg;

				$theLink='<A HREF="/' . $bwCGIalias . '/main.pl?theData=' . $paramsEnc{theData} . '&chunk=1&inst=' . $paramsEnc{inst} . '&max=INIT&qname=' . $paramsEnc{qname} . '&queryID=' . $paramsEnc{queryID} . '&theID=' . $paramsEnc{theID} . '&SQL=' . $sqlQ . '&program=collocations&word=' . $paramsEnc{word} . '&distance=' . $i . '&dbname=' . $paramsEnc{dbname} . '&queryID=' . $paramsEnc{queryID} . '&view=' . $view . '&thMode=' . $history . '&urlTest=yes">' . $i . '</A>';
			}
			
			$percent = int((($frequency / $countTot) * 10000) + 0.5)/ 100 if ($countTot);
			
			print "<TR BGCOLOR=\"#EEEEEE\"><TD BGCOLOR=\"EEEEEE\">&nbsp;&nbsp;&nbsp;&nbsp;$theLink</TD><TD ALIGN=\"MIDDLE\">$frequency</TD><TD ALIGN=\"MIDDLE\">$numFiles</TD><TD ALIGN=\"MIDDLE\"><I>${percent}%</I></TD></TR>\n";
		}
		
		
		print "<TR BGCOLOR=\"#DDDDDD\"><TD>&nbsp;<B>Total</B></TD><TD ALIGN=\"MIDDLE\"><B>$countTot</B></TD><TD>&nbsp;</TD><TD ALIGN=\"MIDDLE\"><B><I>100%</I></B></TD></TR>\n";
		print "</TABLE>\n";

		undef %frequency;
	} else {
		$minusTotal=-($params{totrange});
		print <<END;
		<TABLE BORDER="0" CELLPADDING="2" WIDTH="100%">
			<TR BGCOLOR="#CCCCFF">
				<TD HEIGHT="35">
					<B>&nbsp;<I>$wordClean</I> $tagName does not collocate with <I>$theDataShow</I> within a window of $minusTotal and $params{totrange}.</B>
				</TD>
			</TR>
		</TABLE>
END
	}
}

&printFooter();
exit(0);


sub printForm {
	#set variables for selected items in forms
	$selected{$params{plus}}="SELECTED";
	$selected1{$params{minus}}="SELECTED";
	$selected2{$params{statType}}="SELECTED";
	$selected3{$params{basis}}="SELECTED";
	$selected4{$params{totfreq}}="SELECTED";
	$selected5{$params{delRare}}="SELECTED";
	$selected6{$params{coll}}="SELECTED";
	$tagSelected{$tagClean}="SELECTED";
	$lemmaSelected{$params{lemmaType}}="SELECTED";

	
	$^W=0;
	if (!$params{coll}) {
		$showRestrictions="<SELECT NAME=\"tag\">
			<OPTION $tagSelected{all} VALUE=\"all\" > no restrictions
			<OPTION $tagSelected{'VERB'} VALUE=\"VERB\"> any verb
			<OPTION $tagSelected{\"SUBST\"} VALUE=\"SUBST\"> any noun
			<OPTION $tagSelected{\"ADJ\"} VALUE=\"ADJ\"> any adjective
			<OPTION $tagSelected{\"ADV\"} VALUE=\"ADV\"> any adverb
			<OPTION $tagSelected{\"ART\"} VALUE=\"ART\"> any article
			<OPTION $tagSelected{\"CONJ\"} VALUE=\"CONJ\"> any conjunction
			<OPTION $tagSelected{\"PREP\"} VALUE=\"PREP\"> any preposition
			<OPTION $tagSelected{\"PRON\"} VALUE=\"PRON\"> any pronoun
			<OPTION $tagSelected{\"INTERJ\"} VALUE=\"INTERJ\"> any interjection
			<OPTION $tagSelected{\"STOP\"} VALUE=\"STOP\"> any punctuation
			<OPTION $tagSelected{'AJ0'}>AJ0
			<OPTION $tagSelected{'AJC'}>AJC
			<OPTION $tagSelected{'AJS'}>AJS
			<OPTION $tagSelected{'AT0'}>AT0
			<OPTION $tagSelected{'AV0'}>AV0
			<OPTION $tagSelected{'AVP'}>AVP
			<OPTION $tagSelected{'AVQ'}>AVQ
			<OPTION $tagSelected{'CJC'}>CJC
			<OPTION $tagSelected{'CJS'}>CJS
			<OPTION $tagSelected{'CJT'}>CJT
			<OPTION $tagSelected{'CRD'}>CRD
			<OPTION $tagSelected{'DPS'}>DPS
			<OPTION $tagSelected{'DT0'}>DT0
			<OPTION $tagSelected{'DTQ'}>DTQ
			<OPTION $tagSelected{'EX0'}>EX0
			<OPTION $tagSelected{'ITJ'}>ITJ
			<OPTION $tagSelected{'NN0'}>NN0
			<OPTION $tagSelected{'NN1'}>NN1
			<OPTION $tagSelected{'NN2'}>NN2
			<OPTION $tagSelected{'NP0'}>NP0
			<OPTION $tagSelected{'ORD'}>ORD
			<OPTION $tagSelected{'PNI'}>PNI
			<OPTION $tagSelected{'PNP'}>PNP
			<OPTION $tagSelected{'PNQ'}>PNQ
			<OPTION $tagSelected{'PNX'}>PNX
			<OPTION $tagSelected{'POS'}>POS
			<OPTION $tagSelected{'PRF'}>PRF
			<OPTION $tagSelected{'PRP'}>PRP
			<OPTION $tagSelected{'TO0'}>TO0
			<OPTION $tagSelected{'UNC'}>UNC
			<OPTION $tagSelected{'VBB'}>VBB
			<OPTION $tagSelected{'VBD'}>VBD
			<OPTION $tagSelected{'VBG'}>VBG
			<OPTION $tagSelected{'VBI'}>VBI
			<OPTION $tagSelected{'VBN'}>VBN
			<OPTION $tagSelected{'VBZ'}>VBZ
			<OPTION $tagSelected{'VDB'}>VDB
			<OPTION $tagSelected{'VDD'}>VDD
			<OPTION $tagSelected{'VDG'}>VDG
			<OPTION $tagSelected{'VDI'}>VDI
			<OPTION $tagSelected{'VDN'}>VDN
			<OPTION $tagSelected{'VDZ'}>VDZ
			<OPTION $tagSelected{'VHB'}>VHB
			<OPTION $tagSelected{'VHD'}>VHD
			<OPTION $tagSelected{'VHG'}>VHG
			<OPTION $tagSelected{'VHI'}>VHI
			<OPTION $tagSelected{'VHN'}>VHN
			<OPTION $tagSelected{'VHZ'}>VHZ
			<OPTION $tagSelected{'VM0'}>VM0
			<OPTION $tagSelected{'VVB'}>VVB
			<OPTION $tagSelected{'VVD'}>VVD
			<OPTION $tagSelected{'VVG'}>VVG
			<OPTION $tagSelected{'VVI'}>VVI
			<OPTION $tagSelected{'VVN'}>VVN
			<OPTION $tagSelected{'VVZ'}>VVZ
			<OPTION $tagSelected{'XX0'}>XX0
			<OPTION $tagSelected{'ZZ0'}>ZZ0
			<OPTION $tagSelected{'AJ0_AV0'} VALUE=\"AJ0_AV0\">AJ0-AV0
			<OPTION $tagSelected{'AJ0_NN1'} VALUE=\"AJ0_NN1\">AJ0-NN1
			<OPTION $tagSelected{'AJ0_VVD'} VALUE=\"AJ0_VVD\">AJ0-VVD
			<OPTION $tagSelected{'AJ0_VVG'} VALUE=\"AJ0_VVG\">AJ0-VVG
			<OPTION $tagSelected{'AJ0_VVN'} VALUE=\"AJ0_VVN\">AJ0-VVN
			<OPTION $tagSelected{'AV0_AJ0'} VALUE=\"AV0_AJ0\">AV0-AJ0
			<OPTION $tagSelected{'AVP_PRP'} VALUE=\"AVP_PRP\">AVP-PRP
			<OPTION $tagSelected{'AVQ_CJS'} VALUE=\"AVQ_CJS\">AVQ-CJS
			<OPTION $tagSelected{'CJS_AVQ'} VALUE=\"CJS_AVQ\">CJS-AVQ
			<OPTION $tagSelected{'CJS_PRP'} VALUE=\"CJS_PRP\">CJS-PRP
			<OPTION $tagSelected{'CJT_DT0'} VALUE=\"CJT_DT0\">CJT-DT0
			<OPTION $tagSelected{'CRD_PNI'} VALUE=\"CRD_PNI\">CRD-PNI
			<OPTION $tagSelected{'DT0_CJT'} VALUE=\"DT0_CJT\">DT0-CJT
			<OPTION $tagSelected{'NN1_AJ0'} VALUE=\"NN1_AJ0\">NN1-AJ0
			<OPTION $tagSelected{'NN1_NP0'} VALUE=\"NN1_NP0\">NN1-NP0
			<OPTION $tagSelected{'NN1_VVB'} VALUE=\"NN1_VVB\">NN1-VVB
			<OPTION $tagSelected{'NN1_VVG'} VALUE=\"NN1_VVG\">NN1-VVG
			<OPTION $tagSelected{'NN2_VVZ'} VALUE=\"NN2_VVZ\">NN2-VVZ
			<OPTION $tagSelected{'NP0_NN1'} VALUE=\"NP0_NN1\">NP0-NN1
			<OPTION $tagSelected{'PNI_CRD'} VALUE=\"PNI_CRD\">PNI-CRD
			<OPTION $tagSelected{'PRP_AVP'} VALUE=\"PRP_AVP\">PRP-AVP
			<OPTION $tagSelected{'PRP_CJS'} VALUE=\"PRP_CJS\">PRP-CJS
			<OPTION $tagSelected{'VVB_NN1'} VALUE=\"VVB_NN1\">VVB-NN1
			<OPTION $tagSelected{'VVD_AJ0'} VALUE=\"VVD_AJ0\">VVD-AJ0
			<OPTION $tagSelected{'VVD_VVN'} VALUE=\"VVD_VVN\">VVD-VVN
			<OPTION $tagSelected{'VVG_AJ0'} VALUE=\"VVG_AJ0\">VVG-AJ0
			<OPTION $tagSelected{'VVG_NN1'} VALUE=\"VVG_NN1\">VVG-NN1
			<OPTION $tagSelected{'VVN_AJ0'} VALUE=\"VVN_AJ0\">VVN-AJ0
			<OPTION $tagSelected{'VVN_VVD'} VALUE=\"VVN_VVD\">VVN-VVD
			<OPTION $tagSelected{'VVZ_NN2'} VALUE=\"VVZ_NN2\">VVZ-NN2
			<OPTION $tagSelected{'PUL'}>PUL
			<OPTION $tagSelected{'PUN'}>PUN
			<OPTION $tagSelected{'PUQ'}>PUQ
			<OPTION $tagSelected{'PUR'}>PUR
		</SELECT>";
	} elsif ($params{coll} == 2) {
		
		if ($wordClean) {
			$wordCleanDisplay = $wordClean;
			$wordCleanDisplay=~s/_(ADJ|ADV|ART|CONJ|INTERJ|PREP|PRON|SUBST|VERB|UNC)\Z//;
			$lemmaSelected{$1}="SELECTED";
		}
		$showRestrictions="<SELECT NAME=\"lemmaType\">
			<OPTION VALUE=\"\"> show all </OPTION>
			<OPTION $lemmaSelected{'ADJ'}>ADJ </OPTION>
			<OPTION $lemmaSelected{'ADV'}>ADV </OPTION>
			<OPTION $lemmaSelected{'ART'}>ART </OPTION>
			<OPTION $lemmaSelected{'CONJ'}>CONJ </OPTION>
			<OPTION $lemmaSelected{'INTERJ'}>INTERJ </OPTION>
			<OPTION $lemmaSelected{'PREP'}>PREP </OPTION>
			<OPTION $lemmaSelected{'PRON'}>PRON </OPTION>
			<OPTION $lemmaSelected{'SUBST'}>SUBST </OPTION>
			<OPTION $lemmaSelected{'VERB'}>VERB </OPTION>
			<OPTION $lemmaSelected{'UNC'}>UNC </OPTION>
		</SELECT>";
	} else {
		$showRestrictions="unavailable <INPUT TYPE=\"hidden\" NAME=\"tag\" VALUE=\"$tagClean\">";
	}
	
	$lemmaSelector="<OPTION VALUE=\"2\" $selected6{2}>collocations on lemma" if ($params{includeLemma} eq "yes");
	$theData2=&encodeForm($params{theData});
	
	print <<END;
	
	<FORM ACTION="/$bwCGIalias/dlogs.pl" METHOD=GET>

	<INPUT TYPE="hidden" NAME="qname" VALUE="$params{qname}">
	<INPUT TYPE="hidden" NAME="queryID" VALUE="$params{queryID}">
	<INPUT TYPE="hidden" NAME="dbname" VALUE="$params{dbname}">
	<INPUT TYPE="hidden" NAME="numOfSolutions" VALUE="$params{numOfSolutions}">
	<INPUT TYPE="hidden" NAME="begin" VALUE="0">
	<INPUT TYPE="hidden" NAME="theData" VALUE="$theData2">
	<INPUT TYPE="hidden" NAME="word" VALUE="">
	<INPUT TYPE="hidden" NAME="thinning" VALUE="$params{thinning}">
	<INPUT TYPE="hidden" NAME="excludePun" VALUE="$params{excludePun}">
	<INPUT TYPE="hidden" NAME="down" VALUE="0">
	<INPUT TYPE="hidden" NAME="totrange" VALUE="$params{totrange}">
	<INPUT TYPE="hidden" NAME="thMode" VALUE="$params{thMode}">
	<INPUT TYPE="hidden" NAME="inst" VALUE="$params{inst}">
	<INPUT TYPE="hidden" NAME="includeLemma" VALUE="$params{includeLemma}">
	<INPUT TYPE="hidden" NAME="fListCache" VALUE="$params{fListCache}">
	<INPUT TYPE="hidden" NAME="sentBound" VALUE="$params{sentBound}">
	<INPUT TYPE="hidden" NAME="theID" VALUE="$params{theID}">
	<TR>
		<TD WIDTH=25% BGCOLOR="#DDDDDD">
			Information:
		</TD>		
		<TD WIDTH=25% BGCOLOR="#DDDDDD">
			<SELECT NAME="coll">
				<OPTION VALUE="0" $selected6{0}>collocations
				<OPTION VALUE="1" $selected6{1}>collocations on POS-tags
				$lemmaSelector
			</SELECT>
		</TD>

		<TD WIDTH=25% BGCOLOR="#DDDDDD">
			Statistics:
		</TD>
		<TD WIDTH=25% BGCOLOR="#DDDDDD">
			<SELECT NAME="statType">
				<OPTION VALUE="1" $selected2{1}>Mutual information
				<OPTION VALUE="2" $selected2{2}>MI3
				<OPTION VALUE="3" $selected2{3}>Z-score
				<OPTION VALUE="4" $selected2{4}>T-score
				<OPTION VALUE="6" $selected2{6}>Log-likelihood
				<OPTION VALUE="7" $selected2{7}>Dice coefficient
				<OPTION VALUE="0" $selected2{0}>Rank by frequency
			</SELECT>
		</TD>
	</TR>
	<TR>
	
		<TD WIDTH=25% BGCOLOR="#DDDDDD">
			Collocation window span: 
		</TD>
		<TD WIDTH=25% BGCOLOR="#DDDDDD">
			<SELECT NAME="minus">
END

	$minusTot=0 - $params{totrange};
	for ($t=$minusTot; $t<= $params{totrange}; $t++) {
		next if ($t == 0);
		$showNumber=sqrt($t * $t);
		if ($t < 0) {
			print "<OPTION VALUE=\"$t\" $selected1{$t}>$showNumber Left";
		} else {
			print "<OPTION VALUE=\"$t\" $selected1{$t}>$t Right";
		}

	}
	print "</SELECT> - <SELECT NAME=\"plus\">";
	for ($t=$minusTot; $t<= $params{totrange}; $t++) {
		next if ($t == 0);
		$showNumber=sqrt($t * $t);
		if ($t < 0) {
			print "<OPTION VALUE=\"$t\" $selected{$t}>$showNumber Left";
		} else {
			print "<OPTION VALUE=\"$t\" $selected{$t}>$t Right";
		}

	}


$displayOpt = "<OPTION VALUE=\"\" $selected3{0}>subcorpus" if ($params{fListCache});

	print <<END;
			</SELECT>
		</TD>
		<TD WIDTH=25% BGCOLOR="#DDDDDD">
			Basis:
		</TD>
		<TD WIDTH=25% BGCOLOR="#DDDDDD">
			<SELECT NAME="basis">
				$displayOpt
				<OPTION VALUE="1" $selected3{1}>whole BNC
				<OPTION VALUE="2" $selected3{2}>written texts only
				<OPTION VALUE="3" $selected3{3}>spoken texts only
			</SELECT>
			
		</TD>
		
	</TR>

	<TR>
		<TD WIDTH=25% BGCOLOR="#DDDDDD" nowrap>
			Freq(node, collocate) at least:
		</TD>
		<TD WIDTH=25% BGCOLOR="#DDDDDD">
			<SELECT NAME="delRare">
				<OPTION $selected5{1}>1
				<OPTION $selected5{2}>2
				<OPTION $selected5{3}>3
				<OPTION $selected5{4}>4
				<OPTION $selected5{5}>5
				<OPTION $selected5{6}>6
				<OPTION $selected5{7}>7
				<OPTION $selected5{8}>8
				<OPTION $selected5{9}>9
				<OPTION $selected5{10}>10
				<OPTION $selected5{15}>15
				<OPTION $selected5{20}>20
				<OPTION $selected5{50}>50
				<OPTION $selected5{100}>100
			</SELECT>
		</TD>
		<TD WIDTH=25% BGCOLOR="#DDDDDD">
			Freq(collocate) at least:
		</TD>
		<TD WIDTH=25% BGCOLOR="#DDDDDD">
			<SELECT NAME="totfreq">
				<OPTION $selected4{1}>1
				<OPTION $selected4{2}>2
				<OPTION $selected4{3}>3
				<OPTION $selected4{4}>4
				<OPTION $selected4{5}>5
				<OPTION $selected4{6}>6
				<OPTION $selected4{7}>7
				<OPTION $selected4{8}>8
				<OPTION $selected4{9}>9
				<OPTION $selected4{10}>10
				<OPTION $selected4{15}>15
				<OPTION $selected4{20}>20
				<OPTION $selected4{50}>50
				<OPTION $selected4{100}>100
				<OPTION $selected4{500}>500
				<OPTION $selected4{1000}>1000
				<OPTION $selected4{5000}>5000
				<OPTION $selected4{10000}>10000
				<OPTION $selected4{20000}>20000
				<OPTION $selected4{50000}>50000
			</SELECT>
		</TD>
	</TR>
	
END

	if ($params{coll} == 2) {
		$showRestrictionDisplay="and/or lemma class: $showRestrictions";
		$showCollocateDisplay = "Specific collocate: ";
	} elsif ($params{coll} == 1) {
		$showRestrictionDisplay="<FONT COLOR=\"#666666\">tag restriction: n/a</FONT>";
		$showCollocateDisplay = "Specific tag: ";
	} else {
		$showRestrictionDisplay="and/or tag: $showRestrictions";
		$showCollocateDisplay = "Specific collocate: ";
	}
	
		print <<END;
		<TR>
			<TD BGCOLOR="#BBBBBB" NOWRAP>
				Filter results by:
			</TD>
			<TD BGCOLOR="#BBBBBB" NOWRAP>
				$showCollocateDisplay
				<INPUT TYPE="text" NAME="word" SIZE=15 MAXLENGTH=40 VALUE="$wordCleanDisplay">
			</TD>
			<TD BGCOLOR="#BBBBBB" NOWRAP>
				$showRestrictionDisplay
			</TD>
			<TD BGCOLOR="#BBBBBB" NOWRAP>
					<SELECT NAME="selected">
						<OPTION VALUE="collocation_redirect">Submit changed parameters
						<OPTION VALUE="collocation_download">Download all results
						<OPTION>New Query</OPTION>
					</SELECT>
					<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
					<INPUT TYPE="submit" VALUE=" Go! "> 
			</TD>
			</FORM>
		</TR>
		<TR>
			<TD COLSPAN="4" HEIGHT="1">
			</TD>
		</TR>
	</TABLE>
END

	$^W=1;
}


sub make_SQL_query {

	local($method)=$_[0];
	local($word)=$_[1];
	local($sqlQuery);
	

	if ($word) {
		if (($params{coll} == 2) && (!$params{lemmaType})) {
			$SQL_rest = "where $concatWord = $basisWordOrTag
				and $rangeString
				$tagRegexp
				and $itemOrTag regexp $searchWord 
				group by $itemOrTag";
		
		} else {
			$SQL_rest = "where $concatWord = $basisWordOrTag
				and $rangeString
				$tagRegexp
				and $itemOrTag = $searchWord 
				group by $itemOrTag";
		}
	} else {
		$SQL_rest = "where $concatWord = $basisWordOrTag
			and $rangeString
			$tagRegexp
			and $wordFreq.freq >= $params{totfreq}
			group by $itemOrTag
			$having
			order by result desc $limitString";
	}
	
	if ($method == 1) {
		$sqlQuery="select $itemOrTag, count($itemOrTag) as sum,
			log2($O11 / $E11) as result, $wordFreq.freq, count(distinct(fname)), format($E11, 3)
			from $bwMYSQLusertable.$dbname, $wordFreq
			$SQL_rest";
	} elsif ($method == 2) {
		$sqlQuery="select $itemOrTag, count($itemOrTag) as sum,
			3 * log2($O11) - log2($E11) as result, 
			$wordFreq.freq, count(distinct(fname)), format($E11, 3)
			from $bwMYSQLusertable.$dbname, $wordFreq 
			$SQL_rest";
	} elsif ($method == 3) {
		$sqlQuery="select $itemOrTag, count($itemOrTag) as sum,
			sign($O11 - $E11) * if(abs($O11 - $E11) > 0.5, abs($O11 - $E11) - 0.5, $abs($O11 - $E11) / 2) / sqrt($E11) as result, 
			$wordFreq.freq, count(distinct(fname)), format($E11, 3)
			from $bwMYSQLusertable.$dbname, $wordFreq
			$SQL_rest";
	} elsif ($method == 4) {
		$sqlQuery="select $itemOrTag, count($itemOrTag) as sum,
			($O11 - $E11) / sqrt($O11) as result, 
			$wordFreq.freq, count(distinct(fname)), format($E11, 3)
			from $bwMYSQLusertable.$dbname, $wordFreq
			$SQL_rest";
	} elsif ($method == 6) {
		$sqlQuery="select $itemOrTag, count($itemOrTag) as sum,
			sign($O11 - $E11) * 2 * (
				IF($O11 > 0, $O11 * log($O11 / $E11), 0) +
				IF($O12 > 0, $O12 * log($O12 / $E12), 0) +
				IF($O21 > 0, $O21 * log($O21 / $E21), 0) +
				IF($O22 > 0, $O22 * log($O22 / $E22), 0)
			) as result, 
			$wordFreq.freq, count(distinct(fname)), format($E11, 3)
			from $bwMYSQLusertable.$dbname, $wordFreq 
			$SQL_rest";
	} elsif ($method == 7) {
		$sqlQuery="select $itemOrTag, count($itemOrTag) as sum,
			2 / ((1 / $P_COLL_NODE) + (1 / $P_NODE_COLL)) as result, 
			$wordFreq.freq, count(distinct(fname)), format($E11, 3)
			from $bwMYSQLusertable.$dbname, $wordFreq 
			$SQL_rest";
	}

	return ($sqlQuery);
}
