-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImdbParser.java
More file actions
264 lines (255 loc) · 9.34 KB
/
ImdbParser.java
File metadata and controls
264 lines (255 loc) · 9.34 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
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipInputStream;
/**
* A <code>ImdbParser</code> can parse the movie and actor/actress lists from
* the imdb text data (http://www.imdb.com/interfaces). It uses an
* {@link ImdbReader} forwarding the parsed information.
*/
public class ImdbParser
{
private static final String MOVIES_MARKER = "MOVIES LIST";
private static final int MOVIES_SKIPS = 2;
private static final String ACTRESSES_MARKER = "THE ACTRESSES LIST";
private static final int ACTRESS_SKIPS = 4;
private static final String ACTOR_MARKER = "THE ACTORS LIST";
private static final int ACTOR_SKIPS = 4;
private static final int BUFFER_SIZE = 200;
private final ImdbReader reader;
/**
* Create a new Imdb parser.
* @param reader
* reader this parser will use to forward events to
*/
public ImdbParser( final ImdbReader reader )
{
if ( reader == null )
{
throw new IllegalArgumentException( "Null ImdbReader" );
}
this.reader = reader;
}
/**
* Parsers a tab-separated movie list file, each line containing a movie
* title and the year the movie was released. The file can be .gz or .zip
* compressed, and must then have the corresponding file extension.
* @param file
* name of movie list file
* @throws IOException
* if unable to open the movie list file
*/
public String parseMovies( final String file ) throws IOException
{
final List<MovieData> buffer = new LinkedList<MovieData>();
if ( file == null )
{
throw new IllegalArgumentException( "Null movie file" );
}
BufferedReader fileReader = getFileReader( file, MOVIES_MARKER,
MOVIES_SKIPS );
String line = fileReader.readLine();
int movieCount = 0;
while ( line != null )
{
// get rid of blank lines and TV shows
if ( "".equals( line ) || line.indexOf( "(TV)" ) != -1 )
{
line = fileReader.readLine();
continue;
}
final int yearSep = line.indexOf( '\t' );
if ( yearSep > 0 )
{
final String title = line.substring( 0, yearSep ).trim();
String yearString = line.substring( yearSep ).trim();
if ( yearString.length() > 4 )
{
yearString = yearString.substring( 0, 4 );
}
if ( yearString.length() == 0 || yearString.charAt( 0 ) == '?'
|| title.contains( "{" ) || title.startsWith( "\"" ) )
{
line = fileReader.readLine();
continue;
}
final int year = Integer.parseInt( yearString );
buffer.add( new MovieData( title, year ) );
movieCount++;
if ( movieCount % BUFFER_SIZE == 0 )
{
reader.newMovies( buffer );
buffer.clear();
}
}
line = fileReader.readLine();
}
reader.newMovies( buffer );
return (movieCount + " movies parsed and injected.");
}
/**
* Parsers a tab-separated actors list file. A line begins with actor name
* then followed by a tab and a movie title the actor acted in. Additional
* movies the current actor acted in are found on the following line that
* starts with a tab followed by the movie title.
* @param actorFile
* name of actor list file
* @param actressFile
* TODO
* @throws IOException
* if unable to open actor list file
*/
public String parseActors( final String actorFile, final String actressFile )
throws IOException
{
if ( actorFile == null )
{
throw new IllegalArgumentException( "Null actor file" );
}
if ( actressFile == null )
{
throw new IllegalArgumentException( "Null actress file" );
}
String result = "";
BufferedReader fileReader;
// result += "Actors: " + parseActorItems( fileReader, false ) + "\n";
// fileReader.close();
fileReader = getFileReader( actressFile, ACTRESSES_MARKER,
ACTRESS_SKIPS );
result += "Actresses: " + parseActorItems( fileReader, true );
return result;
}
private String parseActorItems( BufferedReader fileReader, boolean actress )
throws IOException
{
String line = fileReader.readLine();
String currentActor = null;
final List<ActorData> buffer = new LinkedList<ActorData>();
final List<RoleData> movies = new ArrayList<RoleData>();
int movieCount = 0;
int actorCount = 0;
while ( line != null )
{
// get rid of blank lines
if ( "".equals( line ) )
{
line = fileReader.readLine();
continue;
}
int actorSep = line.indexOf( '\t' );
if ( actorSep >= 0 )
{
String actor = line.substring( 0, actorSep ).trim();
if ( !"".equals( actor ) )
{
if ( movies.size() > 0 )
{
buffer.add( new ActorData( currentActor, movies
.toArray( new RoleData[movies.size()] ), actress ) );
actorCount++;
movies.clear();
}
currentActor = actor;
}
String title = line.substring( actorSep ).trim();
if ( title.length() == 0 || title.contains( "{" )
|| title.startsWith( "\"" ) || title.contains( "????" ) )
{
line = fileReader.readLine();
continue;
}
int characterStart = title.indexOf( '[' );
int characterEnd = title.indexOf( ']' );
String character = null;
if ( characterStart > 0 && characterEnd > characterStart )
{
character = title.substring( characterStart + 1,
characterEnd );
}
int creditStart = title.indexOf( '<' );
// int creditEnd = title.indexOf( '>' );
// String credit = null;
// if ( creditStart > 0 && creditEnd > creditStart )
// {
// credit = title.substring( creditStart + 1, creditEnd );
// }
if ( characterStart > 0 )
{
title = title.substring( 0, characterStart ).trim();
}
else if ( creditStart > 0 )
{
title = title.substring( 0, creditStart ).trim();
}
int spaces = title.indexOf( " " );
if ( spaces > 0 )
{
if ( title.charAt( spaces - 1 ) == ')'
&& title.charAt( spaces + 2 ) == '(' )
{
title = title.substring( 0, spaces ).trim();
}
}
movies.add( new RoleData( title, character ) );
movieCount++;
if ( movieCount % BUFFER_SIZE == 0 )
{
reader.newActors( buffer );
buffer.clear();
}
}
line = fileReader.readLine();
}
reader.newActors( buffer );
return (actorCount + " added including " + movieCount + " characters parsed and injected.");
}
/**
* Get fiel reader that corresponds to file extension.
* @param file
* the file name
* @param pattern
* TODO
* @param skipLines
* TODO
* @return a file reader that uncompresses data if needed
* @throws IOException
* @throws FileNotFoundException
*/
private BufferedReader getFileReader( final String file, String pattern,
int skipLines ) throws IOException, FileNotFoundException
{
BufferedReader fileReader;
// support compressed files
if ( file.endsWith( ".gz" ) )
{
fileReader = new BufferedReader( new InputStreamReader(
new GZIPInputStream( new FileInputStream( file ) ) ) );
}
else if ( file.endsWith( ".zip" ) )
{
fileReader = new BufferedReader( new InputStreamReader(
new ZipInputStream( new FileInputStream( file ) ) ) );
}
else
{
fileReader = new BufferedReader( new FileReader( file ) );
}
String line = "";
while ( !pattern.equals( line ) )
{
line = fileReader.readLine();
}
for ( int i = 0; i < skipLines; i++ )
{
line = fileReader.readLine();
}
return fileReader;
}
}