[FeatureSet fs] setDesc   @../step5/featDesc
            fs  setAccess @../step2/featAccess
[FMatrix ldaMatrix]                bload ../step5/ldaMatrix
[CodebookSet cbs fs]                read ../step8/codebookSet
[DistribSet  dss cbs]               read ../step2/distribSet
[PhonesSet ps]                      read ../step2/phonesSet ; ps:phones add pad
[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
HMM hmm dict amo

PTree configure -maxContext 1
PTree configure -addProc ptreeAddProc

proc ptreeAddProc { ptree args } { 

  regexp {(.*)-([bme])} $ptree dummy phone subphone
  set dsName [lindex $args 0]-$subphone
  set cbName "$phone-$subphone"
  if { [dss index $dsName] == -1 } { dss add $dsName $cbName }
  return $dsName
}

foreach ds [dss:] {

  if { $ds != "_-m" && $ds != "+-m" } {
    set phone [string range $ds 0 [expr [string first "-" $ds] -1]]
    dst.ptreeSet add $ds $phone 0 0 -count 1
    dst:$ds configure -ptree [dst.ptreeSet index $ds]
  }
}
dst configure -padPhone [ps:phones index pad]
dst configure -ptreeAdd 1

foreach utt [db] { 
    puts $utt
    makeArray arr [db get $utt]
    hmm make $arr(text) -optWord SIL
}

dss          write distribSet
dst          write distribTree
dst.ptreeSet write ptreeSet