-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathreading_grid.cpp
More file actions
580 lines (501 loc) · 15.8 KB
/
Copy pathreading_grid.cpp
File metadata and controls
580 lines (501 loc) · 15.8 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
// Copyright (c) 2022 and onwards Lukhnos Liu.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#include "reading_grid.h"
#include <algorithm>
#include <chrono>
#include <string>
#include <utility>
#include <vector>
#include "walk_strategy.h"
namespace Formosa::Gramambular2 {
void ReadingGrid::clear() {
cursor_ = 0;
readings_.clear();
spans_.clear();
fixedSpans_.clear();
}
void ReadingGrid::setCursor(size_t cursor) {
assert(cursor <= readings_.size());
cursor_ = cursor;
}
void ReadingGrid::setReadingSeparator(const std::string& separator) {
separator_ = separator;
}
bool ReadingGrid::insertReading(const std::string& reading) {
if (reading.empty() || reading == separator_) {
return false;
}
if (!lm_.hasUnigrams(reading)) {
return false;
}
readings_.insert(readings_.begin() + static_cast<ptrdiff_t>(cursor_),
reading);
expandGridAt(cursor_);
update();
// Cursor must only move after update().
++cursor_;
return true;
}
bool ReadingGrid::deleteReadingBeforeCursor() {
if (!cursor_) {
return false;
}
readings_.erase(readings_.begin() + static_cast<ptrdiff_t>(cursor_ - 1),
readings_.begin() + static_cast<ptrdiff_t>(cursor_));
// Cursor must decrement for grid-shrinking and update to work.
--cursor_;
shrinkGridAt(cursor_);
update();
return true;
}
bool ReadingGrid::deleteReadingAfterCursor() {
if (cursor_ == readings_.size()) {
return false;
}
readings_.erase(readings_.begin() + static_cast<ptrdiff_t>(cursor_),
readings_.begin() + static_cast<ptrdiff_t>(cursor_ + 1));
shrinkGridAt(cursor_);
update();
return true;
}
std::optional<ReadingGrid::NodePtr> ReadingGrid::findInSpan(
size_t cursor, const std::function<bool(const NodePtr&)>& predicate) const {
assert(cursor <= readings_.size());
std::vector<ReadingGrid::NodeInSpan> nodes =
overlappingNodesAt(cursor == readings_.size() ? cursor - 1 : cursor);
auto nodesIt = std::find_if(
nodes.cbegin(), nodes.cend(),
[&](const NodeInSpan& nodeInSpan) { return predicate(nodeInSpan.node); });
return nodesIt == nodes.end()
? std::nullopt
: std::optional<ReadingGrid::NodePtr>(nodesIt->node);
}
namespace {
int64_t GetEpochNowInMicroseconds() {
auto now = std::chrono::system_clock::now();
int64_t timestamp =
std::chrono::time_point_cast<std::chrono::microseconds>(now)
.time_since_epoch()
.count();
return timestamp;
}
} // namespace
void ReadingGrid::setWalkStrategy(std::shared_ptr<WalkStrategy> strategy) {
walkStrategy_ = std::move(strategy);
}
void ReadingGrid::fixSpan(size_t position, NodePtr node) {
assert(node != nullptr);
assert(position < readings_.size());
assert(position + node->spanningLength() <= readings_.size());
size_t newEnd = position + node->spanningLength();
auto it = fixedSpans_.begin();
while (it != fixedSpans_.end()) {
size_t existStart = it->first;
size_t existEnd = existStart + it->second->spanningLength();
// Two spans overlap if their ranges intersect.
if (existStart < newEnd && position < existEnd) {
it = fixedSpans_.erase(it);
} else {
++it;
}
}
fixedSpans_[position] = std::move(node);
}
void ReadingGrid::clearFixedSpans() {
for (auto& [pos, node] : fixedSpans_) {
node->reset();
}
fixedSpans_.clear();
}
ReadingGrid::WalkResult ReadingGrid::walk() {
WalkResult result;
if (spans_.empty()) {
return result;
}
int64_t start = GetEpochNowInMicroseconds();
if (!walkStrategy_) {
walkStrategy_ = std::make_shared<ViterbiStrategy>();
}
const std::map<size_t, NodePtr>* fixedPtr =
fixedSpans_.empty() ? nullptr : &fixedSpans_;
WalkStrategy::WalkInput input{spans_, readings_.size(), fixedPtr};
auto walkOutput = walkStrategy_->walk(input);
result.nodes = std::move(walkOutput.nodes);
result.totalReadings = walkOutput.totalReadings;
result.vertices = walkOutput.vertices;
result.edges = walkOutput.edges;
result.elapsedMicroseconds = GetEpochNowInMicroseconds() - start;
return result;
}
std::vector<ReadingGrid::Candidate> ReadingGrid::candidatesAt(size_t loc) {
std::vector<ReadingGrid::Candidate> result;
if (readings_.empty()) {
return result;
}
if (loc > readings_.size()) {
return result;
}
std::vector<NodeInSpan> nodes =
overlappingNodesAt(loc == readings_.size() ? loc - 1 : loc);
// Sort nodes by reading length.
std::stable_sort(
nodes.begin(), nodes.end(), [](const auto& n1, const auto& n2) {
return n1.node->spanningLength() > n2.node->spanningLength();
});
for (const NodeInSpan& nodeInSpan : nodes) {
for (const LanguageModel::Unigram& unigram : nodeInSpan.node->unigrams()) {
result.emplace_back(nodeInSpan.node->reading(), unigram.value(), unigram.rawValue());
}
}
return result;
}
bool ReadingGrid::overrideCandidate(
size_t loc, const ReadingGrid::Candidate& candidate,
ReadingGrid::Node::OverrideType overrideType) {
return overrideCandidate(loc, &candidate.reading, candidate.value,
overrideType);
}
bool ReadingGrid::overrideCandidate(
size_t loc, const std::string& candidate,
ReadingGrid::Node::OverrideType overrideType) {
return overrideCandidate(loc, nullptr, candidate, overrideType);
}
void ReadingGrid::expandGridAt(size_t loc) {
if (!loc || loc == spans_.size()) {
spans_.insert(spans_.begin() + static_cast<ptrdiff_t>(loc), Span());
return;
}
spans_.insert(spans_.begin() + static_cast<ptrdiff_t>(loc), Span());
removeAffectedNodes(loc);
}
void ReadingGrid::shrinkGridAt(size_t loc) {
if (loc == spans_.size()) {
return;
}
spans_.erase(spans_.begin() + static_cast<ptrdiff_t>(loc));
removeAffectedNodes(loc);
}
void ReadingGrid::removeAffectedNodes(size_t loc) {
// Because of the expansion, certain spans now have "broken" nodes. We need
// to remove those. For example, before:
//
// Span index 0 1 2 3
// (---)
// (-------)
// (-----------)
//
// After we've inserted a span at 2:
//
// Span index 0 1 2 3 4
// (---)
// (---- ----)
// (-------- ----)
//
// Similarly for shrinkage, before:
//
// Span index 0 1 2 3
// (---)
// (-------)
// (-----------)
//
// After we've deleted the span at 2:
//
// Span index 0 1 2 3 4
// (---)
// XXXXX
// XXXXXXXXX
//
if (spans_.empty()) {
return;
}
size_t affectedLength = maxSpanLength_ - 1;
size_t begin = loc <= affectedLength ? 0 : loc - affectedLength;
size_t end = loc >= 1 ? loc - 1 : 0;
for (size_t i = begin; i <= end; ++i) {
spans_[i].removeNodesOfOrLongerThan(loc - i + 1);
}
}
void ReadingGrid::insert(size_t loc, const ReadingGrid::NodePtr& node) {
assert(loc < spans_.size());
spans_[loc].add(node);
}
std::string ReadingGrid::combineReading(
std::vector<std::string>::const_iterator begin,
std::vector<std::string>::const_iterator end) {
std::string result;
for (auto iter = begin; iter != end;) {
result += *iter;
++iter;
if (iter != end) {
result += separator_;
}
}
return result;
}
bool ReadingGrid::hasNodeAt(size_t loc, size_t readingLen,
const std::string& reading) {
if (loc > spans_.size()) {
return false;
}
const NodePtr& n = spans_[loc].nodeOf(readingLen);
if (n == nullptr) {
return false;
}
return reading == n->reading();
}
void ReadingGrid::update() {
size_t begin =
(cursor_ <= maxSpanLength_) ? 0 : cursor_ - maxSpanLength_;
size_t end = cursor_ + maxSpanLength_;
if (end > readings_.size()) {
end = readings_.size();
}
for (size_t pos = begin; pos < end; pos++) {
for (size_t len = 1; len <= maxSpanLength_ && pos + len <= end; len++) {
std::string combinedReading =
combineReading(readings_.begin() + static_cast<ptrdiff_t>(pos),
readings_.begin() + static_cast<ptrdiff_t>(pos + len));
if (!hasNodeAt(pos, len, combinedReading)) {
auto unigrams = lm_.getUnigrams(combinedReading);
if (unigrams.empty()) {
continue;
}
insert(pos, std::make_shared<Node>(combinedReading, len, unigrams));
}
}
}
}
bool ReadingGrid::overrideCandidate(
size_t loc, const std::string* reading, const std::string& value,
ReadingGrid::Node::OverrideType overrideType) {
if (loc > readings_.size()) {
return false;
}
std::vector<NodeInSpan> overlappingNodes =
overlappingNodesAt(loc == readings_.size() ? loc - 1 : loc);
NodeInSpan overridden;
for (NodeInSpan& nis : overlappingNodes) {
if (reading != nullptr && nis.node->reading() != *reading) {
continue;
}
if (nis.node->selectOverrideUnigram(value, overrideType)) {
overridden = nis;
break;
}
}
if (overridden.node == nullptr) {
// Nothing gets overridden.
return false;
}
for (size_t i = overridden.spanIndex;
i < overridden.spanIndex + overridden.node->spanningLength() &&
i < spans_.size();
++i) {
// We also need to reset *all* nodes that share the same location in the
// span. For example, if previously the two walked nodes are "A BC" where
// A and BC are two nodes with overrides. The user now chooses "DEF" which
// is a node that shares the same span location with "A". The node with BC
// will be reset as it's part of the overlapping node, but A is not.
std::vector<NodeInSpan> nodes = overlappingNodesAt(i);
for (NodeInSpan& nis : nodes) {
if (nis.node != overridden.node) {
nis.node->reset();
}
}
}
return true;
}
std::vector<ReadingGrid::NodeInSpan> ReadingGrid::overlappingNodesAt(
size_t loc) const {
std::vector<ReadingGrid::NodeInSpan> results;
if (spans_.empty() || loc >= spans_.size()) {
return results;
}
// First, get all nodes from the span at location.
for (size_t i = 1, len = spans_[loc].maxLength(); i <= len; ++i) {
NodePtr ptr = spans_[loc].nodeOf(i);
if (ptr != nullptr) {
ReadingGrid::NodeInSpan element{.node = std::move(ptr), .spanIndex = loc};
results.emplace_back(std::move(element));
}
}
size_t begin = loc - std::min(loc, maxSpanLength_ - 1);
for (size_t i = begin; i < loc; ++i) {
size_t beginLen = loc - i + 1;
size_t endLen = spans_[i].maxLength();
for (size_t j = beginLen; j <= endLen; ++j) {
NodePtr ptr = spans_[i].nodeOf(j);
if (ptr != nullptr) {
ReadingGrid::NodeInSpan element{.node = std::move(ptr), .spanIndex = i};
results.emplace_back(std::move(element));
}
}
}
return results;
}
LanguageModel::Unigram ReadingGrid::Node::currentUnigram() const {
return unigrams_.empty() ? LanguageModel::Unigram{} : *unigramIter_;
}
std::string ReadingGrid::Node::value() const {
return unigrams_.empty() ? "" : unigramIter_->value();
}
double ReadingGrid::Node::score() const {
if (unigrams_.empty()) {
return 0;
}
switch (overrideType_) {
case OverrideType::kOverrideValueWithHighScore:
return kOverridingScore;
case OverrideType::kOverrideValueWithScoreFromTopUnigram:
return unigrams_[0].score();
case OverrideType::kNone:
default:
return unigramIter_->score();
}
}
bool ReadingGrid::Node::isOverridden() const {
return overrideType_ != OverrideType::kNone;
}
void ReadingGrid::Node::reset() {
unigramIter_ = unigrams_.begin();
overrideType_ = OverrideType::kNone;
}
bool ReadingGrid::Node::selectOverrideUnigram(
const std::string& value, ReadingGrid::Node::OverrideType type) {
assert(type != ReadingGrid::Node::OverrideType::kNone);
for (auto it = unigrams_.begin(), end = unigrams_.end(); it != end; ++it) {
if (value == it->value()) {
unigramIter_ = it;
overrideType_ = type;
return true;
}
}
return false;
}
std::vector<ReadingGrid::NodePtr>::const_iterator
ReadingGrid::WalkResult::findNodeAt(size_t cursor,
size_t* outCursorPastNode) const {
if (nodes.empty()) {
return nodes.cend();
}
if (cursor > totalReadings) {
return nodes.cend();
}
if (cursor == 0) {
auto it = nodes.cbegin();
if (outCursorPastNode != nullptr) {
*outCursorPastNode = (*it)->spanningLength();
}
return it;
}
// Covers both the "cursor is right at end" and "cursor is one reading before
// the end" cases.
if (cursor >= totalReadings - 1) {
if (outCursorPastNode != nullptr) {
*outCursorPastNode = totalReadings;
}
return std::next(nodes.cbegin(), static_cast<ptrdiff_t>(nodes.size() - 1));
}
size_t accumulated = 0;
for (auto i = nodes.cbegin(); i != nodes.cend(); ++i) {
accumulated += (*i)->spanningLength();
if (accumulated > cursor) {
if (outCursorPastNode != nullptr) {
*outCursorPastNode = accumulated;
}
return i;
}
}
// Shouldn't happen.
return nodes.cend();
}
std::vector<std::string> ReadingGrid::WalkResult::valuesAsStrings() const {
std::vector<std::string> result;
for (const NodePtr& node : nodes) {
result.emplace_back(node->value());
}
return result;
}
std::vector<std::string> ReadingGrid::WalkResult::readingsAsStrings() const {
std::vector<std::string> result;
for (const NodePtr& node : nodes) {
result.emplace_back(node->reading());
}
return result;
}
void ReadingGrid::Span::clear() {
nodes_.clear();
maxLength_ = 0;
}
void ReadingGrid::Span::add(const ReadingGrid::NodePtr& node) {
assert(node->spanningLength() > 0);
size_t idx = node->spanningLength() - 1;
if (idx >= nodes_.size()) {
nodes_.resize(idx + 1);
}
nodes_[idx] = node;
if (node->spanningLength() >= maxLength_) {
maxLength_ = node->spanningLength();
}
}
void ReadingGrid::Span::removeNodesOfOrLongerThan(size_t length) {
assert(length > 0);
for (size_t i = length - 1; i < nodes_.size(); ++i) {
nodes_[i] = nullptr;
}
maxLength_ = 0;
if (length == 1) {
return;
}
size_t i = length - 2;
while (true) {
if (nodes_[i] != nullptr) {
maxLength_ = i + 1;
return;
}
if (i == 0) {
return;
}
--i;
}
}
ReadingGrid::NodePtr ReadingGrid::Span::nodeOf(size_t length) const {
assert(length > 0);
if (length - 1 >= nodes_.size()) {
return nullptr;
}
return nodes_[length - 1];
}
std::vector<LanguageModel::Unigram>
ReadingGrid::ScoreRankedLanguageModel::getUnigrams(const std::string& reading) {
auto unigrams = lm_->getUnigrams(reading);
std::stable_sort(
unigrams.begin(), unigrams.end(),
[](const auto& u1, const auto& u2) { return u1.score() > u2.score(); });
return unigrams;
}
bool ReadingGrid::ScoreRankedLanguageModel::hasUnigrams(
const std::string& reading) {
return lm_->hasUnigrams(reading);
}
} // namespace Formosa::Gramambular2