-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartition_triples.rs
More file actions
264 lines (242 loc) · 10.6 KB
/
Copy pathpartition_triples.rs
File metadata and controls
264 lines (242 loc) · 10.6 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
// Copyright 2021 Daniel Harrison. All Rights Reserved.
use std::collections::hash_map::DefaultHasher;
use std::collections::HashMap;
use std::error::Error;
use std::fs::File;
use std::hash::{Hash, Hasher};
use std::io::{self, BufRead, Write};
use std::path::Path;
use flate2::write::GzEncoder;
use flate2::Compression;
use crate::ntriple::{Literal, NTriple, Object, ParseFile, Parser, Subject, TripleURI};
#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
struct IDHash(u64);
#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
struct BaseHash(u64);
fn hash(s: &str) -> u64 {
let mut h = DefaultHasher::new();
s.hash(&mut h);
h.finish()
}
struct IDsByBase {
id_to_base: HashMap<IDHash, BaseHash>,
base_to_str: HashMap<BaseHash, String>,
str_to_base: HashMap<String, BaseHash>,
}
pub fn sort_triples<P: AsRef<Path>>(
ids_file: P,
all_file: P,
output_base: P,
) -> Result<(), Box<dyn Error>> {
let mut ids_file = ParseFile::new(ids_file)?;
let mut all_file = ParseFile::new(all_file)?;
let output_base = output_base.as_ref();
let mut output_by_base: HashMap<String, GzEncoder<File>> = HashMap::new();
let output_fn = |base: &str, triple: &NTriple| -> io::Result<()> {
if let Some(w) = output_by_base.get_mut(base) {
writeln!(w, "{}", triple.raw())
} else {
let mut path = output_base.to_path_buf();
path.push(base);
path.set_extension("nt.gz");
let w = File::create(&path).map_err(|err| {
println!("creating {:?}: {}", &path, err);
err
})?;
let mut w = GzEncoder::new(w, Compression::default());
writeln!(w, "{}", triple.raw())?;
output_by_base.insert(base.to_string(), w);
Ok(())
}
};
sort_triples_fn(&mut ids_file, &mut all_file, output_fn)?;
let finished = output_by_base.drain().map(|(_, w)| w.finish());
for finished in finished.map(|f_res| f_res.map(|f| f.sync_all())) {
finished??;
}
Ok(())
}
fn sort_triples_fn<B: BufRead, F: FnMut(&str, &NTriple) -> io::Result<()>>(
ids_file: &mut ParseFile<B>,
all_file: &mut ParseFile<B>,
mut output_fn: F,
) -> Result<(), Box<dyn Error>> {
let ids_by_base = slurp_ids(ids_file)?;
Parser::new().parse(all_file, |triple| {
let base = match triple.sub() {
Subject::URI(TripleURI::Freebase(x)) => {
if x.starts_with("ns/m.") || x.starts_with("ns/g.") {
match ids_by_base.id_to_base.get(&IDHash(hash(x))) {
Some(base_hash) => match ids_by_base.base_to_str.get(base_hash) {
Some(base) => base.as_str(),
None => return Ok(()),
},
None => return Ok(()),
}
} else {
let base = x.trim_start_matches("ns/").split('.').next().expect("internal error");
base
}
}
x => {
eprintln!("{} sub={:?}", triple.raw(), x);
return Ok(());
}
};
let obj = triple.obj();
match triple.pred() {
TripleURI::W3(_) => return Ok(()),
TripleURI::Freebase(x) => {
if x == "ns/type.type.instance"
|| x.starts_with("key/base.")
|| x.starts_with("key/user.")
|| x.starts_with("ns/base.")
|| x.starts_with("ns/user.")
|| (x.starts_with("key/wikipedia.") && !x.starts_with("key/wikipedia.en"))
{
return Ok(());
} else if x == "ns/type.object.key" {
if let Object::Literal(Literal::Str(y)) = obj {
if y.starts_with("/base/") || y.starts_with("/user/") {
return Ok(());
} else if y.starts_with("/wikipedia/") && !y.starts_with("/wikipedia/en") {
return Ok(());
}
}
} else if x == "ns/common.topic.topic_equivalent_webpage" {
if let Object::URI(TripleURI::Absolute(y)) = obj {
if y.contains(".wikipedia.org/") && !y.contains("en.wikipedia.org/") {
return Ok(());
}
}
}
}
_ => {}
}
match obj {
Object::Literal(Literal::Lang { literal: _, lang }) => {
if lang != "en" {
return Ok(());
}
}
Object::URI(TripleURI::Freebase(x)) => {
if x.starts_with("ns/base") || x.starts_with("ns/user") {
return Ok(());
}
}
_ => {}
}
output_fn(base, &triple)?;
Ok(())
})?;
Ok(())
}
fn slurp_ids<B: BufRead>(ids_file: &mut ParseFile<B>) -> Result<IDsByBase, Box<dyn Error>> {
let parser = Parser::new();
let mut ret = IDsByBase {
id_to_base: HashMap::new(),
base_to_str: HashMap::new(),
str_to_base: HashMap::new(),
};
parser.parse(ids_file, |triple| {
match triple.pred() {
TripleURI::Freebase("ns/kg.object_profile.prominent_type") => {}
_ => return Ok(()),
}
let id_hash = match triple.sub() {
Subject::URI(TripleURI::Freebase(x)) => {
if !(x.starts_with("ns/m.") || x.starts_with("ns/g.")) {
return Ok(());
}
IDHash(hash(x))
}
_ => return Ok(()),
};
let base_hash = match triple.obj() {
Object::URI(TripleURI::Freebase(x)) => {
let base = x.trim_start_matches("ns/").split('.').next().expect("internal error");
if base == "base" || base == "user" {
return Ok(());
}
let base_hash = ret.str_to_base.get(base).map(|base| *base);
base_hash.unwrap_or_else(|| {
let base_hash = BaseHash(hash(base));
ret.str_to_base.insert(base.to_string(), base_hash);
ret.base_to_str.insert(base_hash, base.to_string());
base_hash
})
}
_ => return Ok(()),
};
ret.id_to_base.insert(id_hash, base_hash);
Ok(())
})?;
Ok(ret)
}
#[cfg(test)]
mod test {
use std::error::Error;
use std::io::{self};
use crate::ntriple::{NTriple, ParseFile};
#[test]
fn sort_triples() -> Result<(), Box<dyn Error>> {
// TODO: Tests for the freebase data dump specific modifications to the
// grammar.
let triples = r#"
# All triples for an ID with a prominent_type triple are kept (mod below
# filtering) and output under the "base" (first part of "book.book").
<http://rdf.freebase.com/ns/m.1> <http://rdf.freebase.com/ns/type.object.name> "One"@en .
<http://rdf.freebase.com/ns/m.1> <http://rdf.freebase.com/ns/kg.object_profile.prominent_type> <http://rdf.freebase.com/ns/book.book> .
<http://rdf.freebase.com/ns/m.2> <http://rdf.freebase.com/ns/type.object.name> "Two"@en .
<http://rdf.freebase.com/ns/m.2> <http://rdf.freebase.com/ns/kg.object_profile.prominent_type> <http://rdf.freebase.com/ns/film.film> .
# IDs without a prominent_type are filtered.
<http://rdf.freebase.com/ns/m.3> <http://rdf.freebase.com/ns/type.object.name> "Three"@en-gb .
# Non-english names are filtered.
<http://rdf.freebase.com/ns/m.1> <http://rdf.freebase.com/ns/type.object.name> "One GB"@en-gb .
# Objects in ns/base and ns/user are filtered.
<http://rdf.freebase.com/ns/m.1> <http://rdf.freebase.com/ns/type.object.type> <http://rdf.freebase.com/ns/base/foo> .
<http://rdf.freebase.com/ns/m.1> <http://rdf.freebase.com/ns/type.object.type> <http://rdf.freebase.com/ns/user/foo> .
# Predicates in {key,ns}/{base/user} are filtered.
<http://rdf.freebase.com/ns/m.1> <http://rdf.freebase.com/ns/base.foo> <http://rdf.freebase.com/ns/book.book> .
<http://rdf.freebase.com/ns/m.1> <http://rdf.freebase.com/ns/user.foo> <http://rdf.freebase.com/ns/book.book> .
<http://rdf.freebase.com/ns/m.1> <http://rdf.freebase.com/key/base.foo> <http://rdf.freebase.com/ns/book.book> .
<http://rdf.freebase.com/ns/m.1> <http://rdf.freebase.com/key/user.foo> <http://rdf.freebase.com/ns/book.book> .
# type.object.key to /base and /user and filtered.
<http://rdf.freebase.com/ns/m.1> <http://rdf.freebase.com/ns/type.object.key> "/base/one" .
<http://rdf.freebase.com/ns/m.1> <http://rdf.freebase.com/ns/type.object.key> "/user/one" .
# type.object.key to English Wikipedia is kept but non-English Wikipedia is filtered.
<http://rdf.freebase.com/ns/m.1> <http://rdf.freebase.com/ns/type.object.key> "/wikipedia/en/One" .
<http://rdf.freebase.com/ns/m.1> <http://rdf.freebase.com/ns/type.object.key> "/wikipedia/fr/Un" .
# Other type.object.keys are kept.
<http://rdf.freebase.com/ns/m.1> <http://rdf.freebase.com/ns/type.object.key> "/imdb/one" .
# topic_equivalent_webpage to English Wikipedia is kept but non-English Wikipedia is filtered.
<http://rdf.freebase.com/ns/m.1> <http://rdf.freebase.com/ns/common.topic.topic_equivalent_webpage> <http://en.wikipedia.org/wiki/One> .
<http://rdf.freebase.com/ns/m.1> <http://rdf.freebase.com/ns/common.topic.topic_equivalent_webpage> <http://fr.wikipedia.org/wiki/Un> .
# topic_equivalent_webpage to non-Wikipedia is kept.
<http://rdf.freebase.com/ns/m.1> <http://rdf.freebase.com/ns/common.topic.topic_equivalent_webpage> <http://one.com> .
"#.trim();
let mut ids_file =
ParseFile { buf: triples.as_bytes(), size: triples.len() as u64, gzipped: false };
let mut all_file =
ParseFile { buf: triples.as_bytes(), size: triples.len() as u64, gzipped: false };
let mut output = Vec::new();
let output_fn = |base: &str, triple: &NTriple| -> io::Result<()> {
output.push(format!("{}: {}", base, triple.raw().trim()));
Ok(())
};
super::sort_triples_fn(&mut ids_file, &mut all_file, output_fn)?;
let expected = vec![
r#"book: <http://rdf.freebase.com/ns/m.1> <http://rdf.freebase.com/ns/common.topic.topic_equivalent_webpage> <http://en.wikipedia.org/wiki/One> ."#.to_string(),
r#"book: <http://rdf.freebase.com/ns/m.1> <http://rdf.freebase.com/ns/common.topic.topic_equivalent_webpage> <http://one.com> ."#.to_string(),
r#"book: <http://rdf.freebase.com/ns/m.1> <http://rdf.freebase.com/ns/kg.object_profile.prominent_type> <http://rdf.freebase.com/ns/book.book> ."#.to_string(),
r#"book: <http://rdf.freebase.com/ns/m.1> <http://rdf.freebase.com/ns/type.object.key> "/imdb/one" ."#.to_string(),
r#"book: <http://rdf.freebase.com/ns/m.1> <http://rdf.freebase.com/ns/type.object.key> "/wikipedia/en/One" ."#.to_string(),
r#"book: <http://rdf.freebase.com/ns/m.1> <http://rdf.freebase.com/ns/type.object.name> "One"@en ."#.to_string(),
r#"film: <http://rdf.freebase.com/ns/m.2> <http://rdf.freebase.com/ns/kg.object_profile.prominent_type> <http://rdf.freebase.com/ns/film.film> ."#.to_string(),
r#"film: <http://rdf.freebase.com/ns/m.2> <http://rdf.freebase.com/ns/type.object.name> "Two"@en ."#.to_string(),
];
output.sort();
assert_eq!(expected, output);
Ok(())
}
}