dico.c 3.38 KB
Newer Older
Alexis Nasr's avatar
Alexis Nasr committed
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"dico.h"
#include"util.h"


dico *dico_new(char *name, int size)
{
  dico *d = (dico *)memalloc(sizeof(dico));
  if(name)
    d->name = strdup(name);
  else
    d->name =NULL;
  d->htable = hash_new(size);
  d->array = NULL;
  d->nbelem = 0;
  d->array_size = 0;
  return d;
}

void dico_free(dico *d)
{
  if(d){
    if(d->htable) hash_free(d->htable);
    if(d->array) free(d->array);
    if(d->name) free(d->name);
    free(d);
  }
}

dico *dico_read_fh(FILE *f, float ratio)
{
  dico *d = NULL;
  char buffer[1000];
  int size;
  char name[1000];
  fscanf(f, "%s\n", name);
  fscanf(f, "%d\n", &size);

  /* printf("name = %s\n", name); */
  /* printf("size = %d\n", size); */


  d = dico_new(name, 1/ratio * size);
  while(fgets(buffer, 1000, f)){
    if(feof(f)) 
      break;
    buffer[strlen(buffer) - 1] = '\0';
    /* printf("%s buffer = %s\n", name, buffer);  */
    if(!strcmp(buffer, DICO_END_STR))
    break; 
    dico_add(d, buffer);
  }
  return d;
}

dico *dico_read(char *filename, float ratio)
{
  dico *d = NULL;
  FILE  *f = NULL;
  if(filename == NULL){
    f = stdout;
  }
  else{
    f= fopen(filename, "r");
    if(f == NULL){
      fprintf(stderr, "cannot open file %s\n", filename);
      exit(1);
    }
  }
  d = dico_read_fh(f, ratio);
  fclose(f);
  return d;
}


void dico_print_fh(FILE *f, dico *d)
{
  int i;
  
  fprintf(f,"%s\n", d->name);
  fprintf(f,"%d\n", d->nbelem);
  for(i=0; i<d->nbelem; i++)
    fprintf(f,"%s\n", d->array[i]);

}

void dico_print(char *filename, dico *d)
{
  FILE  *f;
92
  if(filename == NULL)
Alexis Nasr's avatar
Alexis Nasr committed
93
94
95
96
97
98
99
100
101
    f = stdout;
  else{
    f= fopen(filename, "w");
    if(f == NULL){
      fprintf(stderr, "cannot open file %s\n", filename);
      exit(1);
    }
  }
  dico_print_fh(f, d);
102
103
104
  
  if(filename != NULL)
    fclose(f);
Alexis Nasr's avatar
Alexis Nasr committed
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
}

int dico_add(dico *d, char *key)
{
  int val = d->nbelem;
  char *key_copy;
  cell *c;
  
  if((c = hash_lookup(d->htable, key)) != NULL){
    /* printf("already here nbelem  = %d\n", d->nbelem); */
    return c->val;
  }

  key_copy = strdup(key);
  d->nbelem++;
  /* printf("adding it nbelem  = %d\n", d->nbelem); */
  hash_add(d->htable, key_copy, val);
  if(d->nbelem > d->array_size){

    d->array_size = 2 * (d->array_size +1);
    d->array = (char **)realloc(d->array, d->array_size * sizeof(char*));
  }
  d->array[val] =  key_copy;
  return val;
}

char *dico_int2string(dico *d, int val)
{
  if(val < 0) return NULL;
  if(val >= d->array_size) return NULL;
  return d->array[val];
}

int dico_string2int(dico *d, char *string)
{
140
141
142
  cell *c;

  c= hash_lookup(d->htable, string);
Alexis Nasr's avatar
Alexis Nasr committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  if(c)
    return c->val;
  else
    return -1;
}

dico *dico_extract_from_corpus(char *filename, int column, char *dico_name)
{
  dico *d = dico_new(dico_name, 1000);
  FILE *f = myfopen(filename, "r");
  char buffer[10000];
  char *token;   
  int column_nb = 0;

  if(feof(f)) return NULL; /* no more words to read */

  while(fgets(buffer, 10000, f)){
    if(feof(f)) return NULL; /* no more words to read */
    if((buffer[0] == '\n') || (buffer[0] == ' ')) continue;
Alexis Nasr's avatar
Alexis Nasr committed
162
163
    if(buffer[strlen(buffer) - 1] == '\n')
      buffer[strlen(buffer) - 1] = '\0';
Alexis Nasr's avatar
Alexis Nasr committed
164
165
166
167
    token = strtok(buffer, "\t"); /* get index */
    column_nb = 0;
    do{
      if(column_nb == column){
168
	/* printf("token = %s\n", token); */
Alexis Nasr's avatar
Alexis Nasr committed
169
170
171
172
173
174
175
176
	dico_add(d, token);
      }
      column_nb++;
    }while((token = strtok(NULL , "\t")));
  }
  fclose(f);
  return d;
}