-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreconstruct.cpp
167 lines (136 loc) · 4.54 KB
/
reconstruct.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
//
// score_string.cpp
// PST
//
// Created by Niklas Alanko on 24/04/2017.
// Copyright © 2017 University of Helsinki. All rights reserved.
//
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <ctime>
#include <chrono>
#include <streambuf>
#include "String_Depth_Support.hh"
#include "Parent_Support.hh"
#include "LMA_Support.hh"
#include "BPR_tools.hh"
#include "Precalc.hh"
#include "score_string.hh"
#include "build_model.hh"
#include "logging.hh"
#include "globals.hh"
using namespace std;
string read_raw_file(string filename){
std::ifstream t(filename.c_str());
std::string str;
t.seekg(0, std::ios::end);
str.reserve(t.tellg());
t.seekg(0, std::ios::beg);
str.assign((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
return str;
}
class Reconstruction_Config{
private:
Reconstruction_Config(const Reconstruction_Config&); // Prevent copy-construction
Reconstruction_Config& operator=(const Reconstruction_Config&); // Prevent assignment
public:
bool context_stats;
bool only_maxreps;
bool run_length_coding;
int64_t depth_bound;
Context_Callback* cf;
string modeldir;
string filename;
Reconstruction_Config() : context_stats(false), only_maxreps(false), run_length_coding(false), depth_bound(-1), cf(nullptr) {}
void assert_all_ok(){
assert(modeldir != "");
assert(filename != "");
assert(cf != nullptr);
assert(depth_bound != -1);
}
void load_info_file(){
assert(modeldir != "");
assert(filename != "");
string path = modeldir + "/" + filename + ".info";
ifstream file(path);
string ctype; // Old context type. Unused
file >> only_maxreps >> ctype >> run_length_coding >> depth_bound;
if(!file.good()){
cerr << "Error reading file: " << path << endl;
exit(-1);
}
}
};
int score_string_main(int argc, char** argv){
if(argc < 4){
cerr << "Marks contexts again." << endl;
cerr << "Usage: see README.md" << endl;
return -1;
}
Reconstruction_Config C;
for(int64_t i = 1; i < argc; i++){
if(argv[i] == string("--dir")){
i++;
C.modeldir = argv[i];
} else if(argv[i] == string("--file")){
i++;
C.filename = argv[i];
} else if(argv[i] == string("--entropy")){
i++;
double threshold = stod(argv[i]);
C.cf = new Entropy_Formula(threshold);
} else if(argv[i] == string("--KL")){
i++;
double threshold = stod(argv[i]);
C.cf = new KL_Formula(threshold);
} else if(argv[i] == string("--pnorm")){
i++;
int64_t p = stoi(argv[i]);
i++;
double threshold = stod(argv[i]);
C.cf = new pnorm_Formula(p, threshold);
} else if(argv[i] == string("--four-thresholds")){
double t1,t2,t3,t4;
i++; t1 = stod(argv[i]);
i++; t2 = stod(argv[i]);
i++; t3 = stod(argv[i]);
i++; t4 = stod(argv[i]);
C.cf = new EQ234_Formula(t1,t2,t3,t4);
} else if(argv[i] == string("--context-stats")){
C.context_stats = true;
} else{
cerr << "Invalid argument: " << argv[i] << endl;
return -1;
}
}
C.load_info_file();
C.assert_all_ok();
write_log("Loading the model from " + C.modeldir);
Global_Data G;
G.load_all_from_disk(C.modeldir, C.filename, true);
write_log("Starting to rebuild contexts");
Depth_Bounded_SLT_Iterator iterator (G.bibwt.get(), C.depth_bound);
Pruned_Topology_Mapper mapper(G.rev_st_bpr, G.pruning_marks);
Stats_writer wr;
if(C.context_stats){
wr.set_file(C.modeldir + "/stats.depths_and_scores.txt");
}
C.cf->init(G.bibwt.get(), G.rev_st_bpr->size(), mapper, &wr);
iterate_with_callback(iterator, C.cf);
G.rev_st_context_marks = make_shared<Basic_bitvector>(C.cf->get_result());
G.rev_st_context_marks->init_rank_support();
G.rev_st_context_marks->init_select_support();
if(C.context_stats){
write_context_summary(G, C.cf->get_number_of_candidates(), C.modeldir + "/stats.context_summary.txt");
}
G.store_all_to_disk(C.modeldir, C.filename);
write_log("Done");
return 0;
}
int main(int argc, char** argv){
return score_string_main(argc,argv);
}