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

[CodebookSet cbs fs]                read ../step14/codebookSetClustered
[DistribSet  dss cbs]               read ../step14/distribSetClusteredPruned
[PhonesSet ps]                      read ../step14/phonesSet
[Tags tags]                         read ../step2/tags
[Tree dst ps:phones ps tags dss]    read ../step14/distribTreeClusteredPruned

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
[DBase db]                          open ../step1/db.dat ../step1/db.idx -mode r
[Dictionary dict ps:phones tags]    read ../step1/convertedDict
[FMatrix ldaMatrix]                bload ../step15/ldaMatrix

AModelSet amo tpt ROOT
dst configure -padPhone [ps:phones index pad]

cbs load ../step17/codebookWeights.3
dss load ../step17/distribWeights.3

Search configure  -silenceWordPenalty 10 -use3gFwd 1
TreeFwd configure -beamWidth 200 -topN 50 -phoneBeamWidth 200 \
                  -lastPhoneBeamWidth 120 -wordBeamWidth 150  \
                 -lastPhoneAloneBeamWidth 120
FlatFwd configure -beamWidth 250 -topN 50 -phoneBeamWidth 250 \
                  -lastPhoneBeamWidth 180 -wordBeamWidth 190
Lattice configure -beamWidth 120 -topN 40

set baseLz   16
set baseLp  -16

Vocab voc ../step7/vocab -dictionary dict -acousticModel amo ;# -useXwt 1
Lm lm voc langmod -weight $baseLz -penalty $baseLp -cachelines 500
Search search voc lm

proc reportRecogRate { utt } {

  global rr lzList lpList
  
  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" $rr($lz,$lp)] ; puts -nonewline "|"
    }
    puts ""
  }
}

proc testOne { utt } {

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

  set uttinfo [db get $utt]
  makeArray infoArray $uttinfo
  search treeFwd -eval $uttinfo
  regsub -all {\(|\)} [search.treeFwd.hypoList puts -id $utt -style simple] "" hypo
  puts "tree pass: [recogRate $infoArray(text) $hypo]"
  search flatFwd
  regsub -all {\(|\)} [search.flatFwd.hypoList puts -id $utt -style simple] "" hypo
  puts "flat pass: [recogRate $infoArray(text) $hypo]"
  search lattice
  foreach lz $lzList { foreach lp $lpList {
    search.lattice rescore -lz $lz -lp $lp
    regsub -all {\(|\)} [search.lattice.hypoList puts -id $utt -style simple] "" hypo
    set rr($lz,$lp) [recogRate $infoArray(text) $hypo]    
  } }
  reportRecogRate $utt rr
}

foreach utt [db] {
  puts "testing utterance $utt"
  testOne $utt
}

exit