use Lingua::EN::Tagger;
use Data::Dumper;

$text = $ARGV[0];
#print $text."\n";
$text =~ s/\\//igx;
#$text =~ s/``/"/igx;
$sentences = extract_sentences($text);
@entities = split(/,/,$ARGV[1]);
%used_tags;
$output = "";
#print $temp->[0]->[0]."\n";
#$sentences = $temp->[0];
#print $temp[0][1]."\n";
#exit;
foreach $sentence (@$sentences){
	#print Dumper($sentence);
	$sentence =~ s/``/"/gx;
	$sentence =~ s/''/"/gx;
	#print $sentence."\n";
	foreach $entity (@entities){
		#$entity = $entities[$i];
		#print $entity."\n";
		if($sentence =~ /\b$entity\b/ims){
			#print "found";
			$sentence =~ s/\b$entity\b/<span class="Entity">$entity<\/span>/g;
			#print $sentence."\n";
			#delete($entities[$i]);
		}
		else{
		#	print $entity."\n";
		}
	}
	#print $text;
	if($output ne ""){
		$output = $output." ".$sentence;
	}
	else{
		$output = $sentence;
	}
}
#foreach (@$output){
#	print $_."\n";
#i}
print $output."\n";

sub extract_sentences{
        my @split_sentences = ();
        my $tagger = new Lingua::EN::Tagger;
        my $sentences = $tagger->get_sentences($text);
        foreach my $sentence (@$sentences){
                #print "----------\n";
                #print "ORIGINAL:".$sentence."\n";
                my @temp = $sentence =~ /[\.]?(.*?\s\w{2,}[`'\)\s"]*)\.([\w{1,}]?)/imsgx;
                my $i;
                #print "---------\n";
                for ($i =0;$i<scalar(@temp);){
                        if ($i > 1){
                                #print $temp[$i-1].$temp[$i]."\n";
                                @split_sentences = (@split_sentences,$temp[$i-1].$temp[$i]);
                        }
                        else{
                #                print $temp[$i]."\n";
                                @split_sentences = (@split_sentences,$temp[$i]);
                        }
                        $i+=2;
                }
                @split_sentences = (@split_sentences,$temp[scalar(@temp)-1]);
                #print $temp[scalar(@temp)-1]."\n";
        }
        return \@split_sentences;
}

