srsf/devasr/src/devasrvmalgorithmmanager.cpp
branchRCL_3
changeset 23 e36f3802f733
parent 0 bf1d17376201
equal deleted inserted replaced
22:cad71a31b7fc 23:e36f3802f733
       
     1 /*
       
     2 * Copyright (c) 2004 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Algorithm manager for grammar handling algorihtms.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "devasrvmalgorithmmanager.h"
       
    21 //#include "DevASRSrsAlgorithmManager.h"
       
    22 #include <nssdevasr.h>
       
    23 #include <nsssispeechrecognitiondatadevasr.h>
       
    24 #include "rubydebug.h"
       
    25 
       
    26 
       
    27 // ============================ MEMBER FUNCTIONS ===============================
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 // CVMAlgorithmManager::CVMAlgorithmManager
       
    31 // C++ default constructor can NOT contain any code, that
       
    32 // might leave.
       
    33 // -----------------------------------------------------------------------------
       
    34 //
       
    35 CVMAlgorithmManager::CVMAlgorithmManager( MDevASRObserver& aObserver,
       
    36                                           MVmAlgMgrObserver& aVmObserver )
       
    37                                         : iGrCompiler( NULL ), 
       
    38                                           iVocMan( NULL ), 
       
    39                                           iTtp( NULL ), 
       
    40                                           iObserver( aObserver ),
       
    41                                           iTtpState( ETtpStateUninitialized ),
       
    42                                           iGrState( EGrStateUninitialized ),
       
    43                                           iVmAlgObserver( aVmObserver )
       
    44     {
       
    45     // Nothing
       
    46     }
       
    47 
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // CVMAlgorithmManager::ConstructL
       
    51 // Symbian 2nd phase constructor can leave.
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 void CVMAlgorithmManager::ConstructL()
       
    55     {
       
    56     RUBY_DEBUG_BLOCK( "CVMAlgorithmManager::ConstructL()" );
       
    57 
       
    58     // TtpHwDevice
       
    59     iTtp = CASRSTtpHwDevice::NewL( *this );
       
    60     iTtp->InitializeL();
       
    61     iTtp->SetPhonemeNotationL( _L("NIPA8" ) );
       
    62     iTtpState = ETtpStateIdle;
       
    63     // Grammar compiler HW device
       
    64     iGrCompiler = CASRSGrCompilerHwDevice::NewL( *this );
       
    65     iGrCompiler->InitializeL();
       
    66     iGrState = EGrStateIdle;
       
    67     // VocManHwDevice
       
    68     iVocMan = CASRSVocManHwDevice::NewL();
       
    69     iVocMan->InitializeL();
       
    70     }
       
    71 
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CVMAlgorithmManager::NewL
       
    75 // Two-phased constructor.
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 CVMAlgorithmManager* CVMAlgorithmManager::NewL( MDevASRObserver& aObserver, 
       
    79                                                 MVmAlgMgrObserver& aVmObserver )
       
    80     {
       
    81     CVMAlgorithmManager* self = new( ELeave ) CVMAlgorithmManager( aObserver, aVmObserver );
       
    82     
       
    83     CleanupStack::PushL( self );
       
    84     self->ConstructL();
       
    85     CleanupStack::Pop();
       
    86     
       
    87     return self;
       
    88     }
       
    89 
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // CVMAlgorithmManager::~CVMAlgorithmManager
       
    93 // Destructor
       
    94 // -----------------------------------------------------------------------------
       
    95 //
       
    96 CVMAlgorithmManager::~CVMAlgorithmManager()
       
    97     {
       
    98     if ( iGrCompiler != NULL )
       
    99         {
       
   100         iGrCompiler->Clear();
       
   101         }
       
   102     delete iGrCompiler;
       
   103     iGrCompiler = NULL;
       
   104     if ( iVocMan != NULL )
       
   105         {
       
   106         iVocMan->Clear();
       
   107         }
       
   108     delete iVocMan;
       
   109     iVocMan = NULL;
       
   110     if ( iTtp != NULL )
       
   111         {
       
   112         iTtp->Clear();
       
   113         }
       
   114     delete iTtp;
       
   115     iTtp = NULL;
       
   116 //    iDataArray.ResetAndDestroy();
       
   117     }
       
   118 
       
   119 
       
   120 // -----------------------------------------------------------------------------
       
   121 // CVMAlgorithmManager::AdaptVocabL
       
   122 // Use CASRVocManHwDevice to adapt rule.
       
   123 // -----------------------------------------------------------------------------
       
   124 //
       
   125 void CVMAlgorithmManager::AdaptVocabL( const CSIResultSet& aResultSet, 
       
   126                                        TInt aResultIndex )
       
   127     {
       
   128     RUBY_DEBUG_BLOCK( "CVMAlgorithmManager::AdaptVocabL()" );
       
   129 
       
   130     const CSIResult& iCorrectResult = aResultSet.AtL( aResultIndex );
       
   131 
       
   132     TSIGrammarID grammarID( iCorrectResult.GrammarID() );
       
   133     TSIRuleID ruleID( iCorrectResult.RuleID() );
       
   134     TSIRuleVariantID ruleVariantID( iCorrectResult.RuleVariantID() );
       
   135     CSICompiledGrammar* siActiveGrammar = NULL;
       
   136     CSICompiledGrammar* siNonActiveGrammar = NULL;
       
   137     CSICompiledGrammar* siGrammar = NULL;
       
   138   
       
   139     TRAPD( error, iVmAlgObserver.GetGrammarL( grammarID, &siActiveGrammar, &siNonActiveGrammar ) );
       
   140     User::LeaveIfError( error );
       
   141 
       
   142     TInt index = 0;
       
   143 
       
   144     if ( siActiveGrammar != NULL )
       
   145         {
       
   146         siGrammar = siActiveGrammar;
       
   147         }
       
   148     else if ( siNonActiveGrammar != NULL )
       
   149         {
       
   150         siGrammar = siNonActiveGrammar;
       
   151         }
       
   152     else
       
   153         {
       
   154         User::Leave( KErrNotFound );	
       
   155         }
       
   156     
       
   157     index = siGrammar->Find( ruleID );
       
   158     User::LeaveIfError( index );
       
   159     CSIRule& rule = siGrammar->AtL( index );
       
   160     
       
   161     // Do the actual vocabulary adaptation using VocManHwDevice
       
   162     TRAP( error, iVocMan->AdaptL( ruleVariantID, rule ) );
       
   163     // Neglect possible error since it just might be due to the reason
       
   164     // that there are no proper parameters in place within the RuleVariants..
       
   165     }
       
   166 
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // CVMAlgorithmManager::ComputeNewGrammarSizeL
       
   170 // Use CASRVocManHwDevice to compute size.
       
   171 // -----------------------------------------------------------------------------
       
   172 //
       
   173 void CVMAlgorithmManager::ComputeNewGrammarSizeL( const CSIGrammar& aGrammar, 
       
   174                                                   const TUint32 aTargetNRuleVariants, 
       
   175                                                   const TUint32 aMaxNRuleVariants, 
       
   176                                                   const RArray<TUint>& aNewRuleScores, 
       
   177                                                   RArray<TUint>& aNNeNRuleVariants, 
       
   178                                                   TUint32& aNPrune )
       
   179     {
       
   180     RUBY_DEBUG_BLOCK( "CVMAlgorithmManager::ComputeNewGrammarSizeL()" );
       
   181 
       
   182     iVocMan->ComputeNRuleVariantsL( aGrammar, aTargetNRuleVariants, 
       
   183                                     aMaxNRuleVariants, aNewRuleScores, 
       
   184                                     aNNeNRuleVariants, aNPrune );
       
   185     }
       
   186 
       
   187 
       
   188 // -----------------------------------------------------------------------------
       
   189 // CVMAlgorithmManager::CompileGrammarL
       
   190 // Compiles SI grammar.
       
   191 // -----------------------------------------------------------------------------
       
   192 //
       
   193 void CVMAlgorithmManager::CompileGrammarL( CSICompiledGrammar& aGrammar/*, 
       
   194                                            CSIModelBank& aModelBank*/ )
       
   195     {
       
   196     RUBY_DEBUG_BLOCK( "CVMAlgorithmManager::CompileGrammarL()" );
       
   197 
       
   198     if ( iGrState == EGrStateUninitialized )
       
   199         {
       
   200         User::Leave( KErrNotReady );
       
   201         }
       
   202     if ( iGrState == EGrStateBusyCombining || iGrState == EGrStateBusyCompiling )
       
   203         {
       
   204         User::Leave( KErrInUse );
       
   205         }
       
   206 
       
   207     iGrState = EGrStateBusyCompiling;
       
   208 
       
   209     iGrCompiler->CompileGrammarL( aGrammar/*, aModelBank*/ );
       
   210     }
       
   211 
       
   212 
       
   213 // -----------------------------------------------------------------------------
       
   214 // CVMAlgorithmManager::CombineGrammarL
       
   215 // Combines a list of grammars.
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 void CVMAlgorithmManager::CombineGrammarL( const RPointerArray<CSICompiledGrammar>& aCompiledGrammars,
       
   219                                            const RPointerArray<TSIRuleVariantInfo>& aExcludedRules )
       
   220     {
       
   221     RUBY_DEBUG_BLOCK( "CVMAlgorithmManager::CombineGrammarL()" );
       
   222 
       
   223     if ( iGrState == EGrStateUninitialized )
       
   224         {
       
   225         User::Leave( KErrNotReady );
       
   226         }
       
   227     if ( iGrState == EGrStateBusyCombining || iGrState == EGrStateBusyCompiling )
       
   228         {
       
   229         User::Leave( KErrInUse );
       
   230         }
       
   231 
       
   232     iGrState = EGrStateBusyCombining;
       
   233 
       
   234     iGrCompiler->CombineGrammarL( aCompiledGrammars, aExcludedRules );
       
   235     }
       
   236 
       
   237 // -----------------------------------------------------------------------------
       
   238 // CVMAlgorithmManager::PruneGrammar
       
   239 // Use CASRVocManHwDevice to prune a grammar.
       
   240 // -----------------------------------------------------------------------------
       
   241 //
       
   242 TBool CVMAlgorithmManager::PruneGrammar( const CSIGrammar& aGrammar, 
       
   243                                          const TUint32 aMinNumber, 
       
   244                                          RArray<TSIRuleVariantInfo>& aPrunedRuleVariants )
       
   245     {
       
   246     return iVocMan->Prune( aGrammar, aMinNumber, aPrunedRuleVariants );
       
   247     }
       
   248 
       
   249 
       
   250 // -----------------------------------------------------------------------------
       
   251 // CVMAlgorithmManager::ResolveResult
       
   252 // Use CASRVocManHwDevice to resolve a result.
       
   253 // -----------------------------------------------------------------------------
       
   254 //
       
   255 void CVMAlgorithmManager::ResolveResult( const RArray<TUint>& aNBestIDs,
       
   256                                          CSIResultSet& aSIResultSet,
       
   257                                          const RPointerArray<CSICompiledGrammar>& aSICompiledGrammar,
       
   258                                          const TDesC8& aCombinedData/*,
       
   259                                          CSIModelBank& iSIModelBank*/ )
       
   260     {
       
   261     iGrCompiler->ResolveResult( aNBestIDs, aSIResultSet,
       
   262                                 aSICompiledGrammar, aCombinedData );
       
   263     }
       
   264 
       
   265 
       
   266 // -----------------------------------------------------------------------------
       
   267 // CVMAlgorithmManager::TrainFromText
       
   268 // Use CVMAlgorithmManager to train from text.
       
   269 // -----------------------------------------------------------------------------
       
   270 //
       
   271 void CVMAlgorithmManager::TrainFromTextL( CSITtpWordList& aWordList, 
       
   272                                           const RArray<TLanguage>& aDefaultLanguage, 
       
   273                                           const RArray<TUint32>& aMaxNPronunsForWord )
       
   274     {
       
   275     RUBY_DEBUG_BLOCK( "CVMAlgorithmManager::TrainFromTextL()" );
       
   276 
       
   277     if ( iTtpState == ETtpStateUninitialized )
       
   278         {
       
   279         User::Leave( KErrNotReady );
       
   280         }
       
   281     if ( iTtpState == ETtpStateBusy )
       
   282         {
       
   283         User::Leave( KErrInUse );
       
   284         }
       
   285 
       
   286     iTtpState = ETtpStateBusy;
       
   287     
       
   288     TRAPD( error, iTtp->ConvertWordListL( aWordList, aDefaultLanguage, aMaxNPronunsForWord ) );
       
   289     if ( error != KErrNone )
       
   290         {
       
   291         iTtpState = ETtpStateIdle;
       
   292         User::Leave( error );
       
   293         }
       
   294     }
       
   295 
       
   296 
       
   297 // -----------------------------------------------------------------------------
       
   298 // CVMAlgorithmManager::Cancel
       
   299 // Use CVMAlgorithmManager to cancel training.
       
   300 // -----------------------------------------------------------------------------
       
   301 //
       
   302 void CVMAlgorithmManager::Cancel()
       
   303     {
       
   304     if ( iTtpState == ETtpStateBusy )
       
   305         {
       
   306         iTtp->CancelWordListConversion();
       
   307         }
       
   308     if ( iGrState == EGrStateBusyCompiling )
       
   309         {
       
   310         iGrCompiler->CancelCompilation();
       
   311         }
       
   312     if ( iGrState == EGrStateBusyCombining )
       
   313         {
       
   314         iGrCompiler->CancelCombination();
       
   315         }
       
   316     }
       
   317 
       
   318 
       
   319 // -----------------------------------------------------------------------------
       
   320 // GetEnginePropertiesL
       
   321 // Returns properties of the underlying engines.
       
   322 // -----------------------------------------------------------------------------
       
   323 //
       
   324 void CVMAlgorithmManager::GetEnginePropertiesL( const RArray<TInt>& /*aPropertyId*/, 
       
   325                                                 RArray<TInt>& /*aPropertyValue*/ )
       
   326     {
       
   327     // Do nothing
       
   328     }
       
   329 
       
   330 // -----------------------------------------------------------------------------
       
   331 // CVMAlgorithmManager::LoadEnginePropertiesL
       
   332 // Loads properties to engine, invalid IDs are neglected.
       
   333 // -----------------------------------------------------------------------------
       
   334 //
       
   335 void CVMAlgorithmManager::LoadEnginePropertiesL( const RArray<TInt>& /*aParameterId*/,
       
   336                                                  const RArray<TInt>& /*aParameterValue*/ )
       
   337     {
       
   338     // Do nothing
       
   339     }
       
   340 
       
   341 // -----------------------------------------------------------------------------
       
   342 // MASRTtpHwDeviceObserver mixin begins
       
   343 // -----------------------------------------------------------------------------
       
   344 
       
   345 
       
   346 // -----------------------------------------------------------------------------
       
   347 // CVMAlgorithmManager::MathdoConfigurationData
       
   348 // 
       
   349 // -----------------------------------------------------------------------------
       
   350 //
       
   351 HBufC8* CVMAlgorithmManager::MathdoConfigurationData( TUint32 aPackageType,
       
   352                                                       TUint32 aPackageID,
       
   353                                                       TUint32 aStartPosition,
       
   354                                                       TUint32 aEndPosition )
       
   355     {
       
   356     // Call DevASR observer
       
   357     return iObserver.ConfigurationData( aPackageType, aPackageID, 
       
   358                                         aStartPosition, aEndPosition );
       
   359     }
       
   360     
       
   361 
       
   362 // -----------------------------------------------------------------------------
       
   363 // CVMAlgorithmManager::MathdoWordListReady
       
   364 // 
       
   365 // -----------------------------------------------------------------------------
       
   366 //
       
   367 void CVMAlgorithmManager::MathdoWordListReady( const TInt aError )
       
   368     {
       
   369     RUBY_DEBUG0( "CVMAlgorithmManager::MathdoWordListReady()" );
       
   370     
       
   371     iTtpState = ETtpStateIdle;
       
   372 
       
   373     // Call DevASR observer
       
   374     iObserver.DevASREvent( EDevASRTrainFromText, aError );
       
   375     }
       
   376     
       
   377 
       
   378 // -----------------------------------------------------------------------------
       
   379 // MASRTtpHwDeviceObserver mixin ends
       
   380 // -----------------------------------------------------------------------------
       
   381 
       
   382 
       
   383 // -----------------------------------------------------------------------------
       
   384 // MASRGrCompilerObserver mixin begins
       
   385 // -----------------------------------------------------------------------------
       
   386 
       
   387 // -----------------------------------------------------------------------------
       
   388 // CVMAlgorithmManager::MghdoGrammarCompilerComplete
       
   389 // 
       
   390 // -----------------------------------------------------------------------------
       
   391 //
       
   392 void CVMAlgorithmManager::MghdoGrammarCompilerComplete( TInt aError )
       
   393     {
       
   394     iGrState = EGrStateIdle;
       
   395     // Forward callback to DevASR observer
       
   396     iObserver.DevASREvent( EDevASRGrammarCompile, aError );
       
   397     }
       
   398 
       
   399 // -----------------------------------------------------------------------------
       
   400 // CVMAlgorithmManager::MghdoGrammarCombinerComplete
       
   401 // 
       
   402 // -----------------------------------------------------------------------------
       
   403 //
       
   404 void CVMAlgorithmManager::MghdoGrammarCombinerComplete( HBufC8* aResult, 
       
   405                                                         TInt aError )
       
   406     {
       
   407     iGrState = EGrStateIdle;
       
   408     iVmAlgObserver.CombineComplete( aResult, aError );
       
   409     }
       
   410 
       
   411 // -----------------------------------------------------------------------------
       
   412 // CVMAlgorithmManager::MghdoSILexiconL
       
   413 // 
       
   414 // -----------------------------------------------------------------------------
       
   415 //
       
   416 CSILexicon* CVMAlgorithmManager::MghdoSILexiconL( TSILexiconID anID )
       
   417     {
       
   418     RUBY_DEBUG_BLOCK( "CVMAlgorithmManager::MghdoSILexiconL()" );
       
   419 
       
   420     return iObserver.SILexiconL( anID );
       
   421     }
       
   422 
       
   423 // -----------------------------------------------------------------------------
       
   424 // CVMAlgorithmManager::MghdoConfigurationData
       
   425 // 
       
   426 // -----------------------------------------------------------------------------
       
   427 //
       
   428 HBufC8* CVMAlgorithmManager::MghdoConfigurationData( TUint32 aPackageType,
       
   429                                                      TUint32 aPackageID,
       
   430                                                      TUint32 aStartPosition,
       
   431                                                      TUint32 aEndPosition )
       
   432     {
       
   433     // Call DevASR observer
       
   434     return iObserver.ConfigurationData( aPackageType, aPackageID, 
       
   435                                         aStartPosition, aEndPosition );
       
   436     }
       
   437 
       
   438 
       
   439 // -----------------------------------------------------------------------------
       
   440 // MASRGrCompilerObserver mixin ends
       
   441 // -----------------------------------------------------------------------------
       
   442 
       
   443 // End of File