#
# Configure Search and Test Parameters:
#

set onebeam 500

Search configure  -silenceWordPenalty 10 -use3gFwd 1
TreeFwd configure -beamWidth $onebeam -topN 40 -phoneBeamWidth $onebeam \
                  -lastPhoneBeamWidth $onebeam -wordBeamWidth $onebeam  \
                  -lastPhoneAloneBeamWidth $onebeam
FlatFwd configure -beamWidth $onebeam -topN 40 -phoneBeamWidth $onebeam \
                  -lastPhoneBeamWidth $onebeam -wordBeamWidth $onebeam
Lattice configure -beamWidth $onebeam -topN 40

set baseLz   32 
set baseLp    0
set lzList {4 8 16 32 64 128 256}
set lpList {-8 -4 0 4 8}

puts [Search configure]
puts [TreeFwd configure]
puts [Lattice configure]
puts "baseLz: $baseLz,  baseLp: $baseLp"
flush stdout

# ---------------------------------------

set totalRefNum  0; #number of reference words in sentence so far;
set totalFlatErr 0; #number of errors in flat pass so far;
set totalTreeErr 0; #number of errors in tree pass so far;
foreach lz $lzList { 
 foreach lp $lpList {
   set rr($lz,$lp) 0; #number of errors in lattice pass so far for given lz,lp
 }
}

[FeatureSet fs] setDesc   @../step5/featDesc
            fs  setAccess @../step2/featAccess

[CodebookSet cbs fs]                read ../step8/codebookSet
[DistribSet  dss cbs]               read ../step2/distribSet
[PhonesSet ps]                      read ../step2/phonesSet
[Tags tags]                         read ../step2/tags
[Tree dst ps:phones ps tags dss]    read ../step2/distribTree

SenoneSet sns [DistribStream str dss dst]

[TmSet tms]                         read ../step2/transitionModels
[TopoSet tps sns tms]               read ../step2/topologies
[Tree tpt ps:phones ps tags tps]    read ../step2/topologyTree
[Dictionary dict ps:phones tags]    read ../step1/convertedDict

[DBase db] open ../step1/db.dat ../step1/db.idx -mode r
AModelSet amo tpt ROOT

cbs load ../step9/codebookWeights.3
dss load ../step9/distribWeights.3
[FMatrix ldaMatrix]                 bload ../step5/ldaMatrix



Vocab voc ../step7/vocab -dictionary dict -acousticModel amo ;# -useXwt 1
set NewLmItf 1
if $NewLmItf {
  # New LM-Interface:
  Lm lm voc ../step7/langmod -cachelines 500
  lm configure -lz $baseLz -lp $baseLp
} else {
  # Old LM-Interface:
  Lm lm voc ../step7/langmod -weight $baseLz -penalty $baseLp -cachelines 500
}
Search search voc lm

proc errNum { corr hypo {print 0}} {
  set out [align $corr $hypo]
  if {$print} {puts [lindex $out 1]; puts [lindex $out 3]}
  set errN -1
  foreach i [lindex $out 4] { if { $i != "c" } { incr errN } }
  return $errN
}

proc reportRecogRate { utt } {

  global rr lzList lpList totalRefNum
  
  puts $utt
  puts -nonewline "lz\\lp |"
  foreach lp $lpList { puts -nonewline [format " %4d |" $lp] }
  puts -nonewline "\n------+"
  foreach lp $lpList { puts -nonewline "------+" } ; puts ""
  foreach lz $lzList { 
    puts -nonewline [format "%5s" $lz] ; puts -nonewline " |"
    foreach lp $lpList {
        puts -nonewline [format "%6.1f" [expr 100.0 - [expr 100.0 * $rr($lz,$lp) / $totalRefNum.0]]] ; puts -nonewline "|"
    }
    puts ""
  }
}

proc testOne { utt } {

  global rr lzList lpList totalRefNum totalFlatErr totalTreeErr NewLmItf

  set uttinfo [db get $utt]
  makeArray infoArray $uttinfo
  incr totalRefNum [llength $infoArray(text)]

  search treeFwd -eval $uttinfo

  regsub -all {\(|\)} [search.treeFwd.hypoList puts -id $utt -style simple] "" hypo
  regsub -all {\$} $hypo "" hypo
  incr totalTreeErr [errNum $infoArray(text) $hypo]
  puts "tree pass WA (cumulated): [expr 100-[expr 100.0 * $totalTreeErr.0 / $totalRefNum.0]]"
  search flatFwd
  regsub -all {\(|\)} [search.flatFwd.hypoList puts -id $utt -style simple] "" hypo
  regsub -all {\$} $hypo "" hypo

  incr totalFlatErr [errNum $infoArray(text) $hypo 1]
  puts "flat pass WA (cumulated): [expr 100- [expr 100* $totalFlatErr.0 / $totalRefNum.0]]"
  search lattice
  if $NewLmItf { set oldlz [lm configure -lz] ; set oldlp [lm configure -lp] }
  foreach lz $lzList { foreach lp $lpList {
    if $NewLmItf {
      lm configure -lz $lz -lp $lp
      search.lattice rescore lm
    } else {
      search.lattice rescore -lz $lz -lp $lp
    }
    regsub -all {\(|\)} [search.lattice.hypoList puts -id $utt -style simple] "" hypo
    regsub -all {\$} $hypo "" hypo
    incr rr($lz,$lp) [errNum $infoArray(text) $hypo]    
  } }
  if $NewLmItf { lm configure -lz $oldlz -lp $oldlp }
  puts "lattice pass WA (cumulated):"
  reportRecogRate $utt 
}

set fp [open ../step1/testIDs r]

while { [gets $fp utt] != -1 } {
   puts "testing utterance $utt" ; testOne $utt 
}
close $fp
exit