-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain's_functions.cpp
More file actions
199 lines (141 loc) · 3.56 KB
/
main's_functions.cpp
File metadata and controls
199 lines (141 loc) · 3.56 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <fstream>
#include "main's_functions.h"
#include "globals.h"
using namespace std;
void parse_config_file()
{
ifstream in;
string parameter, value;
// open the input stream to read the key
in.open(c_file.c_str());
// check if the file was OK
if (in)
while (!in.eof())
{
in >> parameter;
//cout<<" PARAMETER="<<parameter<<endl;
if (parameter == "number_of_hash_tables:")
in >> ::L;
if (parameter == "number_of_hash_functions:")
in >> ::k;
if (parameter == "number_of_clusters:")
in >> ::n_clusters;
if (parameter == "max_kmeans_iterations:")
in >> ::max_kmeans_iterations;
if (parameter == "check_Gs_in_lsh:")
in >> ::check_g;
}
else
cout<<" problem reading config file:"<<c_file<<" (now using default settings)\n";
}
void parse_arguments(int argc, char **argv)
{
//parse command line arguments using getopt()
bool input_file_given = false;
int c = 0;
while ((c = getopt(argc, argv, "had:c:o:i:")) != -1)
switch (c)
{
case 'a':
complete = true;
break;
case 'c':
c_file = optarg;
break;
case 'i':
in_file = optarg;
input_file_given = true;
break;
case 'o':
out_file = optarg;
break;
case 'd':
metric = static_cast<char>(optarg[0] + 32);
if (metric != 'c' && metric != 'e')
{
cout << "!! no such metric. Use <Euclidean> or <Cosine>\nNow using: Euclidean \n";
metric = 'e';
}
break;
case 'h':
default:
cout << "RUN WITH ./cluster –i <input file> –c <config file> "
"-d <metric> -ο <output file> -a\n\t-a = -complete \n\t-d = Metric = 'E' or 'C' ";
exit(-99);
}
if ( input_file_given == false){
cout<< "plz do not run this program without giving the -i parameter...\nType input file name :";
cin>> in_file;
}
}
void parse_input_file()
{
ifstream in;
in.open(in_file);
if (!in)
{
cout << "Oops. Cannot open " << in_file << endl;
exit(-5);
}
//count /n in the file
num_points = (int) (count(istreambuf_iterator<char>(in), istreambuf_iterator<char>(), '\n'));
//rewind file
in.clear();
in.seekg(0);
string line;
getline(in, line);
int count = 0;
//read each character and count spaces. (!) theres one extra at the end. ??
for (char c : line)
if (c == ' ' || c == '\t' || c == ',')
count++;
dimension = count;
cout << "vector_dimension=" << dimension << endl;
in.clear();
in.seekg(0);
}
vector<Datapoint> create_dataset(int num_points)
{
ifstream in;
in.open(in_file);
if (!in)
{
cout << "Cannot open " << in_file << endl;
exit(-5);
}
char line[5000]; //temp <- getline()
string id; //temp <- id
vector<double> point; //temp <- datapoint
int vectors_read_so_far = 0; //counter
char *token; //get every word separated by space or comma
vector<Datapoint> dataset;
if (in.eof() || !in.is_open())
cout << "\n\n[problem\n\n";
while (in.getline(line, 5000))
{
if (0 || vectors_read_so_far < num_points)
{ // debuging (read num_points)
//get id = string
token = strtok(line, " ,\t");
id = token;
//cout << "+id_" << id << endl;
//get 1st coordinate
token = strtok(NULL, " ,\t");
//get other coordinates
while (token != NULL)
{
point.push_back(atof(token));
token = strtok(NULL, " ,\t");
}
//make a datapoint d and save it to a list
Datapoint d(point, id);
dataset.push_back(d);
point.clear();
}
vectors_read_so_far++;
}
cout << "\n read " << dataset.size() << " vectors.\n";
in.clear();
in.close();
return dataset;
}