#!/usr/bin/python3
"""#### LING 570: Homework #7 - Ryan Timbrook ############
Author: Ryan Timbrook
Date: 11/15/2018
#Q2: Write conv_format.sh
This is a python program that:
    -converts the format of the output file, sys, generated by executing the viterbi.sh script
    -
Format: command line: cat file1 | conv_format.sh > file2

Input File: read into the program via stdin, it's the file created by executing viterbi.sh
    -Format: "observ => state_sequence lgprob
Output File: writen as stdout#   
	-Format: "w1/t1 w2/t2 ... wn/tn"
  -ti is the second tag of the state that generates wi, states are represented as tag1_tag2
  
From Command line, Run as: 
    $ cat sys1 | ./conv_format.sh > sys1_res
    $ cat sys2 | ./conv_format.sh > sys2_res
    $ cat sys3 | ./conv_format.sh > sys3_res
    $ cat sys4 | ./conv_format.sh > sys4_res
    $ cat sys5 | ./conv_format.sh > sys5_res
"""

import sys, time, re

#---- GLOBALS -----------------------------------------#
isTest = False
isLocal = False
cmdArgs = []


##------------------------------------------------------------------------
# Main Procedural Function
##------------------------------------------------------------------------
def main():
    if isTest: print("main: Entering")
        
    if isLocal:
        inputSys="sys"
    

    if not isLocal:
        sysLines = sys.stdin.readlines()
    else:
        with open(inputSys,'rt') as f:
            sysLines = f.readlines()
    if isTest: print("Total lines in sys file:[{0}]".format(len(sysLines)))

    #input lines format: "observ => stateSeq lgprob"
    #ouput lines format: "word1/tag1 word2/tag2 ... wordn/tagn"
    for line in sysLines:
        words,tags = (line.rstrip().split("=>"))
        words = words.strip()
        w_tokens = re.split("\\s+",words)
        t_tokens = splitTags(tags.strip())#REGRADE - there were empty strings that needed stripping
        printConvFormat(w_tokens,t_tokens)


##------------------------------------------------------------------------
# Util Function; Split input tag sequence
# format ex: "BOS_BOS BOS_NNS NNS_IN IN_DT" tags are concatinated by underscores
# ti is the second tag of the state that generates wi, states are represented as tag1_tag2
# skip the last token in tags which is that lgprob
# skip the first token in tags which is the BOS_BOS marker
##------------------------------------------------------------------------
def splitTags(tags):
    tagTokens = []
   
    #for t in re.split("\\s+",tags):
        #print(t)
    
    tagTokens = [t.split('_')[-1] for t in re.split("\\s+",tags)[1:]]#REGRADE - Due to changes in viterbi to accept BOS_BOS as first tag, this line was update from slice 1 to 2

    #tagTokens = [t.split('_')[-1] for t in re.split("\\s+",tags)[2:]]#REGRADE - Due to changes in viterbi to accept BOS_BOS as first tag, this line was update from slice 1 to 2
    
    return tagTokens
##------------------------------------------------------------------------

##------------------------------------------------------------------------
# Print converted format
##------------------------------------------------------------------------
def printConvFormat(words,tags):
    line = ""
    line = ' '.join(["{0}/{1}".format(w,t) for w,t in zip(words,tags)])
    
    print(line)
    
##------------------------------------------------------------------------



##------------------------------------------------------------------------
# Execute Main Function
##------------------------------------------------------------------------
if __name__ == "__main__":
    t0 = time.time()
    if isTest: print("Number of command line arguments:{0}".format(len(sys.argv)));
    #remove program file name from input command list
    sys.argv.remove(sys.argv[0])
    if len(sys.argv) > 0:
        for arg in sys.argv:
            if isTest: print("argument:{0}".format(arg))
            cmdArgs.append(arg.strip())
    main()
    t1 = time.time()
    duration = t1-t0
    if isTest: print("Total processing time:{0:0.10f}".format(duration))