Beispiel #1
0
/**
 * Export midi notes of  data instance into midifile, needs external program midiconvert
 *
 * @param lpsFilename Name of file to export
 * @param iSst        Pointer to instance to export
 * @param lpsFiletype Type of file to export
 * @return <code>O_K</code> if successful, a (negative) error code otherwise
 */
INT16 CGEN_PROTECTED CDlpFile_Midi_ExportMidi
(
  CDlpFile*   _this,
  const char* lpsFilename,
  CDlpObject* iSrc,
  const char* lpsFiletype
)
{
  char  lpsTempFile[L_PATH];
  char  lpsCmdline    [3*L_PATH] = "";
  INT16 nErr                     =O_K;

  strcpy(lpsTempFile,dlp_tempnam(NULL,"dlabpro_midi_export"));
  sprintf(lpsCmdline,"midiconvert %s %s", lpsTempFile, lpsFilename);

  CData *idSrc = AS(CData,iSrc);
  CData_InsertRecs(idSrc, 0, 1, 1);
  CData_Dstore(idSrc, CData_GetDescr(idSrc,DESCR0),0,5);
  IF_NOK(CDlpFile_ExportAsciiFromData(_this,lpsTempFile,iSrc,"csv"))
       nErr = IERROR(iSrc,FIL_EXPORT,lpsTempFile,"csv",0);
  CData_DeleteRecs(idSrc,0,1);

  if (system(lpsCmdline)!=0) { nErr=IERROR(_this,FIL_EXEC,lpsCmdline,0,0); }

  if (remove(lpsTempFile)==-1) nErr=IERROR(_this,FIL_REMOVE,"temporary ",lpsTempFile,0);

  /* Clean up */
  return nErr;
}
Beispiel #2
0
/**
 * Fills a data table with the sample size (frequency) or the estimated a-priori
 * probability of the classes.
 *
 * @param _this
 *          Pointer to this CStatistics instance
 * @param idDst
 *          Pointer to the destination data instance
 * @param bProb
 *          If <code>TRUE</code> the method estimates class a-priori
 *          probabilities otherwise it stores the classes' sample sizes
 * @return <code>O_K</code> if successsfull, a (negative) error code otherwise
 */
INT16 CGEN_PUBLIC CStatistics_FreqEx
(
  CStatistics* _this,
  CData*       idDst,
  BOOL         bProb
)
{
  INT32    c     = 0;                                                            /* Class loop counter                */
  INT32    C     = 0;                                                            /* Number of statistics classes      */
  INT32    nTsz  = 0;                                                            /* Total sample size                 */
  FLOAT64* lpSsz = NULL;                                                         /* Pointer to class' c sample size   */

  /* Validate */                                                                /* --------------------------------- */
  if (!idDst) return IERROR(_this,ERR_NULLARG,"idDst",0,0);                     /* Check output data instance        */
  CData_Reset(idDst,TRUE);                                                      /* Clear destination instance        */
  CHECK_THIS_RV(NOT_EXEC);                                                      /* Check this pointer                */
  IF_NOK(CStatistics_Check(_this))                                              /* Check instance data               */
    return IERROR(_this,STA_NOTSETUP," ( use -status for details)",0,0);        /* ...                               */

  /* Initialize */                                                              /* --------------------------------- */
  C = CStatistics_GetNClasses(_this);                                           /* Get number of statistics classes  */
  CData_Array(idDst,T_DOUBLE,1,C);                                              /* Allocate output data instance     */
  CData_SetNBlocks(idDst,C);                                                    /* Set block number                  */
  if (!CData_XAddr(idDst,0,0)) return IERROR(_this,ERR_NOMEM,0,0,0);            /* Should have been successfull ...  */

  /* Store sample sizes / estimated a-rpior probabilities */                    /* --------------------------------- */
  nTsz = bProb ? CStatistics_GetNSamples(_this) : 1;                            /* Get frequency divident            */
  for (c=0; c<C; c++)                                                           /* Loop over classes                 */
  {                                                                             /* >>                                */
    DLPASSERT((lpSsz = CStatistics_GetPtr(_this,c,STA_DAI_SSIZE)));             /*   Get ptr. to class' c sample size*/
    CData_Dstore(idDst,lpSsz[0]/(FLOAT64)nTsz,c,0);                              /*   Store sample size of class c    */
  }                                                                             /* <<                                */

  return O_K;
}
Beispiel #3
0
/* DP decoder backtrack function
 *
 * This function performes the backtracking after decoding.
 *
 * @param glob  Pointer to the global memory structure
 * @param itDst Destination transducer for backtracking
 * @return <code>NULL</code> if successfull, the error string otherwise
 */
const char *fsts_sdp_backtrack(struct fsts_glob *glob,CFst *itDst){
  INT32 gwi;
  CDlpObject_Copy(BASEINST(itDst),BASEINST(((CFst*)glob->algo)));
  if((gwi=CData_FindComp(AS(CData,itDst->ud),"~GW"))<0){
    CData_AddComp(AS(CData,itDst->ud),"~GW",T_DOUBLE);
    gwi=CData_GetNComps(AS(CData,itDst->ud))-1;
  }
  CData_Dstore(AS(CData,itDst->ud),itDst->m_nGw,0,gwi);
  return NULL;
}
Beispiel #4
0
/*
 * Manual page at statistics.def
 */
INT16 CGEN_PUBLIC CStatistics_Merge(CStatistics* _this, CStatistics* iSrc)
{
	INT32   nB    = 0;                                                            /* Current block                     */
	INT32   nC    = 0;                                                            /* Current component                 */
	INT32   nR    = 0;                                                            /* Current record                    */
	INT32   nXB   = 0;                                                            /* Number of statistics blocks       */
	INT32   nXC   = 0;                                                            /* Number of components              */
	INT32   nXR   = 0;                                                            /* Number of records per block       */
	FLOAT64 nVal  = 0.;                                                           /* Merged statistics value           */
	FLOAT64 nVal1 = 0.;                                                           /* Statistics value of this instance */
	FLOAT64 nVal2 = 0.;                                                           /* Statistics value of source inst.  */

  /* Initialize */                                                              /* --------------------------------- */
  CHECK_THIS_RV(NOT_EXEC);                                                      /* Check this instance               */
  if (_this==iSrc) return O_K;                                                  /* Nothing to be done                */
  IF_NOK(CStatistics_Check(iSrc))                                               /* Check source statistics           */
    return IERROR(_this,ERR_INVALARG,"iSrc",0,0);                               /* ...                               */

  /* Check-up */                                                                /* --------------------------------- */
  nXB = CData_GetNBlocks      (AS(CData,_this->m_idDat));                       /* Get number of statistics blocks   */
  nXC = CData_GetNComps       (AS(CData,_this->m_idDat));                       /* Get number of components          */
  nXR = CData_GetNRecsPerBlock(AS(CData,_this->m_idDat));                       /* Get number of records per block   */
  if                                                                            /* Instances incompatible if ...     */
  (                                                                             /* |                                 */
    nXB != CData_GetNBlocks      (AS(CData,iSrc->m_idDat)) ||                   /* | ... no. of blocks mismatch   OR */
    nXC != CData_GetNComps       (AS(CData,iSrc->m_idDat)) ||                   /* | ... no. of comps. mismatch   OR */
    nXR != CData_GetNRecsPerBlock(AS(CData,iSrc->m_idDat))                      /* | ... no. of records mismatch     */
  )                                                                             /* |                                 */
  {                                                                             /* >>                                */
  	return IERROR(_this,STA_INCOMPAT,0,0,0);                                    /*   So sorry ...                    */
  }                                                                             /* <<                                */

  /* Do merge */                                                                /* --------------------------------- */
  for (nB=0; nB<nXB; nB++)                                                      /* Loop over blocks                  */
  	for (nR=0; nR<nXR; nR++)                                                    /*   Loop over records of block      */
  		for (nC=0; nC<nXC; nC++)                                                  /*     Loop over components          */
			{                                                                         /*     >>                            */
				nVal1 = CData_Dfetch(AS(CData,_this->m_idDat),nB*nXR+nR,nC);            /*       Get value from this         */
				nVal2 = CData_Dfetch(AS(CData,iSrc ->m_idDat),nB*nXR+nR,nC);            /*       Get value from source       */
				switch (nR)                                                             /*       Depending on value type     */
				{                                                                       /*       >>                          */
				case STA_DAI_MIN: nVal = MIN(nVal1,nVal2); break;                       /*         Aggregate minimums        */
				case STA_DAI_MAX: nVal = MAX(nVal1,nVal2); break;                       /*         Aggregate maximums        */
				default         : nVal = nVal1+nVal2;      break;                       /*         Aggregate all other       */
				}                                                                       /*       <<                          */
				CData_Dstore(AS(CData,_this->m_idDat),nVal,nB*nXR+nR,nC);               /*       Store merged value          */
			}                                                                         /*     <<                            */

  /* Aftermath */                                                               /* --------------------------------- */
  return O_K;                                                                   /* Everything's ok                   */
}
Beispiel #5
0
/*
 * Manual page at statistics.def
 */
INT16 CGEN_PUBLIC CStatistics_Pool
(
  CStatistics* _this,
  CStatistics* iSrc,
  CData*       idMap
)
{
  INT32   i          = 0;                                                        /* Current component index           */
  INT32   nC         = 0;                                                        /* Current pooled statistics class   */
  INT32   nCs        = 0;                                                        /* Current source class index        */
  INT32   nXC        = 0;                                                        /* Number of pooled classes          */
  INT32   nRpb       = 0;                                                        /* Statistics raw data block size    */
  INT16  nCheckSave = 0;                                                        /* Saved check level                 */
  CData* idAux      = NULL;                                                     /* Auxilary data instance #1         */
  CData* idPmp      = NULL;                                                     /* Pooling map                       */
  CData* idPcd      = NULL;                                                     /* Pooled class raw data buffer      */

  /* Initialize */                                                              /* --------------------------------- */
  CHECK_THIS_RV(0);                                                             /* Check this instance               */
  IF_NOK(CStatistics_Check(iSrc))                                               /* Check source statistics           */
    return IERROR(_this,ERR_INVALARG,"iSrc",0,0);                               /* ...                               */
  nCheckSave = _this->m_nCheck;                                                 /* Save check level                  */
  CStatistics_Reset(BASEINST(_this),TRUE);                                      /* Reset destination                 */
  _this->m_nCheck = nCheckSave;                                                 /* Restore check level               */
  IFIELD_RESET(CData,"dat");                                                    /* Create pool raw stats. data inst. */

  /* Protocol */                                                                /* --------------------------------- */
  IFCHECK                                                                       /* On verbose level 1                */
  {                                                                             /* >>                                */
    printf("\n"); dlp_fprint_x_line(stdout,'-',dlp_maxprintcols());             /*   Print protocol header           */
    printf("\n   statistics -pool");                                            /*   ...                             */
    printf("\n"); dlp_fprint_x_line(stdout,'-',dlp_maxprintcols());printf("\n");/*   ...                             */
  }                                                                             /* <<                                */

  /* No map --> pool all classes */                                             /* --------------------------------- */
  if (CData_IsEmpty(idMap))                                                     /* NULL or empty map instance        */
  {                                                                             /* >>                                */
    IFCHECK printf("\n   Empty pooling map --> pool all classes");              /*   Protocol (verbose level 1)      */
    CStatistics_PoolInt(_this->m_idDat,iSrc->m_idDat,0);                        /*   Pool sum data                   */
    CStatistics_PoolInt(_this->m_idDat,iSrc->m_idDat,1);                        /*   Pool min data                   */
    CStatistics_PoolInt(_this->m_idDat,iSrc->m_idDat,2);                        /*   Pool max data                   */
    STA_PROTOCOL_FOOTER(1,"done");                                              /*   Print protocol footer           */
    return O_K;                                                                 /*   That's it                       */
  }

  IFCHECK printf("\n   Pooling by map");                                        /* Protocol (verbose level 1)        */
  ICREATEEX(CData,idAux,"CStatistics_Pool.~idAux",NULL);                        /* Create auxilary data instance #1  */
  ICREATEEX(CData,idPmp,"CStatistics_Pool.~idPmp",NULL);                        /* Create pooling map                */
  ICREATEEX(CData,idPcd,"CStatistics_Pool.~idPcs",NULL);                        /* Create pooled raw stats.data inst.*/

  /* Find and copy map component (pooled class) */                              /* --------------------------------- */
  for (i=0; i<CData_GetNComps(idMap); i++)                                      /* Loop over components of idMap     */
    if (dlp_is_numeric_type_code(CData_GetCompType(idMap,i)))                   /*   Is current component numeric?   */
    {                                                                           /*   >> (Yes)                        */
      CData_SelectComps(idPmp,idMap,i,1);                                       /*     Copy component                */
      break;                                                                    /*     Have ready :)                 */
    }                                                                           /*   <<                              */
  if (CData_IsEmpty(idPmp))                                                     /* Have not got map component        */
  {                                                                             /* >>                                */
    IERROR(_this,STA_BADCOMP,"map",BASEINST(idMap)->m_lpInstanceName,"numeric");/*   Error message                   */
    DLPTHROW(STA_BADCOMP);                                                      /*   Throw exception                 */
  }                                                                             /* <<                                */

  /* Create source class component */                                           /* --------------------------------- */
  CData_AddComp(idPmp,"srcc",T_LONG);                                           /* Add source class index component  */
  for (i=0; i<CData_GetNRecs(idPmp); i++) CData_Dstore(idPmp,i,i,1);            /* Fill it                           */

  /* Finish pooling map and initialize pooling */                               /* --------------------------------- */
  CData_Sortup(idPmp,idPmp,0);                                                  /* Sort map by pooled class index    */
  nXC = (INT32)CData_Dfetch(idPmp,CData_GetNRecs(idPmp)-1,0)+1;                 /* Get greatest pooled class index   */
  IFCHECK printf("\n   Pooling %ld statistics classes",(long)nXC);              /* Protocol (verbose level 1)        */
  IFCHECKEX(3) CData_Print(idPmp);                                              /* Print pooling map (verbose lvl.3) */
  nRpb = CData_GetNRecsPerBlock(iSrc->m_idDat);                                 /* Get block size                    */

  /* Prepare pooled statistics */                                               /* --------------------------------- */
  CData_Scopy(_this->m_idDat,iSrc->m_idDat);                                    /* Create target raw data components */
  CData_Allocate(_this->m_idDat,nRpb*nXC);                                      /* Allocate target raw data          */
  CData_SetNBlocks(_this->m_idDat,nXC);                                         /* Set target statistics block number*/

  /* Pooling loop */                                                            /* --------------------------------- */
  for (i=0; i<CData_GetNRecs(idPmp); )                                          /* Loop over pooling map             */
  {                                                                             /* >>                                */
    /* - Copy raw statistics data of one pooled class */                        /*   - - - - - - - - - - - - - - - - */
    nC = (INT32)CData_Dfetch(idPmp,0,0);                                         /*   Get pooled class index          */
    IFCHECK printf("\n     Pooled class %3ld");                                 /*   Protocol (verbose level 1)      */
    for (i=0; i<CData_GetNRecs(idPmp); i++)                                     /*   Loop over partition of pool.map */
    {                                                                           /*   >>                              */
      if (nC != (INT32)CData_Dfetch(idPmp,0,0)) break;                           /*     Not the current class anymore */
      nCs = (INT32)CData_Dfetch(idPmp,i,1);                                      /*     Get source class index        */
      IFCHECK printf("\n     - Source class %3ld",nCs);                         /*     Protocol (verbose level 1)    */
      CData_SelectBlocks(idAux,iSrc->m_idDat,nCs,1);                            /*     Copy raw stats. data block    */
      CData_Cat(idPcd,idAux);                                                   /*     Append to buffer              */
    }                                                                           /*   <<                              */

    /* - Pool data */                                                           /*   - - - - - - - - - - - - - - - - */
    CData_SetNBlocks(idPcd,CData_GetNRecs(idPcd)/nRpb);                         /*   Set block count of aggr. buffer */
    IFCHECK                                                                     /*   Protocol (verbose level 1)      */
      printf("\n     - Aggregating %ld statistics classes",                     /*   |                               */
      (long)CData_GetNBlocks(idPcd));                                           /*   |                               */
    CStatistics_PoolInt(idAux,idPcd,0);                                         /*   Pool sum data                   */
    CStatistics_PoolInt(idAux,idPcd,1);                                         /*   Pool min data                   */
    CStatistics_PoolInt(idAux,idPcd,2);                                         /*   Pool max data                   */

    /* - Store pooled raw statistics data block */                              /*   - - - - - - - - - - - - - - - - */
    dlp_memmove                                                                 /*   Copy pooled raw stats. data     */
    (                                                                           /*   |                               */
      CData_XAddr(_this->m_idDat,nC*nRpb,0),                                    /*   | To target statistics block    */
      CData_XAddr(idAux,0,0),                                                   /*   | From aggregation buffer       */
      CData_GetNRecs(idAux)*CData_GetRecLen(idAux)                              /*   | Length of aggregation buffer  */
    );                                                                          /*   |                               */

    /* - Clean up auxilary instances */                                         /*   - - - - - - - - - - - - - - - - */
    CData_Reset(idPcd,TRUE);                                                    /*   Clear aggregation buffer        */
  }

  /* Clean up */                                                                /* --------------------------------- */
  IDESTROY(idAux);                                                              /* Destroy auxilary data instance #1 */
  IDESTROY(idPmp);                                                              /* Destroy pooling map               */
  IDESTROY(idPcd);                                                              /* Destroy pooled cls. raw data inst.*/
  STA_PROTOCOL_FOOTER(1,"done");                                                /* Print protocol footer             */
  return O_K;                                                                   /* Ok                                */

DLPCATCH(STA_BADCOMP)                                                           /* == Catch STA_BADCOMP exception    */
  IDESTROY(idAux);                                                              /* Destroy auxilary data instance #1 */
  IDESTROY(idPmp);                                                              /* Destroy pooling map               */
  IDESTROY(idPcd);                                                              /* Destroy pooled cls. raw data inst.*/
  STA_PROTOCOL_FOOTER(1,"FAILED");                                              /* Print protocol footer             */
  return NOT_EXEC;                                                              /* Not ok                            */
}
Beispiel #6
0
/*
 * Manual page at fst_man.def
 */
INT16 CGEN_PUBLIC CFst_Rcs(CFst* _this, INT32 nUnit, FLOAT64 nSeed)
{
#ifdef __UNENTANGLE_FST

  return IERROR(_this,FST_INTERNAL,__FILE__,__LINE__,0);
  /* NOTE: __UNENTANGLE_FST was defined (probably in dlp_config.h or fst.def).
   *       Undefine it to use this feature!
   */

#else /* #ifdef __UNENTANGLE_FST */

  INT32      nU     = 0;                                                        /* Current unit                       */
  FST_ITYPE nS     = 0;                                                        /* Current state                      */
  FST_ITYPE nS2    = 0;                                                        /* Current state                      */
  FST_ITYPE nFS    = 0;                                                        /* First state of current unit        */
  FST_ITYPE nXS    = 0;                                                        /* Number of states of current unit   */
  FST_ITYPE nT     = 0;                                                        /* Current transition                 */
  FST_ITYPE nFT    = 0;                                                        /* First transition of current unit   */
  FST_ITYPE nXT    = 0;                                                        /* Number of tran. of current unit    */
  CData*    idP    = NULL;                                                     /* Incidence matrix                   */
  CData*    idB    = NULL;                                                     /* Constanct vector of (idP-E)idX=idB */
  CData*    idI    = NULL;                                                     /* idI = (idP-E)^-1                   */
  CData*    idX    = NULL;                                                     /* Solution of (idP-E)idX=idB         */
  FST_WTYPE nPSum  = 0.;                                                       /* Probability sum/state              */
  INT32      nIcW   = -1;                                                       /* Index of probability comp. in td   */
  INT32      nIcRcs = -1;                                                       /* Index of ref. counter comp. in sd  */
  INT32      nIcRct = -1;                                                       /* Index of ref. counter comp. in td  */

  /* Validation */
  CHECK_THIS_RV(NOT_EXEC);
  CFst_Check(_this);

  if (CFst_Wsr_GetType(_this,&nIcW)!=FST_WSR_PROB)
    return IERROR(_this,FST_MISS,"transition probability component",NC_TD_PSR,"transition table");

  /* Initialize - Find or add reference counters */
  nIcRcs = CData_FindComp(AS(CData,_this->sd),NC_SD_RC);
  nIcRct = CData_FindComp(AS(CData,_this->td),NC_TD_RC);
  if (nIcRcs<0)
  {
    CData_AddComp(AS(CData,_this->sd),NC_SD_RC,DLP_TYPE(FST_WTYPE));
    nIcRcs = CData_GetNComps(AS(CData,_this->sd))-1;
  }
  if (nIcRct<0)
  {
    CData_AddComp(AS(CData,_this->td),NC_TD_RC,DLP_TYPE(FST_WTYPE));
    nIcRct = CData_GetNComps(AS(CData,_this->td))-1;
  }

  /* Initialize - Create auxilary instances */
  ICREATEEX(CData,idP,"~CFst_Reverse.idP",NULL);
  ICREATEEX(CData,idB,"~CFst_Reverse.idB",NULL);
  ICREATEEX(CData,idI,"~CFst_Reverse.idI",NULL);
  ICREATEEX(CData,idX,"~CFst_Reverse.idX",NULL);

  /* Loop over units */
  for (nU=nUnit<0?0:nUnit; nU<UD_XXU(_this); nU++)
  {
    CData_Reset(BASEINST(idP),TRUE);
    CData_Reset(BASEINST(idB),TRUE);
    CData_Reset(BASEINST(idI),TRUE);
    CData_Reset(BASEINST(idX),TRUE);
    nFS = UD_FS(_this,nU);
    nXS = UD_XS(_this,nU);
    nFT = UD_FT(_this,nU);
    nXT = UD_XT(_this,nU);

    /* Export transposed ergodic incidence matrix */
    IF_NOK(CData_AddNcomps(idP,DLP_TYPE(FST_WTYPE),UD_XS(_this,nU)+1)) break;
    IF_NOK(CData_Allocate (idP,UD_XS(_this,nU)+1)                    ) break;
    IF_NOK(CData_AddNcomps(idB,DLP_TYPE(FST_WTYPE),1                )) break;
    IF_NOK(CData_Allocate (idB,UD_XS(_this,nU)+1)                    ) break;

    /* Fill transposed incidence matrix
       (summing up probabilities of parallel transitions) */
    for (nT=nFT; nT<nFT+nXT; nT++)
      *(FST_WTYPE*)CData_XAddr(idP,TD_TER(_this,nT),TD_INI(_this,nT)) +=
        *(FST_WTYPE*)CData_XAddr(AS(CData,_this->td),nT,nIcW);

    for (nS=0; nS<nXS; nS++)
    {
      if ((SD_FLG(_this,nS+nFS)&0x01)==0x01)     /* Connect final states with start state */
      {
        for (nS2=1, nPSum=0.; nS2<nXS; nS2++)
          nPSum += *(FST_WTYPE*)CData_XAddr(idP,nS2,nS);

        *(FST_WTYPE*)CData_XAddr(idP,0,nS) = 1.-nPSum;
      }

      *(FST_WTYPE*)CData_XAddr(idP,nS,nS)-=1.;   /* Subtract eigenvalue 1 from main diagonal */
      *(FST_WTYPE*)CData_XAddr(idP,nXS,nS)=1.;   /* Additional equation implementing constraint sum(P_state)=1 */
      *(FST_WTYPE*)CData_XAddr(idP,nS,nXS)=1.;   /* Additional variable making incidence matrix quadratic */
    }

    /* Fill constant vector */
    CData_Fill(idB,CMPLX(1.),CMPLX(0.));

    /* Calculate eigenvector of length 1 and eigenvalue 1
       --> stationary state probabilities */
    CMatrix_Op(idI,idP,T_INSTANCE,NULL,T_IGNORE,OP_INVT);
    CMatrix_Op(idX,idB,T_INSTANCE,idI,T_INSTANCE,OP_MULT);

    /* Fill in state reference counters */
    if (nSeed>0.) nSeed /= CData_Dfetch(idX,0,0); else nSeed = 1.;
    for (nS=0; nS<nXS; nS++)
      CData_Dstore(AS(CData,_this->sd),nSeed*CData_Dfetch(idX,nS,0),nS+nFS,nIcRcs);

    /* Calculate stationary transition probabilities */
    for (nT=nFT; nT<nFT+nXT; nT++)
      CData_Dstore
      (
        AS(CData,_this->td),
        CData_Dfetch(AS(CData,_this->sd),TD_INI(_this,nT)+nFS,nIcRcs) *
          CData_Dfetch(AS(CData,_this->td),nT,nIcW),
        nT,nIcRct
      );

    /* Clean up */
    IDESTROY(idP);
    IDESTROY(idB);
    IDESTROY(idI);
    IDESTROY(idX);

    /* Stop in single unit mode */
    if (nUnit>=0) break;
  }

  return O_K;

#endif /* #ifdef __UNENTANGLE_FST */
}
Beispiel #7
0
/*
 * Manual page at fst_man.def
 */
INT16 CGEN_PUBLIC CFst_Probs(CFst* _this, INT32 nUnit)
{
  INT32          nU     = 0;                                                     /* Current unit                      */
  FST_ITYPE     nS     = 0;                                                     /* Current state                     */
  FST_ITYPE     nT     = 0;                                                     /* Current transition                */
  FST_ITYPE     nXS    = 0;                                                     /* Number of states in current unit  */
  INT16         nWsrt  = 0;                                                     /* Weight semiring type              */
  INT32          nIcW   = -1;                                                    /* Weight component in td            */
  INT32          nIcPfl = -1;                                                    /* Floor probability component in sd */
  INT32          nIcRct = -1;                                                    /* Reference counter component in td */
  INT32          nTrCnt = 0;                                                     /* Number of outgoing trans./state   */
  FST_WTYPE     nW     = 0.;                                                    /* Current weight                    */
  FST_WTYPE     nRc    = 0.;                                                    /* Current reference counter/prob.   */
  FST_WTYPE     nRcSum = 0.;                                                    /* Reference counter sum/state       */
  FST_WTYPE     nTW    = 0.;                                                    /* trans.weight                      */
  FST_WTYPE     nTWAgg = 0.;                                                    /* trans.weight aggregator for MAP   */
  CData*        idTd   = NULL;                                                  /* Pointer to transition table       */
  FST_TID_TYPE* lpTI   = NULL;                                                  /* Automaton iterator data structure */
  BYTE*         lpT    = NULL;                                                  /* Current transition (iteration)    */
  FST_WTYPE     nMAP   = _this->m_bUsemap ? _this->m_nMapexp : 0.;              /* MAP exponent (0:off,1:min,-1:max) */
  INT32          nMAPi  = 0;                                                     /* MAP iteration index               */

  /* Validation */                                                              /* --------------------------------- */
  CHECK_THIS_RV(NOT_EXEC);                                                      /* Check this pointer                */
  CFst_Check(_this);                                                            /* Check FST(s)                      */

  /* Initialize */                                                              /* --------------------------------- */
  idTd   = AS(CData,_this->td);                                                 /* Get pointer to transition table   */
  nIcRct = CData_FindComp(AS(CData,_this->td),NC_TD_RC);                        /* Find reference counters           */
  nWsrt  = CFst_Wsr_GetType(_this,&nIcW);                                       /* Find weights and get type         */
  switch (nWsrt)                                                                /* branch for weight semiring type   */
  {                                                                             /* >>                                */
    case FST_WSR_NONE:                                                          /*   No weights                      */
      CData_AddComp(idTd,NC_TD_PSR,DLP_TYPE(FST_WTYPE));                        /*     Add some                      */
      nIcW = CData_GetNComps(idTd)-1;                                           /*     Get index of new weight comp. */
      for (nT=0; nT<UD_XXT(_this); nT++)                                        /*     Initialize probabilities      */
        CData_Dstore(idTd,1.,nT,nIcW);                                          /*     ...                           */
      break;                                                                    /*     *                             */
    case FST_WSR_PROB:                                                          /*   Probabilities                   */
      break;                                                                    /*     * (nothing to be done)        */
    case FST_WSR_TROP: /* fall through */                                       /*   Tropical weights                */
    case FST_WSR_LOG:                                                           /*   Logarithmic weights             */
      for (nT=0; nT<UD_XXT(_this); nT++)                                        /*     Loop over all transitions     */
        CData_Dstore(idTd,                                                      /*       Convert to probabilities    */
          exp(-1.*CData_Dfetch(idTd,nT,nIcW)),nT,nIcW);                         /*       |                           */
      break;                                                                    /*     *                             */
    default:                                                                    /*   Unknown weight semiring         */
      DLPASSERT(FMSG("Unknown weight semiring"));                               /*     New weight semiring type?     */
      CData_SetCname(idTd,nIcW,NC_TD_PSR);                                      /*     Rename to probabilities       */
      for (nT=0; nT<UD_XXT(_this); nT++)                                        /*     Initialize probabilities      */
        CData_Dstore(idTd,1.,nT,nIcW);                                          /*     ...                           */
  }                                                                             /* <<                                */
  if (_this->m_nSymbols>0 && _this->m_nRcfloor>0.)                              /* Smoothing enabled                 */
  {                                                                             /* >>                                */
    if (CData_FindComp(AS(CData,_this->sd),"~PFL")<0)                           /*   No floor prob. comp. at states  */
      CData_AddComp(AS(CData,_this->sd),"~PFL",DLP_TYPE(FST_WTYPE));            /*     Add it                        */
    nIcPfl = CData_FindComp(AS(CData,_this->sd),"~PFL");                        /*   Get index of floor prob. comp.  */
  }                                                                             /* <<                                */

  /* Loop over units */                                                         /* --------------------------------- */
  for (nU=nUnit<0?0:nUnit; nU<UD_XXU(_this); nU++)                              /* For all units ...                 */
  {                                                                             /* >>                                */
    lpTI = CFst_STI_Init(_this,nU,FSTI_SORTINI);                                /*   Get sorted transition iterator  */

    /* Loop over states */                                                      /*   - - - - - - - - - - - - - - - - */
    for (nS=0,nXS=UD_XS(_this,nU); nS<nXS; nS++)                                /*   For all states of the unit ...  */
    /* Loop over MAP-Iterations if MAP enabled */                               /*   - - - - - - - - - - - - - - - - */
    for(nMAPi=0;nMAPi<(nMAP!=0.?10:1);nMAPi++)                                  /*   For all MAP-iterations ...      */
    {                                                                           /*   >>                              */
      /* Pass 1: Count outgoing transitions and sum up reference counters */    /*                                   */
      nRcSum = 0.;                                                              /*     Reset ref. ctr. accumulator   */
      nTWAgg = 0.;                                                              /*     Reset MAP TW accumulator      */
      nTrCnt = 0;                                                               /*     Reset transition counter      */
      lpT    = NULL;                                                            /*     Initialize transition pointer */
      while ((lpT=CFst_STI_TfromS(lpTI,nS,lpT))!=NULL)                          /*     Enumerate transitions at nS   */
      {                                                                         /*     >>                            */
        nRc = nIcRct>=0                                                         /*       Get ref. ctr. or prob.      */
            ? CData_Dfetch(idTd,CFst_STI_GetTransId(lpTI,lpT),nIcRct)           /*       |                           */
            : *CFst_STI_TW(lpTI,lpT);                                           /*       |                           */
        if (nRc>=0.) nRcSum+=nRc;                                               /*       Accumulate non-neg. values  */
        if (nMAP!=0. && nRc>0.)                                                 /*       if MAP enabled and T. used  */
        {                                                                       /*       >>                          */
          nTW = *CFst_STI_TW(lpTI,lpT);                                         /*         get trans.weight          */
          if(nTW<=0.) nTW=0.0001;                                               /*         solve prob. with 0 TW's   */
          nTWAgg += nTW*log(nTW);                                               /*         acc. TW's                 */
        }                                                                       /*       <<                          */
        nTrCnt++;                                                               /*       Count transitions           */
      }                                                                         /*     <<                            */
      if (nIcPfl)                                                               /*     Smoothing eneabled            */
        CData_Dstore(AS(CData,_this->sd),                                       /*       Store floor probability     */
          _this->m_nRcfloor / (nRcSum + _this->m_nSymbols*_this->m_nRcfloor),   /*       |                           */
          nS,nIcPfl);                                                           /*       |                           */
                                                                                /*                                   */
      /* Pass 2: Calculate probabilities */                                     /*                                   */
      if (nTrCnt)                                                               /*     If there were trans. at nS    */
      {                                                                         /*     >>                            */
        lpT = NULL;                                                             /*       Initialize transition ptr.  */
        while ((lpT=CFst_STI_TfromS(lpTI,nS,lpT))!=NULL)                        /*       Enumerate transitions at nS */
        {                                                                       /*       >>                          */
          nRc = nIcRct>=0                                                       /*         Get ref. ctr. or prob.    */
              ? CData_Dfetch(idTd,CFst_STI_GetTransId(lpTI,lpT),nIcRct)         /*         |                         */
              : *CFst_STI_TW(lpTI,lpT);                                         /*         |                         */
          if (nMAP!=0.)                                                         /*         if MAP enabled            */
          {                                                                     /*         <<                        */
            nTW = *CFst_STI_TW(lpTI,lpT);                                       /*           get trans.weight        */
            if(nTW<=0.) nTW=0.0001;                                             /*           solve prob. with 0 TW's */
            if(nIcRct>=0 && _this->m_nSymbols>0 && _this->m_nRcfloor>0)         /*           do jeffrey smooth?      */
              nRc = nRcSum * (nRc + (FST_WTYPE)_this->m_nRcfloor) /             /*             smooth nRc            */
                (nRcSum + ((FST_WTYPE)_this->m_nSymbols*_this->m_nRcfloor));    /*             |                     */
            nTW = (nRc<=0. ? 0. : (nRc+nMAP*nTW*log(nTW))/(nRcSum+nMAP*nTWAgg));/*           do MAP-iteration        */
            *CFst_STI_TW(lpTI,lpT) = nTW;                                       /*           write trans.weight      */
          }                                                                     /*         >>                        */
          else                                                                  /*         else                      */
            if (nIcRct<0 || _this->m_nSymbols<=0 || _this->m_nRcfloor<=0.)      /*         No ref.ctrs. or smoothing */
              *CFst_STI_TW(lpTI,lpT) =                                          /*           Store equally distrib.  */
                nRcSum==0. ? 1./(FST_WTYPE)nTrCnt : nRc/nRcSum;                 /*           | or renormalized probs.*/
            else                                                                /*         Ref. ctrs. or smoothing   */
              *CFst_STI_TW(lpTI,lpT) =                                          /*           Store Jeffrey smoothed  */
                (nRc + (FST_WTYPE)_this->m_nRcfloor) /                          /*           | probabilities         */
                (nRcSum + ((FST_WTYPE)_this->m_nSymbols*_this->m_nRcfloor));    /*           |                       */
        }                                                                       /*       <<                          */
      }                                                                         /*     <<                            */
    }                                                                           /*   <<                              */
    CFst_STI_Done(lpTI);                                                        /*   Destroy iterator                */
    if (nUnit>=0) break;                                                        /*   Stop in single unit mode        */
  }                                                                             /* <<                                */

  /* Convert back to logarithmic/tropical weights */                            /* --------------------------------- */
  if (nWsrt==FST_WSR_LOG || nWsrt==FST_WSR_TROP)                                /* Loarithmic or tropical weights    */
    for (nT=0; nT<UD_XXT(_this); nT++)                                          /*   Loop over all transitions       */
    {                                                                           /*   >>                              */
      nW = CData_Dfetch(idTd,nT,nIcW);                                          /*     Get probability               */
      nW = nW>0. ? -log(nW) : _this->m_nWceil;                                  /*     Convert to log./trop. weight  */
      CData_Dstore(idTd,nW,nT,nIcW);                                            /*     Store weight                  */
    }                                                                           /*   <<                              */

  /* Clean up */                                                                /* --------------------------------- */
  return O_K;                                                                   /* The end                           */
}