#!/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;
use DBI;

# connect to mysql
$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");
$userHandle = DBI->connect("DBI:mysql:$bwMYSQLusertable:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd") or bark("Can't connect to mysql on $bwMYSQLhost at port: $bwMysqlPort <BR>" . $userHandle::errstr);

$username = $ENV{'REMOTE_USER'};
$thisProg="sort_freqList_download.pl";

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

$crlf = &linefeed();

if ($params{display} eq "tag") {
	$Text = "Frequency breakdown of tags in position \"$params{showPosition}\"";
} elsif ($params{display} eq "both") {
	$Text = "Frequency breakdown of word-tag combinations in position \"$params{showPosition}\"";
} elsif ($params{display} eq "word") {
	$Text = "Frequency breakdown of word forms in position \"$params{showPosition}\"";
}


$titleBar = &createTitle($params{thMode}, $params{theData}, $params{numOfSolutions}, $params{numOfFiles}, "", $numTotalWords, $numTotalWordsOrig, $params{subcorpus}, $theRestrictions, $timeTaken, $inCache, $numOfSpeakers);
$titleBar=~s/<\/?(b|i)>//gi;
$titleBar=~s/&quot;/"/gi;

print "Content-Type: text/text\n";
print "Content-disposition: attachment; filename=frequency_list.txt\n\n";
print "$titleBar$crlf$crlf$Text$crlf";
print "********************************************$crlf$crlf";

	
$sth = $userHandle->prepare($params{downloadSQL}) or barkfull("Can't prepare $sql: " . $userHandle::errstr);
$sth->execute or bark("Can't execute $sql: $sth::errstr");
$resArrayResult=$sth->fetchall_arrayref or barkfull("Can't fetch $sqlQuery: " . $sth::errstr);

loop: for $k ( 0 .. ($#{$resArrayResult}) ) {
	$theString=$resArrayResult->[$k][0];
	$theFrequency=$resArrayResult->[$k][1];
	$percent = int((($theFrequency / $params{total}) * 10000) + 0.5)/ 100;


	$count = $k + 1;
	print "$count\t$theString\t$theFrequency\t${percent}%$crlf";
	$countTotal += $theFrequency;
}

print "$crlf********************************************$crlf";
print "Total number of tokens: $countTotal$crlf";


exit(0);
