[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
[Dictionary dict ps:phones tags]   read ../step1/convertedDict
[DBase db]                         open ../step1/db.dat ../step1/db.idx -mode r
[FMatrix ldaMatrix]               bload ../step15/ldaMatrix
AModelSet amo tpt ROOT
HMM hmm dict amo
Path path
dst configure -padPhone [ps:phones index pad]

catch { mkdir data }
catch { rm data/* }

SampleSet sms fs LDA 12
foreach cb [cbs:] { sms add $cb  -file data/$cb }
foreach ds [dss:] {
  sms map [dss index $ds] -class [cbs name [dss:$ds configure -cbX]]
}
set fp [open ../step15/ldaCounts] ; makeArray counts [read $fp] ; close $fp
foreach class [sms:] { 
 sms:$class configure -maxCount 200 -modulus [expr 1+($counts($class)+1)/200]
}

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

FMatrix smp
FVector cnt

foreach cb [cbs:] { 

  puts $cb

  if { [file exists data/$cb] } {

    smp bload data/$cb
    smp resize [smp configure -m] [expr [smp configure -n]-1]
    cbs:$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] }
    foreach ds [dss:] { dss:$ds configure -count $sum -val $vec }
  }
}

cbs save codebookWeights
dss save  distribWeights

exit