-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_test_subset.go
139 lines (127 loc) · 2.74 KB
/
create_test_subset.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
package main
import (
"bufio"
"compress/gzip"
"io"
"log"
"os"
"strings"
)
var m map[string]bool
var s []string
func zipLineIter(filename string, ch chan string) {
f, err := os.Open(filename)
if err != nil {
panic(err)
}
z, err := gzip.NewReader(bufio.NewReader(f))
if err != nil {
panic(err)
}
r := bufio.NewReader(z)
for line, _, err := r.ReadLine(); err == nil; line, _, err = r.ReadLine() {
ch <- string(line)
}
close(ch)
f.Close()
}
func searchRels(gzfile string) {
log.Printf("searching for related records\n")
ch := make(chan string, 1024*1024)
go zipLineIter(gzfile, ch)
count := 0
for line := range ch {
count++
if count > 5000000 {
// break
}
if count%100000000 == 0 {
log.Printf("searchRels has scanned %dM lines, triples: %d\n", count/1000000, len(s))
}
lines := strings.Split(line, "\t")
if len(lines) < 3 {
continue
}
if _, ok := m[lines[0]]; ok {
s = append(s, line)
} else if _, ok := m[lines[2]]; ok {
s = append(s, line)
}
}
}
func searchInstances(searchstring, gzfile string) {
log.Printf("searching for instances containing %s\n", searchstring)
ch := make(chan string, 1024*1024)
go zipLineIter(gzfile, ch)
count := 0
for line := range ch {
count++
if count > 400000000 {
break
}
if count%100000000 == 0 {
log.Printf("searchInstances has scanned %dM lines, found %d instances\n", count/1000000, len(m))
}
if len(line) == 0 {
continue
}
if strings.Contains(line, searchstring) &&
strings.Contains(line, "type.type.instance") {
lines := strings.Split(line, "\t")
if len(lines) < 3 {
break
}
m[lines[2]] = true
}
}
}
func filterNodes() {
log.Println("filtering to just nodes and types...")
newS := []string{}
for i, _ := range s {
lines := strings.Split(s[i], "\t")
if len(lines) < 3 {
break
}
if strings.Contains(lines[2], "<http://rdf.freebase.com/ns/m.") {
newS = append(newS, s[i])
m[lines[2]] = true
}
if strings.Contains(lines[0], "<http://rdf.freebase.com/ns/m.") {
newS = append(newS, s[i])
m[lines[0]] = true
}
}
s = newS
}
func outputSubset(outfile string) {
log.Println("writing to file...")
out, err := os.Create(outfile)
if err != nil {
log.Println(err)
}
defer out.Close()
for _, x := range s {
_, err := io.WriteString(out, x+"\n")
if err != nil {
panic(err)
}
}
}
func main() {
if len(os.Args) < 4 {
log.Fatal("not enough args given. usage:\n create_test_subset freebase-dump.gz <searchstring> <outputfile>\n")
}
freebase := os.Args[1]
searchstring := os.Args[2]
outfile := os.Args[3]
m = map[string]bool{}
s = []string{}
searchInstances(searchstring, freebase)
searchRels(freebase)
filterNodes()
searchRels(freebase)
filterNodes()
searchRels(freebase)
outputSubset(outfile)
}