JingyuanHe1222
commited on
Commit
·
2155f76
1
Parent(s):
e29c412
update file structures
Browse files- README.md +25 -0
- cw_data_processing/ClueWeb22Api.py +143 -0
- cw_data_processing/example_dataset.py +56 -0
- test_inter_input.tsv → interaction_splits/test_inter_input.tsv +0 -0
- valid_inter_input.tsv → interaction_splits/valid_inter_input.tsv +0 -0
- valid_inter_target.tsv → interaction_splits/valid_inter_target.tsv +0 -0
- test_input.tsv → ordered_id_splits/test_input.tsv +0 -0
- valid_input.tsv → ordered_id_splits/valid_input.tsv +0 -0
- valid_target.tsv → ordered_id_splits/valid_target.tsv +0 -0
README.md
CHANGED
@@ -2,6 +2,31 @@
|
|
2 |
license: mit
|
3 |
---
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# ClueWeb-Reco:
|
7 |
|
|
|
2 |
license: mit
|
3 |
---
|
4 |
|
5 |
+
---
|
6 |
+
configs:
|
7 |
+
- config_name: ordered_id_splits
|
8 |
+
data_files:
|
9 |
+
- split: train
|
10 |
+
path:
|
11 |
+
- "ordered_id_splits/valid_input.tsv"
|
12 |
+
- "ordered_id_splits/valid_target.tsv"
|
13 |
+
- "ordered_id_splits/test_input.tsv"
|
14 |
+
- split: test
|
15 |
+
path: "ordered_id_splits/ghi.csv"
|
16 |
+
default: true
|
17 |
+
- config_name: interaction_splits
|
18 |
+
data_files:
|
19 |
+
- split: valid
|
20 |
+
path:
|
21 |
+
- "interaction_splits/valid_inter_input.tsv"
|
22 |
+
- "interaction_splits/valid_inter_target.csv"
|
23 |
+
- split: test
|
24 |
+
path: "interaction_splits/test_input.tsv"
|
25 |
+
- config_name: clueweb_id_mapping
|
26 |
+
data_files: "cwid_to_id.tsv"
|
27 |
+
---
|
28 |
+
|
29 |
+
|
30 |
|
31 |
# ClueWeb-Reco:
|
32 |
|
cw_data_processing/ClueWeb22Api.py
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gzip
|
3 |
+
import zipfile
|
4 |
+
|
5 |
+
class ClueWeb22Api:
|
6 |
+
|
7 |
+
"""
|
8 |
+
ClueWeb22Api adapted from source: https://github.com/lemurproject/ClueWeb22/blob/main/ClueWeb22Api.py
|
9 |
+
"""
|
10 |
+
|
11 |
+
def __init__(self, cw22id, cw22root_path):
|
12 |
+
self.cw22id = cw22id
|
13 |
+
self.cw22root_path = cw22root_path
|
14 |
+
|
15 |
+
def get_base_filename_by_id(self, cw22id, cw22root_path, file_type='html'):
|
16 |
+
# /data/datasets/clueweb22/ClueWeb22_B/txt
|
17 |
+
html_path = self.cw22root_path + os.sep + file_type
|
18 |
+
|
19 |
+
# clueweb22-en00{subfolder_id}-{jjsongz_id}-{ddoc_id}"
|
20 |
+
id_parts = cw22id.split('-')
|
21 |
+
doc = int(id_parts[len(id_parts) - 1])
|
22 |
+
|
23 |
+
language = id_parts[1][:2] # en
|
24 |
+
segment = id_parts[1][:4] # en00
|
25 |
+
directory = id_parts[1] # en0000
|
26 |
+
# /data/datasets/clueweb22/ClueWeb22_B/txt + en + en00 + en0000
|
27 |
+
base_path = html_path + os.sep + language + os.sep + segment + os.sep + directory + os.sep
|
28 |
+
base_filename = base_path + id_parts[1] + '-' + id_parts[2]
|
29 |
+
return base_filename
|
30 |
+
|
31 |
+
def get_primary_node_ids(self, annotate_html):
|
32 |
+
annotations = annotate_html.annotations
|
33 |
+
primary_node_ids = []
|
34 |
+
for annotation in annotations:
|
35 |
+
if annotation.type == AnnotateHtml.AnnotationType.Primary:
|
36 |
+
primary_node_ids.append(int(annotation.nodeId))
|
37 |
+
primary_node_ids.sort()
|
38 |
+
return primary_node_ids
|
39 |
+
|
40 |
+
def get_html_from_warc(self):
|
41 |
+
cw22id = self.cw22id
|
42 |
+
cw22root_path = self.cw22root_path
|
43 |
+
base_filename = self.get_base_filename_by_id(cw22id, cw22root_path)
|
44 |
+
|
45 |
+
warc_path = base_filename + '.warc.gz'
|
46 |
+
offset_path = base_filename + '.warc.offset'
|
47 |
+
|
48 |
+
id_parts = cw22id.split('-')
|
49 |
+
doc = int(id_parts[len(id_parts) - 1])
|
50 |
+
|
51 |
+
#Get html from warc using offset
|
52 |
+
offset_length = len('{:010d}\n'.format(0, 0))
|
53 |
+
with open (warc_path,'rb') as f_warc:
|
54 |
+
with open (offset_path, 'r') as f_offset:
|
55 |
+
f_offset.seek(int(doc) * int(offset_length))
|
56 |
+
start_bytes = int (f_offset.read (offset_length).strip())
|
57 |
+
end_bytes = int (f_offset.read (offset_length).strip())
|
58 |
+
f_warc.seek(start_bytes)
|
59 |
+
record = f_warc.read(end_bytes - start_bytes)
|
60 |
+
record = gzip.decompress(record).decode('utf-8')
|
61 |
+
|
62 |
+
#Remove the WARC header to get the htmlStr
|
63 |
+
warc_header = ''
|
64 |
+
for line in record.splitlines():
|
65 |
+
warc_header += line
|
66 |
+
warc_header += '\r\n'
|
67 |
+
if len(line.strip()) == 0:
|
68 |
+
break
|
69 |
+
record = record[len(warc_header):]
|
70 |
+
|
71 |
+
return record
|
72 |
+
|
73 |
+
|
74 |
+
|
75 |
+
def get_json_record(self, record_type):
|
76 |
+
cw22id = self.cw22id
|
77 |
+
cw22root_path = self.cw22root_path
|
78 |
+
base_filename = self.get_base_filename_by_id(cw22id, cw22root_path, file_type=record_type)
|
79 |
+
json_path = base_filename + '.json.gz'
|
80 |
+
offset_path = base_filename + '.offset'
|
81 |
+
|
82 |
+
id_parts = cw22id.split('-')
|
83 |
+
doc = int(id_parts[len(id_parts) - 1])
|
84 |
+
|
85 |
+
offset_length = len('{:010d}\n'.format(0, 0))
|
86 |
+
with open (json_path,'rb') as f_json:
|
87 |
+
with open (offset_path, 'r') as f_offset:
|
88 |
+
f_offset.seek(int(doc) * int(offset_length))
|
89 |
+
start_bytes = int (f_offset.read (offset_length).strip())
|
90 |
+
end_bytes = int (f_offset.read (offset_length).strip())
|
91 |
+
f_json.seek(start_bytes)
|
92 |
+
record = f_json.read(end_bytes - start_bytes)
|
93 |
+
record = gzip.decompress(record).decode('utf-8')
|
94 |
+
return record
|
95 |
+
|
96 |
+
|
97 |
+
def get_clean_text(self):
|
98 |
+
record = self.get_json_record('txt')
|
99 |
+
return record
|
100 |
+
|
101 |
+
def get_inlinks(self):
|
102 |
+
record = self.get_json_record('inlink')
|
103 |
+
return record
|
104 |
+
|
105 |
+
def get_outlinks(self):
|
106 |
+
record = self.get_json_record('outlink')
|
107 |
+
return record
|
108 |
+
|
109 |
+
|
110 |
+
def get_marcoweb_clean_text(self):
|
111 |
+
record = self.get_marcoweb_json_record('txt')
|
112 |
+
return record
|
113 |
+
|
114 |
+
def get_marcoweb_json_record(self, record_type):
|
115 |
+
cw22id = self.cw22id
|
116 |
+
cw22root_path = self.cw22root_path
|
117 |
+
|
118 |
+
def get_base_filename():
|
119 |
+
html_path = cw22root_path + os.sep
|
120 |
+
id_parts = cw22id.split('-') # "clueweb22-en-{jjsongz_id}-{ddoc_id}"
|
121 |
+
language = id_parts[1]
|
122 |
+
base_path = html_path + os.sep + language + os.sep
|
123 |
+
base_filename = base_path + id_parts[1] + '-' + id_parts[2]
|
124 |
+
return base_filename
|
125 |
+
|
126 |
+
# custom file path
|
127 |
+
base_filename = get_base_filename()
|
128 |
+
json_path = base_filename + '.json.gz'
|
129 |
+
offset_path = base_filename + '.offset'
|
130 |
+
|
131 |
+
id_parts = cw22id.split('-')
|
132 |
+
doc = int(id_parts[len(id_parts) - 1])
|
133 |
+
|
134 |
+
offset_length = len('{:010d}\n'.format(0, 0))
|
135 |
+
with open (json_path,'rb') as f_json:
|
136 |
+
with open (offset_path, 'r') as f_offset:
|
137 |
+
f_offset.seek(int(doc) * int(offset_length))
|
138 |
+
start_bytes = int (f_offset.read (offset_length).strip())
|
139 |
+
end_bytes = int (f_offset.read (offset_length).strip())
|
140 |
+
f_json.seek(start_bytes)
|
141 |
+
record = f_json.read(end_bytes - start_bytes)
|
142 |
+
record = gzip.decompress(record).decode('utf-8')
|
143 |
+
return record
|
cw_data_processing/example_dataset.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from ClueWeb22Api import ClueWeb22Api
|
2 |
+
from tqdm import tqdm
|
3 |
+
|
4 |
+
|
5 |
+
def get_clueweb_title(cwid, clueweb_path):
|
6 |
+
clueweb_api = ClueWeb22Api(cwid, clueweb_path)
|
7 |
+
clean_txt = eval(clueweb_api.get_clean_text())
|
8 |
+
content = clean_txt["Clean-Text"]
|
9 |
+
title = content.split('\n')[0].replace("\n", "").replace("\t", "").replace("\r", "").replace("\'", "").replace("\"", "").strip()
|
10 |
+
return title
|
11 |
+
|
12 |
+
|
13 |
+
def load_data_clueweb(filename, id_map_path, clueweb_path):
|
14 |
+
"""
|
15 |
+
args:
|
16 |
+
filename: the validation set input (valid.tsv) or the test set input (test.tsv)
|
17 |
+
id_map_path: the mapping from official clueweb id to our internal clueweb id (cwid_to_id.tsv)
|
18 |
+
clueweb_path: your clueweb dataset path on disk (ex. /../data/datasets/clueweb22/ClueWeb22_B)
|
19 |
+
"""
|
20 |
+
|
21 |
+
# load mapping to official clueweb doc ids
|
22 |
+
id_to_cwid = {}
|
23 |
+
with open(id_map_path, "r") as f:
|
24 |
+
for line in f:
|
25 |
+
parts = line.strip().split("\t")
|
26 |
+
id_to_cwid[int(parts[1])] = parts[0]
|
27 |
+
|
28 |
+
|
29 |
+
data = {}
|
30 |
+
lines = open(filename, 'r').readlines()
|
31 |
+
for line in tqdm(lines[1:]):
|
32 |
+
history_titles = list()
|
33 |
+
line = line.strip().split('\t')
|
34 |
+
|
35 |
+
session_id = line[0]
|
36 |
+
history = line[1].split(",")
|
37 |
+
|
38 |
+
for internal_id in history:
|
39 |
+
internal_id = int(internal_id)
|
40 |
+
# get the title of the item by ClueWebApi
|
41 |
+
title = get_clueweb_title(id_to_cwid[internal_id], clueweb_path)
|
42 |
+
history_titles.append(title)
|
43 |
+
|
44 |
+
data[session_id] = history_titles
|
45 |
+
|
46 |
+
return data
|
47 |
+
|
48 |
+
|
49 |
+
id_map_path="ClueWeb-Reco/cwid_to_id.tsv"
|
50 |
+
seq_data_path="ClueWeb-Reco_public/ordered_id_splits/valid_input.tsv"
|
51 |
+
clueweb_path="/data/datasets/clueweb22/ClueWeb22_B"
|
52 |
+
|
53 |
+
# dictionary with session_ids as key, list of strings of the titles of historically interacted item
|
54 |
+
data = load_data_clueweb(filename=seq_data_path, id_map_path=id_map_path, clueweb_path=clueweb_path)
|
55 |
+
|
56 |
+
|
test_inter_input.tsv → interaction_splits/test_inter_input.tsv
RENAMED
File without changes
|
valid_inter_input.tsv → interaction_splits/valid_inter_input.tsv
RENAMED
File without changes
|
valid_inter_target.tsv → interaction_splits/valid_inter_target.tsv
RENAMED
File without changes
|
test_input.tsv → ordered_id_splits/test_input.tsv
RENAMED
File without changes
|
valid_input.tsv → ordered_id_splits/valid_input.tsv
RENAMED
File without changes
|
valid_target.tsv → ordered_id_splits/valid_target.tsv
RENAMED
File without changes
|