-
Notifications
You must be signed in to change notification settings - Fork 2
/
imdb-similarity-join.py
293 lines (261 loc) · 10.5 KB
/
imdb-similarity-join.py
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
import os
import duckdb
import pandas
import time
loadJob = """CREATE TABLE aka_name (
id integer NOT NULL,
person_id integer NOT NULL,
name text NOT NULL,
imdb_index character varying(12),
name_pcode_cf character varying(5),
name_pcode_nf character varying(5),
surname_pcode character varying(5),
md5sum character varying(32),
primary key(id)
);
CREATE TABLE aka_title (
id integer NOT NULL,
movie_id integer NOT NULL,
title text NOT NULL,
imdb_index character varying(12),
kind_id integer NOT NULL,
production_year integer,
phonetic_code character varying(5),
episode_of_id integer,
season_nr integer,
episode_nr integer,
note text,
md5sum character varying(32),
primary key(id)
);
CREATE TABLE cast_info (
id integer NOT NULL,
person_id integer NOT NULL,
movie_id integer NOT NULL,
person_role_id integer,
note text,
nr_order integer,
role_id integer NOT NULL,
primary key(id)
);
CREATE TABLE char_name (
id integer NOT NULL,
name text NOT NULL,
imdb_index character varying(12),
imdb_id integer,
name_pcode_nf character varying(5),
surname_pcode character varying(5),
md5sum character varying(32),
primary key(id)
);
CREATE TABLE comp_cast_type (
id integer NOT NULL,
kind character varying(32) NOT NULL,
primary key(id)
);
CREATE TABLE company_name (
id integer NOT NULL,
name text NOT NULL,
country_code character varying(255),
imdb_id integer,
name_pcode_nf character varying(5),
name_pcode_sf character varying(5),
md5sum character varying(32),
primary key(id)
);
CREATE TABLE company_type (
id integer NOT NULL,
kind character varying(32) NOT NULL,
primary key(id)
);
CREATE TABLE complete_cast (
id integer NOT NULL,
movie_id integer,
subject_id integer NOT NULL,
status_id integer NOT NULL,
primary key(id)
);
CREATE TABLE info_type (
id integer NOT NULL,
info character varying(32) NOT NULL,
primary key(id)
);
CREATE TABLE keyword (
id integer NOT NULL,
keyword text NOT NULL,
phonetic_code character varying(5),
primary key(id)
);
CREATE TABLE kind_type (
id integer NOT NULL,
kind character varying(15) NOT NULL,
primary key(id)
);
CREATE TABLE link_type (
id integer NOT NULL,
link character varying(32) NOT NULL,
primary key(id)
);
CREATE TABLE movie_companies (
id integer NOT NULL,
movie_id integer NOT NULL,
company_id integer NOT NULL,
company_type_id integer NOT NULL,
note text,
primary key(id)
);
CREATE TABLE movie_info (
id integer NOT NULL,
movie_id integer NOT NULL,
info_type_id integer NOT NULL,
info text NOT NULL,
note text,
primary key(id)
);
CREATE TABLE movie_info_idx (
id integer NOT NULL,
movie_id integer NOT NULL,
info_type_id integer NOT NULL,
info text NOT NULL,
note text,
primary key(id)
);
CREATE TABLE movie_keyword (
id integer NOT NULL,
movie_id integer NOT NULL,
keyword_id integer NOT NULL,
primary key(id)
);
CREATE TABLE movie_link (
id integer NOT NULL,
movie_id integer NOT NULL,
linked_movie_id integer NOT NULL,
link_type_id integer NOT NULL,
primary key(id)
);
CREATE TABLE name (
id integer NOT NULL,
name text NOT NULL,
imdb_index character varying(12),
imdb_id integer,
gender character varying(1),
name_pcode_cf character varying(5),
name_pcode_nf character varying(5),
surname_pcode character varying(5),
md5sum character varying(32),
primary key(id)
);
CREATE TABLE person_info (
id integer NOT NULL,
person_id integer NOT NULL,
info_type_id integer NOT NULL,
info text NOT NULL,
note text,
primary key(id)
);
CREATE TABLE role_type (
id integer NOT NULL,
role character varying(32) NOT NULL,
primary key(id)
);
CREATE TABLE title (
id integer NOT NULL,
title text NOT NULL,
imdb_index character varying(12),
kind_id integer NOT NULL,
production_year integer,
imdb_id integer,
phonetic_code character varying(5),
episode_of_id integer,
season_nr integer,
episode_nr integer,
series_years character varying(49),
md5sum character varying(32),
primary key(id)
);
copy aka_name from 'job-data/aka_name.csv' csv escape '\\' null '';
copy aka_title from 'job-data/aka_title.csv' csv escape '\\' null '';
copy cast_info from 'job-data/cast_info.csv' csv escape '\\' null '';
copy char_name from 'job-data/char_name.csv' csv escape '\\' null '';
copy company_name from 'job-data/company_name.csv' csv escape '\\' null '';
copy company_type from 'job-data/company_type.csv' csv escape '\\' null '';
copy comp_cast_type from 'job-data/comp_cast_type.csv' csv escape '\\' null '';
copy complete_cast from 'job-data/complete_cast.csv' csv escape '\\' null '';
copy info_type from 'job-data/info_type.csv' csv escape '\\' null '';
copy keyword from 'job-data/keyword.csv' csv escape '\\' null '';
copy kind_type from 'job-data/kind_type.csv' csv escape '\\' null '';
copy link_type from 'job-data/link_type.csv' csv escape '\\' null '';
copy movie_companies from 'job-data/movie_companies.csv' csv escape '\\' null '';
copy movie_info from 'job-data/movie_info.csv' csv escape '\\' null '';
copy movie_info_idx from 'job-data/movie_info_idx.csv' csv escape '\\' null '';
copy movie_keyword from 'job-data/movie_keyword.csv' csv escape '\\' null '';
copy movie_link from 'job-data/movie_link.csv' csv escape '\\' null '';
copy name from 'job-data/name.csv' csv escape '\\' null '';
copy person_info from 'job-data/person_info.csv' csv escape '\\' null '';
copy role_type from 'job-data/role_type.csv' csv escape '\\' null '';
copy title from 'job-data/title.csv' csv escape '\\' null '';
"""
con = duckdb.connect(database=':memory:')
con.execute(loadJob)
startDuckDB = time.time()
df1 = con.execute("""
select t.id,t.title
from title t, movie_info mi, info_type it
where t.id=mi.movie_id
and it.id=mi.info_type_id
and it.info = 'genres'
and mi.info='Crime'
""").fetchall()
df2 = con.execute("""
select t.id,t.title
from title t, movie_info mi, info_type it
where t.id=mi.movie_id
and it.id=mi.info_type_id
and it.info = 'genres'
and mi.info='Crime'
""").fetchall()
endDuckDB = time.time()
# print(df)
def ngrams(s, n):
res = []
for i in range(0, len(s) - n):
res.append(s[i:i + n + 1])
return res
def similarity_join(df1, df2):
ngram_idx = {}
n = 5
for r in df1:
for ngram in ngrams(r[1], n):
if ngram in ngram_idx:
ngram_idx[ngram].append(r[0])
else:
ngram_idx[ngram] = [r[0]]
idA = []
idB = []
for r in df2:
counts = {}
for ngram in ngrams(r[1], n):
if ngram in ngram_idx:
for id in ngram_idx[ngram]:
if id != r[0]:
if id in counts:
counts[id] += 1
else:
counts[id] = 1
if counts[id] == 10:
idA.append(r[0])
idB.append(id)
return pandas.DataFrame.from_dict({'a': idA, 'b': idB})
startSimilarityJoin = time.time()
similar_ids = similarity_join(df1, df2)
endSimilarityJoin = time.time()
startDuckDB2 = time.time()
result = con.execute("""
select t.title,t2.title
from title t, title t2, similar_ids s
where t.id=s.a and t2.id=s.b
""").fetchdf()
endDuckDB2 = time.time()
print("DuckDB:", (endDuckDB - startDuckDB)+(endDuckDB2 - startDuckDB2))
print("Python:", endSimilarityJoin-startSimilarityJoin)
print(result)