📄 hdman.c
字号:
i = db->script; printf("Script %s\n",name); if (db->rawMode) printf("%2d. Raw input mode\n",++j); while (i != NULL) { ++j; printf("%2d. ",j); src = *(i->cmd.args); switch (i->cmd.op) { case UCWORD: printf("Change Words to Upper Case\n"); break; case UCPHONE: printf("Change Phones to Upper Case\n"); break; case LCWORD: printf("Change Words to Lower Case\n"); break; case LCPHONE: printf("Change Phones to Lower Case\n"); break; case DELETEW: printf("Delete Defs For Words ["); PrintIdList(i->cmd.args); printf("]\n"); break; case DELSOURCE: printf("Delete Mult Prons from Source [%s]\n",src->name); break; case DELDEF: printf("Delete Def For %s = ",src->name); PrintIdList(i->cmd.args+1); printf("\n"); break; case FUNCW: printf("Make Function Word Specific Defs ["); PrintIdList(i->cmd.args); printf("]\n"); break; case REPLACEP: printf("Replace Phones ["); PrintIdList(i->cmd.args+1); printf("] by %s\n",src->name); break; case REPLACEW: printf("Replace Words ["); PrintIdList(i->cmd.args+1); printf("] by %s\n",src->name); break; case CONREPLACE: printf("Replace Phone %s in context %s_%s", (*(i->cmd.args+2))->name, (*(i->cmd.args+1))->name,(*(i->cmd.args+3))->name); printf(" by %s\n",src->name); break; case MERGEP: printf("Merge ["); PrintIdList(i->cmd.args+1); printf("] to %s\n",src->name); break; case SPLITP: printf("Split %s to [",src->name); PrintIdList(i->cmd.args+1); printf("]\n"); break; case APPSIL: printf("Append Silences ["); PrintIdList(i->cmd.args); printf("]\n"); break; case REMSTRESS: printf("Remove stress marks (mode %s)\n",src->name); break; case DELETEP: printf("Delete Phones ["); PrintIdList(i->cmd.args); printf("]\n"); break; case LCTXT: printf("Left Context ["); PrintIdList(i->cmd.args); printf("]\n"); break; case RCTXT: printf("Right Context ["); PrintIdList(i->cmd.args); printf("]\n"); break; case TCTXT: printf("Triphonise ["); PrintIdList(i->cmd.args); printf("]\n"); break; default: HError(1450,"PrintScript: Unknown Item number %d",i->cmd.op); break; } i = i->next; } if (db->numCons>0){ printf("Contexts:\n"); for (j=0; j<db->numCons; j++){ printf("%2d. %s = ",j+1,db->contexts[j].args[0]->name); PrintIdList(db->contexts[j].args+1); printf("\n"); } }}/* ReadCmd: returns next command on input f or NOCMD if none */EdOp ReadCmd(Source *src){ char cmd[MAXSTRLEN]; int i; EdOp cmdop; SkipComment(src); if (!ReadString(src,cmd)) return(NOCMD); SkipWhiteSpace(src); for (i=0,cmdop=UCWORD; i<nCmds; i++,cmdop = (EdOp) (cmdop+1)) if (strcmp(cmdMap[i],cmd) == 0) return cmdop; HError(1450,"ReadCmd: Invalid Command <%s> in file %s",cmd,src->name); return NOCMD;}/* ReadIdList: Read a list of args and store in argList, return num args */int ReadIdList(Source *src, LabId *argList){ char buf[MAXSTRLEN]; int count = 0; do { SkipWhiteSpace(src); if (src->wasNewline) break; if (!ReadString(src,buf)) break; if (count == MAXARGS) HError(1430,"ReadIdList: MAXARGS exceeded"); *argList++ = GetLabId(buf,TRUE); ++count; } while (TRUE); *argList = NULL; return count; }/* ReadScript: read a file of cmds and store script in db. If it is an input script UW/LW must be applied during reading to prevent sort order breaking. */void ReadScript(char *scriptFn, DBuffer *db, Boolean isInput){ ScriptItem *i, *head, *tail; EdOp op; Source source; FILE *file; db->rawMode=FALSE; if ((file=fopen(scriptFn,"r"))==NULL){ if (isLogging) fprintf(logF,"WARNING: no script file %s\n",scriptFn); db->script = NULL; return; } fclose(file); if(InitSource(scriptFn,&source,NoFilter)<SUCCESS) HError(1410,"ReadScript: Can't open file %s", scriptFn); head = tail = NULL; while (TRUE) { if ((op = ReadCmd(&source)) == NOCMD) break; if (op==DEFCON) { if (db->numCons == MAXCONS) HError(1430,"ReadScript: MAXCONS exceeded"); db->contexts[db->numCons].nArgs = ReadIdList(&source,db->contexts[db->numCons].args); db->contexts[db->numCons].op = DEFCON; ++db->numCons; } else if (op==RAWMODE) { db->rawMode=TRUE; } else { i = (ScriptItem *)New(&memStak,sizeof(ScriptItem)); i->cmd.op = op; i->cmd.nArgs = ReadIdList(&source,i->cmd.args); switch (op) { case DELETEW: case FUNCW: case REPLACEP: case REPLACEW: case MERGEP: case SPLITP: case DELETEP: case DELDEF: case APPSIL: if (i->cmd.nArgs==0) HError(1450,"ReadScript: Command %s with too few args", cmdMap[op]); break; case CONREPLACE: if (i->cmd.nArgs != 4) HError(1450,"ReadScript: CR command must have 4 args"); break; case DELSOURCE: case REMSTRESS: case LCTXT: case RCTXT: if (i->cmd.nArgs > 1) HError(1450,"ReadScript: Command %s has too many args", cmdMap[op]); break; case TCTXT: if (i->cmd.nArgs > 2) HError(1450,"ReadScript: TC command has too many args"); break; case UCWORD: case LCWORD: if (i->cmd.nArgs > 0) HError(1450,"ReadScript: %s command has too many args", cmdMap[op]); if (isInput) /* set input preprocessor op */ db->wop = op; break; case UCPHONE: case LCPHONE: if (i->cmd.nArgs > 0) HError(1450,"ReadScript: %s command has too many args", cmdMap[op]); break; default: break; } i->next = NULL; if (head==NULL) head = tail = i; else { tail->next = i; tail = i; } } } CloseSource(&source); db->script = head; if (trace&T_SCPT) PrintScript(scriptFn,db);}/* --------------------- Buffer Creation -------------------------- *//* SkipHeader: skip the dictionary header lines */void SkipHeader(Source *src, int skipHeaderLines){ int i; for (i=1; i <= skipHeaderLines; i++) SkipLine(src);}/* CreateBuffer: initialise an input or output buffer */void CreateBuffer(char *dName, Boolean isInput){ DBuffer *db; char buf[256],scriptFN[256],*src; Boolean ReadNextWord(DBuffer *db); if (isInput) { db = inbuf+nInputs; ++nInputs; strcpy(buf,dName); strcat(buf,".ded"); MakeFN(buf,scriptDir,NULL,scriptFN); if(InitSource(dName,&db->src,DictFilter)<SUCCESS) HError(1410,"CreateBuffer: Can't open file %s", dName); SkipHeader(&db->src,db->headSkip); }else{ db = &outbuf; db->isPipe = FALSE; if (gScriptFN==NULL) MakeFN("global.ded",scriptDir,NULL,scriptFN); else MakeFN(gScriptFN,scriptDir,NULL,scriptFN); if (!nullOutput){ if ((outfile = fopen(dName,"w")) == NULL) HError(1411,"CreateBuffer: cannot create dictionary file %s",dName); } } db->numCons = 0; db->wop = NOCMD; db->totalWords = db->totalProns = 0; db->wordsUsed = db->pronsUsed = 0; ReadScript(scriptFN,db,isInput); db->name = NewString(&memStak,strlen(dName)); strcpy(db->name,dName); src = strrchr(db->name,PATHCHAR); if (src == NULL) src = db->name; else ++src; db->source = GetLabId(src,TRUE); if (isInput) if (!ReadNextWord(db)) HError(1413,"CreateBuffer: cannot read first word in dict %s",dName); if (trace&T_TOP) printf("%s dictionary %s opened %s\n",isInput?"Source":"Output", dName,db->isPipe?"via pipe":"");}/* -------------------- Load Word List ---------------------------- *//* CmpWord: qsort compare routine for word list sorting */static int CmpWord(const void *p1, const void *p2){ LabId w1, w2; w1 = * ((LabId *) p1); w2 = * ((LabId *) p2); return strcmp(w1->name, w2->name);}/* LoadWordList: load a word list from file wListFN */void LoadWordList(void){ Source src; int i; char buf[MAXSTRLEN]; Boolean mustSort = FALSE; if(InitSource(wListFN,&src,NoFilter)<SUCCESS) HError(1410,"LoadWordList: Can't open file %s", wListFN); for (nWords=0;ReadString(&src,buf);nWords++) SkipLine(&src); CloseSource(&src); wList = (LabId *)New(&memStak,sizeof(LabId)*nWords); if(InitSource(wListFN,&src,NoFilter)<SUCCESS) HError(1410,"LoadWordList: Can't open file %s", wListFN); for (i=0;i<nWords;i++) { ReadString(&src,buf); wList[i] = GetLabId(buf,TRUE); if (!mustSort && i>0) /* check in sort order */ mustSort = strcmp(wList[i-1]->name,buf) > 0; SkipLine(&src); } CloseSource(&src); if (mustSort) /* word list not in sort order */ qsort(wList, nWords, sizeof(LabId), CmpWord);}/* ------------------- Read/Write Dictionary Entries ------------------ *//* UCase: convert id to upper case and return new id */LabId UCase(LabId id){ static char s[255]; int len,i; strcpy(s,id->name); len = strlen(s); for (i=0;i<len;i++) s[i] = toupper(s[i]); return(GetLabId(s,TRUE));}/* LCase: convert id to lower case and return new id */LabId LCase(LabId id){ static char s[255]; int len,i; strcpy(s,id->name); len = strlen(s); for (i=0;i<len;i++) s[i] = tolower(s[i]); return(GetLabId(s,TRUE));}/* IsCommentChar: Test whether c is in commentChars */Boolean IsCommentChar(int c){ char *s; for (s = commentChars; *s != '\0'; s++) if (c == *s) return TRUE; return FALSE;}/* SetCase: of given string in s */static void SetCase(EdOp cmd, char *s){ int len,i; len = strlen(s); for (i=0;i<len;i++) switch(cmd) { case UCWORD: s[i] = toupper(s[i]); break; case LCWORD: s[i] = tolower(s[i]); break; default: HError(1450, "SetCase: invalid case op %d",cmd); }}/* ReadNextWord: read next word, return FALSE if EOF */Boolean ReadNextWord(DBuffer *db){ static LabId labels[MAXPHONES+3]; char buf[MAXSTRLEN]; char *ptr; float prob=1.0,p=-1.0,v; int ch,i,n,len; db->nextWord=NULL; while ((ch = GetCh(&db->src)) != EOF) { if (!IsCommentChar(ch)) break; SkipLine(&db->src); } if (ch == EOF) return FALSE; UnGetCh(ch,&db->src); if (db->rawMode) { if(!ReadRawString(&db->src,buf)) return FALSE; if (db->wop != NOCMD) /* make any case changes before merging */ SetCase(db->wop,buf); labels[0]=GetLabId(buf,TRUE); labels[1]=NULL;n=0; SkipWhiteSpace(&db->src); while (!db->src.wasNewline) { if (!ReadRawString(&db->src,buf)) HError(1451,"ReadNextWord: Phone or outsym expected in word %s", labels[0]->name); len = strlen(buf); if (buf[0] == '[' && buf[len-1] == ']') { /* outsym */ if (labels[1]!=NULL) HError(1451,"ReadNextWord: Only single outsym allowed for word %s", labels[0]->name); buf[len-1] = '\0'; labels[1] = GetLabId(buf+1,TRUE); } else { if (n==0 && p<0) v=strtod(buf,&ptr); else v=0.0,ptr=buf; if (ptr!=buf) { if (v<=0.0 || v>1.0 || *ptr!=0) HError(8050,"ReadDict: Probability malformed %s",buf); prob=p=v; } else { if (n==MAXPHONES) HError(1451,"ReadNextWord: Too many phones in word %s", labels[0]->name); labels[2+n++] = GetLabId(buf,TRUE); } } SkipWhiteSpace(&db->src); } labels[n+2] = NULL; } else { if(ReadDictWord(&db->src,labels,&prob,&n)<SUCCESS) HError(1413, "ReadNextWord: ReadDictWord failed"); if (n<0) return FALSE; if (db->wop != NOCMD) { switch(db->wop) { case UCWORD: labels[0] = UCase(labels[0]); break; case LCWORD: labels[0] = LCase(labels[0]); break; default: HError(1450, "ReadNextWord: invalid case op %d",db->wop); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -