[FeatureSet fs] setDesc   @../step2/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
[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
dst configure -padPhone [ps:phones index pad]

LDA lda fs MSC 16
foreach ds [dss:] { lda add $ds ; lda map [dss index $ds] -class $ds }

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
  lda accu path
}

lda update

DMatrix A
DMatrix K
A simdiag K lda.matrixT lda.matrixW
[FMatrix B] DMatrix A

B bsave ldaMatrix

set fp [open ldaCounts w]
foreach cl [lda:] { puts $fp "{ $cl [lda:$cl.mean configure -count] }" }
close $fp

exit