📄 hdman.c
字号:
db->nextWord=labels[0]; db->pbuf.source=db->source; if (labels[1]!=NULL) db->nextOutSym=labels[1]; else db->nextOutSym=labels[0]; db->pbuf.phone[0]=wdBnd; for (i=0;i<n;i++) db->pbuf.phone[i+1]=labels[i+2]; db->pbuf.phone[n+1]=wdBnd; db->pbuf.nPhone=n+2; db->pbuf.prob=prob; return TRUE;}/* WriteEntry: write out a dictionary entry and update newPhones */void WriteEntry(FILE *f, LabId word, LabId outsym, Pronunciation *p, int margin, Boolean findNew){ int i,st,en; char buf[256],m[20]; if (p->nPhone == 0) return; if (!nullOutput) { if (word != NULL) strcpy(buf,ReWriteString(word->name,NULL,ESCAPE_CHAR)); else strcpy(buf,""); for (i=0; i<margin; i++) m[i] = ' '; m[i]='\0'; fprintf(f,"%s%-15s",m,buf); if (incOutSyms) { strcpy(buf,"["); strcat(buf,ReWriteString(outsym->name,NULL,ESCAPE_CHAR)); strcat(buf,"]"); fprintf(f," %-15s",buf); } if (incProbs) { if (p->prob<1.0) fprintf(f," %8.6f",p->prob); else fprintf(f," "); } } st = 1; if (p->phone[0] != wdBnd){ st = 0; if (isLogging && margin==0) fprintf(logF,"WARNING: no left word bnd in word %s\n",word->name); } en = p->nPhone-1; if (p->phone[en] != wdBnd){ ++en; if (isLogging && margin==0) fprintf(logF,"WARNING: no right word bnd in word %s\n",word->name); } for (i=st; i<en; i++) { if (!nullOutput) fprintf(f," %s",ReWriteString((p->phone[i])->name,NULL,ESCAPE_CHAR)); if (findNew) PutPhone(p->phone[i]); } if (tagSources && !nullOutput) fprintf(f," [%s]",ReWriteString(p->source->name,NULL,ESCAPE_CHAR)); if (!nullOutput) fprintf(f,"\n");}/* ----------------- Read/Write Dictionary Words --------------- *//* ReadDictProns: read entries for next word, return FALSE if none found */Boolean ReadDictProns(DBuffer *db){ LabId thisWord; Boolean ok; int i = 0; if (db->nextWord == NULL) return FALSE; if (db->wbuf.word != NULL && strcmp(db->wbuf.word->name,db->nextWord->name) > 0 ) HError(1452,"ReadDictProns: word %s out of order in dict %s", db->nextWord->name,db->name); db->wbuf.word = thisWord = db->nextWord; db->wbuf.outsym = db->nextOutSym; db->wbuf.pron[i++]=db->pbuf; ok = ReadNextWord(db); while (ok && db->nextWord == thisWord){ if (i == MAXPRONS) HError(1430,"ReadDictProns: max prons exceeded in word %s from dict %s", thisWord->name,db->name); db->wbuf.pron[i++]=db->pbuf; ok = ReadNextWord(db); } db->wbuf.nPron = i; ++db->totalWords; db->totalProns += i; return TRUE;}/* WriteDictWord: write current word to output file */void WriteDictWord(DBuffer *db, FILE *f, int margin, Boolean findNew){ int i; if (f==NULL) return; for (i=0; i<db->wbuf.nPron; i++) WriteEntry(f,db->wbuf.word,db->wbuf.outsym,db->wbuf.pron+i,margin, findNew);}void ShowDB(DBuffer *db, char * title){ printf("%s - DB %s: word %s, next %s\n", title, db->name, db->wbuf.word==NULL?"NULL":db->wbuf.word->name, db->nextWord==NULL ?"NULL":db->nextWord->name); fflush(stdout);}/* --------------------------- Editing ------------------------- *//* IsInIdList: return true if id is in idlist */Boolean IsInIdList(LabId id, LabId *idlist){ while (*idlist != NULL) { if (id == *idlist) return TRUE; ++idlist; } return FALSE;}/* UCPhoneOp: Upper Case Phone command */void UCPhoneOp(WordBuf *wb){ int i,j; Pronunciation *p; for (i=0;i<wb->nPron; i++){ p = wb->pron+i; for (j=0; j<p->nPhone; j++) p->phone[j] = UCase(p->phone[j]); }}/* LCPhoneOp: Lower Case Phone command */void LCPhoneOp(WordBuf *wb){ int i,j; Pronunciation *p; for (i=0; i<wb->nPron; i++){ p = wb->pron+i; for (j=0; j<p->nPhone; j++) p->phone[j] = LCase(p->phone[j]); }}/* DeleteWordOp: Delete Word Command - marks by setting nPron to zero */void DeleteWordOp(WordBuf *wb, LabId *args){ if (IsInIdList(wb->word,args)) wb->nPron = 0;}/* DeleteSourceOp: del source if more than one */int DeleteSourceOp(WordBuf *wb, LabId *args){ int i,j,k,del=0; Pronunciation *p, *s, *t; if (wb->nPron < 2) return 0; for (i=0; i<wb->nPron; i++){ p = wb->pron+i; if (p->source == *args){ if (trace&T_DSOP) printf("Removing pron %s from word %s\n", (*args)->name,wb->word->name); --wb->nPron; ++del; for (j=i; j<wb->nPron; j++){ t = wb->pron+j; s = t+1; t->nPhone = s->nPhone; t->source = s->source; t->prob = s->prob; for (k=0; k<t->nPhone; k++) t->phone[k] = s->phone[k]; } } } return del;}/* DelDefOp: Delete Word Command - marks by setting nPron to zero */void DelDefOp(WordBuf *wb, LabId *args){ int i,j,idx=0; Boolean found = FALSE; Pronunciation *p; LabId *ph; char buf[256]; if (wb->word == *args){ for (i=0; !found && i<wb->nPron; i++){ found = TRUE; idx = i; p = wb->pron+i; ph = args+1; for (j=1; *ph != NULL && j<p->nPhone; j++,ph++){ if (p->phone[j] != *ph){ found = FALSE; break; } } } if (found){ if (trace&T_DWOP){ buf[0] = '\0'; for (ph=args; *ph!=NULL; ph++){ strcat(buf," "); strcat(buf,(*ph)->name); } printf(" deleting definition %s\n",buf); } for (j=idx+1; j<wb->nPron; j++) wb->pron[j-1] = wb->pron[j]; --wb->nPron; }else{ buf[0] = '\0'; for (ph=args; *ph!=NULL; ph++){ strcat(buf," "); strcat(buf,(*ph)->name); } HError(-1431,"DelDefOp: pron %s not found",buf); } }}/* FunctionWordOp: Function Word Command - makes fn word phones of form W.A */void FunctionWordOp(WordBuf *wb, LabId *args){ int i,j; static char s[255]; Pronunciation *p; if (IsInIdList(wb->word,args)) for (i=0; i<wb->nPron; i++){ p = wb->pron+i; for (j=1; j<p->nPhone-1; j++){ sprintf(s,"%s.%s",(wb->word)->name,(p->phone[j])->name); p->phone[j] = GetLabId(s,TRUE); } }} /* ReplacePhoneOp: Replace Phone Command */void ReplacePhoneOp(WordBuf *wb, LabId *args){ int i,j; Pronunciation *p; for (i=0; i<wb->nPron; i++){ p = wb->pron+i; for (j=0; j<p->nPhone; j++) if (IsInIdList(p->phone[j],args+1)) p->phone[j] = *args; }} /* ReplaceWordOp: Replace Word Command */void ReplaceWordOp(WordBuf *wb, LabId *args){ if (IsInIdList(wb->word,args+1)) { if (wb->word == wb->outsym) wb->word = wb->outsym = *args; else wb->word = *args; }} /* GetContextList: extract context list for id if any */ LabId *GetContextList(LabId id, DBuffer *db){ int i; LabId *list; for (i=0; i<db->numCons; i++){ list = db->contexts[i].args; if (id == *list) return list+1; } return NULL;}/* ContextRep: do context replace on given pronunciation - note * matches any context */void ContextRep(Pronunciation *p, LabId *args, DBuffer *db){ LabId lc,rc,cc; Boolean ltrue, replace; LabId *lcList,*rcList; int i; lc = *(args+1); cc = *(args+2); rc = *(args+3); lcList = GetContextList(lc,db); rcList = GetContextList(rc,db); for (i=1; i<p->nPhone-1; i++) { replace = FALSE; if (p->phone[i] == cc) { if (lcList != NULL) ltrue = IsInIdList(p->phone[i-1],lcList); else ltrue = (lc == asterix || lc == p->phone[i-1] ); if (ltrue){ if (rcList != NULL) replace = IsInIdList(p->phone[i+1],rcList); else replace = (rc == asterix || rc == p->phone[i+1] ); } } if (replace) p->phone[i] = *args; } }/* ContextReplaceOp: ContextReplace Command */void ContextReplaceOp(WordBuf *wb, LabId *args, DBuffer *db){ int i; Pronunciation *p; for (i=0; i<wb->nPron; i++){ p = wb->pron+i; ContextRep(p,args,db); }} /* SeqMatch: Returns true if the nMerge sequence of LabIds are the same in list1 and list2 */Boolean SeqMatch(int nMerge, LabId *list1, LabId *list2){ int i; for (i=0; i<nMerge; i++,list1++,list2++) if (*list1 != *list2) return FALSE; return TRUE;} /* MergePhon: merge all occs of phone sequence in args */void MergePhon(Pronunciation *p, int nArgs, LabId *args){ int i,j,nMerge; nMerge = nArgs-1; for (i=0;i<=p->nPhone-nMerge;i++) if (SeqMatch(nMerge,p->phone+i,args+1)) { p->phone[i] = *args; for (j=i+1;j<p->nPhone;j++) p->phone[j] = p->phone[j+nMerge-1]; p->nPhone -= nMerge-1; }}/* MergePhoneOp: MergePhone Command */void MergePhoneOp(WordBuf *wb, int nArgs, LabId *args){ int i; Pronunciation *p; for (i=0; i<wb->nPron; i++){ p = wb->pron+i; MergePhon(p,nArgs,args); }} /* SplitPhon: split a phone sequence into multiple phones */void SplitPhon(Pronunciation *p, int nArgs, LabId *args){ int i,j,nSplit,nExtra; nSplit = nArgs-1; nExtra = nSplit-1; for (i=0;i<p->nPhone;) if (p->phone[i] == args[0]) { if (p->nPhone+nExtra > MAXPHONES) HError(1430,"SplitPhon: Too many phones to split"); for (j=p->nPhone-1+nExtra; j>=i+1; j--) p->phone[j] = p->phone[j-nExtra]; for (j=0; j<nSplit; j++) p->phone[i+j] = args[j+1]; i += nExtra+1; p->nPhone += nExtra; } else i++;} /* SplitPhoneOp: SplitPhone Command */void SplitPhoneOp(WordBuf *wb, int nArgs, LabId *args){ int i; Pronunciation *p; for (i=0; i<wb->nPron; i++){ p = wb->pron+i; SplitPhon(p,nArgs,args); }} /* DeletePhoneOp: DeletePhone Command */void DeletePhoneOp(WordBuf *wb, LabId *args){ int i,j,k; Pronunciation *p; for (k=0; k<wb->nPron; k++){ p = wb->pron+k; for (i=0;i<p->nPhone;) if (IsInIdList(p->phone[i],args)) { for (j=i+1;j<p->nPhone;j++) p->phone[j-1] = p->phone[j]; p->nPhone--; } else i++; }}/* AppendPhone: append id to given pronunciation */void AppendPhone(Pronunciation *p, LabId id){ if (p->nPhone == MAXPHONES) HError(1430,"AppendPhone: MAXPHONES exceeded"); p->phone[p->nPhone-1] = id; p->phone[p->nPhone++] = wdBnd;}/* DuplicatePron: duplicate selected pron and insert at end of wbuf */void DuplicatePron(WordBuf *wb, int i){ int j; Pronunciation *s, *t; if (wb->nPron == MAXPRONS) HError(1430,"DuplicatePron: MAXPRONS exceeded"); s = wb->pron+i; t = wb->pron+wb->nPron; t->source = s->source; t->nPhone = s->nPhone; t->prob = s->prob; for (j=0; j<s->nPhone; j++) t->phone[j] = s->phone[j]; ++wb->nPron; }/* AppendSilenceOp: AppendSilences Command */void AppendSilenceOp(WordBuf *wb, LabId *args){ LabId *i; int j,n,k; n = wb->nPron-1; for (j=n; j>=0; j--){ for (i=args+1; *i != NULL; i++){ DuplicatePron(wb,j); k = wb->nPron-1; AppendPhone(wb->pron+k,*i); } AppendPhone(wb->pron+j, *args); }}/* MakeTriId: concatenate args separated by - and +'s and return its id */LabId MakeTriId(LabId l, LabId c, LabId r){ char buf[100];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -