📄 hled.c
字号:
/* ----------------------------------------------------------- *//* *//* ___ *//* |_| | |_/ SPEECH *//* | | | | \ RECOGNITION *//* ========= SOFTWARE */ /* *//* *//* ----------------------------------------------------------- *//* Copyright: Microsoft Corporation *//* 1995-2000 Redmond, Washington USA *//* http://www.microsoft.com *//* *//* Use of this software is governed by a License Agreement *//* ** See the file License for the Conditions of Use ** *//* ** This banner notice must not be removed ** *//* *//* ----------------------------------------------------------- *//* File: HLEd.c: Edit label file(s) *//* ----------------------------------------------------------- */char *hled_version = "!HVER!HLEd: 3.3 [CUED 28/04/05]";char *hled_vc_id = "$Id: HLEd.c,v 1.2 2005/05/12 15:51:28 jal58 Exp $";#include "HShell.h"#include "HMem.h"#include "HMath.h"#include "HWave.h"#include "HLabel.h"#include "HDict.h"/* This program reads in a list of 'editing' commands from a script file and then makes an edited copy of one or more label files. Each edit command in the script file must be on a separate line. The commands supported are printed by the Summary routine. Some commands can be 1 or 2 letters. The 1 letter form is retained from the original for compatibility.*//* -------------------------- Trace Flags & Vars ------------------------ */#define T_TOP 0001 /* basic progress reporting */#define T_RDSC 0002 /* output edit script details */#define T_CMDX 0004 /* trace general command operation */#define T_CHOP 0010 /* trace Change operation */#define T_SLEV 0020 /* trace level split/merge operation */#define T_DLEV 0040 /* trace delete level operation */#define T_EDIN 0100 /* trace edit file input */#define T_MEM 0200 /* trace memory usage */#define T_DIC 0400 /* trace dictionary substitution */static int trace = 0; /* trace level */static ConfParam *cParm[MAXGLOBS]; /* configuration parameters */static int nParm = 0; /* total num params *//* -------------------------- Global Variables etc ---------------------- */#define MAXARGS 50 /* in any single HLEd command */#define MAXIW 10 /* max number of interword boundaries */#define MAXIV 10 /* max number of non-interword boundaries */static char * newDir = NULL; /* dest label file directory */static char * newExt = "lab"; /* dest label file extension */typedef enum { NOCMD=0, REPLACE, CHANGE, FIND, MERGE, EDOP_DELETE, DEFCON, TRIST, SBDEF, EXPAND, IFILL, SORT, WBDEF, VBDEF, LCTXT, RCTXT, TCTXT, SETLEV, DELLEV, SPLLEV, ISIL, LASTCMD} EdOp;static int nCmds = LASTCMD-1;static char *cmdmap[] = {"","RE","CH","FI","ME","DE","DC","IT", "SB","EX","FG","SO","WB","NB","LC", "RC","TC","ML","DL","SP","IS","" };static int n1Cmds = 17;static char oldmap[] = " RCFMDXZBEISWVLGTY ";typedef struct { EdOp op; short nArgs; LabId args[MAXARGS];}EditCmd;typedef struct _ScriptItem{ EditCmd cmd; struct _ScriptItem *next;}ScriptItem;static ScriptItem *script; /* linked list of edit commands */static LabId asterix; /* id of an asterix */static LabId sentMarker=NULL; /* sentence bndary marker set by SB cmd */static FILE *newLabs = NULL; /* list of newly created labels */static LabId wbnd[MAXIW]; /* interword bndaries set via WB cmd */static LabId vbnd[MAXIV]; /* non-interwd bndaries set via NB cmd */static int nWB = 0; /* number of interword boundaries */static int nVB = 0; /* number of non-interword boundaries */static Boolean noBounds = FALSE; /* suppress boundary times if TRUE */static Boolean triStrip = FALSE; /* ignore triphone contexts in matching */static Boolean levSplit = FALSE; /* Split levels into lists */static Boolean sortFirst = FALSE; /* Sort on loading */static FileFormat ifmt=UNDEFF; /* Label input file format */static FileFormat ofmt=UNDEFF; /* Label output file format */static HTime minGap = 50000.0; /* minimum interword gap */static char *dictFn = NULL; /* Dictionary used for EX command */static Vocab vocab; /* And the associated vocab */static MemHeap tempHeap; /* Storage for current file */static MemHeap permHeap; /* Permanent storage *//* ------------------ Process Command Line ------------------------- *//* Summary: print a summary of all HLEd commands */void Summary(void){ printf("\nHLEd Command Summary\n\n"); printf("CH/C X A Y B - replace Y in context of A_B by X\n"); printf("DC/X A B C ... - define context A as labels B,C,...\n"); printf("DE/D A B ... - delete labels A,B,...\n"); printf("DL [N] - delete all current level [or level N]\n"); printf("EX/E - expand labels from dictionary or form A_B_C\n"); printf("FG/I X - fill interlabel gaps with label X\n"); printf("FI/F A Y B - find all occurrences of pattern AYB\n"); printf("IS A B - insert A at start and B at end\n"); printf("IT/Z - ignore triphone contexts in CH/FI cmds\n"); printf("LC/L [X] - convert phonemes to Left-context dependent\n"); printf("ML N - move to level N (1-99)\n"); printf("ME/M X A B ... - merge label seq A,B,.. and rename X\n"); printf("NB/V X - label X is not an interword boundary\n"); printf("RC/G [X] - convert phonemes to right-context dependent\n"); printf("RE/R X A B ... - replace labels A,B,... by X\n"); printf("SB/B X - define X as a sentence boundary marker\n"); printf("SO/S - sort labels into time order\n"); printf("SP - split multiple levels into multiple lists\n"); printf("TC/T [X [Y]] - convert phonemes to Triphones\n"); printf("WB/W X - define X as an interword boundary\n\n"); Exit(0);}void ReportUsage(void){ printf("\nUSAGE: HLEd [options] edCmdFile labFiles...\n\n"); printf(" Option Default\n\n"); printf(" -b suppress boundary times off\n"); printf(" -d s load dictionary from s off\n"); printf(" -i s Output transcriptions to MLF s off\n"); printf(" -g f Set min gap for FG command to f 50000.0\n"); printf(" -l s Dir to store output label file(s) current\n"); printf(" -m Strip to monophones on loading off\n"); printf(" -n f Output list of all new labs to f off\n"); PrintStdOpts("GIPQX"); printf("\n\n");}int main(int argc, char *argv[]){ char * labFn, *scriptFn, *newFn=NULL, *s; int fidx; MLFEntry *me; void Initialise(void); void EditFile(char *labfn); void ReadScript(char *scriptFn); void ListFinds(void); if(InitShell(argc,argv,hled_version,hled_vc_id)<SUCCESS) HError(1200,"HLEd: InitShell failed"); InitMem(); InitMath(); InitWave(); InitLabel(); InitDict(); if (!InfoPrinted() && NumArgs() == 0) ReportUsage(); if (NumArgs() == 0) Exit(0); Initialise(); while (NextArg() == SWITCHARG) { s = GetSwtArg(); if (strlen(s)!=1) HError(1219,"HLEd: Bad switch %s; must be single letter",s); switch(s[0]){ case 'b': noBounds = TRUE; break; case 'd': if (NextArg()!=STRINGARG) HError(1219,"HLEd: Dictionary filename expected"); dictFn = GetStrArg(); break; case 'g': if (NextArg() != FLOATARG) HError(1219,"HLEd: Min fill gap expected (100ns units)"); minGap = GetChkedFlt(0.0,100000000.0,s); break; case 'i': if (NextArg()!=STRINGARG) HError(1219,"HLEd: Output MLF file name expected"); if(SaveToMasterfile(GetStrArg())<SUCCESS) HError(1214,"HCopy: Cannot write to MLF"); break; case 'l': if (NextArg()!=STRINGARG) HError(1219,"HLEd: Output label file directory expected"); newDir = GetStrArg(); break; case 'm': LTriStrip(TRUE); break; case 'n': if (NextArg() != STRINGARG) HError(1219,"HLEd: New labels file name expected"); newFn = GetStrArg(); break; case 'G': if (NextArg() != STRINGARG) HError(1219,"HLEd: Input label file format expected"); if((ifmt = Str2Format(GetStrArg())) == ALIEN) HError(-1289,"HLEd: Warning ALIEN input label file format set"); break; case 'I': if (NextArg() != STRINGARG) HError(1219,"HLEd: Input MLF file name expected"); LoadMasterFile(GetStrArg()); break; case 'P': if (NextArg() != STRINGARG) HError(1219,"HLEd: Output label file format expected"); if((ofmt = Str2Format(GetStrArg())) == ALIEN) HError(-1289,"HLEd: Warning ALIEN Label output file format set"); break; case 'Q': Summary(); break; case 'T': trace = GetChkedInt(0,0777,s); break; case 'X': if (NextArg()!=STRINGARG) HError(1219,"HLEd: Output label file extension expected"); newExt = GetStrArg(); break; default: HError(1219,"HLEd: Unknown switch %s",s); } } if (NextArg()!=STRINGARG) HError(1219,"HLEd: Edit script file name expected"); scriptFn = GetStrArg(); ReadScript(scriptFn); if (newFn!=NULL) if ((newLabs = fopen(newFn,"w")) == NULL) HError(1211,"HLEd: Cannot create new label file %s",newFn); if (dictFn!=NULL) { InitVocab(&vocab); if(ReadDict(dictFn,&vocab)<SUCCESS) HError(1213,"HLEd: ReadDict failed"); } while (NumArgs()>0){ if (NextArg()!=STRINGARG) HError(1219,"HLEd: Input label file name expected"); labFn = GetStrArg(); if (IsMLFFile(labFn)){ fidx = NumMLFFiles(); if ((me=GetMLFTable()) != NULL) { while(me->next != NULL) me=me->next; LoadMasterFile(labFn); me=me->next; }else{ LoadMasterFile(labFn); me=GetMLFTable(); } while (me != NULL) { if (me->type == MLF_IMMEDIATE && me->def.immed.fidx == fidx) EditFile(me->pattern); me = me->next; } } else EditFile(labFn); } ListFinds(); if (newLabs != NULL) fclose(newLabs); Exit(0); return (0); /* never reached -- make compiler happy */}/* --------------------- Initialisation ----------------------- *//* SetConfParms: set conf parms relevant to HLEd */void SetConfParms(void){ int i; nParm = GetConfig("HLED", TRUE, cParm, MAXGLOBS); if (nParm>0) { if (GetConfInt(cParm,nParm,"TRACE",&i)) trace = i; }}/* Initialise: confparms, str->int map and memory */void Initialise(void){ int i; char buf[MAXSTRLEN]; LabId labid; SetConfParms(); asterix = GetLabId("*",TRUE); for (i=1;i<=99;i++) { sprintf(buf,"%d",i); labid=GetLabId(buf,TRUE); labid->aux=(void*) i; } CreateHeap(&permHeap, "permHeap", MSTAK, 1, 1.2, 512, 4096); CreateHeap(&tempHeap, "tempHeap", MSTAK, 1, 1.2, 4096, 8192);}/* ------------------- Context List Handling ------------------ */typedef struct _Context{ short nDefs; LabId cname; LabId cdef[MAXARGS]; struct _Context *next;} Context;static Context *xlist = NULL;/* DefineContext: add given context to list */void DefineContext(LabId name, LabId *def, int nDefs){ Context *p; p = (Context *)New(&permHeap,sizeof(Context)); p->next = xlist; xlist = p; p->cname = name; p->nDefs = nDefs; memcpy(p->cdef,def,nDefs*sizeof(LabId)); p->cdef[nDefs] = NULL;}/* PrintContexts: print all contexts */void PrintContexts(void){ Context *p = xlist; void PrintIdList(LabId *i); if (p == NULL){ printf("No Contexts Defined\n"); return; } printf("Contexts Defined:\n"); while (p != NULL) { printf(" Context %s == ",p->cname->name); PrintIdList(p->cdef); printf("\n"); p = p->next; }} /* GetContext: return list of ids defined by name */LabId *GetContext(LabId name){ Context *p = xlist; while (p != NULL) { if (p->cname == name) return p->cdef; p = p->next; } HError(1230,"GetContext: Context %s not defined by DC command",name->name); return NULL;}/* IsWBnd: return true if given id is an Interword boundary */Boolean IsWBnd(LabId id){ int i; for (i=0; i<nWB; i++) if (wbnd[i] == id) return TRUE; return FALSE;}/* PrintWBounds: print a list of defined interword boundaries */void PrintWBounds(void){ int i; if (nWB==0) printf("No inter-word boundaries defined\n"); else { printf("Word Bounds Defined:\n"); for (i=0;i<nWB;i++) printf(" %s",wbnd[i]->name); printf("\n"); }}/* IsVBnd: return true if given id is a non-interword boundary */Boolean IsVBnd(LabId id){ int i; for (i=0; i<nVB; i++) if (vbnd[i] == id) return TRUE; return FALSE;}/* PrintVBounds: print a list of defined non-interword boundaries */void PrintVBounds(void){ int i; if (nVB==0) printf("No non inter-word boundaries defined\n"); else { printf("Non Inter-Word Boundaries Defined:\n"); for (i=0;i<nVB;i++) printf(" %s",vbnd[i]->name); printf("\n"); }}/* ------------------- New Label Recording -------------------- *//* PutLab: if given label new then output it to newLabs */void PutLab(LabId id){ if (id->aux == NULL) { /* not seen this label before */ id->aux = (void*) -1; fprintf(newLabs,"%s\n",id->name); }}/* ------------------- Find Accumulators ---------------------- */typedef struct _FindAcc{ LabId *pattern; /* pattern to find */ int acc; /* num of matches */ struct _FindAcc *next;} FindAcc;static FindAcc *facc = NULL;/* GetFindAccumulator: return a pointer to acc for given pattern */int *GetFindAccumulator(LabId *pat){ FindAcc *p = facc; while (p != NULL) { if (p->pattern == pat) return &(p->acc); p = p->next; } HError(1292,"GetFindAccumulator: Acc %s %s %s not defined", pat[0]->name,pat[1]->name,pat[2]->name); return NULL;}/* MakeFindAccumulator: create a new find acc for given pattern */void MakeFindAccumulator(LabId *pat){ FindAcc *p; p = (FindAcc *)New(&permHeap,sizeof(FindAcc)); p->next = facc; facc = p; p->pattern = pat; p->acc = 0;}/* ListFinds: list accumulated totals in all find accs */void ListFinds(void){ FindAcc *p = facc; LabId *i; if (p != NULL) { printf("Find Totals:\n"); while (p != NULL){ i = p->pattern; printf(" %4d %s-%s+%s\n",p->acc, i[0]->name,i[1]->name,i[2]->name); p = p->next; } }}/* ---------------------- Print Script ---------------------- *//* PrintIdList: print list of ids */void PrintIdList(LabId *i){ while (*i != NULL) { printf(" %s",(*i)->name); ++i; }}/* PrintScript: prints the currently loaded script - for tracing only */void PrintScript(char *scriptFN){ ScriptItem *i; int j; LabId src; printf("HLEd: script %s\n",scriptFN);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -