1 | #include <stdio.h> |
---|
2 | #include <alloc.h> |
---|
3 | #include "corlist.h" |
---|
4 | |
---|
5 | #define min(x,y) ((x<y)?(x):(y)) |
---|
6 | |
---|
7 | |
---|
8 | Weight CorList::GetValue(char X[100], char Y[100], Weight (*H2)[100], int i, int j) |
---|
9 | { |
---|
10 | Weight R = 9999*PREC; // (+nieskonczonosc) |
---|
11 | int n; |
---|
12 | |
---|
13 | for (n=0; n<total; n++) |
---|
14 | { |
---|
15 | int la = List[n].la; |
---|
16 | int lb = List[n].lb; |
---|
17 | if (la<=i+1 && lb<=j+1) |
---|
18 | if (strncmp(List[n].a,X+i+1-la,la)==0 && strncmp(List[n].b,Y+j+1-lb,lb)==0) |
---|
19 | R = min(R,H2[i-la][j-lb]+List[n].w); |
---|
20 | if (la<=j+1 && lb<=i+1) |
---|
21 | if (strncmp(List[n].b,X+i+1-lb,lb)==0 && strncmp(List[n].a,Y+j+1-la,la)==0) |
---|
22 | R = min(R,H2[i-lb][j-la]+List[n].w); |
---|
23 | } |
---|
24 | return R; |
---|
25 | } |
---|
26 | |
---|
27 | |
---|
28 | int CorList::loadCWL(char *Name) |
---|
29 | { |
---|
30 | FILE *f = fopen(Name,"r"); |
---|
31 | int len=MAX_LEN*2+100; |
---|
32 | char a[100],b[100], buf[len+1]; |
---|
33 | float wtmp; |
---|
34 | CorWeight w; |
---|
35 | |
---|
36 | cor_stdcor = 1 * PREC; |
---|
37 | cor_xchg = 1 * PREC; |
---|
38 | |
---|
39 | List = (CorWeight*)malloc(sizeof(CorWeight)); // 100 BO NIE DZIALA REALLOC |
---|
40 | total=0; |
---|
41 | |
---|
42 | if (!f) { fprintf(stderr,"\nCan't open correction weight list file!\n"); return -1; } |
---|
43 | while (!feof(f) && fgets(buf,len,f)) |
---|
44 | { |
---|
45 | if (buf[0]=='%') |
---|
46 | { |
---|
47 | sscanf(buf+1,"%s %f",&a,&wtmp); |
---|
48 | int ok=0; |
---|
49 | if (strcmp(a,"stdcor")==0) { ok=1; cor_stdcor=Weight(wtmp*PREC); /*printf("Standard letter correction set to: %1.2f\n",wtmp);*/ } |
---|
50 | if (strcmp(a,"xchg")==0) { ok=1; cor_xchg=Weight(wtmp*PREC); /*printf("Inverted letters correction set to: %1.2f\n",wtmp);*/ } |
---|
51 | if (!ok) { fprintf(stderr,"Error in file %s: Unknown keyword: '%s'.\n",Name,a); return -1; } |
---|
52 | } |
---|
53 | else |
---|
54 | { |
---|
55 | sscanf(buf,"%s %s %f",&a,&b,&wtmp); |
---|
56 | w.w=(Weight)(wtmp*PREC); |
---|
57 | w.la=strlen(a); w.lb=strlen(b); |
---|
58 | if (w.la>MAX_LEN) { printf("ERROR in file %s: the string '%s' exceeds maximum length of %d characters.\n",Name,a,MAX_LEN); fclose(f); return -1; } |
---|
59 | if (w.lb>MAX_LEN) { printf("ERROR in file %s: the string '%s' exceeds maximum length of %d characters.\n",Name,b,MAX_LEN); fclose(f); return -1; } |
---|
60 | strcpy(w.a,a), strcpy(w.b,b); |
---|
61 | total++; |
---|
62 | List = (CorWeight*)realloc(List,total*sizeof(CorWeight)); |
---|
63 | List[total-1]=w; |
---|
64 | // printf("%s\t<->\t%s\t%1.2f\n",w.a,w.b,((float)w.w/PREC)); |
---|
65 | } |
---|
66 | } |
---|
67 | fclose(f); |
---|
68 | // printf("Total: %d\n\n",total); |
---|
69 | return(total); |
---|
70 | } |
---|