#!/usr/local/bin/python
import sys,string;

Threshold=0.01;

def TranslateDutchTags(DutchTags):
  result=[];
  for DutchTag in DutchTags:
    if (DutchTag[0:2]=="N("):
      DutchTag="NOUN";
    elif (DutchTag[0:2]=="V("):
      DutchTag="VERB";
    elif (DutchTag[0:4]=="Adj("):
      DutchTag="ADJ";
    elif (DutchTag[0:21]=="Num(hoofd,bep,onverv)"):
      DutchTag="CD";
    elif (DutchTag[0:5]=="Punc("):
      DutchTag="";
    elif (DutchTag[0:4]=="Adv("):
      DutchTag="ADV";
    elif (DutchTag[0:5]=="Pron("):
      DutchTag="PRON";
    elif (DutchTag[0:4]=="Prep"):
      DutchTag="PREP";
    elif (DutchTag[0:4]=="Num("):
      DutchTag="ADJ";
    else:
      DutchTag="";
#     This basically skips foreign words and conjunctions at the moment,
#     lets hope they have another tag and else they're not common anyway
    if (not result.count(DutchTag)) and (DutchTag!=""):
      result.append(DutchTag);
  return result;

def TranslateEnglishTags(EnglishTags):
  result=[];
  for EnglishTag in EnglishTags:
    if (EnglishTag[0:2]=="NN"):
      EnglishTag="NOUN";
    elif (EnglishTag[0:2]=="VB"):
      EnglishTag="VERB";
    elif (EnglishTag[0:2]=="JJ"):
      EnglishTag="ADJ";
    elif (EnglishTag[0:2]=="CD"):
      EnglishTag="CD";
    elif (EnglishTag[0:2]=="RB"):
      EnglishTag="ADV";
    elif (EnglishTag[0:3]=="PRP"):
      EnglishTag="PRON";
    elif (EnglishTag[0:2]=="WP"):
      EnglishTag="PRON";
    elif (EnglishTag[0:4]=="TO"):
      EnglishTag="PREP";
    elif (EnglishTag[0:4]=="IN"):
      EnglishTag="PREP";
    else:
      EnglishTag="";
#     This basically skips foreign words and conjunctions at the moment,
#     lets hope they have another tag and else they're not common anyway
    if (not result.count(EnglishTag)) and (EnglishTag!=""):
      result.append(EnglishTag);
  return result;

def intersect(list1,list2):
  result=[];
  for item in list1:
    if (list2.count(item)) and (not result.count(item)):
      result.append(item);
  return result;


go="T";
DutchTagFile=sys.argv[1];
fd=open(DutchTagFile,'r+');
DutchTags=fd.readlines();
DutchTags=string.join(DutchTags,"\n|");
fd.close();
EnglishTagFile=sys.argv[2];
fd=open(EnglishTagFile,'r+');
EnglishTags=fd.readlines();
EnglishTags=string.join(EnglishTags,"\n|");
fd.close();

while (go=="T"):
  line = sys.stdin.readline();
  i=0;
  j=0;
  SourceToken="";
  TargetToken="";
  if (line==""):
    go="F";
  else:
    line=string.strip(line);
    i=string.find(line," ")+1;
  if (i>0):
    SourceToken=line[0:i-1];   
    j=string.find(line," ",i)+1;
  if (j>0):
    TargetToken=line[i:j-1];
    Probability=float(line[j:]);
  if(Probability>Threshold):    

#   Ok now we have a SourceToken and its translation the TargetToken,
#   now we want to try to determine the POS

#   Find Source POS
    ThisDutchTags=[];
    ThisEnglishTags=[];

    i=string.find(DutchTags,"|"+SourceToken+" ");
    if (i>0):
      i=string.find(DutchTags," ",i)+1;
      i=string.find(DutchTags," ",i)+1;
      j=string.find(DutchTags,"\n",i)+1;
      ThisDutchTags=string.split(DutchTags[i:j],"-");
      ThisDutchTags=TranslateDutchTags(ThisDutchTags);
    
#   Find Target POS

    i=string.find(EnglishTags,"|"+TargetToken+" ");
    if (i>0):
      i=string.find(EnglishTags," ",i)+1;
      i=string.find(EnglishTags," ",i)+1;
      j=string.find(EnglishTags,"\n",i)+1;
      ThisEnglishTags=string.split(EnglishTags[i:j],"-");
      ThisEnglishTags=TranslateEnglishTags(ThisEnglishTags);

    IntersectTags=intersect(ThisEnglishTags,ThisDutchTags);
    if (len(IntersectTags)>=1):
      for tag in IntersectTags:    
        output=tag+"::"+tag+" |: [\""+SourceToken+"\"] -> [\""+TargetToken+"\"]\n";
        output+="(\n  (x1::y1)\n)\n";
#        print Probability;
        print string.strip(output);
    else:
      POSType="UNK";
#    output=POSType+"::"+POSType+" |: [\""+SourceToken+"\"] -> [\""+TargetToken+"\"]\n";
#    output+="(\n  (x1::y1)\n)\n";
#    print output;
#  else:
#    output=line[j:]+": Skipping "+SourceToken+" -> "+TargetToken;
#    print output;










