#!/bin/bash

SIZE=xl
SET=train
SET=test

#KBIN=/home/p106777/Downloads/PPDB-KB_v1/${SET}/all_predicted_labels.txt
KBIN=/home/p106777/Downloads/PPDB-KB_v4/${SET}/${SIZE}.txt
KBIN=/home/p106777/Downloads/PPDB-KB_v4/${SET}/lexical_human_labels.handcorrected.txt
KBIN=/Users/johanbos/Downloads/lexical_human_labels.handcorrected.txt
KBIN=${USER}/Downloads/vanilla-KB/${SET}/${SIZE}.txt
KBIN=${USER}/Downloads/PPDB-KB_v4/${SET}/all_predicted_labels.txt

KBOUT=working/wordnet/vanilla_${SET}_${SIZE}.pl
KBOUT=working/wordnet/ellie_${SET}.pl
KBOUT=working/wordnet/human_${SET}.pl

init()
{
    rm -f ${KBOUT}
    # some placeholders for synp synr to prevent bin/nc from complaining
    echo "% this file generated by: $0" >> ${KBOUT}
    echo "synp(nil,1,nil,2)." >> ${KBOUT}
    echo "synr(nil,1,nil,2)." >> ${KBOUT}
}

convert()
{
    echo "hypernyms n"
    echo "isan(nil,1,nil,2)." >> ${KBOUT}
    cat ${KBIN} | grep ^n | grep hypernym | cut -f4 > working/KBpiece2.txt
    cat ${KBIN} | grep ^n | grep hypernym | cut -f5 > working/KBpiece1.txt
    paste working/KBpiece1.txt working/KBpiece2.txt | sort -u | tr "\t" "," | sed 's/^/isan(/' | sed 's/$/,1)./' | sed 's/,/,1,/' >> ${KBOUT}
    echo "synonyms n"
    echo "synn(nil,1,nil,2)." >> ${KBOUT}
    cat ${KBIN} | grep ^n | grep synonym | cut -f4 > working/KBpiece2.txt
    cat ${KBIN} | grep ^n | grep synonym | cut -f5 > working/KBpiece1.txt
    paste working/KBpiece1.txt working/KBpiece2.txt | sort -u | tr "\t" "," | sed 's/^/synn(/' | sed 's/$/,1)./' | sed 's/,/,1,/' >> ${KBOUT}
    echo "antonyms n"
    echo "isnotan(nil,1,nil,2)." >> ${KBOUT}
    cat ${KBIN} | grep ^n | grep exclusion | cut -f4 > working/KBpiece2.txt
    cat ${KBIN} | grep ^n | grep exclusion | cut -f5 > working/KBpiece1.txt
    paste working/KBpiece1.txt working/KBpiece2.txt | sort -u | tr "\t" "," | sed 's/^/isnotan(/' | sed 's/$/,1)./' | sed 's/,/,1,/' >> ${KBOUT}
    echo "hypernyms v"
    echo "isav(nil,1,nil,2)." >> ${KBOUT}
    cat ${KBIN} | grep ^v | grep hypernym | cut -f4 > working/KBpiece2.txt
    cat ${KBIN} | grep ^v | grep hypernym | cut -f5 > working/KBpiece1.txt
    paste working/KBpiece1.txt working/KBpiece2.txt | sort -u | tr "\t" "," | sed 's/^/isav(/' | sed 's/$/,1)./' | sed 's/,/,1,/' >> ${KBOUT}
    echo "synonyms v"
    echo "synv(nil,1,nil,2)." >> ${KBOUT}
    cat ${KBIN} | grep ^v | grep synonym | cut -f4 > working/KBpiece2.txt
    cat ${KBIN} | grep ^v | grep synonym | cut -f5 > working/KBpiece1.txt
    paste working/KBpiece1.txt working/KBpiece2.txt | sort -u | tr "\t" "," | sed 's/^/synv(/' | sed 's/$/,1)./' | sed 's/,/,1,/' >> ${KBOUT}
    echo "antonyms v"
    echo "isnotav(nil,1,nil,2)." >> ${KBOUT}
    cat ${KBIN} | grep ^v | grep exclusion | cut -f4 > working/KBpiece2.txt
    cat ${KBIN} | grep ^v | grep exclusion | cut -f5 > working/KBpiece1.txt
    paste working/KBpiece1.txt working/KBpiece2.txt | sort -u | tr "\t" "," | sed 's/^/isnotav(/' | sed 's/$/,1)./' | sed 's/,/,1,/' >> ${KBOUT}
    echo "synonyms a"
    echo "syna(nil,1,nil,2)." >> ${KBOUT}
    cat ${KBIN} | grep ^a | grep synonym | cut -f4 > working/KBpiece2.txt
    cat ${KBIN} | grep ^a | grep synonym | cut -f5 > working/KBpiece1.txt
    paste working/KBpiece1.txt working/KBpiece2.txt | sort -u | tr "\t" "," | sed 's/^/syna(/' | sed 's/$/,1)./' | sed 's/,/,1,/' >> ${KBOUT}
    echo "antonyms a"
    echo "isnotaa(nil,1,nil,2)." >> ${KBOUT}
    cat ${KBIN} | grep ^a | grep exclusion | cut -f4 > working/KBpiece2.txt
    cat ${KBIN} | grep ^a | grep exclusion | cut -f5 > working/KBpiece1.txt
    paste working/KBpiece1.txt working/KBpiece2.txt | sort -u | tr "\t" "," | sed 's/^/isnotaa(/' | sed 's/$/,1)./' | sed 's/,/,1,/' >> ${KBOUT}
    echo "cleaning up"
    rm -fr working/KBpiece?.txt
}

init
convert
