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

print "Content-Type: text/text\n";
print "Content-disposition: attachment; filename=collocations.txt\n\n";

$|=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'};

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

$theDataShow=$params{theData};
#$theDataShow =~ s/\650/\'/go;
$theDataShow=~s/\</&lt\;/g;
$theDataShow=~s/\>/&gt\;/g;

$wordClean=$params{word};
$wordClean=~s/ /~/g;

$tagClean=$params{tag};

$hitGroupTag=0;
$check=$tagClean=~m/(ADJ|ADV|ART|CONJ|INTERJ|PREP|PRON|SUBST|VERB|UNC|STOP)/;
$hitGroupTag=1 if ($check);

$having = "HAVING sum >= $params{delRare}";

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

$crlf = &linefeed();

print "Collocation Database$crlf$crlf";
$limitString="";

# set variables for different calculations of collocation values depending on sub-corpus chosen
$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);
		}
	}
	$dispBasis="the whole BNC";			
	$totBasis=112102325 * $params{thinning};
} 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 subcorpor - 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}== 4);
	
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";
	$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 = "lemmata";
} else {
	$sqlQuery = "SELECT distinct(item) FROM $dbname";
	$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";
}

#$check = $params{thMode}=~m/\AM(\d+)#?/;


$titleBar = &createTitle($params{thMode}, $theDataShow, $numOfSolutions);
$titleBar=~s/\&quot\;/\"/g;
$titleBar=~s/<[^>]+>//g;
print "There are $typesTotal different $blurb in your collocation database for the query \"$theDataShow\". ($titleBar)$crlf";

$theMinus="\xAD";
$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";
}

print "The selected range was $params{minus} to $params{plus}.$crlf";
print "Corpus basis for calculation: $dispBasis. $crlf";
print "Type of calculation: $baseOfCalc $crlf" unless ($params{word});
print "Tag restriction: $tagRestriction $crlf" if ($tagRestriction);
print "Collocates occur at least $params{totfreq} times in $dispBasis. $crlf";
print "Words collocate at least $params{delRare} times. $crlf";

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) {
		print "------------------------------------------------------------------------------$crlf";

		if ($params{statType} == 0) {
			if (!$params{coll}) {
				print "No.\tWord\tn\tn expected\tn observed\tn texts $crlf";
			} elsif ($params{coll} == 2) {
				print "No.\tLemma\tn\tn expected\tn observed\tn texts $crlf";
			} else {
				print "No.\tTag\tn\tn expected\tn observed\tn texts $crlf";
			}
		} else {
			if (!$params{coll}) {
				print "No.\tWord\tn\tn expected\tn observed\tn texts\tColl. value $crlf";
			} elsif ($params{coll} == 2) {
				print "No.\tLemma\tn\tn expected\tn observed\tn texts\tColl. value $crlf";
			} else {
				print "No.\tTag\tn\tn expected\tn observed\tn texts\tColl. value $crlf";
			}
		}
		print "------------------------------------------------------------------------------$crlf";
		

		# 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;
			$indicator=$k + 1;

			if ($theColValue) {
				if ($params{statType} == 0) {
					print "$indicator\t$theWord\t$theOccurValue\t$expectedValue\t$theFrequency\t$numOfFiles$crlf";
				} else {
					print "$indicator\t$theWord\t$theOccurValue\t$expectedValue\t$theFrequency\t$numOfFiles\t$theColValue$crlf";
				}
			} else {
				print "$indicator\t$theWord\t$theOccurValue\t$expectedValue\t$theFrequency\t$numOfFiles\tn/a$crlf";
			}

		}		
	} else {
		print "------------------------------------------------------------------------------$crlf";
		print "No.\tWord\tn\tn expected\tn observed\tn texts$crlf";
		print "------------------------------------------------------------------------------$crlf";

		$sqlQuery="select $itemOrTag, count($itemOrTag) 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];
			$indicator=$k + 1;
			
			print "$indicator\t$theWord\t$theOccurValue\t$expectedValue\t$theFrequency\t$numOfFiles$crlf";
		}
	}
	print "------------------------------------------------------------------------------$crlf";
	
} else {
	# user selected word information

	if ($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 sisze 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="";
	}
	
# 	unless (($params{tag} eq "all") || ($params{coll})) {
# 		$tagName=" with tag restriction <I>$tag</I>";
# 		$tagName=" with tag restriction <I>any verb</I>" if ($tagClean =~m/\AV\[/);
# 		$tagName=" with tag restriction <I>any noun</I>" if ($tagClean =~m/\AN\[/);
# 		$tagName=" with tag restriction <I>any conjunction</I>" if ($tagClean =~m/\ACJ\[/);
# 		$tagName=" with tag restriction <I>any adjective</I>" if ($tagClean =~m/\AAJ\[/);
# 		$tagName=" with tag restriction <I>any adverb</I>" if ($tagClean =~m/\AAV\[/);
# 		$tagName=" with tag restriction <I>any pronoun</I>" if ($tagClean =~m/\APN\[/);
# 	} else {
# 		$tagName="";
# 	}
	
	$tagName=~s/<\/?I>/\"/g;
	
	$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 ($countTot) {



		print "${crlf}$displayWord information for the node \"$theDataShow\" and \"$theShowWord\" ($theOccurValue occurrences in whole BNC)${tagName}$crlf";
		print "------------------------------------------------------------------------------$crlf";
		print "Type of Statistics\tValue (for window size $params{minus} to $params{plus})$crlf";
		print "------------------------------------------------------------------------------$crlf";
		for ($i = 1; $i <= 7; $i++) {
			next if ($i == 5); # we got rid of chi-squared
			print "$statTypeName[$i]\t$theColValue[$i]$crlf";
			
		}
		print "------------------------------------------------------------------------------$crlf$crlf";
		print "Within the window $params{minus} to $params{plus}, \"$theShowWord\" occurs $theFrequency times in $numOfFiles different files.$crlf";
		print "------------------------------------------------------------------------------$crlf";
		print "Distance\tNo. of Occurrences\tIn No. of Files\tPercent$crlf";
		print "------------------------------------------------------------------------------$crlf";
		
		for ($i = $params{minus}; $i <= $params{plus}; $i++) {
			next if ($i == 0);
			$frequency = $frequency{$i};
			$numFiles = $fileNums{$i};
			if (!$frequency) {
				$theLink="$i";
				$frequency="0";
				$percent = "0";
			}
			
			$percent = int((($frequency / $countTot) * 10000) + 0.5)/ 100 if ($countTot);
			
			print "$i\t$frequency\t$numFiles\t$percent$crlf";
		}
		
		print "------------------------------------------------------------------------------$crlf";
		print "Total\t$countTot\t\t100%$crlf";
		
		undef %frequency;
	} else {
		print "$wordClean $tagName does not collocate with \"$theDataShow\" within a window of $minusTotal and $params{totrange}.$crlf";
	}
}


exit(0);

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";
	} elsif ($method == 0) {
		

		$sqlQuery="select $itemOrTag, count($itemOrTag) as sum, 1, $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";


	}

	return ($sqlQuery);
}
