# -*- coding: utf-8 -*-

# Author: Franck Sajous
# Copyright: CNRS/CLLE-ERSS
# Last update: 2018-03-21
# See info and licence at: http://redac.univ-tlse2.fr/lexicons/glawi/tools/

use strict;
use warnings;
use utf8;

binmode (STDOUT, ":encoding(utf8)");
binmode (STDERR, ":encoding(utf8)");

if ( ($#ARGV < 1) || ($#ARGV > 2))
{
    print ("Usage: $0 GLAWIfile labelValueRegexp [outFile]\n");
    print "\tExtracts titles:\n";
    print "\t  - either from monosemic entries whose definition (gloss) includes a label matching the specified value.\n";
    die "\t  - or from polysemic entries whose all definitions (glosses) include a label matching the specified value.\n";
} # if ( ($#ARGV < 1) || ($#ARGV > 2))

my $glawiFile  = $ARGV[0];
my $labelValue = $ARGV[1];
my $outFile    = $ARGV[2];

open (INFILE, "<", $glawiFile) or die ("Unable to read: $glawiFile\n");
binmode (INFILE, ":encoding(utf8)");

if (defined ($outFile))
{
    open (OUTFILE, ">", $outFile) or die ("Unable to write: $outFile\n");
    binmode (OUTFILE, ":encoding(utf8)");
} # if (defined ($outFile))

my ($line, $currentTitle);
my ($inArticle, $inDefinitions, $inGloss) = (0, 0, 0);
my ($nbMatchingLabels, $nbSenses);

while ($line = <INFILE>)
{
    if ($line =~ /<article>/)
    {
	$inArticle = 1;
	&parseLine ($'); #'
    } # if ($line =~ /<article>/)
    elsif ($inArticle)
    {
	if ($line =~ /<\/article>/)
	{
	    &parseLine ($`);
	    $inArticle = 0;
	} # elsif ($line =~ /<\/article>/)
	else
	{
	    &parseLine ($line);
	} # elsif ($line =~ /<\/article>/) else {
    } # elsif ($inArticle)
} # while (<INFILE>)

close (OUTFILE) if (defined ($outFile));
close (INFILE);

sub parseLine ()
{
    my $currentLine = shift;

    if ($currentLine =~ /<title>(.*?)<\/title>/)
    {
	$currentTitle = $1;
    } # if ($currentLine =~ /<title>(.*?)<\/title>/)
    elsif ($currentLine =~ /<definitions>/)
    {
	$nbSenses = 0;
	$nbMatchingLabels = 0;
    } # elsif ($currentLine =~ /<gloss>/)
    elsif ($currentLine =~ /<\/definitions>/)
    {
	if (($nbSenses > 0) && ($nbMatchingLabels >= $nbSenses))
	{
	    if (defined ($outFile))
	    {
		print OUTFILE $currentTitle . "\n";
	    } # if (defined ($outFile))
	    else
	    {
		print $currentTitle . "\n";		
	    } # if (defined ($outFile)) else {
	} # if ($nbMatchingLabels >= $nbSenses)
    } # elsif ($currentLine =~ /<\/definitions>/)
    elsif ($currentLine =~ /<\/gloss>/)
    {
	my ($l, $r) = ($`, $'); #'
	&parseLine ($l);
	$inGloss = 0;
	&parseLine ($r);
    } # elsif ($currentLine =~ /<\/gloss>/)
    elsif ($currentLine =~ /<gloss>/)
    {
	my ($l, $r) = ($`, $'); #'
	&parseLine ($l);
	$inGloss = 1;
	$nbSenses++;
	&parseLine ($r);
    } # elsif ($currentLine =~ /<gloss>/)
    elsif ($currentLine =~ /<\/gloss>/)
    {
	my ($l, $r) = ($`, $'); #'
	&parseLine ($l);
	$inGloss = 0;
	&parseLine ($r);
    } # elsif ($currentLine =~ /<\/gloss>/)
    elsif ($inGloss)
    {
	if ($line =~ /<label .*?value="([^"]*?)"/)
	{
	    my $currentValue = $1;
	    $nbMatchingLabels++ if ($currentValue =~ /$labelValue/i);
	} # if ($line =~ /<label .*?value="([^"]*?)"/)
    } # elsif ($inGloss)
} # parseLine ()
