Skip to content

Commit

Permalink
Merge remote-tracking branch 'fork/master' into disconnect_userdict
Browse files Browse the repository at this point in the history
  • Loading branch information
shewer committed Jun 20, 2024
2 parents 78f182d + 2f89098 commit 8c293fc
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linux-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:

jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Build dependencies
- compiler with C++17 support
- cmake>=3.12
- libboost>=1.74
- libglog (optional)
- libglog>=0.7 (optional)
- libleveldb
- libmarisa
- libopencc>=1.0.2
Expand Down Expand Up @@ -75,6 +75,7 @@ Official:
Community:
- [emacs-rime](https://github.com/DogLooksGood/emacs-rime): frontend for Emacs
- [coc-rime](https://github.com/tonyfettes/coc-rime): frontend for Vim
- [zsh-rime](https://github.com/Freed-Wu/zsh-rime): frontend for Zsh
- [fcitx-rime](https://github.com/fcitx/fcitx-rime): Fcitx frontend for Linux
- [fcitx5-rime](https://github.com/fcitx/fcitx5-rime): Fcitx5 frontend for Linux
- [Hamster](https://github.com/imfuxiao/Hamster): frontend for iOS
Expand Down
2 changes: 1 addition & 1 deletion src/rime/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
#include <list>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <utility>
#include <unordered_map>
#include <unordered_set>
#include <utility>
Expand Down
5 changes: 3 additions & 2 deletions src/rime/config/config_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,16 @@ bool ConfigData::LoadFromFile(const path& file_path, ConfigCompiler* compiler) {
modified_ = false;
root.reset();
if (!std::filesystem::exists(file_path)) {
LOG(WARNING) << "nonexistent config file '" << file_path << "'.";
if (!boost::ends_with(file_path.u8string(), ".custom.yaml"))
LOG(WARNING) << "nonexistent config file '" << file_path << "'.";
return false;
}
LOG(INFO) << "loading config file '" << file_path << "'.";
try {
YAML::Node doc = YAML::LoadFile(file_path.string());
root = ConvertFromYaml(doc, compiler);
} catch (YAML::Exception& e) {
LOG(ERROR) << "Error parsing YAML: " << e.what();
LOG(ERROR) << "Error parsing YAML \"" << file_path << "\" : " << e.what();
return false;
}
return true;
Expand Down
22 changes: 11 additions & 11 deletions src/rime/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,27 +143,27 @@ bool Context::Highlight(size_t index) {
return true;
}

bool Context::DeleteCandidate(
function<an<Candidate>(Segment& seg)> get_candidate) {
bool Context::DeleteCandidate(std::optional<size_t> index) {
if (composition_.empty())
return false;
Segment& seg(composition_.back());
if (auto cand = get_candidate(seg)) {
DLOG(INFO) << "Deleting candidate: '" << cand->text();
delete_notifier_(this);
return true; // CAVEAT: this doesn't mean anything is deleted for sure
auto cand = index ? seg.GetCandidateAt(*index) : seg.GetSelectedCandidate();
if (!cand)
return false;
DLOG(INFO) << "Deleting candidate: " << cand->text();
if (index) {
seg.selected_index = *index;
}
return false;
delete_notifier_(this);
return true; // CAVEAT: this doesn't mean anything is deleted for sure
}

bool Context::DeleteCandidate(size_t index) {
return DeleteCandidate(
[index](Segment& seg) { return seg.GetCandidateAt(index); });
return DeleteCandidate(std::optional{index});
}

bool Context::DeleteCurrentSelection() {
return DeleteCandidate(
[](Segment& seg) { return seg.GetSelectedCandidate(); });
return DeleteCandidate({});
}

bool Context::ConfirmCurrentSelection() {
Expand Down
4 changes: 3 additions & 1 deletion src/rime/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class RIME_API Context {
bool get_option(const string& name) const;
void set_property(const string& name, const string& value);
string get_property(const string& name) const;
const map<string, bool>& options() const { return options_; }
const map<string, string>& properties() const { return properties_; }
// options and properties starting with '_' are local to schema;
// others are session scoped.
void ClearTransientOptions();
Expand All @@ -92,7 +94,7 @@ class RIME_API Context {

private:
string GetSoftCursor() const;
bool DeleteCandidate(function<an<Candidate>(Segment& seg)> get_candidate);
bool DeleteCandidate(std::optional<size_t> index);

string input_;
size_t caret_pos_ = 0;
Expand Down
18 changes: 14 additions & 4 deletions src/rime/dict/entry_collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ void EntryCollector::LoadPresetVocabulary(DictSettings* settings) {

void EntryCollector::Collect(const path& dict_file) {
LOG(INFO) << "collecting entries from " << dict_file;
current_dict_file = dict_file.u8string();
line_number = 0;
// read table
std::ifstream fin(dict_file.c_str());
DictSettings settings;
Expand All @@ -69,13 +71,15 @@ void EntryCollector::Collect(const path& dict_file) {
int weight_column = settings.GetColumnIndex("weight");
int stem_column = settings.GetColumnIndex("stem");
if (text_column == -1) {
LOG(ERROR) << "missing text column definition.";
LOG(ERROR) << "missing text column definition in file: " << dict_file
<< ".";
return;
}
bool enable_comment = true;
string line;
while (getline(fin, line)) {
boost::algorithm::trim_right(line);
line_number++;
// skip empty lines and comments
if (line.empty())
continue;
Expand All @@ -90,7 +94,9 @@ void EntryCollector::Collect(const path& dict_file) {
auto row = strings::split(line, "\t");
int num_columns = static_cast<int>(row.size());
if (num_columns <= text_column || row[text_column].empty()) {
LOG(WARNING) << "Missing entry text at #" << num_entries << ".";
LOG(WARNING) << "Missing entry text at #" << num_entries
<< ", line: " << line_number
<< " of file: " << current_dict_file << ".";
continue;
}
const auto& word(row[text_column]);
Expand Down Expand Up @@ -168,15 +174,19 @@ void EntryCollector::CreateEntry(const string& word,
try {
percentage = std::stod(weight_str.substr(0, weight_str.length() - 1));
} catch (...) {
LOG(WARNING) << "invalid entry definition at #" << num_entries << ".";
LOG(WARNING) << "invalid entry definition at #" << num_entries
<< ", line: " << line_number
<< " of file: " << current_dict_file << ".";
percentage = 100.0;
}
e->weight *= percentage / 100.0;
} else if (!weight_str.empty()) { // absolute weight
try {
e->weight = std::stod(weight_str);
} catch (...) {
LOG(WARNING) << "invalid entry definition at #" << num_entries << ".";
LOG(WARNING) << "invalid entry definition at #" << num_entries
<< ", line: " << line_number
<< " of file: " << current_dict_file << ".";
e->weight = 0.0;
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/rime/dict/entry_collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class EntryCollector : public PhraseCollector {
set<string /* word */> collection;
WordMap words;
WeightMap total_weight;

private:
string current_dict_file;
size_t line_number;
};

} // namespace rime
Expand Down
5 changes: 4 additions & 1 deletion src/rime/gear/matcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ Matcher::Matcher(const Ticket& ticket) : Segmentor(ticket) {
// read schema settings
if (!ticket.schema)
return;
if (name_space_ == "segmentor") {
name_space_ = "recognizer";
}
Config* config = ticket.schema->config();
patterns_.LoadConfig(config);
patterns_.LoadConfig(config, name_space_);
}

bool Matcher::Proceed(Segmentation* segmentation) {
Expand Down
11 changes: 7 additions & 4 deletions src/rime/gear/recognizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ static void load_patterns(RecognizerPatterns* patterns, an<ConfigMap> map) {
}
}

void RecognizerPatterns::LoadConfig(Config* config) {
load_patterns(this, config->GetMap("recognizer/patterns"));
void RecognizerPatterns::LoadConfig(Config* config, const string& name_space) {
load_patterns(this, config->GetMap(name_space + "/patterns"));
}

RecognizerMatch RecognizerPatterns::GetMatch(
Expand Down Expand Up @@ -72,9 +72,12 @@ RecognizerMatch RecognizerPatterns::GetMatch(
Recognizer::Recognizer(const Ticket& ticket) : Processor(ticket) {
if (!ticket.schema)
return;
if (name_space_ == "processor") {
name_space_ = "recognizer";
}
if (Config* config = ticket.schema->config()) {
patterns_.LoadConfig(config);
config->GetBool("recognizer/use_space", &use_space_);
patterns_.LoadConfig(config, name_space_);
config->GetBool(name_space_ + "/use_space", &use_space_);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/rime/gear/recognizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct RecognizerMatch {

class RecognizerPatterns : public map<string, boost::regex> {
public:
void LoadConfig(Config* config);
void LoadConfig(Config* config, const string& name_space);
RecognizerMatch GetMatch(const string& input,
const Segmentation& segmentation) const;
};
Expand Down
7 changes: 6 additions & 1 deletion src/rime/gear/speller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@ static inline bool is_table_entry(const an<Candidate>& cand) {
return type == "table" || type == "user_table";
}

static inline bool is_simple_candidate(const an<Candidate>& cand) {
return bool(As<SimpleCandidate>(cand));
}

static bool is_auto_selectable(const an<Candidate>& cand,
const string& input,
const string& delimiters) {
return
// reaches end of input
cand->end() == input.length() && is_table_entry(cand) &&
cand->end() == input.length() &&
(is_table_entry(cand) || is_simple_candidate(cand)) &&
// no delimiters
input.find_first_of(delimiters, cand->start()) == string::npos;
}
Expand Down
6 changes: 5 additions & 1 deletion src/rime/setup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ RIME_API void SetupLogging(const char* app_name,
// Do not allow other users to read/write log files created by current
// process.
FLAGS_logfile_mode = 0600;
google::InitGoogleLogging(app_name);
if (google::IsGoogleLoggingInitialized()) {
LOG(WARNING) << "Glog is already initialized.";
} else {
google::InitGoogleLogging(app_name);
}
#endif // RIME_ENABLE_LOGGING
}

Expand Down
20 changes: 20 additions & 0 deletions tools/rime_api_console.cc
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,26 @@ bool execute_special_command(const char* line, RimeSessionId session_id) {
if (!strcmp(line, "synchronize")) {
return rime->sync_user_data();
}
const char* kDeleteCandidateOnCurrentPage = "delete on current page ";
command_length = strlen(kDeleteCandidateOnCurrentPage);
if (!strncmp(line, kDeleteCandidateOnCurrentPage, command_length)) {
const char* index_str = line + command_length;
int index = atoi(index_str);
if (!rime->delete_candidate_on_current_page(session_id, index)) {
fprintf(stderr, "failed to delete\n");
}
return true;
}
const char* kDeleteCandidate = "delete ";
command_length = strlen(kDeleteCandidate);
if (!strncmp(line, kDeleteCandidate, command_length)) {
const char* index_str = line + command_length;
int index = atoi(index_str);
if (!rime->delete_candidate(session_id, index)) {
fprintf(stderr, "failed to delete\n");
}
return true;
}
return false;
}

Expand Down

0 comments on commit 8c293fc

Please sign in to comment.