⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlreader.c

📁 General Hidden Markov Model Library 一个通用的隐马尔科夫模型的C代码库
💻 C
📖 第 1 页 / 共 3 页
字号:
      /* parsing emission probabilities */      s = (char *)xmlNodeGetContent(elem);      switch (f->modelType & PTR_TYPE_MASK) {      case (GHMM_kDiscreteHMM):	f->model.d[modelNo]->s[state].desc = desc;	f->model.d[modelNo]->s[state].pi = pi;	f->model.d[modelNo]->s[state].fix = fixed;	if (f->modelType & GHMM_kHigherOrderEmissions) {	  f->model.d[modelNo]->order[state] = order;	  if (f->model.d[modelNo]->maxorder < order) {	    f->model.d[modelNo]->maxorder = order;	    estr = ighmm_mprintf(NULL, 0, "Updated maxorder to %d\n",				 f->model.d[modelNo]->maxorder);	    GHMM_LOG(LDEBUG, estr);	    m_free(estr);	  }	}	ARRAY_MALLOC(emissions, pow(f->model.d[modelNo]->M, order+1));	parseCSVList(s, pow(f->model.d[modelNo]->M, order+1), emissions, rev);	f->model.d[modelNo]->s[state].b = emissions;	break;      case (GHMM_kDiscreteHMM+GHMM_kTransitionClasses):	f->model.ds[modelNo]->s[state].desc = desc;	f->model.ds[modelNo]->s[state].pi = pi;	f->model.ds[modelNo]->s[state].fix = fixed;	if (f->modelType & GHMM_kHigherOrderEmissions)	  f->model.ds[modelNo]->order[state] = order;	ARRAY_MALLOC(emissions, pow(f->model.ds[modelNo]->M, order+1));	parseCSVList(s, pow(f->model.ds[modelNo]->M, order+1), emissions, rev);	f->model.ds[modelNo]->s[state].b = emissions;	break;      default:	GHMM_LOG(LERROR, "invalid modelType");	goto STOP;      }      m_free(s);    }    /* ======== continuous state ========================================== */    if ((!xmlStrcmp(elem->name, BAD_CAST "mixture"))) {      assert(f->modelType & GHMM_kContinuousHMM);      M = 0;      child = elem->children;      while (child != NULL) {        if ((!xmlStrcmp(child->name, BAD_CAST "normal")) ||             (!xmlStrcmp(child->name, BAD_CAST "normalTruncatedLeft")) ||            (!xmlStrcmp(child->name, BAD_CAST "normalTruncatedRight")) ||            (!xmlStrcmp(child->name, BAD_CAST "uniform"))){          M ++;                  }        child = child->next;      }      ghmm_cstate_alloc(f->model.c[modelNo]->s + state, M, inDegree[state], outDegree[state], f->model.c[modelNo]->cos);      f->model.c[modelNo]->s[state].desc = desc;      f->model.c[modelNo]->s[state].M = M;      f->model.c[modelNo]->s[state].pi = pi;      if( f->model.c[modelNo]->M < M)               f->model.c[modelNo]->M = M;             child = elem->children;            i = 0;      while (child != NULL) {          if ((!xmlStrcmp(child->name, BAD_CAST "normal"))) {          f->model.c[modelNo]->s[state].mue[i] = getDoubleAttribute(child, "mean", &error);          f->model.c[modelNo]->s[state].u[i] = getDoubleAttribute(child, "variance", &error);          f->model.c[modelNo]->s[state].density[i] = (ghmm_density_t)normal;          aprox = getIntAttribute(child, "aproximate", &error);          f->model.c[modelNo]->s[state].density[i] = (ghmm_density_t)normal;          fixed = getIntAttribute(child, "fixed", &error);                    if (error)            fixed = 0;          stateFixed = fixed && stateFixed;          f->model.c[modelNo]->s[state].mixture_fix[i] = fixed;          prior = getDoubleAttribute(child, "prior", &error);          if (error)            prior = 1.0;	  f->model.c[modelNo]->s[state].c[i] = prior;          i++;                          }        if ((!xmlStrcmp(child->name, BAD_CAST "normalTruncatedLeft"))) {          f->model.c[modelNo]->s[state].mue[i] = getDoubleAttribute(child, "mean", &error);          f->model.c[modelNo]->s[state].u[i] = getDoubleAttribute(child, "variance", &error);          f->model.c[modelNo]->s[state].a[i] = getDoubleAttribute(child, "min", &error);          f->model.c[modelNo]->s[state].density[i] = (ghmm_density_t)normal_left;          fixed = getIntAttribute(child, "fixed", &error);          if (error)            fixed = 0;          stateFixed = fixed && stateFixed;          f->model.c[modelNo]->s[state].mixture_fix[i] = fixed;          prior = getDoubleAttribute(child, "prior", &error);          if (error)            prior = 1.0;	  f->model.c[modelNo]->s[state].c[i] = prior;          i++;                  }        if ((!xmlStrcmp(child->name, BAD_CAST "normalTruncatedRight"))) {          f->model.c[modelNo]->s[state].mue[i] = getDoubleAttribute(child, "mean", &error);          f->model.c[modelNo]->s[state].u[i] = getDoubleAttribute(child, "variance", &error);          f->model.c[modelNo]->s[state].a[i] = getDoubleAttribute(child, "max", &error);          f->model.c[modelNo]->s[state].density[i] = (ghmm_density_t)normal_right;          fixed = getIntAttribute(child, "fixed", &error);          if (error)            fixed = 0;          stateFixed = fixed && stateFixed;          f->model.c[modelNo]->s[state].mixture_fix[i] = fixed;          prior = getDoubleAttribute(child, "prior", &error);          if (error)            prior = 1.0;	  f->model.c[modelNo]->s[state].c[i] = prior;          i++;          }        if ((!xmlStrcmp(child->name, BAD_CAST "uniform"))) {          f->model.c[modelNo]->s[state].mue[i] = getDoubleAttribute(child, "max", &error);          f->model.c[modelNo]->s[state].u[i] = getDoubleAttribute(child, "min", &error);          f->model.c[modelNo]->s[state].density[i] = (ghmm_density_t)uniform;          fixed = getIntAttribute(child, "fixed", &error);          if (error)            fixed = 0;          stateFixed = fixed && stateFixed;          f->model.c[modelNo]->s[state].mixture_fix[i] = fixed;          prior = getDoubleAttribute(child, "prior", &error);          if (error)            prior = 1.0;	  f->model.c[modelNo]->s[state].c[i] = prior;          i++;              }        child = child->next;               }      f->model.c[modelNo]->s[state].fix = stateFixed;    }    /* ======== pair hmm state ============================================ */    if ((!xmlStrcmp(elem->name, BAD_CAST "pair"))) {    }    /* -------- background name  ------------------------------------------ */    if ((!xmlStrcmp(elem->name, BAD_CAST "backgroundKey"))) {            assert(f->modelType & GHMM_kBackgroundDistributions);      s = (char *)xmlNodeGetContent(elem);      for (i=0; i<f->model.d[modelNo]->bp->n; i++) {	if (0 == strcmp(s, f->model.d[modelNo]->bp->name[i])) {	  if (order != f->model.d[modelNo]->bp->order[i]) {	    estr = ighmm_mprintf(NULL, 0, "order of background %s and state %d"				 " does not match",				 f->model.d[modelNo]->bp->name[i], state);	    GHMM_LOG(LERROR, estr);	    m_free(estr);	    goto STOP;	  } else {	    f->model.d[modelNo]->background_id[state] = i;	    break;	  }	}      }      if (i == f->model.d[modelNo]->bp->n) {	estr = ighmm_mprintf(NULL, 0, "can't find background with name %s in"			     " state %d", s, state);	GHMM_LOG(LERROR, estr);	m_free(estr);	goto STOP;      }      m_free(s);    }    /* -------- tied to --------------------------------------------------- */    if ((!xmlStrcmp(elem->name, BAD_CAST "class"))) {            assert(f->modelType & GHMM_kLabeledStates);      s = (char *)xmlNodeGetContent(elem);      label = atoi(s);      m_free(s);      if ((f->modelType & PTR_TYPE_MASK) == GHMM_kDiscreteHMM) {	if (f->model.d[modelNo]->label_alphabet->size > label)	  f->model.d[modelNo]->label[state] = label;	else	  GHMM_LOG(LWARN, "蟦valid label");      }    }    /* -------- tied to --------------------------------------------------- */    if ((!xmlStrcmp(elem->name, BAD_CAST "tiedTo"))) {      assert(f->modelType & GHMM_kTiedEmissions);      s = (char *)xmlNodeGetContent(elem);      tied = atoi(s);      if (state>=tied) {	f->model.d[modelNo]->tied_to[state] = tied;	if (f->model.d[modelNo]->tied_to[tied] != tied) {	  estr = ighmm_mprintf(NULL, 0, "state %d not tied to tie group leader", state);	  GHMM_LOG(LERROR, estr);	  m_free(estr);	  goto STOP;	}      } else {	estr = ighmm_mprintf(NULL, 0, "state %d tiedTo (%d) is invalid", state, tied);	GHMM_LOG(LERROR, estr);	m_free(estr);	goto STOP;      }      m_free(s);    }    /* -------- position for graphical editing ---------------------------- */    if ((!xmlStrcmp(elem->name, BAD_CAST "position"))) {      curX = getIntAttribute(elem, "x", &error);      if (error)	GHMM_LOG(LWARN, "failed to read x position");      curY = getIntAttribute(elem, "y", &error);      if (error)	GHMM_LOG(LWARN, "failed to read y position");      switch (f->modelType & PTR_TYPE_MASK) {      case GHMM_kDiscreteHMM:        f->model.d[modelNo]->s[state].xPosition = curX;        f->model.d[modelNo]->s[state].yPosition = curY;	break;      case GHMM_kDiscreteHMM+GHMM_kTransitionClasses:        f->model.ds[modelNo]->s[state].xPosition = curX;        f->model.ds[modelNo]->s[state].yPosition = curY;	break;      case GHMM_kDiscreteHMM+GHMM_kPairHMM:      case GHMM_kDiscreteHMM+GHMM_kPairHMM+GHMM_kTransitionClasses:        f->model.dp[modelNo]->s[state].xPosition = curX;        f->model.dp[modelNo]->s[state].yPosition = curY;	break;      case GHMM_kContinuousHMM:      case GHMM_kContinuousHMM+GHMM_kTransitionClasses:        f->model.c[modelNo]->s[state].xPosition = curX;        f->model.c[modelNo]->s[state].yPosition = curY;	break;      default:	GHMM_LOG(LERROR, "invalid modelType");	goto STOP;      }    }    elem = elem->next;  }  return 0;STOP:  m_free(s);  m_free(desc);  m_free(emissions)  return -1;#undef CUR_PROC}/*===========================================================================*/static int parseSingleTransition(xmlDocPtr doc, xmlNodePtr cur, ghmm_xmlfile* f,				 int modelNo) {#define CUR_PROC "parseTransition"  int retval=-1;  int source, target, error;  int in_state, out_state;  double p;  char * s;  xmlNodePtr elem;  assert((f->modelType & GHMM_kTransitionClasses) == 0);  source = getIntAttribute(cur, "source", &error);  target = getIntAttribute(cur, "target", &error);  elem = cur->children;  while (elem!=NULL) {    if ((!xmlStrcmp(elem->name, BAD_CAST "probability"))) {      s = (char *)xmlNodeGetContent(elem);      p = atof(s);      m_free(s)      break;    }    elem = elem->next;  }  switch (f->modelType & PTR_TYPE_MASK) {  case GHMM_kDiscreteHMM:    out_state = f->model.d[modelNo]->s[source].out_states++;    in_state  = f->model.d[modelNo]->s[target].in_states++;    f->model.d[modelNo]->s[source].out_id[out_state] = target;    f->model.d[modelNo]->s[source].out_a[out_state]  = p;    f->model.d[modelNo]->s[target].in_id[in_state]   = source;    f->model.d[modelNo]->s[target].in_a[in_state]    = p;    break;  case (GHMM_kDiscreteHMM+GHMM_kPairHMM):    out_state = f->model.dp[modelNo]->s[source].out_states++;    in_state  = f->model.dp[modelNo]->s[target].in_states++;    f->model.dp[modelNo]->s[source].out_id[out_state]   = target;    f->model.dp[modelNo]->s[source].out_a[out_state][0] = p;    f->model.dp[modelNo]->s[target].in_id[in_state]     = source;    f->model.dp[modelNo]->s[target].in_a[in_state][0]   = p;    break;  case GHMM_kContinuousHMM:    out_state = f->model.c[modelNo]->s[source].out_states++;    in_state  = f->model.c[modelNo]->s[target].in_states++;    f->model.c[modelNo]->s[source].out_id[out_state]   = target;    f->model.c[modelNo]->s[source].out_a[0][out_state] = p;    f->model.c[modelNo]->s[target].in_id[in_state]     = source;    f->model.c[modelNo]->s[target].in_a[0][in_state]   = p;    break;  default:    GHMM_LOG(LERROR, "invalid modelType");    goto STOP;  }    retval = 0;STOP:  return retval;#undef CUR_PROC}/*===========================================================================*/static int parseMultipleTransition(xmlDocPtr doc, xmlNodePtr cur,				   ghmm_xmlfile* f, int modelNo,                                   int nrTransitionClasses) {#define CUR_PROC "parseMultipleTransition"  int i, retval=-1;  int source, target, error;  int in_state, out_state;  double * probs;  char * s;  xmlNodePtr elem;  assert(f->modelType & GHMM_kTransitionClasses);  source = getIntAttribute(cur, "source", &error);  target = getIntAttribute(cur, "target", &error);  elem = cur->children;  while (elem!=NULL) {    if ((!xmlStrcmp(elem->name, BAD_CAST "probability"))) {           s = (char *)xmlNodeGetContent(elem);      ARRAY_MALLOC(probs, nrTransitionClasses);      parseCSVList(s, nrTransitionClasses, probs, 0);      m_free(s);      break;    }    elem = elem->next;  }  switch (f->modelType & PTR_TYPE_MASK) {  case (GHMM_kDiscreteHMM + GHMM_kTransitionClasses):    out_state = f->model.ds[modelNo]->s[source].out_states++;    in_state  = f->model.ds[modelNo]->s[target].in_states++;    f->model.ds[modelNo]->s[source].out_id[out_state] = target;/*     f->model.ds[modelNo]->s[source].out_a[out_state]  = probs; */    f->model.ds[modelNo]->s[target].in_id[in_state]   = source;/*     f->model.ds[modelNo]->s[target].in_a[in_state]    = probs; */    break;  case (GHMM_kDiscreteHMM + GHMM_kPairHMM + GHMM_kTransitionClasses):    out_state = f->model.dp[modelNo]->s[source].out_states++;    in_state  = f->model.dp[modelNo]->s[target].in_states++;    f->model.dp[modelNo]->s[source].out_id[out_state] = target;    f->model.dp[modelNo]->s[source].out_a[out_state]  = probs;    f->model.dp[modelNo]->s[target].in_id[in_state]   = source;    f->model.dp[modelNo]->s[target].in_a[in_state]    = probs;    break;  case (GHMM_kContinuousHMM + GHMM_kTransitionClasses):    out_state = f->model.c[modelNo]->s[source].out_states++;    in_state  = f->model.c[modelNo]->s[target].in_states++;    f->model.c[modelNo]->s[source].out_id[out_state] = target;    f->model.c[modelNo]->s[target].in_id[in_state]   = source;    for (i=0; i<nrTransitionClasses; i++) {      f->model.c[modelNo]->s[source].out_a[i][out_state] = probs[i];      f->model.c[modelNo]->s[target].in_a[i][in_state]   = probs[i];    }    break;  default:    GHMM_LOG(LERROR, "invalid modelType");    goto STOP;  }  retval = 0;STOP:  m_free(probs);  return retval;#undef CUR_PROC}/*===========================================================================*/static int parseHMM(ghmm_xmlfile* f, xmlDocPtr doc, xmlNodePtr cur, int modelNo) {#define CUR_PROC "parseHMM"  char * estr;    xmlNodePtr child;    int i, id, error;  int source, target;  int N = 0;  int nrBackgrounds=0, M=-1;  int * inDegree = NULL;    int * outDegree = NULL;  int cos;  float prior;  int modeltype=0;  char * mt;  unsigned char * modelname;  int * bg_orders = NULL;  double * * bg_ptr = NULL;  ghmm_alphabet * alfa;  ghmm_alphabet * * alphabets=NULL;  int nrAlphabets=0;  child = cur->children;   /*xmlElemDump(stdout, doc, cur);*/  /* parse HMM for counting */  GHMM_LOG(LINFO, "parseHMM to count ");  while (child != NULL) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -