Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clang-tidy and include fix on libime/core #85

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libime/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ set(LIBIME_HDRS

set(LIBIME_SRCS
datrie.cpp
dictionary.cpp
decoder.cpp
languagemodel.cpp
inputbuffer.cpp
Expand Down
3 changes: 3 additions & 0 deletions src/libime/core/datrie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@
#include <cmath>
#include <cstdint>
#include <cstring>
#include <fcitx-utils/macros.h>
#include <fstream>
#include <ios>
#include <istream>
#include <limits>
#include <memory>
#include <ostream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <sys/types.h>
#include <tuple>
#include <vector>

Expand Down
5 changes: 5 additions & 0 deletions src/libime/core/datrie.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@

#include "libimecore_export.h"

#include <cstddef>
#include <cstdint>
#include <fcitx-utils/macros.h>
#include <functional>
#include <istream>
#include <memory>
#include <ostream>
#include <string>
#include <string_view>
#include <tuple>
#include <vector>

namespace libime {
Expand Down
3 changes: 2 additions & 1 deletion src/libime/core/decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ void DecoderPrivate::backwardSearch(const SegmentGraph &graph, Lattice &l,
std::priority_queue<std::shared_ptr<NBestNode>,
std::vector<std::shared_ptr<NBestNode>>,
NBestNodeLess<std::shared_ptr<NBestNode>>>;
PriorityQueueType q, result;
PriorityQueueType q;
PriorityQueueType result;

auto *eos = &lattice[nullptr][0];
auto newNBestNode = [](const LatticeNode *node) {
Expand Down
9 changes: 7 additions & 2 deletions src/libime/core/decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
#define _FCITX_LIBIME_CORE_DECODER_H_

#include "libimecore_export.h"
#include <cstddef>
#include <fcitx-utils/macros.h>
#include <libime/core/dictionary.h>
#include <libime/core/languagemodel.h>
#include <libime/core/lattice.h>
#include <libime/core/segmentgraph.h>
#include <limits>
#include <memory>
#include <string_view>
#include <utility>

namespace libime {

Expand Down Expand Up @@ -55,8 +60,8 @@ class LIBIMECORE_EXPORT Decoder {
const State &state, float cost, std::unique_ptr<LatticeNodeData> data,
bool onlyPath) const;

virtual bool needSort(const SegmentGraph &,
const SegmentGraphNode *) const {
virtual bool needSort(const SegmentGraph & /*graph*/,
const SegmentGraphNode * /*node*/) const {
return true;
}

Expand Down
9 changes: 9 additions & 0 deletions src/libime/core/dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@
*/

#include "dictionary.h"
#include "segmentgraph.h"
#include <unordered_set>

void libime::Dictionary::matchPrefix(
const SegmentGraph &graph, const GraphMatchCallback &callback,
const std::unordered_set<const SegmentGraphNode *> &ignore,
void *helper) const {
matchPrefixImpl(graph, callback, ignore, helper);
}
13 changes: 6 additions & 7 deletions src/libime/core/dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,24 @@
#include <functional>
#include <libime/core/lattice.h>
#include <libime/core/segmentgraph.h>
#include <string_view>
#include <memory>
#include <unordered_set>

namespace libime {

class WordNode;

// The callback accepts the passed path that matches the word.
typedef std::function<void(const SegmentGraphPath &, WordNode &, float,
std::unique_ptr<LatticeNodeData>)>
GraphMatchCallback;
using GraphMatchCallback =
std::function<void(const SegmentGraphPath &, WordNode &, float,
std::unique_ptr<LatticeNodeData>)>;

class LIBIMECORE_EXPORT Dictionary {
public:
void
matchPrefix(const SegmentGraph &graph, const GraphMatchCallback &callback,
const std::unordered_set<const SegmentGraphNode *> &ignore = {},
void *helper = nullptr) const {
matchPrefixImpl(graph, callback, ignore, helper);
}
void *helper = nullptr) const;

protected:
virtual void
Expand Down
39 changes: 26 additions & 13 deletions src/libime/core/historybigram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,36 @@
#include "historybigram.h"
#include "constants.h"
#include "datrie.h"
#include "lattice.h"
#include "utils.h"
#include "zstdfilter.h"
#include <algorithm>
#include <array>
#include <boost/algorithm/cxx11/all_of.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/algorithm/for_each.hpp>
#include <cassert>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <fcitx-utils/macros.h>
#include <fcitx-utils/stringutils.h>
#include <functional>
#include <istream>
#include <iterator>
#include <list>
#include <memory>
#include <ostream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <unordered_set>
#include <utility>
#include <vector>

namespace libime {

Expand Down Expand Up @@ -53,7 +70,7 @@ struct WeightedTrie {

void decFreq(std::string_view s, int32_t delta) {
auto v = trie_.exactMatchSearch(s.data(), s.size());
if (trie_.isNoValue(v)) {
if (TrieType::isNoValue(v)) {
return;
}
if (v <= delta) {
Expand All @@ -68,7 +85,7 @@ struct WeightedTrie {

void eraseByKey(std::string_view s) {
auto v = trie_.exactMatchSearch(s.data(), s.size());
if (trie_.isNoValue(v)) {
if (TrieType::isNoValue(v)) {
return;
}
trie_.erase(s);
Expand Down Expand Up @@ -121,10 +138,7 @@ struct WeightedTrie {
}
words.emplace(std::move(buf));

if (maxSize > 0 && words.size() >= maxSize) {
return false;
}
return true;
return maxSize <= 0 || words.size() < maxSize;
});
}

Expand Down Expand Up @@ -168,8 +182,9 @@ class HistoryBigramPool {
std::vector<std::string> lines;
while (std::getline(in, buf)) {
lines.emplace_back(buf);
if (lines.size() >= maxSize_)
if (lines.size() >= maxSize_) {
break;
}
}
for (auto &line : lines | boost::adaptors::reversed) {
std::vector<std::string> sentence =
Expand Down Expand Up @@ -348,8 +363,6 @@ class HistoryBigramPool {
// And then we define alpha as p = 1 / (1 + alpha).
class HistoryBigramPrivate {
public:
HistoryBigramPrivate() {}

void populateSentence(std::list<std::vector<std::string>> popedSentence) {
for (size_t i = 1; !popedSentence.empty() && i < pools_.size(); i++) {
std::list<std::vector<std::string>> nextSentences;
Expand Down Expand Up @@ -405,7 +418,7 @@ HistoryBigram::HistoryBigram()
d->poolWeight_.reserve(poolSize.size());
for (auto size : poolSize) {
d->pools_.emplace_back(size);
float portion = 1.0f;
float portion = 1.0F;
if (d->pools_.size() != poolSize.size()) {
portion *= 1 - p;
}
Expand Down Expand Up @@ -472,11 +485,11 @@ float HistoryBigram::score(std::string_view prev, std::string_view cur) const {
auto bf = d->bigramFreq(prev, cur);
auto uf1 = d->unigramFreq(cur);

float bigramWeight = d->useOnlyUnigram_ ? 0.0f : 0.8f;
float bigramWeight = d->useOnlyUnigram_ ? 0.0F : 0.8F;
// add 0.5 to avoid div 0
float pr = 0.0f;
float pr = 0.0F;
pr += bigramWeight * float(bf) / float(uf0 + d->poolWeight_[0] / 2);
pr += (1.0f - bigramWeight) * float(uf1) /
pr += (1.0F - bigramWeight) * float(uf1) /
float(d->unigramSize() + d->poolWeight_[0] / 2);

if (pr >= 1.0) {
Expand Down
4 changes: 4 additions & 0 deletions src/libime/core/historybigram.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
#define _FCITX_LIBIME_CORE_HISTORYBIGRAM_H_

#include "libimecore_export.h"
#include <cstddef>
#include <fcitx-utils/macros.h>
#include <istream>
#include <libime/core/lattice.h>
#include <memory>
#include <ostream>
#include <string>
#include <string_view>
#include <unordered_set>
#include <vector>

namespace libime {
Expand Down
8 changes: 3 additions & 5 deletions src/libime/core/inputbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "inputbuffer.h"
#include <cstddef>
#include <fcitx-utils/utf8.h>
#include <string_view>

namespace libime {

std::string_view InputBuffer::at(size_t i) const {
size_t start, end;
std::tie(start, end) = rangeAt(i);
return std::string_view(userInput()).substr(start, end - start);
}
std::string_view InputBuffer::at(size_t i) const { return viewAt(i); }
} // namespace libime
8 changes: 5 additions & 3 deletions src/libime/core/inputbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#define _FCITX_LIBIME_CORE_INPUTBUFFER_H_

#include "libimecore_export.h"
#include <boost/iterator/iterator_categories.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <cstddef>
#include <fcitx-utils/inputbuffer.h>
#include <string_view>

Expand All @@ -21,7 +23,7 @@ class LIBIMECORE_EXPORT InputBuffer : public fcitx::InputBuffer {
boost::bidirectional_traversal_tag,
std::string_view> {
public:
iterator() {}
iterator() = default;
iterator(const InputBuffer *buffer, size_t idx)
: buffer_(buffer), idx_(idx) {}

Expand Down Expand Up @@ -49,9 +51,9 @@ class LIBIMECORE_EXPORT InputBuffer : public fcitx::InputBuffer {

std::string_view operator[](size_t i) const { return at(i); }

iterator begin() { return iterator(this, 0); }
iterator begin() { return {this, 0}; }

iterator end() { return iterator(this, size()); }
iterator end() { return {this, size()}; }
};
} // namespace libime

Expand Down
26 changes: 22 additions & 4 deletions src/libime/core/languagemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,29 @@
#include "languagemodel.h"
#include "config.h"
#include "constants.h"
#include "datrie.h"
#include "lattice.h"
#include "lm/config.hh"
#include "lm/lm_exception.hh"
#include "lm/model.hh"
#include "lm/state.hh"
#include "lm/word_index.hh"
#include "util/string_piece.hh"
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <fcitx-utils/fs.h>
#include <fcitx-utils/macros.h>
#include <fcitx-utils/stringutils.h>
#include <fstream>
#include <ios>
#include <memory>
#include <string>
#include <string_view>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>

namespace libime {

Expand Down Expand Up @@ -74,13 +91,14 @@ float LanguageModelBase::singleWordScore(const State &state,
float LanguageModelBase::wordsScore(
const State &_state, const std::vector<std::string_view> &words) const {
float s = 0;
State state = _state, outState;
State state = _state;
State outState;
std::vector<WordNode> nodes;
for (auto word : words) {
auto idx = index(word);
nodes.emplace_back(word, idx);
s += score(state, nodes.back(), outState);
state = std::move(outState);
state = outState;
}
return s;
}
Expand Down Expand Up @@ -188,10 +206,10 @@ float LanguageModel::score(const State &state, const WordNode &node,
return d->unknown_;
}
return d->model()->Score(lmState(state), node.idx(), lmState(out)) +
(node.idx() == unknown() ? d->unknown_ : 0.0f);
(node.idx() == unknown() ? d->unknown_ : 0.0F);
}

bool LanguageModel::isUnknown(WordIndex idx, std::string_view) const {
bool LanguageModel::isUnknown(WordIndex idx, std::string_view /*word*/) const {
return idx == unknown();
}

Expand Down
9 changes: 6 additions & 3 deletions src/libime/core/languagemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
#define _FCITX_LIBIME_CORE_LANGUAGEMODEL_H_

#include "libimecore_export.h"
#include <array>
#include <cstddef>
#include <fcitx-utils/macros.h>
#include <libime/core/datrie.h>
#include <limits>
#include <memory>
#include <string>
#include <string_view>
#include <vector>

Expand Down Expand Up @@ -77,10 +80,10 @@ class LIBIMECORE_EXPORT LanguageModel : public LanguageModelBase {
WordIndex unknown() const override;
const State &beginState() const override;
const State &nullState() const override;
WordIndex index(std::string_view view) const override;
float score(const State &state, const WordNode &word,
WordIndex index(std::string_view word) const override;
float score(const State &state, const WordNode &node,
State &out) const override;
bool isUnknown(WordIndex idx, std::string_view view) const override;
bool isUnknown(WordIndex idx, std::string_view word) const override;
void setUnknownPenalty(float unknown);
float unknownPenalty() const;

Expand Down
Loading
Loading