-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
232 lines (184 loc) · 5.13 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"io"
"os"
"github.com/grailbio/base/tsv"
)
type Title struct {
TConst string `tsv:"tconst"`
TitleType string `tsv:"titleType"`
PrimaryTitle string `tsv:"primaryTitle"`
OriginalTitle string `tsv:"originalTitle"`
IsAdult string `tsv:"isAdult"`
StartYear string `tsv:"startYear"`
EndYear string `tsv:"endYear"`
RuntimeMinutes string `tsv:"runtimeMinutes"`
Genres string `tsv:"genres"`
}
type Principal struct {
TConst string `tsv:"tconst"`
Ordering int `tsv:"ordering"`
NConst string `tsv:"nconst"`
Category string `tsv:"category"`
Job string `tsv:"job"`
Characters string `tsv:"characters"`
}
type Name struct {
NConst string `tsv:"nconst"`
PrimaryName string `tsv:"primaryName"`
BirthYear string `tsv:"birthYear"`
DeathYear string `tsv:"deathYear"`
PrimaryProfession string `tsv:"primaryProfession"`
KnownForTitles string `tsv:"knownForTitles"`
}
type Episode struct {
TConst string `tsv:"tconst"`
ParentTConst string `tsv:"parentTconst"`
SeasonNumber string `tsv:"seasonNumber"`
EpisodeNumber string `tsv:"episodeNumber"`
}
type node struct {
name string
parent string
job map[string]string
neighbors map[string]*node
}
var (
from string
to string
)
func init() {
flag.StringVar(&from, "from", "", "")
flag.StringVar(&to, "to", "", "")
}
func main() {
flag.Parse()
principals, _ := os.OpenFile("./title.principals.tsv", os.O_RDONLY, 0)
basics, _ := os.OpenFile("./title.basics.tsv", os.O_RDONLY, 0)
names, _ := os.OpenFile("./name.basics.tsv", os.O_RDONLY, 0)
episodes, _ := os.OpenFile("./title.episode.tsv", os.O_RDONLY, 0)
graph := map[string]*node{}
r := tsv.NewReader(basics)
r.HasHeaderRow = true
r.UseHeaderNames = true
var title Title
var err error
fmt.Printf("Populating title index...\n")
for err = r.Read(&title); err != io.EOF; err = r.Read(&title) {
if err != nil {
continue
}
// Only include movies and tv
if title.TitleType != "movie" { //} && !strings.Contains(title.TitleType, "tv") {
continue
}
graph[title.TConst] = &node{
name: title.PrimaryTitle,
job: map[string]string{},
neighbors: map[string]*node{},
}
}
r = tsv.NewReader(principals)
r.HasHeaderRow = true
r.UseHeaderNames = true
var principal Principal
fmt.Printf("Populating principal index...\n")
for err = r.Read(&principal); err != io.EOF; err = r.Read(&principal) {
if err != nil {
continue
}
if principal.Category != "actor" && principal.Category != "actress" && principal.Category != "self" {
continue
}
titleNode, ok := graph[principal.TConst]
// Skip if this refers to a title we skipped
if !ok {
continue
}
principalNode, ok := graph[principal.NConst]
if !ok {
principalNode = &node{
name: principal.NConst,
neighbors: map[string]*node{},
}
graph[principal.NConst] = principalNode
}
titleNode.neighbors[principal.NConst] = principalNode
titleNode.job[principal.NConst] = principal.Category
principalNode.neighbors[principal.TConst] = titleNode
}
r = tsv.NewReader(names)
r.HasHeaderRow = true
r.UseHeaderNames = true
var name Name
fmt.Printf("Enriching principal index...\n")
for err = r.Read(&name); err != io.EOF; err = r.Read(&name) {
if err != nil {
continue
}
principalNode, ok := graph[name.NConst]
// Skip if this refers to a title we skipped
if !ok {
continue
}
principalNode.name = name.PrimaryName
}
r = tsv.NewReader(episodes)
r.HasHeaderRow = true
r.UseHeaderNames = true
var episode Episode
for err = r.Read(&episode); err != io.EOF; err = r.Read(&episode) {
if err != nil {
continue
}
titleNode, ok := graph[episode.TConst]
// Skip if this refers to a title we skipped
if !ok {
continue
}
titleNode.parent = episode.ParentTConst
}
fmt.Printf("Performing graph search...\n")
path := find(graph, from, to, map[string]struct{}{}, 7)
if path == nil {
fmt.Printf("No connection found within 7 jumps\n")
return
}
fmt.Printf("%+v\n", path)
fmt.Printf("============================\n")
fmt.Printf("%s --> %s\n", graph[path[0]].name, graph[path[len(path)-1]].name)
fmt.Printf("============================\n")
for i := 1; i < len(path); i += 2 {
titleNode := graph[path[i]]
fmt.Printf("%s: %s (%s) --> %s (%s)\n", titleDescription(graph, path[i]), graph[path[i-1]].name, titleNode.job[path[i-1]], graph[path[i+1]].name, titleNode.job[path[i+1]])
}
}
func titleDescription(graph map[string]*node, tConst string) string {
node := graph[tConst]
if node.parent != "" {
parentNode := graph[node.parent]
return fmt.Sprintf("%s (%s)", parentNode.name, node.name)
}
return node.name
}
func find(graph map[string]*node, from, to string, visited map[string]struct{}, limit int) []string {
if from == to {
return []string{from}
} else if limit == 0 {
return nil
}
for n := range graph[from].neighbors {
if _, ok := visited[n]; ok {
continue
}
visited[from] = struct{}{}
path := find(graph, n, to, visited, limit-1)
if path != nil {
return append(path, from)
}
delete(visited, from)
}
return nil
}