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

[CodebookSet cbs fs]                read ../step2/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
[FMatrix ldaMatrix]                bload ../step5/ldaMatrix
AModelSet amo tpt ROOT
HMM hmm dict amo
Path path

SampleSet sms fs LDA 12
foreach ds [dss:] { sms add $ds ; sms map [dss index $ds] -class $ds }
set fp [open ../step5/ldaCounts] ; makeArray counts [read $fp] ; close $fp
foreach class [sms:] { 
 sms:$class configure -maxCount 500 -modulus [expr 1+($counts($class)+1)/500]
}

foreach utt [db] {

  puts $utt
  set uttInfo [db get $utt]
  makeArray arr $uttInfo
  fs eval $uttInfo
  hmm make $arr(text) -optWord SIL
  path bload ../step4/labels/$utt 
  path map hmm -senoneSet sns -stream 0
  sms accu path
}

sms flush

CodebookSet cbs2 fs
FMatrix smp
FVector cnt

foreach cb [cbs:] { 

  puts $cb
  cbs2 add $cb LDA [cbs:$cb configure -refN] 12 [cbs:$cb configure -type] 

  smp bload $cb
  smp resize [smp configure -m] [expr [smp configure -n]-1]
  cbs2:$cb.mat neuralGas smp -maxIter 5 -tempS 0 -counts cnt
  
  set sum 0 ; set vec {}
  foreach x  [cnt puts] { set sum [expr $sum + $x]   }
  foreach x  [cnt puts] { lappend vec [expr $x/$sum] }
  dss:$cb configure -count $sum -val $vec
}

cbs2 write codebookSet
cbs2 save  codebookWeights
dss  save  distribWeights