#! @TCL@ -f
#
# file: dec_read_0.tcl
#

# procedures to read the data from file, and set up lists for each
# line of data i.e. each word hypothesis
#

# procedure to open and read file
#
proc read_proc {filename} {

    # declare globals
    #
    global hyp_list
    global phone_list
    
    global traces_new
    global traces_del
    global traces_tot

    # define local variables
    #
    global word_hyp
    global word_item

    global p

    # declare the hypo and phone list in array variable 
    #
    global current_frame
    global num_step
    global array_hyp
    global array_phone

    # declare and compute the previous frame
    #
    global pre_frame
    set pre_frame [ expr $current_frame - $num_step ]

    # initialize word hypotheses list
    #
    set hyp_list {}
    set phone_list {}
    set flag 1
    set wstart 0

    # enable the text area
    #
    $p.stats configure -state normal
    $p.stats delete 1.0 end
    
    # make sure file exists
    #
    if {[file exists $filename] == 0} {
	$p.stats configure -state disabled
	return
    }

    # open the file
    #
    set fileptr [open $filename r]

    # read the trace statistics information first and display it
    #
    set ff 0
    while {$flag == 1} {
	
	# read the file line by line
	#
	set str [gets $fileptr]
	set str [string trim $str]
	
	# read the trace statistics
	#
	if {$ff == 0} {
	    scan $str "%s = %d\n" Time time
	} elseif {$ff == 1} {
	    set str "\n"
	} elseif {$ff == 2} {
	    scan $str "%s %s %s %s" w1 w2 w3 w4
	    set str [format "\t\t%8s\t%8s\t%8s\t%8s\n" $w1 $w2 $w3 $w4]
	    $p.stats insert end $str
	} else {
	    scan $str "%s %d %d %d %d" w1 w2 w3 w4 w5
	    set str [format "\t%-8s\t%8d\t%8d\t%8d\t%8d\n" $w1 $w2 $w3 $w4 $w5]
	    $p.stats insert end $str
	}
	incr ff
	if {$ff == 6} {
	    set flag 0
	}
    }
	
    # disable the text area
    #
    $p.stats configure -state disabled

    # set the flag 
    #
    set inside_str 0


    # now read the path information
    #
    while {![eof $fileptr]} {

	
	# read the file line by line
	#
	set str [gets $fileptr]
	set str [string trim $str]
	
	if { [string match $str ""] == 0 } {

	    # create a new hypothesis
	    #
	    set wstart 0
	    set word_hyp {}
	    set ph_sequence {}
	    set inside_str 1
	    
	    # only if this is not a blank line
	    #
	    while {[string match $str ""] == 0} {
		
		# scan the string for start and end time and phone
		#
		scan $str "%d %d %s %s" start_f end_f phstr score
		
		# set a phone item
		#
		set phone {}
		lappend phone $start_f $end_f $score $phstr
		lappend ph_sequence $phone

		# check if this is a word end
		#
		set ind [expr [string last $score $str] + \
			[string length $score]]

		set score [format "%.6f" $score]

		if {$ind != -1} {
		    incr ind
		    set rest_str [string range $str $ind end]
		} else {
		    set rest_str ""
		}
		set str [string trim $rest_str]
		if {[string match $str ""] == 0} {
		    
		    # scan the string for word data
		    #
		    scan $str "%s %f %f" word word_penalty score
		    
		    # create a word item
		    #
		    set word_item {}
		    lappend word_item $wstart $end_f $score $word
		    lappend word_hyp $word_item
		    set wstart $end_f
		}
		
		# read the next line
		#
		set str [gets $fileptr]
		set str [string trim $str]
	    } 

	    # append the word hypothesis to list of hyps
	    #
	    lappend hyp_list $word_hyp

	    # append the phone hypothesis to the phone list
	    #
	    lappend phone_list $ph_sequence

	    # store the hypothesis and phone list for displaying while
	    # trace_projector computing the next score
	    #
	    set array_hyp($current_frame) $word_hyp
	    set array_phone($current_frame) $ph_sequence

	} 

    }

    # initialize the hypo and phone array 
    #
    set start_frame 10
    set array_hyp($start_frame) ""
    set array_phone($start_frame) ""
    
    for {set i 0} {$i < $start_frame} {incr i} {
	set array_hyp($i) ""
	set array_phone($i) ""
    }
    
    # display the last score while projector computing next best hyphothesis
    #
    if { $current_frame > $start_frame && $inside_str != 1} {

	# initialize the temporal string for hypo and phone
	set temp_hypo ""
	set temp_phone ""

	# current hypo and phone is empty, so set to null string
	#
	set array_hyp($current_frame) ""
	set array_phone($current_frame) ""
	
	# if previous frame is empty, put the last string to current array
	#
	if { [string match $array_hyp($pre_frame) ""  ] == 0  } {
	    
	    # current is empty, so fill up with last hypo or phone  
	    #
	    set array_hyp($current_frame) $array_hyp($pre_frame)
	    set array_phone($current_frame) $array_phone($pre_frame)

	    # copy the previous array value to string
	    #
	    set temp_hypo $array_hyp($pre_frame)
	    set temp_phone $array_phone($pre_frame)
	} 

	# append the word hypothesis to list of hyps
	#
	lappend hyp_list $temp_hypo
	    
	# append the phone hypothesis to the phone list
	#	
	lappend phone_list $temp_phone
   
    }

    # close file
    #
    close $fileptr

}

# end of file
#
