-
Notifications
You must be signed in to change notification settings - Fork 0
/
documents.cpp
223 lines (178 loc) · 6.66 KB
/
documents.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright (c) 2021 Christopher Taylor
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <fstream>
#include <memory>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <cassert>
#include <unicode/unistr.h>
#include <unicode/ustream.h>
#include <unicode/ucnv.h>
#include <unicode/regex.h>
#include <blaze/Math.h>
#include <openssl/ssl.h>
#include "jch.hpp"
#include "documents.hpp"
#ifdef ICU69
using namespace icu_69;
#else
using namespace icu_66;
#endif
using blaze::DynamicMatrix;
std::size_t path_to_vector(fs::path const& p, std::vector<fs::path> & paths) {
for(auto& p : fs::recursive_directory_iterator(p)) {
paths.push_back(p);
}
return paths.size();
}
void read_content(fs::path const& p, std::vector<UnicodeString> & fcontent) {
std::ifstream istrm(p, std::ios::in | std::ios::binary);
UnicodeString rd;
while(istrm >> rd) {
fcontent.push_back( rd.toLower() );
}
istrm.close();
}
static void read_file_content(std::vector<fs::path>::iterator & beg, std::vector<fs::path>::iterator & end, std::vector<UnicodeString> & fcontent) {
for(auto pth = beg; pth != end; ++pth) {
std::ifstream istrm(*pth, std::ios::in | std::ios::binary);
const std::istream::pos_type curpos = istrm.tellg();
istrm.seekg(0, std::ifstream::end);
const std::istream::pos_type stream_sz = istrm.tellg() - curpos;
istrm.seekg(curpos);
std::string contents;
contents.resize(static_cast<std::size_t>(stream_sz));
istrm.read(&contents[0], stream_sz);
fcontent.push_back( UnicodeString::fromUTF8(contents).toLower() );
/*
UnicodeString content, rd;
while(istrm >> rd) {
content += (" " + rd.toLower());
}
fcontent.push_back( std::move(content) );
istrm.close();
*/
}
}
// https://unicode-org.github.io/icu-docs/apidoc/dev/icu4c/classicu_1_1UnicodeString.html
//
// https://github.com/jpakkane/cppunicode/blob/main/cppunicode.cpp
//
void inverted_index_to_matrix(std::unordered_map<std::string, std::size_t> const& voc, inverted_index_t const& idx, const std::size_t ndocs, CompressedMatrix<double> & mat, const bool debug) {
const std::size_t nvoc = voc.size();
mat.resize(nvoc, ndocs);
const auto voc_end = voc.end();
for(auto & w : idx) {
const auto& ventry = voc.find(w.first);
if(ventry != voc_end) {
const std::size_t docsz = w.second.size();
const std::size_t vocidx = ventry->second;
mat.reserve(vocidx, docsz);
for(const auto d : w.second) {
mat.append(vocidx, d.first, d.second);
}
mat.finalize(vocidx);
}
else if(debug) {
std::cerr << "word in corpus but not dictionary\t" << w.first << std::endl;
}
}
}
std::size_t document_path_to_inverted_index(std::vector<fs::path>::iterator & beg, std::vector<fs::path>::iterator & end, UnicodeString & regexp, inverted_index_t & ii, std::unordered_map<std::string, std::size_t> const& voc) {
std::vector<UnicodeString> files_content;
read_file_content(beg, end, files_content);
UErrorCode status = U_ZERO_ERROR;
std::unique_ptr<RegexMatcher> matcher{ new RegexMatcher(regexp, 0, status) };
std::size_t fc_count = 0;
std::size_t entry_count = 0;
const auto ii_end = ii.end();
const auto voc_end = voc.end();
for(auto & fc : files_content) {
matcher->reset(fc);
while(matcher->find()) {
const auto beg = matcher->start(status);
const auto end = matcher->end(status);
auto token = fc.tempSubString(beg, end-beg);
std::string matched_token;
token.toUTF8String(matched_token);
const auto voc_find = voc.find(matched_token);
if(voc_find != voc_end) {
const auto idx = ii.find(matched_token);
if( idx != ii_end ) {
if( idx->second.find(fc_count) != idx->second.end() ) {
++(idx->second[fc_count]);
}
else {
idx->second[fc_count] = 1;
++entry_count;
}
}
else {
ii[matched_token][fc_count] = 1;
++entry_count;
}
}
//else {
// std::cerr << "not found\t" << matched_token << std::endl;
//}
}
++fc_count;
}
return entry_count;
}
std::size_t document_path_to_inverted_index(std::vector<fs::path>::iterator & beg, std::vector<fs::path>::iterator & end, UnicodeString & regexp, inverted_index_t & ii) {
std::vector<UnicodeString> files_content;
read_file_content(beg, end, files_content);
UErrorCode status = U_ZERO_ERROR;
std::unique_ptr<RegexMatcher> matcher{ new RegexMatcher(regexp, 0, status) };
std::size_t fc_count = 0;
std::size_t entry_count = 0;
const auto ii_end = ii.end();
for(auto & fc : files_content) {
matcher->reset(fc);
while(matcher->find()) {
const auto beg = matcher->start(status);
const auto end = matcher->end(status);
auto token = fc.tempSubString(beg, end-beg);
std::string matched_token;
token.toUTF8String(matched_token);
const auto idx = ii.find(matched_token);
if( idx != ii_end ) {
if( idx->second.find(fc_count) != idx->second.end() ) {
++(idx->second[fc_count]);
}
else {
idx->second[fc_count] = 1;
++entry_count;
}
}
else {
ii[matched_token][fc_count] = 1;
++entry_count;
}
}
++fc_count;
}
return entry_count;
}
void matrix_to_vector(CompressedMatrix<double> const& mat, std::vector<std::size_t> & tokens) {
const std::size_t wcount = static_cast<std::size_t>(std::floor(blaze::sum(mat)));
tokens.resize(wcount);
std::fill(std::begin(tokens), std::end(tokens), 0);
}
std::size_t load_wordlist(fs::path const& pth, std::unordered_map<std::string, std::size_t> & vocab) {
std::vector<UnicodeString> voc;
read_content(pth, voc);
const std::size_t vcz = voc.size();
for(std::size_t i = 0; i < vcz; ++i) {
std::string tok;
voc[i].toUTF8String(tok);
vocab[tok] = i;
}
return vcz;
}