1 //
2 // Copyright RIME Developers
3 // Distributed under the BSD License
4 //
5 // 2012-01-05 GONG Chen <chen.sst@gmail.com>
6 // 2014-07-06 GONG Chen <chen.sst@gmail.com> redesigned binary file format.
7 //
8 #ifndef RIME_REVERSE_LOOKUP_DICTIONARY_H_
9 #define RIME_REVERSE_LOOKUP_DICTIONARY_H_
10 
11 #include <stdint.h>
12 #include <rime/common.h>
13 #include <rime/component.h>
14 #include <rime/dict/mapped_file.h>
15 #include <rime/dict/string_table.h>
16 #include <rime/dict/vocabulary.h>
17 
18 namespace rime {
19 
20 namespace reverse {
21 
22 struct Metadata {
23   static const int kFormatMaxLength = 32;
24   char format[kFormatMaxLength];
25   uint32_t dict_file_checksum;
26   String dict_settings;
27   List<StringId> index;
28   OffsetPtr<char> key_trie;
29   uint32_t key_trie_size;
30   OffsetPtr<char> value_trie;
31   uint32_t value_trie_size;
32 };
33 
34 }  // namespace reverse
35 
36 struct Ticket;
37 class DictSettings;
38 
39 class ReverseDb : public MappedFile {
40  public:
41   explicit ReverseDb(const string& file_name);
42 
43   bool Load();
44   bool Lookup(const string& text, string* result);
45 
46   bool Build(DictSettings* settings,
47              const Syllabary& syllabary,
48              const Vocabulary& vocabulary,
49              const ReverseLookupTable& stems,
50              uint32_t dict_file_checksum);
51 
52   uint32_t dict_file_checksum() const;
metadata()53   reverse::Metadata* metadata() const { return metadata_; }
54 
55  private:
56   reverse::Metadata* metadata_ = nullptr;
57   the<StringTable> key_trie_;
58   the<StringTable> value_trie_;
59 };
60 
61 class ReverseLookupDictionary
62     : public Class<ReverseLookupDictionary, const Ticket&> {
63  public:
64   explicit ReverseLookupDictionary(an<ReverseDb> db);
65   bool Load();
66   bool ReverseLookup(const string& text, string* result);
67   bool LookupStems(const string& text, string* result);
68   an<DictSettings> GetDictSettings();
69 
70  protected:
71   an<ReverseDb> db_;
72 };
73 
74 class ResourceResolver;
75 
76 class ReverseLookupDictionaryComponent
77     : public ReverseLookupDictionary::Component {
78  public:
79   ReverseLookupDictionaryComponent();
80   ReverseLookupDictionary* Create(const Ticket& ticket);
81  private:
82   map<string, weak<ReverseDb>> db_pool_;
83   the<ResourceResolver> resource_resolver_;
84 };
85 
86 }  // namespace rime
87 
88 #endif  // RIME_REVERSE_LOOKUP_DICTIONARY_H_
89