Exemplo n.º 1
0
void 
write_grammar(FILE *fp, grammar g, si_t si) 
{
  sihashbrsit	bhit;
  sihashursit	uhit;
  size_t	i;

  for (bhit=sihashbrsit_init(g.brs); sihashbrsit_ok(bhit); bhit=sihashbrsit_next(bhit)) 
    for (i=0; i<bhit.value.n; i++) 
      fprintf(fp, "%g	%s " REWRITES " %s %s\n", (double) bhit.value.e[i]->prob, 
	      si_index_string(si, bhit.value.e[i]->parent),
	      si_index_string(si, bhit.value.e[i]->left),
	      si_index_string(si, bhit.value.e[i]->right));

  for (uhit=sihashursit_init(g.urs); sihashursit_ok(uhit); uhit=sihashursit_next(uhit)) 
    for (i=0; i<uhit.value.n; i++) 
      fprintf(fp, "%g	%s " REWRITES " %s\n", (double) uhit.value.e[i]->prob, 
	      si_index_string(si, uhit.value.e[i]->parent),
	      si_index_string(si, uhit.value.e[i]->child));
}
Exemplo n.º 2
0
Arquivo: lncky.c Projeto: mjpost/cky
static void
apply_unary(sihashcc chart_entry, grammar g, int right_pos, 
	    sihashcc left_vertex)
{
  sihashursit	ursit;
  size_t	i;

  for (ursit=sihashursit_init(g.parent_urs); sihashursit_ok(ursit); 
       ursit = sihashursit_next(ursit)) {
    /* look up the rule's child category */
    chart_cell c = sihashcc_ref(chart_entry, ursit.key);	
    
    if (c)			/* such categories exist in this cell */
      for (i=0; i<ursit.value.n; i++) {
	chart_cell cc = add_edge(chart_entry, ursit.value.e[i]->parent, 
				 &c->tree, NULL, 
				 c->prob * ursit.value.e[i]->prob,
				 right_pos, left_vertex);
	if (cc)
	  follow_unary(cc, chart_entry, g, right_pos, left_vertex);
      }}}
Exemplo n.º 3
0
grammar
read_grammar(FILE *fp, si_t si) 
{
  sihashbrs left_brules_ht = make_sihashbrs(NLABELS);
  sihashurs child_urules_ht = make_sihashurs(NLABELS);
  sihashf parent_weight_ht = make_sihashf(NLABELS);
  brihashbr brihtbr = make_brihashbr(NLABELS);
  int n;
  double weight;
  urule ur;
  sihashbrsit bhit;
  sihashursit uhit;
  size_t  root_label = 0, lhs, cat, rhs[MAXRHS];

  while ((n = fscanf(fp, " %lg ", &weight)) == 1) {	/* read the count */
    lhs = read_cat(fp, si);
    assert(weight > 0);
    assert(lhs);
    if (!root_label)
      root_label = lhs;
    
    fscanf(fp, " " REWRITES);				/* read the rewrites symbol */

    for (n=0; n<MAXRHS; n++) {				/* read the rhs, n is length of rhs */
      cat = read_cat(fp, si);
      if (!cat)
	break;
      rhs[n] = cat;
    }

    if (n >= MAXRHS) {
      fprintf(stderr, "read_grammar() in grammar.c: rule rhs too long\n");
      exit(EXIT_FAILURE);
    }

    switch (n) {
    case 0: 
      fprintf(stderr, "read_grammar() in grammar.c: rule with empty rhs\n");
      exit(EXIT_FAILURE);
      break;
    case 1: 
      ur = make_urule(weight, lhs, rhs[0]);
      push_urule(child_urules_ht, ur->child, ur);
      sihashf_inc(parent_weight_ht, ur->parent, weight);
      break;
    case 2:
      add_brule(left_brules_ht, brihtbr, weight, lhs, rhs[0], rhs[1]);
      sihashf_inc(parent_weight_ht, lhs, weight);
      break;
    default: 
      { int start, i, j;
        char bcat[MAXBLABELLEN], *s;
	si_index bparent, left, right;

	right = rhs[n-1];		/* rightmost category */
	for (start=n-2; start>=1; start--) {
	  
	  i = 0;			/* i is index into bcat[] */
	  for (j=start; j<n; j++) {     /* j is index into rhs[] */
	    if (j!=start) {
	      bcat[i++] = BINSEP;
	      assert(i < MAXBLABELLEN);
	    }
	    
	    s = si_index_string(si, rhs[j]);
	    while (*s) {
	      bcat[i++] = *s++;
	      assert(i < MAXBLABELLEN);
	  }}

	  bcat[i] = '\0';
	  bparent = si_string_index(si, bcat);
	  left = rhs[start];
	  add_brule(left_brules_ht, brihtbr, weight, bparent, left, right);
	  sihashf_inc(parent_weight_ht, bparent, weight);
	  right = bparent;
	}
	
	add_brule(left_brules_ht, brihtbr, weight, lhs, rhs[0], right);
	sihashf_inc(parent_weight_ht, lhs, weight);
      }}}
  
  free_brihashbr(brihtbr);	/* free brindex hash table */

  { 
    int i; /* normalize grammar rules */

    for (bhit = sihashbrsit_init(left_brules_ht); sihashbrsit_ok(bhit); bhit = sihashbrsit_next(bhit))
      for (i=0; i<bhit.value.n; i++) 
	bhit.value.e[i]->prob /= sihashf_ref(parent_weight_ht, bhit.value.e[i]->parent);

    for (uhit = sihashursit_init(child_urules_ht); sihashursit_ok(uhit); uhit = sihashursit_next(uhit))
      for (i=0; i<uhit.value.n; i++) 
	uhit.value.e[i]->prob /= sihashf_ref(parent_weight_ht, uhit.value.e[i]->parent);
  }
  
  free_sihashf(parent_weight_ht);
 
  {
    grammar g;
    g.urs = child_urules_ht;
    g.brs = left_brules_ht;
    g.root_label = root_label;
    return g;
  }
}