#!/usr/bin/perl

use strict;
use warnings;

my $dt1 = $ARGV[0];
my $dt2 = $ARGV[1];

open(DT1,"$dt1") or die;

my $lemma_hash;
while(<DT1>){
    chomp;

    s/^\s+//; 
    s/\s+$//;
    s/ //g;

    next if $_ eq "";

    if(/\t/){
	my ($lemma,$freq) = split /\t/;
	if($freq){
	    $lemma_hash->{$lemma} = $freq;
	}else{
	    $lemma_hash->{$lemma} = 0;
	}
    }else{
	$lemma_hash->{$_} = 0;
    }
}
close DT1;

open(DT2,"$dt2") or die;

my $lemma_hash2;
while(<DT2>){
    chomp;

    s/^\s+//; 
    s/\s+$//;
    s/ //g;

    next if $_ eq "";

    if(/\t/){
	my ($lemma,$freq) = split /\t/;
	if($freq){
	    $lemma_hash2->{$lemma} = $freq;
	}else{
	    $lemma_hash2->{$lemma} = "0";
	}
    }else{
	$lemma_hash2->{$_} = "0";
    }
}
close DT2;

foreach my $key (sort {$lemma_hash2->{$b} <=> $lemma_hash2->{$a}} keys %$lemma_hash2){
    if(exists $lemma_hash->{$key}){
	    print $key,"\t",$lemma_hash->{$key},"\t",$lemma_hash2->{$key},"\n";
	}
    else{
	print $key,"\t",0,"\t",$lemma_hash2->{$key},"\n";
    }
}

foreach my $key (sort {$lemma_hash->{$b} <=> $lemma_hash->{$a}} keys %$lemma_hash){
    unless(exists $lemma_hash2->{$key}){
	print $key,"\t",$lemma_hash->{$key},"\t",0,"\n";
    }
}
