librime 1.2
Rime Input Method Engine, the core library
poet.h
Go to the documentation of this file.
1//
2// Copyright RIME Developers
3// Distributed under the BSD License
4//
5// simplistic sentence-making
6//
7// 2011-10-06 GONG Chen <chen.sst@gmail.com>
8//
9
10#ifndef RIME_POET_H_
11#define RIME_POET_H_
12
13#include <rime/common.h>
14#include <rime/translation.h>
17
18namespace rime {
19
20using WordGraph = map<int, map<int, DictEntryList>>;
21
22class Grammar;
23class Language;
24struct Line;
25
26class Poet {
27 public:
28 // Line "less", used to compare composed line of the same input range.
29 using Compare = function<bool(const Line&, const Line&)>;
30
31 static bool CompareWeight(const Line& one, const Line& other);
32 static bool LeftAssociateCompare(const Line& one, const Line& other);
33
34 Poet(const Language* language,
35 Config* config,
36 Compare compare = CompareWeight);
37 ~Poet();
38
40 size_t total_length,
41 const string& preceding_text);
42 deque<an<Sentence>> MakeSentences(const WordGraph& graph,
43 size_t total_length,
44 const string& preceding_text,
45 size_t count,
46 double cutoff_threshold);
47
48 template <class TranslatorT>
50 const string& input,
51 size_t start,
52 TranslatorT* translator) {
53 if (!translator->contextual_suggestions() || !grammar_) {
54 return translation;
55 }
56 auto preceding_text = translator->GetPrecedingText(start);
57 if (preceding_text.empty()) {
58 return translation;
59 }
60 return New<ContextualTranslation>(translation, input, preceding_text,
61 grammar_.get());
62 }
63
64 private:
65 template <class Strategy>
66 an<Sentence> MakeSentenceWithStrategy(const WordGraph& graph,
67 size_t total_length,
68 const string& preceding_text);
69
70 const Language* language_;
71 the<Grammar> grammar_;
72 Compare compare_;
73};
74
75} // namespace rime
76
77#endif // RIME_POET_H_
Definition algebra.cc:12
std::shared_ptr< T > an
Definition common.h:60
map< int, map< int, DictEntryList > > WordGraph
Definition poet.h:20
std::unique_ptr< T > the
Definition common.h:58
an< T > New(Args &&... args)
Definition common.h:77
Definition config_component.h:21
Definition grammar.h:11
Definition poet.cc:21
static bool CompareWeight(const Line &one, const Line &other)
Definition poet.cc:88
an< Sentence > MakeSentence(const WordGraph &graph, size_t total_length, const string &preceding_text)
Definition poet.cc:246
an< Translation > ContextualWeighted(an< Translation > translation, const string &input, size_t start, TranslatorT *translator)
Definition poet.h:49
static bool LeftAssociateCompare(const Line &one, const Line &other)
Definition poet.cc:93
function< bool(const Line &, const Line &)> Compare
Definition poet.h:29
Poet(const Language *language, Config *config, Compare compare=CompareWeight)
Definition poet.cc:81
deque< an< Sentence > > MakeSentences(const WordGraph &graph, size_t total_length, const string &preceding_text, size_t count, double cutoff_threshold)
Definition poet.cc:258
Definition language.h:12