-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerator.java
More file actions
194 lines (164 loc) · 4.61 KB
/
Generator.java
File metadata and controls
194 lines (164 loc) · 4.61 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
package org.julien.datastuff;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Generator {
private final List<String> firstNamesMale;
private final List<String> firstNamesFemale;
private final List<String> lastNames;
private final List<String> states;
private final Random rand;
public List<String> getFirstNamesMale() {
return firstNamesMale;
}
public List<String> getFirstNamesFemale() {
return firstNamesFemale;
}
public List<String> getLastNames() {
return lastNames;
}
public List<String> getStates() {
return states;
}
public Random getRand() {
return rand;
}
public Generator() {
firstNamesMale = new ArrayList<String>();
firstNamesFemale = new ArrayList<String>();
lastNames = new ArrayList<String>();
states = new ArrayList<String>();
rand = new Random(System.currentTimeMillis());
}
/**
* This methods loads strings from a text file into a list
*
* @param list
* the list to store strings into
* @param filename
* the file to load strings from
* @param delimiter
* the field delimiter to use for the file
*/
private void load(List<String> list, String filename, String delimiter) {
Scanner sc = null;
try {
sc = new Scanner(new FileReader(filename));
sc.useDelimiter(delimiter);
while (sc.hasNext()) {
String line = sc.nextLine();
String name = line.split(delimiter)[0];
list.add(name);
}
sc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("Loaded " + list.size() + " strings from "
+ filename);
}
/**
* This method builds the a log line.
*
* @param delimiter
* the field delimiter to use in the output string
* @return a complete line of text data
*/
private String buildLine(String delimiter) {
StringBuffer sb = new StringBuffer();
float multiplier = 1f;
// Add last name
sb.append(getLastNames().get(rand.nextInt(getLastNames().size())));
sb.append(delimiter);
// Add first name and gender
int gender = rand.nextInt(2);
if (gender == 0) {
// Ladies first ;)
sb.append(getFirstNamesFemale().get(
rand.nextInt(getFirstNamesFemale().size())));
sb.append(delimiter).append("F").append(delimiter);
} else {
sb.append(getFirstNamesMale().get(
rand.nextInt(getFirstNamesMale().size())));
sb.append(delimiter).append("M").append(delimiter);
}
if (gender == 0) {
multiplier *= 1.25f;
}
// Add state
String state = getStates().get(rand.nextInt(getStates().size()));
sb.append(state);
sb.append(delimiter);
switch (state) {
case "California":
case "Florida":
multiplier *= 1.50f;
break;
case "New York":
multiplier *= 1.75f;
break;
case "District of Columbia":
multiplier *= 2f;
break;
}
// Add age
int age = 18 + rand.nextInt(70);
sb.append(age).append(delimiter);
if ((age > 25) && (age < 45)) {
multiplier *= 1.2f;
}
// Add day of year
int day = rand.nextInt(365);
sb.append(1 + day).append(delimiter);
if ( (day > 305) && (day <=335)) {
multiplier *= 1.5f;
}
else if ( (day > 335) && (day <=350)) {
multiplier *= 2f;
} else if (day > 350) {
multiplier *= 2.5f;
}
// Add hour and minutes
sb.append(1 + rand.nextInt(24)).append(delimiter);
sb.append(1 + rand.nextInt(60)).append(delimiter);
// Add number of items purchased
int items = 1 + rand.nextInt(10);
sb.append(items).append(delimiter);
// Add basket price
sb.append(Math.abs((int)(rand.nextGaussian()*50+(100*multiplier))));
sb.append("\n");
return sb.toString();
}
/**
* This method builds a randomly-generated CSV file
* @param filename the file to store data in
* @param lines the number of lines to store
*/
private void save(String filename, int lines) {
try {
PrintWriter writer = new PrintWriter(filename, "UTF-8");
for (int i = 0; i < lines; i++) {
writer.write(buildLine(","));
}
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
Generator g = new Generator();
g.load(g.getLastNames(), "dist.all.last", " ");
g.load(g.getFirstNamesMale(), "dist.male.first", " ");
g.load(g.getFirstNamesFemale(), "dist.female.first", " ");
g.load(g.getStates(), "US_States.txt", ",");
g.save("data-batch-predicton.txt", 1000);
System.out.println("Done.");
}
}