librime: poet.h Source File

| 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>
15#include <rime/gear/translator_commons.h>
16#include <rime/gear/contextual_translation.h>
17
18namespace rime {
19
20 using WordGraph = map<int, map<int, DictEntryList>>;
21
22class Grammar;
23class Language;
24struct Line;
25
26 class 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
39 an<Sentence> MakeSentence(const WordGraph& graph,
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>
49 an<Translation> ContextualWeighted(an<Translation> translation,
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_
translation.h
common.h
translator_commons.h
contextual_translation.h
rime
Definition algebra.cc:12
rime::an
std::shared_ptr< T > an
Definition common.h:60
rime::WordGraph
map< int, map< int, DictEntryList > > WordGraph
Definition poet.h:20
rime::the
std::unique_ptr< T > the
Definition common.h:58
rime::New
an< T > New(Args &&... args)
Definition common.h:77
rime::Config
Definition config_component.h:21
rime::Grammar
Definition grammar.h:11
rime::Line
Definition poet.cc:21
rime::Poet::CompareWeight
static bool CompareWeight(const Line &one, const Line &other)
Definition poet.cc:88
rime::Poet::MakeSentence
an< Sentence > MakeSentence(const WordGraph &graph, size_t total_length, const string &preceding_text)
Definition poet.cc:246
rime::Poet::ContextualWeighted
an< Translation > ContextualWeighted(an< Translation > translation, const string &input, size_t start, TranslatorT *translator)
Definition poet.h:49
rime::Poet::LeftAssociateCompare
static bool LeftAssociateCompare(const Line &one, const Line &other)
Definition poet.cc:93
rime::Poet::Compare
function< bool(const Line &, const Line &)> Compare
Definition poet.h:29
rime::Poet::Poet
Poet(const Language *language, Config *config, Compare compare=CompareWeight)
Definition poet.cc:81
rime::Poet::MakeSentences
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
rime::Language
Definition language.h:12