Skip to content

Commit

Permalink
Sync engine source with fcitx5-mcbopomofo
Browse files Browse the repository at this point in the history
  • Loading branch information
lukhnos committed Jul 15, 2022
1 parent 4b1090b commit 6d69ca3
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 39 deletions.
23 changes: 11 additions & 12 deletions Source/Engine/AssociatedPhrases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@

#include "AssociatedPhrases.h"

#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <fstream>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>

#include "KeyValueBlobReader.h"

namespace McBopomofo {

AssociatedPhrases::AssociatedPhrases()
: fd(-1)
, data(0)
, length(0)
: fd(-1)
, data(0)
, length(0)
{
}

Expand All @@ -51,15 +51,15 @@ AssociatedPhrases::~AssociatedPhrases()
}
}

const bool AssociatedPhrases::isLoaded()
bool AssociatedPhrases::isLoaded()
{
if (data) {
return true;
}
return false;
}

bool AssociatedPhrases::open(const char *path)
bool AssociatedPhrases::open(const char* path)
{
if (data) {
return false;
Expand Down Expand Up @@ -87,8 +87,7 @@ bool AssociatedPhrases::open(const char *path)

KeyValueBlobReader reader(static_cast<char*>(data), length);
KeyValueBlobReader::KeyValue keyValue;
KeyValueBlobReader::State state;
while ((state = reader.Next(&keyValue)) == KeyValueBlobReader::State::HAS_PAIR) {
while (reader.Next(&keyValue) == KeyValueBlobReader::State::HAS_PAIR) {
keyRowMap[keyValue.key].emplace_back(keyValue.key, keyValue.value);
}
return true;
Expand All @@ -113,15 +112,15 @@ const std::vector<std::string> AssociatedPhrases::valuesForKey(const std::string
const std::vector<Row>& rows = iter->second;
for (const auto& row : rows) {
std::string_view value = row.value;
v.push_back({value.data(), value.size()});
v.push_back({ value.data(), value.size() });
}
}
return v;
}

const bool AssociatedPhrases::hasValuesForKey(const std::string& key)
bool AssociatedPhrases::hasValuesForKey(const std::string& key)
{
return keyRowMap.find(key) != keyRowMap.end();
}

}; // namespace McBopomofo
} // namespace McBopomofo
23 changes: 13 additions & 10 deletions Source/Engine/AssociatedPhrases.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,42 @@
#ifndef ASSOCIATEDPHRASES_H
#define ASSOCIATEDPHRASES_H

#include <string>
#include <map>
#include <iostream>
#include <map>
#include <string>
#include <vector>

namespace McBopomofo {

class AssociatedPhrases
{
class AssociatedPhrases {
public:
AssociatedPhrases();
~AssociatedPhrases();

const bool isLoaded();
bool open(const char *path);
bool isLoaded();
bool open(const char* path);
void close();
const std::vector<std::string> valuesForKey(const std::string& key);
const bool hasValuesForKey(const std::string& key);
bool hasValuesForKey(const std::string& key);

protected:
struct Row {
Row(std::string_view& k, std::string_view& v) : key(k), value(v) {}
Row(std::string_view& k, std::string_view& v)
: key(k)
, value(v)
{
}
std::string_view key;
std::string_view value;
};

std::map<std::string_view, std::vector<Row>> keyRowMap;

int fd;
void *data;
void* data;
size_t length;
};

}

#endif /* AssociatedPhrases_hpp */
#endif // ASSOCIATEDPHRASES_H
2 changes: 2 additions & 0 deletions Source/Engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ add_subdirectory(gramambular2)

include_directories("Gramambular")
add_library(McBopomofoLMLib
AssociatedPhrases.h
AssociatedPhrases.cpp
KeyValueBlobReader.cpp
KeyValueBlobReader.h
ParselessPhraseDB.cpp
Expand Down
1 change: 1 addition & 0 deletions Source/Engine/Mandarin/.clang-format
9 changes: 3 additions & 6 deletions Source/Engine/Mandarin/Mandarin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ namespace Mandarin {

class PinyinParseHelper {
public:
static const bool ConsumePrefix(std::string& target,
const std::string& prefix) {
static bool ConsumePrefix(std::string& target, const std::string& prefix) {
if (target.length() < prefix.length()) {
return false;
}
Expand Down Expand Up @@ -601,10 +600,10 @@ const BPMF BPMF::FromComposedString(const std::string& str) {
const std::string BPMF::composedString() const {
std::string result;
#define APPEND(c) \
if (syllable_ & c) \
if (syllable_ & c) \
result += \
(*BopomofoCharacterMap::SharedInstance().componentToCharacter.find( \
syllable_ & c)) \
syllable_ & c)) \
.second
APPEND(ConsonantMask);
APPEND(MiddleVowelMask);
Expand All @@ -614,8 +613,6 @@ const std::string BPMF::composedString() const {
return result;
}



const BopomofoCharacterMap& BopomofoCharacterMap::SharedInstance() {
static BopomofoCharacterMap* map = new BopomofoCharacterMap();
return *map;
Expand Down
21 changes: 10 additions & 11 deletions Source/Engine/Mandarin/Mandarin.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ class BopomofoSyllable {

Component consonantComponent() const { return syllable_ & ConsonantMask; }

Component middleVowelComponent() const {
return syllable_ & MiddleVowelMask;
}
Component middleVowelComponent() const { return syllable_ & MiddleVowelMask; }

Component vowelComponent() const { return syllable_ & VowelMask; }

Expand Down Expand Up @@ -113,8 +111,8 @@ class BopomofoSyllable {

const BopomofoSyllable operator+(const BopomofoSyllable& another) const {
Component newSyllable = syllable_;
#define OP_SOVER(mask) \
if (another.syllable_ & mask) { \
#define OP_SOVER(mask) \
if (another.syllable_ & mask) { \
newSyllable = (newSyllable & ~mask) | (another.syllable_ & mask); \
}
OP_SOVER(ConsonantMask);
Expand All @@ -126,8 +124,8 @@ class BopomofoSyllable {
}

BopomofoSyllable& operator+=(const BopomofoSyllable& another) {
#define OPE_SOVER(mask) \
if (another.syllable_ & mask) { \
#define OPE_SOVER(mask) \
if (another.syllable_ & mask) { \
syllable_ = (syllable_ & ~mask) | (another.syllable_ & mask); \
}
OPE_SOVER(ConsonantMask);
Expand Down Expand Up @@ -183,7 +181,7 @@ class BopomofoKeyboardLayout {

BopomofoKeyboardLayout(const BopomofoKeyToComponentMap& ktcm,
const std::string& name)
: m_keyToComponent(ktcm), m_name(name) {
: m_name(name), m_keyToComponent(ktcm) {
for (BopomofoKeyToComponentMap::const_iterator miter =
m_keyToComponent.begin();
miter != m_keyToComponent.end(); ++miter)
Expand Down Expand Up @@ -382,6 +380,8 @@ class BopomofoReadingBuffer {
}
}

const BopomofoKeyboardLayout* keyboardLayout() const { return layout_; }

bool isValidKey(char k) const {
if (!pinyin_mode_) {
return layout_ ? (layout_->keyToComponents(k)).size() > 0 : false;
Expand Down Expand Up @@ -468,11 +468,10 @@ class BopomofoReadingBuffer {

bool hasToneMarkerOnly() const {
return syllable_.hasToneMarker() &&
!(syllable_.hasConsonant() || syllable_.hasMiddleVowel() ||
syllable_.hasVowel());
!(syllable_.hasConsonant() || syllable_.hasMiddleVowel() ||
syllable_.hasVowel());
}


protected:
const BopomofoKeyboardLayout* layout_;
BPMF syllable_;
Expand Down

0 comments on commit 6d69ca3

Please sign in to comment.