|
| 1 | +/* |
| 2 | + * table_translator.cc |
| 3 | + * Copyright (C) 2023 Shewer Lu <shewer@gmail.com> |
| 4 | + * |
| 5 | + * Distributed under terms of the MIT license. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <rime/gear/script_translator.h> |
| 9 | +#include <rime/algo/syllabifier.h> |
| 10 | +#include <rime/dict/corrector.h> |
| 11 | +#include <rime/gear/poet.h> |
| 12 | + |
| 13 | + |
| 14 | +#include "translator.h" |
| 15 | + |
| 16 | +using namespace rime; |
| 17 | + |
| 18 | +namespace { |
| 19 | +namespace ScriptTranslatorReg { |
| 20 | + |
| 21 | + class LScriptTranslator : public ScriptTranslator { |
| 22 | + public: |
| 23 | + LScriptTranslator(const Ticket& ticket, Lua* lua) |
| 24 | + : ScriptTranslator(ticket), lua_(lua) {}; |
| 25 | + |
| 26 | + virtual bool Memorize(const CommitEntry& commit_entry); |
| 27 | + bool memorize(const CommitEntry& commit_entry); |
| 28 | + bool update_entry(const DictEntry& index, |
| 29 | + int commits, const string& new_entory_prefix); |
| 30 | + |
| 31 | + SET_(memorize_callback, an<LuaObj>); |
| 32 | + bool memorize_callback(); |
| 33 | + |
| 34 | + // TranslatorOptions |
| 35 | + SET_(contextual_suggestions, bool); |
| 36 | + SET_(delimiters, string&); |
| 37 | + SET_(preedit_formatter, Projection&); |
| 38 | + SET_(comment_formatter, Projection&); |
| 39 | + bool reload_user_dict_disabling_patterns(an<ConfigList>); |
| 40 | + |
| 41 | + // ScriptTranslator member |
| 42 | + ACCESS_(spelling_hints, int); |
| 43 | + ACCESS_(always_show_comments, bool); |
| 44 | + ACCESS_(max_homophones, int); |
| 45 | + GET_(enable_correction, bool); |
| 46 | + void set_enable_correction(bool); |
| 47 | + |
| 48 | + protected: |
| 49 | + Lua* lua_; |
| 50 | + an<LuaObj> memorize_callback_; |
| 51 | + void init_correction(); |
| 52 | + }; |
| 53 | + |
| 54 | + using T = LScriptTranslator; |
| 55 | + |
| 56 | + bool T::memorize_callback() { |
| 57 | + return (memorize_callback_) ? true : false; |
| 58 | + } |
| 59 | + |
| 60 | + bool T::memorize(const CommitEntry& commit_entry) { |
| 61 | + return ScriptTranslator::Memorize(commit_entry); |
| 62 | + } |
| 63 | + |
| 64 | + bool T::Memorize(const CommitEntry& commit_entry) { |
| 65 | + if (!memorize_callback_) { |
| 66 | + return memorize(commit_entry); |
| 67 | + } |
| 68 | + |
| 69 | + auto r = lua_->call<bool, an<LuaObj>, LScriptTranslator*, const CommitEntry&>( |
| 70 | + memorize_callback_, this, commit_entry); |
| 71 | + if (!r.ok()) { |
| 72 | + auto e = r.get_err(); |
| 73 | + LOG(ERROR) << "LScriptTranslator of " << name_space_ |
| 74 | + << ": memorize_callback error(" << e.status << "): " << e.e; |
| 75 | + return false; |
| 76 | + } |
| 77 | + return r.get(); |
| 78 | + } |
| 79 | + |
| 80 | + void T::init_correction() { |
| 81 | + if (auto* corrector = Corrector::Require("corrector")) { |
| 82 | + Ticket ticket(engine_, name_space_); |
| 83 | + corrector_.reset(corrector->Create(ticket)); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + void T::set_enable_correction(bool enable) { |
| 88 | + if (enable_correction_ = enable && !corrector_) |
| 89 | + init_correction(); |
| 90 | + } |
| 91 | + |
| 92 | + bool T::update_entry(const DictEntry& entry, |
| 93 | + int commits, const string& new_entory_prefix) { |
| 94 | + if (user_dict_ && user_dict_->loaded()) |
| 95 | + return user_dict_->UpdateEntry(entry, commits, new_entory_prefix); |
| 96 | + |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + bool T::reload_user_dict_disabling_patterns(an<ConfigList> cl) { |
| 101 | + return cl ? user_dict_disabling_patterns_.Load(cl) : false; |
| 102 | + } |
| 103 | + |
| 104 | + static const luaL_Reg funcs[] = { |
| 105 | + {NULL, NULL}, |
| 106 | + }; |
| 107 | + |
| 108 | + static const luaL_Reg methods[] = { |
| 109 | + {"query", WRAPMEM(T, Query)}, // string, segment |
| 110 | + {"start_session", WRAPMEM(T, StartSession)}, |
| 111 | + {"finish_session", WRAPMEM(T, FinishSession)}, |
| 112 | + {"discard_session", WRAPMEM(T, DiscardSession)}, |
| 113 | + WMEM(memorize), // delegate TableTransaltor::Momorize |
| 114 | + WMEM(update_entry), // delegate UserDictionary::UpdateEntry |
| 115 | + WMEM(reload_user_dict_disabling_patterns), |
| 116 | + Set_WMEM(memorize_callback), // an<LuaObj> callback function |
| 117 | + {NULL, NULL}, |
| 118 | + }; |
| 119 | + |
| 120 | + static const luaL_Reg vars_get[] = { |
| 121 | + Get_WMEM(name_space), // string |
| 122 | + Set_WMEM(memorize_callback), // an<LuaObj> callback function |
| 123 | + // ScriptTranslator member |
| 124 | + Get_WMEM(max_homophones), // int |
| 125 | + Get_WMEM(spelling_hints), // int |
| 126 | + Get_WMEM(always_show_comments), // bool |
| 127 | + Get_WMEM(enable_correction), // bool |
| 128 | + //TranslatorOptions |
| 129 | + Get_WMEM(delimiters), // string& |
| 130 | + Get_WMEM(tag), // string |
| 131 | + Get_WMEM(enable_completion), // bool |
| 132 | + Get_WMEM(contextual_suggestions), // bool |
| 133 | + Get_WMEM(strict_spelling), // bool |
| 134 | + Get_WMEM(initial_quality), // double |
| 135 | + Get_WMEM(preedit_formatter), // Projection& |
| 136 | + Get_WMEM(comment_formatter), // Projection& |
| 137 | + // Memory |
| 138 | + Get_WMEM(dict), |
| 139 | + Get_WMEM(user_dict), |
| 140 | + {NULL, NULL}, |
| 141 | + }; |
| 142 | + |
| 143 | + static const luaL_Reg vars_set[] = { |
| 144 | + // ScriptTranslator member |
| 145 | + Set_WMEM(max_homophones), // int |
| 146 | + Set_WMEM(spelling_hints), // int |
| 147 | + Set_WMEM(always_show_comments), // bool |
| 148 | + Set_WMEM(enable_correction), // bool |
| 149 | + // TranslatorOptions |
| 150 | + Set_WMEM(delimiters), // string& |
| 151 | + Set_WMEM(tag), // string |
| 152 | + Set_WMEM(enable_completion), // bool |
| 153 | + Set_WMEM(contextual_suggestions), // bool |
| 154 | + Set_WMEM(strict_spelling), // bool |
| 155 | + Set_WMEM(initial_quality), // double |
| 156 | + Set_WMEM(preedit_formatter), // Projection& |
| 157 | + Set_WMEM(comment_formatter), // Projection& |
| 158 | + {NULL, NULL}, |
| 159 | + }; |
| 160 | + |
| 161 | + void reg_Component(lua_State* L) { |
| 162 | + lua_getglobal(L, "Component"); |
| 163 | + if (lua_type(L, -1) != LUA_TTABLE) { |
| 164 | + LOG(ERROR) << "table of _G[\"Component\"] not found."; |
| 165 | + } else { |
| 166 | + lua_pushcfunction(L, raw_make_translator<T>); |
| 167 | + lua_setfield(L, -2, "ScriptTranslator"); |
| 168 | + } |
| 169 | + lua_pop(L, 1); |
| 170 | + } |
| 171 | + |
| 172 | +} // namespace ScriptTranslatorReg |
| 173 | +} // namespace |
| 174 | + |
| 175 | +void LUAWRAPPER_LOCAL script_translator_init(lua_State* L) { |
| 176 | + EXPORT(ScriptTranslatorReg, L); |
| 177 | + ScriptTranslatorReg::reg_Component(L); |
| 178 | +} |
0 commit comments