6
6
#include " historybigram.h"
7
7
#include " constants.h"
8
8
#include " datrie.h"
9
+ #include " lattice.h"
9
10
#include " utils.h"
10
11
#include " zstdfilter.h"
12
+ #include < algorithm>
13
+ #include < array>
11
14
#include < boost/algorithm/cxx11/all_of.hpp>
12
15
#include < boost/algorithm/string.hpp>
16
+ #include < boost/algorithm/string/predicate.hpp>
13
17
#include < boost/range/adaptor/reversed.hpp>
14
18
#include < boost/range/adaptor/transformed.hpp>
15
19
#include < boost/range/algorithm.hpp>
20
+ #include < boost/range/algorithm/for_each.hpp>
21
+ #include < cassert>
16
22
#include < cmath>
23
+ #include < cstddef>
24
+ #include < cstdint>
25
+ #include < fcitx-utils/macros.h>
17
26
#include < fcitx-utils/stringutils.h>
27
+ #include < functional>
28
+ #include < istream>
18
29
#include < iterator>
30
+ #include < list>
31
+ #include < memory>
19
32
#include < ostream>
20
33
#include < stdexcept>
21
34
#include < string>
35
+ #include < string_view>
36
+ #include < unordered_set>
37
+ #include < utility>
38
+ #include < vector>
22
39
23
40
namespace libime {
24
41
@@ -53,7 +70,7 @@ struct WeightedTrie {
53
70
54
71
void decFreq (std::string_view s, int32_t delta) {
55
72
auto v = trie_.exactMatchSearch (s.data (), s.size ());
56
- if (trie_. isNoValue (v)) {
73
+ if (TrieType:: isNoValue (v)) {
57
74
return ;
58
75
}
59
76
if (v <= delta) {
@@ -68,7 +85,7 @@ struct WeightedTrie {
68
85
69
86
void eraseByKey (std::string_view s) {
70
87
auto v = trie_.exactMatchSearch (s.data (), s.size ());
71
- if (trie_. isNoValue (v)) {
88
+ if (TrieType:: isNoValue (v)) {
72
89
return ;
73
90
}
74
91
trie_.erase (s);
@@ -121,10 +138,7 @@ struct WeightedTrie {
121
138
}
122
139
words.emplace (std::move (buf));
123
140
124
- if (maxSize > 0 && words.size () >= maxSize) {
125
- return false ;
126
- }
127
- return true ;
141
+ return maxSize <= 0 || words.size () < maxSize;
128
142
});
129
143
}
130
144
@@ -168,8 +182,9 @@ class HistoryBigramPool {
168
182
std::vector<std::string> lines;
169
183
while (std::getline (in, buf)) {
170
184
lines.emplace_back (buf);
171
- if (lines.size () >= maxSize_)
185
+ if (lines.size () >= maxSize_) {
172
186
break ;
187
+ }
173
188
}
174
189
for (auto &line : lines | boost::adaptors::reversed) {
175
190
std::vector<std::string> sentence =
@@ -348,8 +363,6 @@ class HistoryBigramPool {
348
363
// And then we define alpha as p = 1 / (1 + alpha).
349
364
class HistoryBigramPrivate {
350
365
public:
351
- HistoryBigramPrivate () {}
352
-
353
366
void populateSentence (std::list<std::vector<std::string>> popedSentence) {
354
367
for (size_t i = 1 ; !popedSentence.empty () && i < pools_.size (); i++) {
355
368
std::list<std::vector<std::string>> nextSentences;
@@ -405,7 +418,7 @@ HistoryBigram::HistoryBigram()
405
418
d->poolWeight_ .reserve (poolSize.size ());
406
419
for (auto size : poolSize) {
407
420
d->pools_ .emplace_back (size);
408
- float portion = 1 .0f ;
421
+ float portion = 1 .0F ;
409
422
if (d->pools_ .size () != poolSize.size ()) {
410
423
portion *= 1 - p;
411
424
}
@@ -472,11 +485,11 @@ float HistoryBigram::score(std::string_view prev, std::string_view cur) const {
472
485
auto bf = d->bigramFreq (prev, cur);
473
486
auto uf1 = d->unigramFreq (cur);
474
487
475
- float bigramWeight = d->useOnlyUnigram_ ? 0 .0f : 0 .8f ;
488
+ float bigramWeight = d->useOnlyUnigram_ ? 0 .0F : 0 .8F ;
476
489
// add 0.5 to avoid div 0
477
- float pr = 0 .0f ;
490
+ float pr = 0 .0F ;
478
491
pr += bigramWeight * float (bf) / float (uf0 + d->poolWeight_ [0 ] / 2 );
479
- pr += (1 .0f - bigramWeight) * float (uf1) /
492
+ pr += (1 .0F - bigramWeight) * float (uf1) /
480
493
float (d->unigramSize () + d->poolWeight_ [0 ] / 2 );
481
494
482
495
if (pr >= 1.0 ) {
0 commit comments