| 1 | /* Program P7B: dumps the field contents of all active records |
|---|
| 2 | of a given data base |
|---|
| 3 | use b7_() |
|---|
| 4 | */ |
|---|
| 5 | #include <stdio.h> |
|---|
| 6 | #include <string.h> |
|---|
| 7 | #include <math.h> |
|---|
| 8 | |
|---|
| 9 | #include "cisis.h" /* CISIS Interface header file */ |
|---|
| 10 | #include "cirun.h" /* CISIS Interface runtime declarations */ |
|---|
| 11 | |
|---|
| 12 | #define TAG20 2 |
|---|
| 13 | #define MAXRELAT 101 |
|---|
| 14 | |
|---|
| 15 | typedef struct termweight { |
|---|
| 16 | unsigned short d; /* numero do termo (dct word) */ |
|---|
| 17 | float w; /* seu peso */ |
|---|
| 18 | } TERMWEIGHT; |
|---|
| 19 | |
|---|
| 20 | #define MAXREGISTROS 10000 |
|---|
| 21 | #define MAXTERMOS 6 |
|---|
| 22 | |
|---|
| 23 | TERMWEIGHT Vetores[MAXREGISTROS+1][MAXTERMOS]; |
|---|
| 24 | |
|---|
| 25 | void main(int argc, char *argv[]) |
|---|
| 26 | { |
|---|
| 27 | |
|---|
| 28 | RECSTRU *recp; /* mandatory for defines REC,MFR,DIR,FIELDP */ |
|---|
| 29 | LONGX irec; |
|---|
| 30 | |
|---|
| 31 | char *dbnp; |
|---|
| 32 | |
|---|
| 33 | LONGX mfn,mfn1,mfn2,maxmfn=0; |
|---|
| 34 | int diridx,dirloop; |
|---|
| 35 | |
|---|
| 36 | char *p; |
|---|
| 37 | |
|---|
| 38 | char areaval[80+1]; |
|---|
| 39 | unsigned short i,j,g1; |
|---|
| 40 | |
|---|
| 41 | float W, DW; |
|---|
| 42 | float somap,somaq2,somad2,denom,sim; |
|---|
| 43 | |
|---|
| 44 | float TABSIM[MAXRELAT]; |
|---|
| 45 | LONGX TABSIMMFN[MAXRELAT]; |
|---|
| 46 | int idx1,idx2,idx3,idx4; |
|---|
| 47 | |
|---|
| 48 | LONGX parmfrom=1,parmto=99999999L; |
|---|
| 49 | LONGX parmdfrom=1,parmdto=99999999L; |
|---|
| 50 | int parmtrace=0; |
|---|
| 51 | |
|---|
| 52 | if (argc < 2) |
|---|
| 53 | { |
|---|
| 54 | printf("P7B <dbname> <parmfrom> <parmto> <parmdfrom> <parmdto> [<parmtrace>]\n"); |
|---|
| 55 | exit(1); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | if (argc >= 2) dbnp=argv[1]; |
|---|
| 59 | if (argc >= 3) if (sscanf(argv[2],"%"_LD_,&parmfrom) != 1) fatal(argv[2]); |
|---|
| 60 | if (argc >= 4) if (sscanf(argv[3],"%"_LD_,&parmto) != 1) fatal(argv[3]); |
|---|
| 61 | if (argc >= 5) if (sscanf(argv[4],"%"_LD_,&parmdfrom) != 1) fatal(argv[4]); |
|---|
| 62 | if (argc >= 6) if (sscanf(argv[5],"%"_LD_,&parmdto) != 1) fatal(argv[5]); |
|---|
| 63 | if (argc >= 7) if (sscanf(argv[6],"%d",&parmtrace) != 1) fatal(argv[6]); |
|---|
| 64 | |
|---|
| 65 | if (parmto>MAXREGISTROS) fatal("parmto>=MAXREGISTROS"); |
|---|
| 66 | |
|---|
| 67 | /* arma Vetores */ |
|---|
| 68 | memset(Vetores,0x00,sizeof(Vetores)); |
|---|
| 69 | |
|---|
| 70 | irec=nrecs; |
|---|
| 71 | for (mfn=parmfrom; mfn<=parmto; mfn++) |
|---|
| 72 | { |
|---|
| 73 | int icol=0; |
|---|
| 74 | RECORD(irec,dbnp,mfn); |
|---|
| 75 | if (RECrc == RCEOF) break; |
|---|
| 76 | if (RECrc != RCNORMAL) continue; |
|---|
| 77 | maxmfn=mfn; |
|---|
| 78 | |
|---|
| 79 | for (diridx=0, dirloop=MFRnvf; dirloop--; diridx++) |
|---|
| 80 | { |
|---|
| 81 | if (DIRtag(diridx) != TAG20) continue; |
|---|
| 82 | p=FIELDP(diridx); |
|---|
| 83 | subfield((UBYTE *)p, DIRlen(diridx), 'w', (UBYTE *)areaval); |
|---|
| 84 | W=atof(areaval); |
|---|
| 85 | subfield((UBYTE *)p, DIRlen(diridx), 'd', (UBYTE *)areaval); |
|---|
| 86 | g1=(unsigned short)atoi(areaval); |
|---|
| 87 | /* armazena ate MAXTERMOS celulas */ |
|---|
| 88 | if (icol<MAXTERMOS) { |
|---|
| 89 | Vetores[mfn][icol].d = g1; |
|---|
| 90 | Vetores[mfn][icol].w = W; |
|---|
| 91 | icol++; |
|---|
| 92 | } |
|---|
| 93 | else { |
|---|
| 94 | // compara, reloca etc |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | for (mfn1=parmfrom; mfn1<=maxmfn; mfn1++) |
|---|
| 101 | { |
|---|
| 102 | if (parmtrace & 0x01) fprintf(stderr,"+++ %"_LD_"\n",mfn1); |
|---|
| 103 | printf("%"_LD_,mfn1); |
|---|
| 104 | |
|---|
| 105 | memset(TABSIM,0x00,sizeof(TABSIM)); |
|---|
| 106 | memset(TABSIMMFN,0x00,sizeof(TABSIMMFN)); |
|---|
| 107 | |
|---|
| 108 | for (mfn2=parmfrom; mfn2<=maxmfn; mfn2++) { |
|---|
| 109 | |
|---|
| 110 | if (parmtrace & 0x02) fprintf(stderr,"+++ mfn2=%"_LD_"\n",mfn2); |
|---|
| 111 | if (mfn2 <= 0L) break; |
|---|
| 112 | if (mfn2 < parmdfrom) continue; |
|---|
| 113 | if (mfn2 > parmdto) break; |
|---|
| 114 | |
|---|
| 115 | // Calcula a similaridade |
|---|
| 116 | for (somap=somaq2=somad2=0, i=0; i<MAXTERMOS; i++) { |
|---|
| 117 | W=Vetores[mfn1][i].w; |
|---|
| 118 | somaq2 += W*W; |
|---|
| 119 | } |
|---|
| 120 | for (i=0; i<MAXTERMOS; i++) { |
|---|
| 121 | g1=Vetores[mfn1][i].d; |
|---|
| 122 | for (j=0; j<MAXTERMOS; j++) { |
|---|
| 123 | if (g1 == Vetores[mfn2][j].d) { |
|---|
| 124 | W=Vetores[mfn1][i].w; |
|---|
| 125 | DW=Vetores[mfn2][j].w; |
|---|
| 126 | somap += W*DW; |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | } |
|---|
| 130 | for (j=0; j<MAXTERMOS; j++) { |
|---|
| 131 | DW=Vetores[mfn2][j].w; |
|---|
| 132 | somad2 += DW*DW; |
|---|
| 133 | } |
|---|
| 134 | denom=sqrt(somaq2*somad2); |
|---|
| 135 | if (denom > 0) sim=somap/denom; |
|---|
| 136 | else sim=0; |
|---|
| 137 | |
|---|
| 138 | if (parmtrace & 0x04) fprintf(stderr,"+++ %"_LD_"|%"_LD_"|%f\n",mfn1,mfn2,sim); |
|---|
| 139 | //Coloca na fila de maiores; |
|---|
| 140 | |
|---|
| 141 | if (sim == 0) continue; |
|---|
| 142 | |
|---|
| 143 | for (idx1=1 ; idx1 < MAXRELAT; ++idx1) { |
|---|
| 144 | if (sim > TABSIM[idx1]) break; |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | idx1++; |
|---|
| 148 | for (idx2=MAXRELAT-1; idx2 >= idx1; --idx2) { |
|---|
| 149 | idx4=idx2-1; |
|---|
| 150 | TABSIM[idx2]=TABSIM[idx4]; |
|---|
| 151 | TABSIMMFN[idx2]=TABSIMMFN[idx4]; |
|---|
| 152 | } |
|---|
| 153 | idx3=idx1-1; |
|---|
| 154 | TABSIM[idx3]=sim; |
|---|
| 155 | TABSIMMFN[idx3]=mfn2; |
|---|
| 156 | //fprintf(stderr,". %"_LD_"|%f\n",mfn2,TABSIMMFN[idx3]); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | for (i=1; i<MAXRELAT; i++) |
|---|
| 160 | { |
|---|
| 161 | if (TABSIM[i] == 0) break; |
|---|
| 162 | printf("|%"_LD_"|%9.6f",TABSIMMFN[i],TABSIM[i]); |
|---|
| 163 | } |
|---|
| 164 | printf(" \n"); |
|---|
| 165 | |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | exit(0); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | /* end of main */ |
|---|