Skip to content

Commit

Permalink
Implement action for pinyin (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
wengxt authored May 25, 2024
1 parent 7fd71f2 commit ab4a95b
Show file tree
Hide file tree
Showing 8 changed files with 588 additions and 103 deletions.
66 changes: 58 additions & 8 deletions im/pinyin/customphrase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,32 @@
*
*/
#include "customphrase.h"
#include <algorithm>
#include <cassert>
#include <charconv>
#include <chrono>
#include <cstdint>
#include <ctime>
#include <fcitx-utils/charutils.h>
#include <fcitx-utils/log.h>
#include <fcitx-utils/stringutils.h>
#include <fmt/chrono.h>
#include <fmt/core.h>
#include <fmt/format.h>
#include <functional>
#include <istream>
#include <iterator>
#include <libime/core/datrie.h>
#include <limits>
#include <map>
#include <optional>
#include <ostream>
#include <string>
#include <string_view>
#include <system_error>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

namespace fcitx {

Expand Down Expand Up @@ -178,11 +190,15 @@ std::string toChineseTwoDigitNumber(int num, bool leadingZero) {
return prefix + suffix;
}

bool CustomPhrase::isDynamic() const {
return stringutils::startsWith(value(), "#");
}

std::string CustomPhrase::evaluate(
const std::function<std::string(std::string_view key)> &evaluator) const {
assert(evaluator);

if (!stringutils::startsWith(value_, "#")) {
if (!isDynamic()) {
return value_;
}
std::string_view content = value_;
Expand Down Expand Up @@ -431,18 +447,53 @@ CustomPhraseDict::lookup(std::string_view key) const {
return &data_[index];
}

void CustomPhraseDict::addPhrase(std::string_view key, std::string_view value,
int order) {
std::vector<CustomPhrase> *
CustomPhraseDict::getOrCreateEntry(std::string_view key) {

auto index = index_.exactMatchSearch(key);
if (TrieType::isNoValue(index)) {
if (data_.size() >= std::numeric_limits<int32_t>::max()) {
return;
return nullptr;
}
index = data_.size();
index_.set(key, index);
data_.push_back({});
}
data_[index].push_back(CustomPhrase(order, std::string(value)));
return &data_[index];
}

void CustomPhraseDict::addPhrase(std::string_view key, std::string_view value,
int order) {
if (order == 0) {
return;
}
if (auto *entry = getOrCreateEntry(key)) {
entry->push_back(CustomPhrase(order, std::string(value)));
}
}

void CustomPhraseDict::pinPhrase(std::string_view key, std::string_view value) {
removePhrase(key, value);
if (auto *entry = getOrCreateEntry(key)) {
// enabled item is 1 based.
entry->insert(entry->begin(), CustomPhrase(1, std::string(value)));
normalizeData(*entry);
}
}

void CustomPhraseDict::removePhrase(std::string_view key,
std::string_view value) {

auto index = index_.exactMatchSearch(key);
if (TrieType::isNoValue(index)) {
return;
}

data_[index].erase(std::remove_if(data_[index].begin(), data_[index].end(),
[&value](const CustomPhrase &item) {
return value == item.value();
}),
data_[index].end());
}

void CustomPhraseDict::save(std::ostream &out) const {
Expand All @@ -466,7 +517,7 @@ void CustomPhraseDict::save(std::ostream &out) const {
} else {
out << phrase.value();
}
out << std::endl;
out << '\n';
}
return true;
});
Expand All @@ -476,5 +527,4 @@ void CustomPhraseDict::clear() {
index_.clear();
data_.clear();
}

} // namespace fcitx
13 changes: 12 additions & 1 deletion im/pinyin/customphrase.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
#ifndef _PINYIN_CUSTOMPHRASE_H_
#define _PINYIN_CUSTOMPHRASE_H_

#include <cstddef>
#include <cstdint>
#include <fcitx-utils/macros.h>
#include <functional>
#include <istream>
#include <libime/core/datrie.h>
#include <ostream>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

namespace fcitx {
Expand All @@ -27,6 +33,8 @@ class CustomPhrase {
void setOrder(int order) { order_ = order; }
std::string &mutableValue() { return value_; }

bool isDynamic() const;

std::string evaluate(const std::function<std::string(std::string_view key)>
&evaluator) const;

Expand All @@ -49,6 +57,8 @@ class CustomPhraseDict {
const std::vector<CustomPhrase> *lookup(std::string_view key) const;

void addPhrase(std::string_view key, std::string_view value, int order);
void pinPhrase(std::string_view key, std::string_view value);
void removePhrase(std::string_view key, std::string_view value);

template <typename T>
void foreach(const T &callback) {
Expand All @@ -62,10 +72,11 @@ class CustomPhraseDict {
}

private:
std::vector<CustomPhrase> *getOrCreateEntry(std::string_view key);
TrieType index_;
std::vector<std::vector<CustomPhrase>> data_;
};

} // namespace fcitx

#endif // _PINYIN_SYMBOLDICTIONARY_H_
#endif // _PINYIN_SYMBOLDICTIONARY_H_
Loading

0 comments on commit ab4a95b

Please sign in to comment.