Blame view

tools/sctk-2.4.10/src/sclite/cores.c 4.31 KB
8dcb6dfcb   Yannick Estève   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
  
  #include "sctk.h"
  
  void print_CORES(void *p){
    int i;
    CORES *cs = (CORES *)p;
  
    printf("
      Correspondence Set:
  ");
    for (i=0; i<cs->nword; i++){
      printf((i == 0) ? "        Words: " : "               ");
      print_WORD_wt((void *)(cs->words[i]));
    }
    printf("        Max_conf= %f  min_conf=%f  avg_conf=%f  avg_time=%f  avg_dur=%f  bias_nword= %f
  ",
  	 cs->max_conf, cs->min_conf, cs->avg_conf, cs->avg_time, cs->avg_dur, cs->bias_nword);
  }
  
  int equal_CORES(void *p1, void *p2){
    return 0;
  }
  
  void *get_CORES(void){
    CORES *tcs;
    alloc_singarr(tcs,1,CORES);
    tcs->use = 1;
    tcs->sum_conf = 0.0;
    tcs->max_conf = -1.0;
    tcs->min_conf = -1.0;
    tcs->avg_conf = -1.0;
    tcs->avg_time = -1.0;
    tcs->avg_dur = -1.0;
    tcs->nword = 0;
    tcs->bias_nword = 0.0;
    return((void *)tcs);
  }
  
  void release_CORES(void *p){
    int i;
    CORES *cs = (CORES *)p;
    
    cs->use --;
    if (cs->use > 0)    return;
    for (i=0; i<cs->nword; i++)
      release_WORD((void *)cs->words[i]);
    free_singarr(cs, CORES);  
  }
  
  int null_alt_CORES(void *p){
    return 0;
  }
  
  int opt_del_CORES (void *p){
    return 0;
  }
  
  void *copy_CORES(void *p){
    int i;
    CORES *tcs, *cs = (CORES *)p;
  
    tcs = get_CORES();
    for (i=0; i<cs->nword; i++)
      tcs->words[i] = (WORD *)copy_WORD((void *)cs->words[i]);
    tcs->max_conf = cs->max_conf;
    tcs->min_conf = cs->min_conf;
    tcs->avg_conf = cs->avg_conf;
    tcs->bias_nword = cs->bias_nword;
    tcs->nword = cs->nword;
    return(tcs);
  }
  
  void *make_empty_CORES(void *p){
    CORES *tcs;
  
    tcs = get_CORES();
    return(tcs);
  }
  
  int use_count_CORES(void *p, int i){
    ((CORES *)p)->use += i;
    return(((CORES *)p)->use);
  }
  
  /****************************************************************************/
  /****************************************************************************/
  static void add_to_node_list(NODE *node, void *ptr);
  static int lookup_node(NODE **list, int cnt, NODE *node);
  static NODE **node_list;
  static int max_node = 0, num_node = 0;
  NETWORK *Network_WORD_to_CORES(NETWORK *net);
  
  static int lookup_node(NODE **list, int cnt, NODE *node){
    int nn;
  
    for (nn=0; nn<cnt; nn++)
      if (list[nn] == node) 
        return nn;
    return(-1);
  }
  
  static void add_to_node_list(NODE *node, void *ptr){
    if (lookup_node(node_list, num_node, node) != -1)
        return;
  
    if (num_node == max_node)
      expand_1dim(node_list,num_node,max_node,2,NODE *, 1);
    node_list[num_node] = node;
    node->flag1 = num_node;
    num_node++;
  }
  
  NETWORK *Network_WORD_to_CORES(NETWORK *net){
      char *proc = "Network_WORD_to_CORES";
      int n;
      NETWORK *csnet;
  
      if (db > 0) printf("Entering: %s
  ",proc);
  
      /* initialize variables */
      num_node = 0;
      max_node = net->node_count + 10;
      alloc_singZ(node_list,max_node,NODE *,(NODE *)0);
      Network_traverse(net,add_to_node_list,0,0,0,0);
  
      if ((csnet = Network_init(print_CORES, equal_CORES, release_CORES,
  			      null_alt_CORES, opt_del_CORES, copy_CORES,
  			      make_empty_CORES,use_count_CORES,
  			      rsprintf("CORES net of %s",net->name))) == 0)
        return((NETWORK *)0);
      
      for (n=0; n<num_node; n++){
        CORES *cs[50];
        int ncs, cur_cs, pre_existing;
        ARC_LIST_ATOM *oa;
  
        ncs = 0;
        if (node_list[n]->out_arcs != NULL){
  	/* build a correspondence set */
  	for (oa = node_list[n]->out_arcs; oa != NULL; oa = oa->next){
  	  /* lood for a pre-existing word in a correspondence set */
  	  pre_existing = 0;
  	  for (cur_cs = 0; cur_cs < ncs; cur_cs++)
  	    if (equal_WORD2(cs[cur_cs]->words[0], oa->arc->data) == 0){
  	      cs[cur_cs]->words[cs[cur_cs]->nword] =
  		(WORD *)copy_WORD(oa->arc->data);
  	      cs[cur_cs]->nword++;
  	      pre_existing = 1;
  	    }
  	  if (!pre_existing){
  	    cs[ncs] = (CORES *)get_CORES();
  	    cs[ncs]->words[cs[ncs]->nword] = (WORD *)copy_WORD(oa->arc->data);
  	    cs[ncs]->nword++;
  	    ncs++;	    
  	  }
  	}      
        }
        if (ncs > 0){
  	int x;
  	/* add the first corresp set to the tail of the network */
  	if (Network_add_arc_to_tail(csnet,(void *)cs[0]) > 0){
  	  fprintf(stderr,"Error: Network_add_arc_to_tail failed in %s
  ",
  		  proc);
  	  return((NETWORK *)0);
  	}
  	for (x = 1; x<ncs ; x++)
  	  /* merge the other corresp set into the same place */
  	  if (NETWORK_insert_arc_between_node(csnet,
  				   csnet->stop_node->in_arcs->arc->from_node,
  					      csnet->stop_node,
  					      cs[x]) > 0)
  	    return((NETWORK*)0);
        }
      }
      return(csnet);
  }