This repository was archived by the owner on Oct 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.cpp
More file actions
149 lines (124 loc) · 3.54 KB
/
parser.cpp
File metadata and controls
149 lines (124 loc) · 3.54 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
/**
* Builds the XTrie
* @author Francisco Claude
*/
#include <libxml++/libxml++.h>
#include <libxml++/parsers/textreader.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <algorithm>
#include <cassert>
#include <basics.h>
#include "NaiveTrie.h"
#include "Trie.h"
using namespace std;
using namespace xmlpp;
/* Time meassuring */
double ticks= (double)sysconf(_SC_CLK_TCK);
struct tms t1,t2;
void start_clock() {
times (&t1);
}
double stop_clock() {
times (&t2);
return (t2.tms_utime-t1.tms_utime)/ticks;
}
/* end Time meassuring */
uint fake_id = 1;
map<string, int> ids;
int max_id = 1;
NaiveTrie trie;
uint getId(const string & tag) {
uint ret = ids[tag];
if(ret==0) {
ids[tag] = max_id++;
return max_id-1;
} else {
return ret;
}
}
void saveDictionary(string fname) {
ofstream out(fname.c_str());
assert(out.good());
out << ids.size() << endl;
for(map<string,int>::iterator iter = ids.begin(); iter != ids.end(); ++iter)
out << iter->first << " " << iter->second << endl;
out.close();
}
class MySaxParser : public SaxParser {
protected:
vector<uint> path;
public:
MySaxParser() : SaxParser() {}
virtual ~MySaxParser() {}
protected:
virtual void on_start_document() {}
virtual void on_end_document() {}
virtual void on_start_element(const Glib::ustring & name,
const AttributeList & properties) {
path.push_back(getId(name));
vector<uint> rpath;
rpath.insert(rpath.end(),path.rbegin(),path.rend());
trie.insertPath(rpath,fake_id++);
rpath.insert(rpath.begin(),0);
for(AttributeList::const_iterator iter=properties.begin(); iter!=properties.end(); ++iter) {
rpath[0] = getId(iter->name);
trie.insertPath(rpath,fake_id++);
}
}
virtual void on_end_element(const Glib::ustring & name) {
assert(getId(name)==path[path.size()-1]);
path.pop_back();
}
virtual void on_characters(const Glib::ustring & characters) {}
virtual void on_comment(const Glib::ustring & text) {}
virtual void on_warning(const Glib::ustring & text) {}
virtual void on_error(const Glib::ustring & text) {}
virtual void on_fatal_error(const Glib::ustring & text) {
cerr << "Fatal error parsing XML file" << endl;
exit(-1);
}
};
int main(int argc, char* argv[])
{
string filepath, fileout;
if(argc == 3) {
filepath = argv[1];
fileout = argv[2];
} else {
cout << "usage: " << argv[0] << " <input file> <output base name>" << endl;
return 0;
}
try {
cout << "Parsing XML file." << endl;
start_clock();
ifstream xmlFile(filepath.c_str());
char buffer[1024*1024*8];
MySaxParser parser;
parser.set_substitute_entities(false);
parser.set_validate(false);
do {
xmlFile.read(buffer,63);
Glib::ustring input(buffer, xmlFile.gcount());
parser.parse_chunk(input);
} while(xmlFile);
cout << "Finished parsing XML file: " << 1000.*stop_clock() << "ms." << endl;
cout << "Building Trie." << endl;
start_clock();
Trie t(&trie);
cout << "Finished building Trie: " << 1000.*stop_clock() << "ms." << endl;
cout << "Saving files." << endl;
start_clock();
ofstream out((fileout+".index").c_str(),ios::binary);
t.save(out);
out.close();
saveDictionary(fileout+".dict");
cout << "Finished saving files: " << 1000.*stop_clock() << "ms." << endl;
} catch(const std::exception& ex) {
cout.flush();
cout << "Exception caught: " << ex.what() << std::endl;
}
return 0;
}