-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRetrosheetToGameState.java
More file actions
342 lines (290 loc) · 10.4 KB
/
Copy pathRetrosheetToGameState.java
File metadata and controls
342 lines (290 loc) · 10.4 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
class GameState {
/**
* The visitor's score at the time of the state
*/
public int visitorScore;
/**
* The home team's score at the time of the state
*/
public int homeScore;
/**
* The number of innings completed at the time of the state
*/
public int innings;
/**
* Indicator of the winning team. True if the home team won, false if the
* visitors won
*/
public boolean homeWin;
/**
* The first line that should be inserted in a CSV list of game states, showing the column titles
*/
public static final String CSV_HEADERS = "visitorScore,homeScore,innings,homeWin";
/**
* Reads a line of comma-separated values.
*
* @param line
* the string to be read.
* @return the array of values in order.
*/
public static String[] readCSVLine(String line) {
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter(",");
ArrayList<String> output = new ArrayList<String>();
while (lineScanner.hasNext()) {
output.add(lineScanner.next());
}
lineScanner.close();
return output.toArray(new String[] {});
}
/**
* @param visitorScore
* @param homeScore
* @param innings
* @param homeWin
*/
public GameState(int visitorScore, int homeScore, int innings, boolean homeWin) {
this.visitorScore = visitorScore;
this.homeScore = homeScore;
this.innings = innings;
this.homeWin = homeWin;
}
/**
* Construct a list of game states from a CSV line score.
*
* @param gameLog a CSV line in the format of visLine,homeLine,visScore,homeScore
* @return a list of game states from the given game
*/
public static ArrayList<GameState> lineToStates(String line) {
String[] fields = readCSVLine(line);
ArrayList<GameState> output = new ArrayList<GameState>();
// Variables to keep track of score by inning
int visitorScore = 0, homeScore = 0, innings = 0;
boolean homeWin = Integer.parseInt(fields[3]) > Integer.parseInt(fields[2]);
// The line scores will shrink from the front as innings are read and processed
String visitorLine = fields[0],
homeLine = fields[1];
// Start of game, 0 - 0 thru 0 innings
output.add(new GameState(visitorScore, homeScore, innings, homeWin));
while (visitorLine.length() > 0) {
// Advance an inning
if (visitorLine.charAt(0) == '(') {
// 10+ run inning
visitorScore += Integer.parseInt(visitorLine.substring(1, visitorLine.indexOf(')')));
visitorLine = visitorLine.substring(visitorLine.indexOf(')') + 1);
} else {
visitorScore += Integer.parseInt(visitorLine.substring(0, 1));
visitorLine = visitorLine.substring(1);
}
if (homeLine.charAt(0) == '(') {
// 10+ run inning
homeScore += Integer.parseInt(homeLine.substring(1, homeLine.indexOf(')')));
homeLine = homeLine.substring(homeLine.indexOf(')') + 1);
} else {
homeScore += Integer.parseInt(homeLine.substring(0, 1));
homeLine = homeLine.substring(1);
}
innings++;
output.add(new GameState(visitorScore, homeScore, innings, homeWin));
}
// Strike last inning
output.remove(output.size() - 1);
return output;
}
/**
* Construct a list of game states from a CSV game log for a single game.
*
* Qualifications for a game to be included (field numbers are 1-based):
*
* - Fields 10 & 11, the final score must be uneven; game must not end in a tie
*
* - Field 12, length of game must be at least 51 outs; game must not be
* rain-shortened
*
* - Field 15, forfeit must be ""; game must not be forfeited
*
* - Field 161, acquisition information must be "Y"; game information must be
* complete
*
* For games that do not qualify, an empty list is returned.
*
* @param gameLog
* a CSV game log for a game, under Retrosheet specifications
* @return a list of game states from the given game
*/
public static ArrayList<GameState> logToStates(String gameLog) {
String[] fields = readCSVLine(gameLog);
ArrayList<GameState> output = new ArrayList<GameState>();
if (
Integer.parseInt(fields[9]) == Integer.parseInt(fields[10]) ||
Integer.parseInt(fields[11]) < 51 ||
!(fields[14].equals("\"\"")) ||
!(fields[160].equals("\"Y\""))
)
// Game disqualified, return empty list
return output;
// Variables to keep track of score by inning
int visitorScore = 0, homeScore = 0, innings = 0;
boolean homeWin = Integer.parseInt(fields[10]) > Integer.parseInt(fields[9]);
int totalInnings = Integer.parseInt(fields[11]) / 6;
// Above calculation rounds down. Add one if last inning is less than 6 outs
if (Integer.parseInt(fields[11]) % 6 != 0)
totalInnings++;
// The line scores will shrink from the front as innings are read and processed
// Strike the first and last characters, which are quotes
String visitorLine = fields[19].substring(1, fields[19].length() - 1),
homeLine = fields[20].substring(1, fields[20].length() - 1);
// Start of game, 0 - 0 thru 0 innings
output.add(new GameState(visitorScore, homeScore, innings, homeWin));
while (innings < totalInnings - 1) { // Last inning not included
// Advance an inning
if (visitorLine.charAt(0) == '(') {
// 10+ run inning
visitorScore += Integer.parseInt(visitorLine.substring(1, visitorLine.indexOf(')')));
visitorLine = visitorLine.substring(visitorLine.indexOf(')') + 1);
} else {
visitorScore += Integer.parseInt(visitorLine.substring(0, 1));
visitorLine = visitorLine.substring(1);
}
if (homeLine.charAt(0) == '(') {
// 10+ run inning
homeScore += Integer.parseInt(homeLine.substring(1, homeLine.indexOf(')')));
homeLine = homeLine.substring(homeLine.indexOf(')') + 1);
} else {
homeScore += Integer.parseInt(homeLine.substring(0, 1));
homeLine = homeLine.substring(1);
}
innings++;
output.add(new GameState(visitorScore, homeScore, innings, homeWin));
}
return output;
}
public String toString() {
String output = visitorScore + " - " + homeScore + " through " + innings + " inning(s), ";
if (homeWin)
output += "home win";
else
output += "visitor win";
return output;
}
/**
* Builds an CSV representation of the game state.
*
* @return visitorScore,homeScore,innings, homeWin
*/
public String toCSV() {
return visitorScore + "," + homeScore + "," + innings + "," + homeWin;
}
}
public class RetrosheetToGameState {
public static GameState[] lineFilesToStates() {
// Directory where the list and game log files are stored
final String PATH_PREFIX = "***";
// File name for a list of file names; each file should be a list of line scores
final String LIST_OF_FILES = "***";
// Reads list of files
System.out.println("Reading list of files...");
ArrayList<String> listOfFiles = new ArrayList<String>();
try {
Scanner listScanner;
listScanner = new Scanner(new File(PATH_PREFIX + LIST_OF_FILES));
while (listScanner.hasNextLine()) {
listOfFiles.add(listScanner.nextLine());
}
listScanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("File list read, " + listOfFiles.size() + " files in list");
// Reads files
ArrayList<GameState> stateList = new ArrayList<GameState>();
for (String fileName : listOfFiles) {
// Reads a file
System.out.println("Reading " + fileName + "...");
try {
Scanner fileScanner = new Scanner(new File(PATH_PREFIX + fileName));
int count = 0;
while (fileScanner.hasNextLine()) {
stateList.addAll(GameState.lineToStates(fileScanner.nextLine()));
count++;
System.out.println(count);
}
fileScanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
System.out.println("All files read, " + stateList.size() + " game states in total");
return stateList.toArray(new GameState[] {});
}
public static GameState[] filesToStates() {
// Directory where the list and game log files are stored
final String PATH_PREFIX = "***";
// File name for a list of file names; each file should be a list of game logs
final String LIST_OF_FILES = "***";
// Reads list of files
System.out.println("Reading list of files...");
ArrayList<String> listOfFiles = new ArrayList<String>();
try {
Scanner listScanner;
listScanner = new Scanner(new File(PATH_PREFIX + LIST_OF_FILES));
while (listScanner.hasNextLine()) {
listOfFiles.add(listScanner.nextLine());
}
listScanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("File list read, " + listOfFiles.size() + " files in list");
// Reads files
ArrayList<GameState> stateList = new ArrayList<GameState>();
for (String fileName : listOfFiles) {
// Reads a file
System.out.println("Reading " + fileName + "...");
try {
Scanner fileScanner = new Scanner(new File(PATH_PREFIX + fileName));
while (fileScanner.hasNextLine()) {
//System.out.println(fileScanner.nextLine());
stateList.addAll(GameState.logToStates(fileScanner.nextLine()));
}
fileScanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
System.out.println("All files read, " + stateList.size() + " game states in total");
return stateList.toArray(new GameState[] {});
}
public static String generateCSVStates(GameState[] gameStates) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(GameState.CSV_HEADERS);
for(GameState gameState : gameStates) {
stringBuilder.append("\n");
stringBuilder.append(gameState.toCSV());
}
return stringBuilder.toString();
}
public static void writeToFile(String content) {
// Path and filename of the output file
final String OUTPUT_PATH = "***";
FileWriter fileWriter;
try {
fileWriter = new FileWriter(OUTPUT_PATH);
fileWriter.write(content);
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Finished writing to file");
}
public static void main(String[] args) {
writeToFile(generateCSVStates(filesToStates()));
writeToFile(generateCSVStates(lineFilesToStates()));
}
}