-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainnbc.c
executable file
·112 lines (82 loc) · 2.88 KB
/
trainnbc.c
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
#include <stdio.h>
#include <stdlib.h>
/* Train the classfier */
int trainnbc(char ttype, char *trngdatapath) {
FILE *trngdatafp;
char *temp, trngline[MAXLINELENGTH];
Word_t *Rolecount=0, *attrcount=0, *Cmdcount=0, *columnval;
short attrid, i;
uint8_t *Roleindex, *attr, *attrindex, *attridbuf, *Cmdindex, *Cmdval;
char index[3];
if ((trngdatafp = fopen(trngdatapath, "r")) == NULL) {
printf("\n Error opening training data file = %s \n", trngdatapath);
return -1;
}
/*Browse through the training data line by line*/
while(fgets(trngline, MAXLINELENGTH, trngdatafp) != NULL) {
numtrngrecords++; /* counting no. of trng records */
temp = strdup(trngline);
/* Populate Role count array */
Roleindex = (char *)strtok(temp, delimtab);
JSLI(Rolecount, Rolecountarray, Roleindex);
(*Rolecount)++;
if (ttype == 'c' || ttype == 's') {
attrid = 0; /* Reinitialise attribute index */
while ((attr = (char *)strtok(NULL, delimtab)) != NULL) {
attrindex = (char *)malloc(ATTRINDEXSIZE_cs*sizeof(char));
attridbuf = (char *)malloc(10*sizeof(char));
/* Form the attribute index */
strcpy(attrindex, Roleindex);
strcat(attrindex, "_");
sprintf(attridbuf, "%d", attrid++);
strcat(attrindex, attridbuf);
strcat(attrindex, "_");
strcat(attrindex, attr);
JSLI(attrcount, Attrcountarray, attrindex);
(*attrcount)++;
free(attrindex);
free(attridbuf);
}
}
else {
attrid = 0; /* Reinitialise attribute index */
attridbuf = (char *)malloc(10*sizeof(char));
sprintf(attridbuf, "%d", attrid);
/* Populate Command count in the attribute array */
Cmdval = (char *)strtok(NULL, delimtab);
Cmdindex = (char *)malloc(ATTRINDEXSIZE_cs*sizeof(char));
strcpy(Cmdindex, Roleindex);
strcat(Cmdindex, "_");
strcat(Cmdindex, attridbuf); /* Command is attribute 0 */
strcat(Cmdindex, "_");
strcat(Cmdindex, Cmdval);
// printf("\n Cmd index = %s \n", Cmdindex);
JSLI(Cmdcount, Attrcountarray, Cmdindex);
(*Cmdcount)++;
attrid++; /* Increment attribute counter */
/* Start populating the Attcountarray for the fine triplet attributes */
strcpy(index, "");
JSLF(columnval, Columncountarray, index);
while (columnval != NULL) {
attrindex = (char *)malloc(ATTRINDEXSIZE_f*sizeof(char));
attridbuf = (char *)malloc(10*sizeof(char));
strcpy(attrindex, Roleindex);
strcat(attrindex, "_");
sprintf(attridbuf, "%d", attrid++);
strcat(attrindex, attridbuf);
strcat(attrindex, "_");
i = *columnval;
while(i-- > 0) {
attr = (char *)strtok(NULL, delimtab);
strcat(attrindex, attr);
}
JSLI(attrcount, Attrcountarray, attrindex);
(*attrcount)++;
free(attrindex);
free(attridbuf);
JSLN(columnval, Columncountarray, index);
}
}
if (numattributes == 0) numattributes = attrid;
}
}