1 //
2 // Copyright RIME Developers
3 // Distributed under the BSD License
4 //
5 // 2011-07-10 GONG Chen <chen.sst@gmail.com>
6 //
7 #ifndef RIME_TABLE_TRANSLATOR_H_
8 #define RIME_TABLE_TRANSLATOR_H_
9 
10 #include <rime/common.h>
11 #include <rime/config.h>
12 #include <rime/translation.h>
13 #include <rime/translator.h>
14 #include <rime/algo/algebra.h>
15 #include <rime/dict/dictionary.h>
16 #include <rime/dict/user_dictionary.h>
17 #include <rime/gear/memory.h>
18 #include <rime/gear/translator_commons.h>
19 
20 namespace rime {
21 
22 class Poet;
23 class UnityTableEncoder;
24 
25 class TableTranslator : public Translator,
26                         public Memory,
27                         public TranslatorOptions {
28  public:
29   TableTranslator(const Ticket& ticket);
30 
31   virtual an<Translation> Query(const string& input,
32                                 const Segment& segment);
33   virtual bool Memorize(const CommitEntry& commit_entry);
34 
35   an<Translation> MakeSentence(const string& input,
36                                size_t start,
37                                bool include_prefix_phrases = false);
38   string GetPrecedingText(size_t start) const;
encoder()39   UnityTableEncoder* encoder() const { return encoder_.get(); }
40 
41  protected:
42   bool enable_charset_filter_ = false;
43   bool enable_encoder_ = false;
44   bool enable_sentence_ = true;
45   bool sentence_over_completion_ = false;
46   bool encode_commit_history_ = true;
47   int max_phrase_length_ = 5;
48   int max_homographs_ = 1;
49   the<Poet> poet_;
50   the<UnityTableEncoder> encoder_;
51 };
52 
53 class TableTranslation : public Translation {
54  public:
55 
56   TableTranslation(TranslatorOptions* options,
57                    const Language* language,
58                    const string& input,
59                    size_t start,
60                    size_t end,
61                    const string& preedit,
62                    DictEntryIterator&& iter = {},
63                    UserDictEntryIterator&& uter = {});
64 
65   virtual bool Next();
66   virtual an<Candidate> Peek();
67 
68  protected:
FetchMoreUserPhrases()69   virtual bool FetchMoreUserPhrases() { return false; }
FetchMoreTableEntries()70   virtual bool FetchMoreTableEntries() { return false; }
71 
72   bool CheckEmpty();
73   bool PreferUserPhrase();
74 
PreferredEntry(bool prefer_user_phrase)75   an<DictEntry> PreferredEntry(bool prefer_user_phrase) {
76     return prefer_user_phrase ? uter_.Peek() : iter_.Peek();
77   }
78 
79   TranslatorOptions* options_;
80   const Language* language_;
81   string input_;
82   size_t start_;
83   size_t end_;
84   string preedit_;
85   DictEntryIterator iter_;
86   UserDictEntryIterator uter_;
87 };
88 
89 }  // namespace rime
90 
91 #endif  // RIME_TABLE_TRANSLATOR_H_
92