1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
//
// Copyleft RIME Developers
// License: GPLv3
//
// 2012-01-03 GONG Chen <chen.sst@gmail.com>
//
#include <boost/algorithm/string.hpp>
#include <rime/candidate.h>
#include <rime/engine.h>
#include <rime/schema.h>
#include <rime/segmentation.h>
#include <rime/ticket.h>
#include <rime/translation.h>
#include <rime/algo/syllabifier.h>
#include <rime/dict/dictionary.h>
#include <rime/dict/reverse_lookup_dictionary.h>
#include <rime/gear/reverse_lookup_translator.h>
#include <rime/gear/translator_commons.h>
#include <rime/gear/table_translator.h>
//static const char *quote_left = "\xef\xbc\x88";
//static const char *quote_right = "\xef\xbc\x89";
//static const char *separator = "\xef\xbc\x8c";
namespace rime {
class ReverseLookupTranslation : public TableTranslation {
public:
ReverseLookupTranslation(ReverseLookupDictionary* dict,
TranslatorOptions* options,
const std::string& input,
size_t start, size_t end,
const std::string& preedit,
const DictEntryIterator &iter,
bool quality)
: TableTranslation(options, NULL, input, start, end, preedit, iter),
dict_(dict), options_(options), quality_(quality) {
}
virtual shared_ptr<Candidate> Peek();
virtual int Compare(shared_ptr<Translation> other,
const CandidateList &candidates);
protected:
ReverseLookupDictionary* dict_;
TranslatorOptions* options_;
bool quality_;
};
shared_ptr<Candidate> ReverseLookupTranslation::Peek() {
if (exhausted())
return shared_ptr<Candidate>();
const shared_ptr<DictEntry> &e(iter_.Peek());
std::string tips;
if (dict_) {
dict_->ReverseLookup(e->text, &tips);
if (options_) {
options_->comment_formatter().Apply(&tips);
}
//if (!tips.empty()) {
// boost::algorithm::replace_all(tips, " ", separator);
//}
}
shared_ptr<Candidate> cand = boost::make_shared<SimpleCandidate>(
"reverse_lookup",
start_,
end_,
e->text,
!tips.empty() ? (/*quote_left + */tips/* + quote_right*/) : e->comment,
preedit_);
return cand;
}
int ReverseLookupTranslation::Compare(shared_ptr<Translation> other,
const CandidateList &candidates) {
if (!other || other->exhausted()) return -1;
if (exhausted()) return 1;
shared_ptr<const Candidate> theirs = other->Peek();
if (!theirs)
return -1;
if (quality_ && theirs->type() == "completion") {
return -1;
}
if (theirs->type() == "sentence") {
return -1;
}
return 1;
}
ReverseLookupTranslator::ReverseLookupTranslator(const Ticket& ticket)
: Translator(ticket), tag_("reverse_lookup"), initialized_(false) {
if (ticket.name_space == "translator") {
name_space_ = "reverse_lookup";
}
if (!ticket.schema) return;
Config* config = ticket.schema->config();
config->GetString(name_space_ + "/tag", &tag_);
}
void ReverseLookupTranslator::Initialize() {
initialized_ = true; // no retry
if (!engine_) return;
Ticket ticket(engine_, name_space_);
options_.reset(new TranslatorOptions(ticket));
Config *config = engine_->schema()->config();
if (!config) return;
config->GetString(name_space_ + "/prefix", &prefix_);
config->GetString(name_space_ + "/suffix", &suffix_);
config->GetString(name_space_ + "/tips", &tips_);
{
bool enabled = false;
if (!config->GetBool(name_space_ + "/enable_completion", &enabled))
options_->set_enable_completion(false); // overridden default
}
DictionaryComponent *component =
dynamic_cast<DictionaryComponent*>(Dictionary::Require("dictionary"));
if (!component) return;
dict_.reset(component->Create(ticket));
if (dict_)
dict_->Load();
else
return;
ReverseLookupDictionary::Component *rev_component =
ReverseLookupDictionary::Require("reverse_lookup_dictionary");
if (!rev_component) return;
// lookup target defaults to "translator/dictionary"
std::string rev_target("translator");
config->GetString(name_space_ + "/target", &rev_target);
Ticket rev_ticket(engine_, rev_target);
rev_dict_.reset(rev_component->Create(rev_ticket));
if (rev_dict_)
rev_dict_->Load();
}
shared_ptr<Translation> ReverseLookupTranslator::Query(const std::string &input,
const Segment &segment,
std::string* prompt) {
if (!segment.HasTag(tag_))
return shared_ptr<Translation>();
if (!initialized_) Initialize(); // load reverse dict at first use
if (!dict_ || !dict_->loaded())
return shared_ptr<Translation>();
DLOG(INFO) << "input = '" << input
<< "', [" << segment.start << ", " << segment.end << ")";
const std::string& preedit(input);
size_t start = 0;
if (!prefix_.empty() && boost::starts_with(input, prefix_))
start = prefix_.length();
std::string code(input.substr(start));
if (!suffix_.empty() && boost::ends_with(code, suffix_))
code.resize(code.length() - suffix_.length());
if (start > 0 && prompt) {
*prompt = tips_;
}
DictEntryIterator iter;
bool quality = false;
if (start < input.length()) {
if (options_ && options_->enable_completion()) {
dict_->LookupWords(&iter, code, true, 100);
quality = !iter.exhausted() &&
(iter.Peek()->remaining_code_length == 0);
}
else {
// 2012-04-08 gongchen: fetch multi-syllable words from rev-lookup table
SyllableGraph graph;
Syllabifier syllabifier("", true, options_->strict_spelling());
size_t consumed = syllabifier.BuildSyllableGraph(code,
*dict_->prism(),
&graph);
if (consumed == code.length()) {
shared_ptr<DictEntryCollector> collector = dict_->Lookup(graph, 0);
if (collector && !collector->empty() &&
collector->rbegin()->first == consumed) {
iter = collector->rbegin()->second;
quality = !graph.vertices.empty() &&
(graph.vertices.rbegin()->second == kNormalSpelling);
}
}
}
}
if (!iter.exhausted()) {
return New<CacheTranslation>(
boost::make_shared<ReverseLookupTranslation>(
rev_dict_.get(),
options_.get(),
code,
segment.start,
segment.end,
preedit,
iter, quality));
}
return shared_ptr<Translation>();
}
} // namespace rime
|