-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_ids.cpp
182 lines (170 loc) · 5.65 KB
/
convert_ids.cpp
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
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <filesystem>
#include <windows.h>
namespace fs = std::filesystem;
using std::cout;
using std::cerr;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::string;
using std::stringstream;
using std::vector;
using std::pair;
using std::sort;
using std::unordered_set;
using std::unordered_map;
struct Options {
const char *dataset_file_path = "fulldocs-new.trec";
} options;
bool startswith(const char *str, const char *keyword, int len) {
return strncmp(str, keyword, len) == 0;
}
void print_usage(char *program_name) {
printf("Usage: %s [-h] [-d dataset_file_path]\n"
"Options:\n"
"\t-d\tdataset path, default: fulldocs-new.trec\n"
"\t-h\thelp\n", program_name);
}
void parse_args(int argc, char *argv[]) {
for (int i = 1; i < argc; i += 2) {
char *option = argv[i];
if (strcmp(option, "-h") == 0) {
print_usage(argv[0]);
exit(EXIT_SUCCESS);
}
if (i + 1 < argc) {
char *value = argv[i + 1];
if (strcmp(option, "-d") == 0) options.dataset_file_path = value;
else {
cerr << "Unknown option: " << option << endl;
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
} else {
cerr << "Missing value for option: " << argv[i] << endl;
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
}
}
int main(int argc, char *argv[]) {
auto start = std::chrono::steady_clock::now();
parse_args(argc, argv);
// Map the large dataset file to memory
HANDLE dataset_file = CreateFileA(options.dataset_file_path, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (dataset_file == INVALID_HANDLE_VALUE) {
fprintf(stderr, "failed to open dataset file\n");
exit(EXIT_FAILURE);
}
LARGE_INTEGER dataset_file_size_li;
if (!GetFileSizeEx(dataset_file, &dataset_file_size_li)) {
fprintf(stderr, "failed to get dataset file size\n");
exit(EXIT_FAILURE);
}
long long dataset_file_size = dataset_file_size_li.QuadPart;
HANDLE dataset_file_mapping = CreateFileMapping(dataset_file, nullptr, PAGE_READONLY, 0, 0, nullptr);
if (dataset_file_mapping == nullptr) {
fprintf(stderr, "failed to create dataset file mapping\n");
exit(EXIT_FAILURE);
}
const char *buffer = (char *) MapViewOfFile(dataset_file_mapping, FILE_MAP_READ, 0, 0, 0);
if (buffer == nullptr) {
fprintf(stderr, "failed to map dataset file to memory\n");
exit(EXIT_FAILURE);
}
const char *cur = buffer;
unordered_map<string, unsigned> ids;
for (unsigned doc_id = 0;; doc_id++) {
if (!startswith(cur, "<DOC>", 5)) {
fprintf(stderr, "unexpected format: <DOC> not found\n");
exit(EXIT_FAILURE);
}
cur += 5;
if (*cur++ != '\n') {
fprintf(stderr, "warning: <DOC> not followed by newline\n");
cur--;
}
if (!startswith(cur, "<DOCNO>", 7)) {
fprintf(stderr, "unexpected format: <DOCNO> not found\n");
exit(EXIT_FAILURE);
}
cur += 7;
const char *docno_begin = cur;
while (*cur++ != '<') {
}
cur--;
const char *docno_end = cur;
string s = string(docno_begin, docno_end);
ids[string(docno_begin, docno_end)] = doc_id;
if (!startswith(cur, "</DOCNO>", 8)) {
fprintf(stderr, "unexpected format: </DOCNO> not found\n");
exit(EXIT_FAILURE);
}
cur += 8;
if (*cur++ != '\n') {
fprintf(stderr, "warning: <DOC> not followed by newline\n");
cur--;
}
if (!startswith(cur, "<TEXT>", 6)) {
fprintf(stderr, "unexpected format: <TEXT> not found\n");
exit(EXIT_FAILURE);
}
cur += 6;
while (!startswith(cur, "</TEXT>", 7)) {
cur++;
}
cur += 7;
if (*cur++ != '\n') {
fprintf(stderr, "warning: </TEXT> not followed by newline\n");
cur--;
}
if (!startswith(cur, "</DOC>", 6)) {
fprintf(stderr, "unexpected format: </DOC> not found\n");
exit(EXIT_FAILURE);
}
cur += 6;
if (*cur++ != '\n') {
fprintf(stderr, "warning: </DOC> not followed by newline\n");
cur--;
}
if (cur - buffer >= dataset_file_size) {
break;
}
if (doc_id % 10000 == 0) {
cout << "processed " << doc_id << " documents" << endl;
}
}
ifstream qrels("msmarco-doctrain-qrels.tsv");
if (!qrels.is_open()) {
fprintf(stderr, "failed to open qrels file\n");
exit(EXIT_FAILURE);
}
ofstream qrels_new("msmarco-doctrain-qrels-idconverted.tsv");
if (!qrels_new.is_open()) {
fprintf(stderr, "failed to open new qrels file\n");
exit(EXIT_FAILURE);
}
string line;
while (getline(qrels, line)) {
stringstream ss(line);
string qid, _, docno;
ss >> qid >> _ >> docno >> _;
qrels_new << qid << " " << ids[docno] << endl;
}
qrels.close();
qrels_new.close();
// Unmap the dataset file
UnmapViewOfFile(buffer);
CloseHandle(dataset_file_mapping);
CloseHandle(dataset_file);
auto end = std::chrono::steady_clock::now();
cout << "total time used " << std::chrono::duration_cast<std::chrono::seconds>(end - start).count() << "s" << endl;
return 0;
}