-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilter_labels.py
79 lines (63 loc) · 2.47 KB
/
filter_labels.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
from tqdm import tqdm
from argparse import ArgumentParser
import gzip
import shlex
# Both methods are used together with a compressed (gz) n-triples Wikidata dump
def filter_labels_no_loading(
filepath: str, output_file: str = "predicate_labels.nt", total=1190348725
):
input_file = gzip.open(filepath)
output_file = open(output_file, "w")
line = input_file.readline()
pbar = tqdm(total=total)
while line:
pbar.update(1)
split_line = shlex.split(line)
if not len(split_line) >= 4 or not split_line[0].startswith("http://www.wikidata.org/entity/P"):
continue
if (
"http://www.w3.org/2004/02/skos/core#altLabel" in line
or "http://www.w3.org/2000/01/rdf-schema#label" in line
):
if (
"http://www.w3.org/2004/02/skos/core#altLabel" not in split_line[0]
and "http://www.w3.org/2000/01/rdf-schema#label" not in split_line[0]
):
output_file.write(line)
line = input_file.readline()
pbar.close()
def filter_labels_and_descriptions_no_loading(
filepath: str,
output_file_labels: str = "labels.nt",
output_file_descriptions="description.nt",
total=1325821109,
):
input_file = gzip.open(filepath)
output_labels_file = open(output_file_labels, "w")
output_descriptions_file = open(output_file_descriptions, "w")
line = input_file.readline()
pbar = tqdm(total=total)
while line:
pbar.update(1)
split_line = shlex.split(line)
if not len(split_line) >= 4 or not split_line[0].startswith("http://www.wikidata.org/entity/Q"):
continue
if (
"http://www.w3.org/2004/02/skos/core#altLabel" in line
or "http://www.w3.org/2000/01/rdf-schema#label" in line
):
if (
"http://www.w3.org/2004/02/skos/core#altLabel" not in split_line[0]
and "http://www.w3.org/2000/01/rdf-schema#label" not in split_line[0]
):
output_labels_file.write(line)
if "http://schema.org/description" in line:
if "http://schema.org/description" not in split_line[0]:
output_descriptions_file.write(line)
line = input_file.readline()
pbar.close()
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("filename", type=str)
args = parser.parse_args()
filter_labels_and_descriptions_no_loading(args.filename)