#!/usr/bin/perl
use warnings;
use Time::Stopwatch; 

################################################################																                   
# MUST HAVE TREETAGGER INSTALLED  !!	
# WORKS ONLY IN BASH TERMINAL
# ADD YOUR $ADDRESS TO TREETAGGER AND KEEP COMMENTED OTHERS
################################################################

tie my $timer, 'Time::Stopwatch'; #start counting time

#printing usage if no argument set
if (!$ARGV[0]){
  $0 =~ s/.*\\(.*)/$1/; #name of this script without path
  print "\nUSAGE:\nperl $0 <directory containing results>\n";
  exit;
}

#openning the directory with extracted titles and description
$dir = $ARGV[0];
opendir(DIR, $dir) or die "I cannot open dir: $dir";

# ADD YOURS ADDRESS AND COMMENT THE OTHERS --- HERE ---
my $address = "~/../../usr/local/TreeTagger/"; #Karolina_linux

my $tokenize = $address."cmd/tokenise-utf8.pl";
my $treetagger = $address."bin/tree-tagger";
my $french = $address."lib/french-utf8.par";

while (my $file = readdir(DIR)) { #open directory
   next unless ($file =~ m/[^(_t)]\.txt$/); #open each file.txt
	
	my $file = $dir."/".$file;
	my $save_into = substr($file,0,-4)."_t.txt";
	
	print "\ntagging of file : $file\n";
   
   #run treetagger using terminal
   system("perl $tokenize $file | $treetagger -lemma -token $french > $save_into");    
  
}

closedir(DIR);

print "\n\n === DONE in $timer s ===\n"; #printing time
