📄 hresults.c
字号:
}/* DoCompare: fill the grid */void DoCompare(void){ CellPtr gridi,gridi1; int h,d,v,i,j; Boolean refnull,testnull; for (i=1;i<=nTest;i++){ gridi = grid[i]; gridi1 = grid[i-1]; testnull = (lTest[i] == nulClass); for (j=1;j<=nRef;j++) { refnull = (lRef[j] == nulClass); if (refnull && testnull) { /* both ref and test are null */ h = gridi1[j].score; d = gridi1[j-1].score; v = gridi[j-1].score; if (d<=v && d<=h) { gridi[j] = gridi1[j-1]; gridi[j].dir = DIAG; } else if (h<v) { gridi[j] = gridi1[j]; gridi[j].dir = HOR; } else { gridi[j] = gridi[j-1]; gridi[j].dir = VERT; } } else if (refnull) { /* ref is null */ gridi[j] = gridi[j-1]; gridi[j].dir = VERT; } else if (testnull) { /* test is null */ gridi[j] = gridi1[j]; gridi[j].dir = HOR; } else { /* normal case */ h = gridi1[j].score +insPen; d = gridi1[j-1].score; if (lRef[j] != lTest[i]) d += subPen; v = gridi[j-1].score + delPen; if (d<=h && d<=v) { /* DIAG = hit or sub */ gridi[j] = gridi1[j-1]; gridi[j].score = d; gridi[j].dir = DIAG; if (lRef[j] == lTest[i]) ++gridi[j].hit; else ++gridi[j].sub; } else if (h<v) { /* HOR = ins */ gridi[j] = gridi1[j]; gridi[j].score = h; gridi[j].dir = HOR; ++ gridi[j].ins; } else { /* VERT = del */ gridi[j] = gridi[j-1]; gridi[j].score = v; gridi[j].dir = VERT; ++gridi[j].del; } } } /* for j */ } /* for i */ grid[0][0].dir = NIL;}/* DoCompareNIST: fill the grid using NIST alignment rules*/void DoCompareNIST(void){ CellPtr gridi,gridi1; int h,d,v,i,j; Boolean refnull,testnull; for (i=1;i<=nTest;i++){ gridi = grid[i]; gridi1 = grid[i-1]; testnull = (lTest[i] == nulClass); for (j=1;j<=nRef;j++) { refnull = (lRef[j] == nulClass); if (refnull && testnull) { /* both ref and test are null */ h = gridi1[j].score; d = gridi1[j-1].score; v = gridi[j-1].score; if (v <= d && v <= h) { gridi[j] = gridi[j-1]; gridi[j].dir = VERT; } else if (d <= h) { gridi[j] = gridi1[j-1]; gridi[j].dir = DIAG; } else { gridi[j] = gridi1[j]; gridi[j].dir = HOR; } } else if (refnull) { /* ref is null */ gridi[j] = gridi[j-1]; gridi[j].dir = VERT; } else if (testnull) { /* test is null */ gridi[j] = gridi1[j]; gridi[j].dir = HOR; } else { /* normal case */ h = gridi1[j].score +insPenNIST; d = gridi1[j-1].score; if (lRef[j] != lTest[i]) d += subPenNIST; v = gridi[j-1].score + delPenNIST; if (v <= d && v <= h) { /* VERT = del */ gridi[j] = gridi[j-1]; gridi[j].score = v; gridi[j].dir = VERT; ++gridi[j].del; } else if (d <= h) { /* DIAG = hit or sub */ gridi[j] = gridi1[j-1]; gridi[j].score = d; gridi[j].dir = DIAG; if (lRef[j] == lTest[i]) ++gridi[j].hit; else ++gridi[j].sub; } else { /* HOR = ins */ gridi[j] = gridi1[j]; gridi[j].score = h; gridi[j].dir = HOR; ++ gridi[j].ins; } } } /* for j */ } /* for i */ grid[0][0].dir = NIL;}/* ------------------- Aligned Transcriptions --------------- *//* AppendItem: appends item padded to width spaces to s */void AppendItem(char *s, char *item, int len, int width){ char buf[64]; int i; for (i=0; i<len; i++) buf[i] = item[i]; for (i=len; i<width; i++) buf[i] = ' '; buf[width] = '\0'; strcat(s,buf);}/* AppendPair: append a to linea and b to lineb */void AppendPair(char *linea, char *a, char *lineb, char *b){ int lena,lenb,width; lena = strlen(a); lenb = strlen(b); width = ((lena>=lenb) ? lena : lenb)+1; AppendItem(linea,a,lena,width); AppendItem(lineb,b,lenb,width);}/* AppendCell: path upto grid[i][j] to tb and rb (recursive) */void AppendCell(int i, int j, char *tb, char *rb){ char *rlab,*tlab; LabId rid=NULL,tid=NULL; char empty[1]; if (i<0 || j<0) HError(3391,"AppendCell: Trace back failure"); empty[0] = '\0'; rlab = tlab = empty; switch (grid[i][j].dir) { case DIAG: tid = lTest[i]; tlab = tid->name; rid = lRef[j]; rlab = rid->name; AppendCell(i-1,j-1,tb,rb); break; case HOR: tid = lTest[i]; tlab = tid->name; rid = NULL; rlab = empty; AppendCell(i-1,j,tb,rb); break; case VERT: tid = NULL; tlab = empty; rid = lRef[j]; rlab = rid->name; AppendCell(i,j-1,tb,rb); break; case NIL: return; } if (tid != nulClass && rid != nulClass) AppendPair(rb,rlab,tb,tlab);}/* OutTrans: output aligned transcriptions using best path in grid */void OutTrans(void){ char refBuf[4096]; /* no checking of output length so */ char testBuf[4096]; /* these are generous sizes */ strcpy(refBuf," LAB: "); strcpy(testBuf," REC: "); AppendCell(nTest,nRef,testBuf,refBuf); printf("Aligned transcription: %s vs %s\n", labfn, recfn); printf("%s\n",refBuf); printf("%s\n",testBuf); fflush(stdout);}/* ----------------- HMMList handling ----------- */static int nLabs;static LabId *names;Boolean ReadWordFromLine(Source *src, char *s){ char *p; int ch; p=s; if (s!=NULL) *p=0; SkipWhiteSpace(src); while ((ch=GetCh(src))!=EOF) { if (isspace(ch)) break; if (s!=NULL) *p++=ch; } if (s!=NULL) *p=0; UnGetCh(ch,src); SkipLine(src); if (ch==-1 && s==p) return(FALSE); else return(TRUE);}/* ReadHMMList: Read in List of Labels from file fn */void ReadHMMList(char *fn){ Source source; LabId labid; char buf[MAXSTRLEN]; int i; /* Once to find out how many lines */ if(InitSource(fn,&source,HMMListFilter)<SUCCESS) HError(3310,"ReadHMMList: Can't open file %s", fn); i=0; while(ReadWordFromLine(&source,NULL)) i++; CloseSource(&source); /* Now do it for real */ nLabs=i; names=(LabId*)New(&permHeap,sizeof(LabId)*nLabs); --names; if(InitSource(fn,&source,HMMListFilter)<SUCCESS) HError(3310,"ReadHMMList: Can't open file %s", fn); for (i=1;i<=nLabs;i++) { ReadWordFromLine(&source,buf); labid=GetLabId(buf,TRUE); names[i]=labid; labid->aux = (Ptr)i; } CloseSource(&source);}int Index(LabId labid){ int i; i=(int)labid->aux; if (wSpot && i==0) return(0); if (i<1 || i>nLabs || names[i]!=labid) HError(3331,"Index: Label %s not in list[%d of %d]", labid->name,i,nLabs); return(i);}/* ----------------- Confusion Matrix (Phoneme Stats) ----------- */#define MAXCONMATSIZE 200static ShortVec *conMat; /* confusion matrix, conMat[i][j] is the number of times label i was recognised as label j */static ShortVec conDel,conIns; /* corresponding deletion and insertion counts *//* InitConMat: allocate and initialise confusion matrix */void InitConMat(void){ int i; if (nLabs>MAXCONMATSIZE) HError(3332,"InitConMat: Confusion matrix would be too large"); conMat = (ShortVec *) New(&permHeap, nLabs*sizeof(ShortVec)); --conMat; /* index is 1..nLabs */ for (i=1;i<=nLabs;i++){ conMat[i]=CreateShortVec(&permHeap,nLabs); ZeroShortVec(conMat[i]); } conDel=CreateShortVec(&permHeap,nLabs); ZeroShortVec(conDel); conIns=CreateShortVec(&permHeap,nLabs); ZeroShortVec(conIns);}/* OutConMat: output the confusion matrix */void OutConMat(void){ Boolean *seen; int i,j,k,err,rowerr,maxlen; char *s,c,buf[64]; float correct, errprop; seen=(Boolean*)New(&tempHeap,sizeof(Boolean)*nLabs); seen--; maxlen = 0; for (i=1;i<=nLabs;i++) { k = strlen(names[i]->name); if (k > maxlen) maxlen = k; } if (maxlen>5) maxlen = 5; PrintBar(0,htkWidth,'-',"Confusion Matrix"); for (j=1; j<=nLabs; j++) { for (i=1,k=conIns[j];i<=nLabs;i++) k+=conMat[i][j]; if (k==0) seen[j]=FALSE; else seen[j]=TRUE; } for (j=0;j<maxlen;j++) { for (k=0; k<=4 ; k++) printf(" "); for (i=1;i<=nLabs;i++) { if (!seen[i]) continue; s = names[i]->name; c = (j<strlen(s))?s[j]:' '; printf(" %c ",c); } if (j==maxlen-1) printf(" Del [ %%c / %%e]"); printf("\n"); } for (i=1;i<=nLabs;i++){ for (j=1,k=conDel[j];j<=nLabs;j++) k+=conMat[i][j]; if (k==0) continue; strcpy(buf,names[i]->name); buf[4] = '\0'; printf("%4s ",buf); rowerr = 0; for (j=1; j<=nLabs; j++){ if (!seen[j]) continue; err = conMat[i][j]; if (i!=j) rowerr += err; if (err<100) printf(" %2d ",err); else printf("%4d",err); } printf("%4d",conDel[i]); if (rowerr>0) { correct = 100.0*(float)conMat[i][i]/(float)(conMat[i][i]+rowerr); errprop = 100.0*(float)rowerr/(float)nsyms; printf(" [%4.1f/%3.1f]\n",correct,errprop); } else printf("\n"); } printf("Ins "); for (j=1; j<=nLabs; j++) { if (!seen[j]) continue; printf("%4d",conIns[j]); } printf("\n"); seen++; Dispose(&tempHeap,seen);} /* CollectStats: trace back from grid[i][j] collecting phoneme stats */void CollectStats(int i,int j){ int ri,ti; LabId rlab,tlab; do { switch(grid[i][j].dir) { case NIL: return; case DIAG: rlab = lRef[j--]; tlab = lTest[i--]; if (rlab==nulClass || tlab==nulClass) break; ri=Index(rlab); ti=Index(tlab); ++conMat[ri][ti]; break; case VERT: rlab = lRef[j--]; if (rlab==nulClass) break; ri=Index(rlab); ++conDel[ri]; break; case HOR: tlab = lTest[i--]; if (tlab==nulClass) break; ti=Index(tlab); ++conIns[ti]; break; } } while (!(i==0 && j==0));}/* ---------------- Recognition Match Routines ---------------- *//* MatchRecFiles: match sequence in test vs sequence in ref */void MatchRecFiles(void){ Cell bp,*p; int i,n,err,berr,best; char buf[255]; n=(ans->numLists>maxNDepth)?maxNDepth:ans->numLists; best=0;berr=INT_MAX; for (i=1;i<=n;i++) { test=GetLabelList(ans,i); if (test->head->succ == test->tail) { HError(-3330,"MatchRecFiles: Test Output List %s(%d) is Empty",recfn,i); break; } NormaliseName(test,tlev); CreateGrid(); if (nistAlign) DoCompareNIST(); else DoCompare();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -