-
Notifications
You must be signed in to change notification settings - Fork 58
/
backward.go
183 lines (154 loc) · 4.14 KB
/
backward.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
farm "github.com/dgryski/go-farm"
"github.com/dgraph-io/dgraph/x"
)
const (
filename = "/home/jchiu/dgraphtest/goldendata"
)
var (
invGraph map[string]map[uint64][]uint64
goodUIDs []uint64
)
func bracketed(s string) bool {
return strings.HasPrefix(s, "<") && strings.HasSuffix(s, ">")
}
func removeFirstLast(s string) string {
return s[1 : len(s)-1]
}
// uniqueUIDs return the unique UIDs in the values of map m. It also returns
// a map from unique UIDs to their counts, i.e., the number of times they
// appear in the values of map m.
func uniqueUIDs(m map[uint64][]uint64) ([]uint64, []int) {
uniq := make(map[uint64]int)
for _, v := range m {
for _, u := range v {
uniq[u]++
}
}
out := make([]uint64, 0, len(uniq))
outCount := make([]int, 0, len(uniq))
for k, v := range uniq {
out = append(out, k)
outCount = append(outCount, v)
}
return out, outCount
}
func expand(uids []uint64) map[string]map[uint64][]uint64 {
x.AssertTrue(invGraph != nil)
out := make(map[string]map[uint64][]uint64)
for pred, m := range invGraph {
outM := make(map[uint64][]uint64)
for _, u := range uids { // srcUID.
z := m[u]
if z == nil {
continue
}
outM[u] = z
}
if len(outM) > 0 {
out[pred] = outM
}
}
return out
}
func main() {
x.Init()
fin, err := os.Open(filename)
x.Check(err)
defer fin.Close()
scanner := bufio.NewScanner(fin)
var numLines, numValues, numNames, numReleaseDates int
invGraph = make(map[string]map[uint64][]uint64)
for scanner.Scan() {
numLines++
tokens := strings.Split(scanner.Text(), "\t")
x.AssertTruef(len(tokens) == 4, scanner.Text())
src := tokens[0]
x.AssertTrue(bracketed(src))
src = removeFirstLast(src)
srcUID := farm.Fingerprint64([]byte(src))
pred := tokens[1]
x.AssertTrue(bracketed(pred))
pred = removeFirstLast(pred)
value := tokens[2]
if bracketed(value) {
// Normal edge.
value = removeFirstLast(value)
destUID := farm.Fingerprint64([]byte(value))
m, found := invGraph[pred]
if !found {
m = make(map[uint64][]uint64)
invGraph[pred] = m
}
// We are building an inverse map!
m[destUID] = append(m[destUID], srcUID)
} else {
// A value.
numValues++
value = removeFirstLast(value)
if pred == "type.object.name" {
numNames++
// Do some custom processing here.
value = strings.ToLower(value)
vTokens := strings.Split(value, " ")
var found bool
for _, t := range vTokens {
if t == "the" {
found = true
break
}
}
if found {
goodUIDs = append(goodUIDs, srcUID)
}
} else if pred == "film.film.initial_release_date" {
numReleaseDates++
}
}
}
fmt.Printf("Num lines read: %d\n", numLines)
fmt.Printf("Num predicates: %d\n", len(invGraph))
fmt.Printf("Num values read: %d\n", numValues)
fmt.Printf("Num names read: %d\n", numNames)
fmt.Printf("Num release dates read: %d\n", numReleaseDates)
fmt.Printf("Num good UIDs: %d\n", len(goodUIDs))
x.AssertTrue(numLines > 0)
x.AssertTrue(len(invGraph) > 0)
x.AssertTrue(numValues > 0)
x.AssertTrue(numNames > 0)
x.AssertTrue(numReleaseDates > 0)
x.AssertTrue(len(goodUIDs) > 0)
doGood()
}
func doGood() {
// goodUIDs are UIDs with type.object.name containing "good".
results := expand(goodUIDs)
directorEdges := results["film.director.film"]
x.AssertTrue(len(directorEdges) > 100)
// Directors are UIDs which go to goodUIDs via "film.director.film".
directorUIDs, _ := uniqueUIDs(directorEdges)
results = expand(directorUIDs)
filmEdges := results["film.film.directed_by"]
x.AssertTrue(len(filmEdges) > 100)
// Films are UIDs which go to directorUIDs via "film.film.directed_by".
filmUIDs, _ := uniqueUIDs(filmEdges)
results = expand(filmUIDs)
directorEdges = results["film.director.film"]
x.AssertTrue(len(directorEdges) > 100)
// Directors are UIDs which go to filmUIDs via "film.director.film".
directorUIDs, counts := uniqueUIDs(directorEdges)
var maxCount int
var maxDirector uint64
for i, c := range counts {
if c > maxCount {
maxCount = c
maxDirector = directorUIDs[i]
}
}
fmt.Printf("maxDirector %d with count %d\n", maxDirector, maxCount)
}