libime
decoder.h
1 /*
2  * SPDX-FileCopyrightText: 2017-2017 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  */
6 #ifndef _FCITX_LIBIME_CORE_DECODER_H_
7 #define _FCITX_LIBIME_CORE_DECODER_H_
8 
9 #include <cstddef>
10 #include <limits>
11 #include <memory>
12 #include <string_view>
13 #include <utility>
14 #include <fcitx-utils/macros.h>
15 #include <libime/core/dictionary.h>
16 #include <libime/core/languagemodel.h>
17 #include <libime/core/lattice.h>
18 #include <libime/core/libimecore_export.h>
19 #include <libime/core/segmentgraph.h>
20 
21 namespace libime {
22 
23 class DecoderPrivate;
24 class Dictionary;
25 
26 class LIBIMECORE_EXPORT Decoder {
27  friend class DecoderPrivate;
28 
29 public:
30  Decoder(const Dictionary *dict, const LanguageModelBase *model);
31  virtual ~Decoder();
32 
33  constexpr static const size_t beamSizeDefault = 20;
34  constexpr static const size_t frameSizeDefault = 40;
35 
36  const Dictionary *dict() const;
37  const LanguageModelBase *model() const;
38 
39  bool decode(Lattice &lattice, const SegmentGraph &graph, size_t nbest,
40  const State &state,
41  float max = std::numeric_limits<float>::max(),
42  float min = -std::numeric_limits<float>::max(),
43  size_t beamSize = beamSizeDefault,
44  size_t frameSize = frameSizeDefault,
45  void *helper = nullptr) const;
46 
47 protected:
48  LatticeNode *
49  createLatticeNode(const SegmentGraph &graph, const LanguageModelBase *model,
50  std::string_view word, WordIndex idx,
51  SegmentGraphPath path, const State &state, float cost = 0,
52  std::unique_ptr<LatticeNodeData> data = nullptr,
53  bool onlyPath = false) const {
54  return createLatticeNodeImpl(graph, model, word, idx, std::move(path),
55  state, cost, std::move(data), onlyPath);
56  }
57  virtual LatticeNode *createLatticeNodeImpl(
58  const SegmentGraphBase &graph, const LanguageModelBase *model,
59  std::string_view word, WordIndex idx, SegmentGraphPath path,
60  const State &state, float cost, std::unique_ptr<LatticeNodeData> data,
61  bool onlyPath) const;
62 
63  virtual bool needSort(const SegmentGraph & /*graph*/,
64  const SegmentGraphNode * /*node*/) const {
65  return true;
66  }
67 
68 private:
69  std::unique_ptr<DecoderPrivate> d_ptr;
70  FCITX_DECLARE_PRIVATE(Decoder);
71 };
72 } // namespace libime
73 
74 #endif // _FCITX_LIBIME_CORE_DECODER_H_