66#include " historybigram.h"
77#include " constants.h"
88#include " datrie.h"
9+ #include " lattice.h"
910#include " utils.h"
1011#include " zstdfilter.h"
12+ #include < algorithm>
13+ #include < array>
1114#include < boost/algorithm/cxx11/all_of.hpp>
1215#include < boost/algorithm/string.hpp>
16+ #include < boost/algorithm/string/predicate.hpp>
1317#include < boost/range/adaptor/reversed.hpp>
1418#include < boost/range/adaptor/transformed.hpp>
1519#include < boost/range/algorithm.hpp>
20+ #include < boost/range/algorithm/for_each.hpp>
21+ #include < cassert>
1622#include < cmath>
23+ #include < cstddef>
24+ #include < cstdint>
25+ #include < fcitx-utils/macros.h>
1726#include < fcitx-utils/stringutils.h>
27+ #include < functional>
28+ #include < istream>
1829#include < iterator>
30+ #include < list>
31+ #include < memory>
1932#include < ostream>
2033#include < stdexcept>
2134#include < string>
35+ #include < string_view>
36+ #include < unordered_set>
37+ #include < utility>
38+ #include < vector>
2239
2340namespace libime {
2441
@@ -53,7 +70,7 @@ struct WeightedTrie {
5370
5471 void decFreq (std::string_view s, int32_t delta) {
5572 auto v = trie_.exactMatchSearch (s.data (), s.size ());
56- if (trie_. isNoValue (v)) {
73+ if (TrieType:: isNoValue (v)) {
5774 return ;
5875 }
5976 if (v <= delta) {
@@ -68,7 +85,7 @@ struct WeightedTrie {
6885
6986 void eraseByKey (std::string_view s) {
7087 auto v = trie_.exactMatchSearch (s.data (), s.size ());
71- if (trie_. isNoValue (v)) {
88+ if (TrieType:: isNoValue (v)) {
7289 return ;
7390 }
7491 trie_.erase (s);
@@ -121,10 +138,7 @@ struct WeightedTrie {
121138 }
122139 words.emplace (std::move (buf));
123140
124- if (maxSize > 0 && words.size () >= maxSize) {
125- return false ;
126- }
127- return true ;
141+ return maxSize <= 0 || words.size () < maxSize;
128142 });
129143 }
130144
@@ -168,8 +182,9 @@ class HistoryBigramPool {
168182 std::vector<std::string> lines;
169183 while (std::getline (in, buf)) {
170184 lines.emplace_back (buf);
171- if (lines.size () >= maxSize_)
185+ if (lines.size () >= maxSize_) {
172186 break ;
187+ }
173188 }
174189 for (auto &line : lines | boost::adaptors::reversed) {
175190 std::vector<std::string> sentence =
@@ -348,8 +363,6 @@ class HistoryBigramPool {
348363// And then we define alpha as p = 1 / (1 + alpha).
349364class HistoryBigramPrivate {
350365public:
351- HistoryBigramPrivate () {}
352-
353366 void populateSentence (std::list<std::vector<std::string>> popedSentence) {
354367 for (size_t i = 1 ; !popedSentence.empty () && i < pools_.size (); i++) {
355368 std::list<std::vector<std::string>> nextSentences;
@@ -405,7 +418,7 @@ HistoryBigram::HistoryBigram()
405418 d->poolWeight_ .reserve (poolSize.size ());
406419 for (auto size : poolSize) {
407420 d->pools_ .emplace_back (size);
408- float portion = 1 .0f ;
421+ float portion = 1 .0F ;
409422 if (d->pools_ .size () != poolSize.size ()) {
410423 portion *= 1 - p;
411424 }
@@ -472,11 +485,11 @@ float HistoryBigram::score(std::string_view prev, std::string_view cur) const {
472485 auto bf = d->bigramFreq (prev, cur);
473486 auto uf1 = d->unigramFreq (cur);
474487
475- float bigramWeight = d->useOnlyUnigram_ ? 0 .0f : 0 .8f ;
488+ float bigramWeight = d->useOnlyUnigram_ ? 0 .0F : 0 .8F ;
476489 // add 0.5 to avoid div 0
477- float pr = 0 .0f ;
490+ float pr = 0 .0F ;
478491 pr += bigramWeight * float (bf) / float (uf0 + d->poolWeight_ [0 ] / 2 );
479- pr += (1 .0f - bigramWeight) * float (uf1) /
492+ pr += (1 .0F - bigramWeight) * float (uf1) /
480493 float (d->unigramSize () + d->poolWeight_ [0 ] / 2 );
481494
482495 if (pr >= 1.0 ) {
0 commit comments