Mandarin // Indentation Changes to Xcode Default.

This commit is contained in:
ShikiSuen 2022-02-11 18:39:28 +08:00
parent a6fbeff683
commit cbb565e9b6
2 changed files with 1573 additions and 1573 deletions

File diff suppressed because it is too large Load Diff

View File

@ -29,176 +29,176 @@ namespace Taiyan {
namespace Mandarin { namespace Mandarin {
class BopomofoSyllable { class BopomofoSyllable {
public: public:
typedef uint16_t Component; typedef uint16_t Component;
explicit BopomofoSyllable(Component syllable = 0) : syllable_(syllable) {} explicit BopomofoSyllable(Component syllable = 0) : syllable_(syllable) {}
BopomofoSyllable(const BopomofoSyllable&) = default; BopomofoSyllable(const BopomofoSyllable&) = default;
BopomofoSyllable(BopomofoSyllable&& another) = default; BopomofoSyllable(BopomofoSyllable&& another) = default;
BopomofoSyllable& operator=(const BopomofoSyllable&) = default; BopomofoSyllable& operator=(const BopomofoSyllable&) = default;
BopomofoSyllable& operator=(BopomofoSyllable&&) = default; BopomofoSyllable& operator=(BopomofoSyllable&&) = default;
// takes the ASCII-form, "v"-tolerant, TW-style Hanyu Pinyin (fong, pong, bong // takes the ASCII-form, "v"-tolerant, TW-style Hanyu Pinyin (fong, pong, bong
// acceptable) // acceptable)
static const BopomofoSyllable FromHanyuPinyin(const std::string& str); static const BopomofoSyllable FromHanyuPinyin(const std::string& str);
// TO DO: Support accented vowels // TO DO: Support accented vowels
const std::string HanyuPinyinString(bool includesTone, const std::string HanyuPinyinString(bool includesTone,
bool useVForUUmlaut) const; bool useVForUUmlaut) const;
// const std::string HanyuPinyinString(bool includesTone, bool useVForUUmlaut, // const std::string HanyuPinyinString(bool includesTone, bool useVForUUmlaut,
// bool composeAccentedVowel) const; // bool composeAccentedVowel) const;
// PHT = Pai-hua-tsi // PHT = Pai-hua-tsi
static const BopomofoSyllable FromPHT(const std::string& str); static const BopomofoSyllable FromPHT(const std::string& str);
const std::string PHTString(bool includesTone) const; const std::string PHTString(bool includesTone) const;
static const BopomofoSyllable FromComposedString(const std::string& str); static const BopomofoSyllable FromComposedString(const std::string& str);
const std::string composedString() const; const std::string composedString() const;
void clear() { syllable_ = 0; } void clear() { syllable_ = 0; }
bool isEmpty() const { return !syllable_; } bool isEmpty() const { return !syllable_; }
bool hasConsonant() const { return !!(syllable_ & ConsonantMask); } bool hasConsonant() const { return !!(syllable_ & ConsonantMask); }
bool hasMiddleVowel() const { return !!(syllable_ & MiddleVowelMask); } bool hasMiddleVowel() const { return !!(syllable_ & MiddleVowelMask); }
bool hasVowel() const { return !!(syllable_ & VowelMask); } bool hasVowel() const { return !!(syllable_ & VowelMask); }
bool hasToneMarker() const { return !!(syllable_ & ToneMarkerMask); } bool hasToneMarker() const { return !!(syllable_ & ToneMarkerMask); }
Component consonantComponent() const { return syllable_ & ConsonantMask; } Component consonantComponent() const { return syllable_ & ConsonantMask; }
Component middleVowelComponent() const { Component middleVowelComponent() const {
return syllable_ & MiddleVowelMask; return syllable_ & MiddleVowelMask;
} }
Component vowelComponent() const { return syllable_ & VowelMask; } Component vowelComponent() const { return syllable_ & VowelMask; }
Component toneMarkerComponent() const { return syllable_ & ToneMarkerMask; } Component toneMarkerComponent() const { return syllable_ & ToneMarkerMask; }
bool operator==(const BopomofoSyllable& another) const { bool operator==(const BopomofoSyllable& another) const {
return syllable_ == another.syllable_; return syllable_ == another.syllable_;
} }
bool operator!=(const BopomofoSyllable& another) const { bool operator!=(const BopomofoSyllable& another) const {
return syllable_ != another.syllable_; return syllable_ != another.syllable_;
} }
bool isOverlappingWith(const BopomofoSyllable& another) const { bool isOverlappingWith(const BopomofoSyllable& another) const {
#define IOW_SAND(mask) ((syllable_ & mask) && (another.syllable_ & mask)) #define IOW_SAND(mask) ((syllable_ & mask) && (another.syllable_ & mask))
return IOW_SAND(ConsonantMask) || IOW_SAND(MiddleVowelMask) || return IOW_SAND(ConsonantMask) || IOW_SAND(MiddleVowelMask) ||
IOW_SAND(VowelMask) || IOW_SAND(ToneMarkerMask); IOW_SAND(VowelMask) || IOW_SAND(ToneMarkerMask);
#undef IOW_SAND #undef IOW_SAND
} }
// consonants J, Q, X all require the existence of vowel I or UE // consonants J, Q, X all require the existence of vowel I or UE
bool belongsToJQXClass() const { bool belongsToJQXClass() const {
Component consonant = syllable_ & ConsonantMask; Component consonant = syllable_ & ConsonantMask;
return (consonant == J || consonant == Q || consonant == X); return (consonant == J || consonant == Q || consonant == X);
} }
// zi, ci, si, chi, chi, shi, ri // zi, ci, si, chi, chi, shi, ri
bool belongsToZCSRClass() const { bool belongsToZCSRClass() const {
Component consonant = syllable_ & ConsonantMask; Component consonant = syllable_ & ConsonantMask;
return (consonant >= ZH && consonant <= S); return (consonant >= ZH && consonant <= S);
} }
Component maskType() const { Component maskType() const {
Component mask = 0; Component mask = 0;
mask |= (syllable_ & ConsonantMask) ? ConsonantMask : 0; mask |= (syllable_ & ConsonantMask) ? ConsonantMask : 0;
mask |= (syllable_ & MiddleVowelMask) ? MiddleVowelMask : 0; mask |= (syllable_ & MiddleVowelMask) ? MiddleVowelMask : 0;
mask |= (syllable_ & VowelMask) ? VowelMask : 0; mask |= (syllable_ & VowelMask) ? VowelMask : 0;
mask |= (syllable_ & ToneMarkerMask) ? ToneMarkerMask : 0; mask |= (syllable_ & ToneMarkerMask) ? ToneMarkerMask : 0;
return mask; return mask;
} }
const BopomofoSyllable operator+(const BopomofoSyllable& another) const { const BopomofoSyllable operator+(const BopomofoSyllable& another) const {
Component newSyllable = syllable_; Component newSyllable = syllable_;
#define OP_SOVER(mask) \ #define OP_SOVER(mask) \
if (another.syllable_ & mask) { \ if (another.syllable_ & mask) { \
newSyllable = (newSyllable & ~mask) | (another.syllable_ & mask); \ newSyllable = (newSyllable & ~mask) | (another.syllable_ & mask); \
} }
OP_SOVER(ConsonantMask); OP_SOVER(ConsonantMask);
OP_SOVER(MiddleVowelMask); OP_SOVER(MiddleVowelMask);
OP_SOVER(VowelMask); OP_SOVER(VowelMask);
OP_SOVER(ToneMarkerMask); OP_SOVER(ToneMarkerMask);
#undef OP_SOVER #undef OP_SOVER
return BopomofoSyllable(newSyllable); return BopomofoSyllable(newSyllable);
} }
BopomofoSyllable& operator+=(const BopomofoSyllable& another) { BopomofoSyllable& operator+=(const BopomofoSyllable& another) {
#define OPE_SOVER(mask) \ #define OPE_SOVER(mask) \
if (another.syllable_ & mask) { \ if (another.syllable_ & mask) { \
syllable_ = (syllable_ & ~mask) | (another.syllable_ & mask); \ syllable_ = (syllable_ & ~mask) | (another.syllable_ & mask); \
} }
OPE_SOVER(ConsonantMask); OPE_SOVER(ConsonantMask);
OPE_SOVER(MiddleVowelMask); OPE_SOVER(MiddleVowelMask);
OPE_SOVER(VowelMask); OPE_SOVER(VowelMask);
OPE_SOVER(ToneMarkerMask); OPE_SOVER(ToneMarkerMask);
#undef OPE_SOVER #undef OPE_SOVER
return *this; return *this;
} }
uint16_t absoluteOrder() const { uint16_t absoluteOrder() const {
// turn BPMF syllable into a 4*14*4*22 number // turn BPMF syllable into a 4*14*4*22 number
return (uint16_t)(syllable_ & ConsonantMask) + return (uint16_t)(syllable_ & ConsonantMask) +
(uint16_t)((syllable_ & MiddleVowelMask) >> 5) * 22 + (uint16_t)((syllable_ & MiddleVowelMask) >> 5) * 22 +
(uint16_t)((syllable_ & VowelMask) >> 7) * 22 * 4 + (uint16_t)((syllable_ & VowelMask) >> 7) * 22 * 4 +
(uint16_t)((syllable_ & ToneMarkerMask) >> 11) * 22 * 4 * 14; (uint16_t)((syllable_ & ToneMarkerMask) >> 11) * 22 * 4 * 14;
} }
const std::string absoluteOrderString() const { const std::string absoluteOrderString() const {
// 5*14*4*22 = 6160, we use a 79*79 encoding to represent that // 5*14*4*22 = 6160, we use a 79*79 encoding to represent that
uint16_t order = absoluteOrder(); uint16_t order = absoluteOrder();
char low = 48 + static_cast<char>(order % 79); char low = 48 + static_cast<char>(order % 79);
char high = 48 + static_cast<char>(order / 79); char high = 48 + static_cast<char>(order / 79);
std::string result(2, ' '); std::string result(2, ' ');
result[0] = low; result[0] = low;
result[1] = high; result[1] = high;
return result; return result;
} }
static BopomofoSyllable FromAbsoluteOrder(uint16_t order) { static BopomofoSyllable FromAbsoluteOrder(uint16_t order) {
return BopomofoSyllable((order % 22) | ((order / 22) % 4) << 5 | return BopomofoSyllable((order % 22) | ((order / 22) % 4) << 5 |
((order / (22 * 4)) % 14) << 7 | ((order / (22 * 4)) % 14) << 7 |
((order / (22 * 4 * 14)) % 5) << 11); ((order / (22 * 4 * 14)) % 5) << 11);
} }
static BopomofoSyllable FromAbsoluteOrderString(const std::string& str) { static BopomofoSyllable FromAbsoluteOrderString(const std::string& str) {
if (str.length() != 2) return BopomofoSyllable(); if (str.length() != 2) return BopomofoSyllable();
return FromAbsoluteOrder((uint16_t)(str[1] - 48) * 79 + return FromAbsoluteOrder((uint16_t)(str[1] - 48) * 79 +
(uint16_t)(str[0] - 48)); (uint16_t)(str[0] - 48));
} }
friend std::ostream& operator<<(std::ostream& stream, friend std::ostream& operator<<(std::ostream& stream,
const BopomofoSyllable& syllable); const BopomofoSyllable& syllable);
static constexpr Component static constexpr Component
ConsonantMask = 0x001f, // 0000 0000 0001 1111, 21 consonants ConsonantMask = 0x001f, // 0000 0000 0001 1111, 21 consonants
MiddleVowelMask = 0x0060, // 0000 0000 0110 0000, 3 middle vowels MiddleVowelMask = 0x0060, // 0000 0000 0110 0000, 3 middle vowels
VowelMask = 0x0780, // 0000 0111 1000 0000, 13 vowels VowelMask = 0x0780, // 0000 0111 1000 0000, 13 vowels
ToneMarkerMask = 0x3800, // 0011 1000 0000 0000, 5 tones (tone1 = 0x00) ToneMarkerMask = 0x3800, // 0011 1000 0000 0000, 5 tones (tone1 = 0x00)
B = 0x0001, P = 0x0002, M = 0x0003, F = 0x0004, D = 0x0005, T = 0x0006, B = 0x0001, P = 0x0002, M = 0x0003, F = 0x0004, D = 0x0005, T = 0x0006,
N = 0x0007, L = 0x0008, G = 0x0009, K = 0x000a, H = 0x000b, J = 0x000c, N = 0x0007, L = 0x0008, G = 0x0009, K = 0x000a, H = 0x000b, J = 0x000c,
Q = 0x000d, X = 0x000e, ZH = 0x000f, CH = 0x0010, SH = 0x0011, R = 0x0012, Q = 0x000d, X = 0x000e, ZH = 0x000f, CH = 0x0010, SH = 0x0011, R = 0x0012,
Z = 0x0013, C = 0x0014, S = 0x0015, I = 0x0020, U = 0x0040, Z = 0x0013, C = 0x0014, S = 0x0015, I = 0x0020, U = 0x0040,
UE = 0x0060, // ue = u umlaut (we use the German convention here as an UE = 0x0060, // ue = u umlaut (we use the German convention here as an
// ersatz to the /ju:/ sound) // ersatz to the /ju:/ sound)
A = 0x0080, O = 0x0100, ER = 0x0180, E = 0x0200, AI = 0x0280, EI = 0x0300, A = 0x0080, O = 0x0100, ER = 0x0180, E = 0x0200, AI = 0x0280, EI = 0x0300,
AO = 0x0380, OU = 0x0400, AN = 0x0480, EN = 0x0500, ANG = 0x0580, AO = 0x0380, OU = 0x0400, AN = 0x0480, EN = 0x0500, ANG = 0x0580,
ENG = 0x0600, ERR = 0x0680, Tone1 = 0x0000, Tone2 = 0x0800, ENG = 0x0600, ERR = 0x0680, Tone1 = 0x0000, Tone2 = 0x0800,
Tone3 = 0x1000, Tone4 = 0x1800, Tone5 = 0x2000; Tone3 = 0x1000, Tone4 = 0x1800, Tone5 = 0x2000;
protected: protected:
Component syllable_; Component syllable_;
}; };
inline std::ostream& operator<<(std::ostream& stream, inline std::ostream& operator<<(std::ostream& stream,
const BopomofoSyllable& syllable) { const BopomofoSyllable& syllable) {
stream << syllable.composedString(); stream << syllable.composedString();
return stream; return stream;
} }
typedef BopomofoSyllable BPMF; typedef BopomofoSyllable BPMF;
@ -207,309 +207,308 @@ typedef std::map<char, std::vector<BPMF::Component> > BopomofoKeyToComponentMap;
typedef std::map<BPMF::Component, char> BopomofoComponentToKeyMap; typedef std::map<BPMF::Component, char> BopomofoComponentToKeyMap;
class BopomofoKeyboardLayout { class BopomofoKeyboardLayout {
public: public:
static const BopomofoKeyboardLayout* StandardLayout(); static const BopomofoKeyboardLayout* StandardLayout();
static const BopomofoKeyboardLayout* ETenLayout(); static const BopomofoKeyboardLayout* ETenLayout();
static const BopomofoKeyboardLayout* HsuLayout(); static const BopomofoKeyboardLayout* HsuLayout();
static const BopomofoKeyboardLayout* ETen26Layout(); static const BopomofoKeyboardLayout* ETen26Layout();
static const BopomofoKeyboardLayout* IBMLayout(); static const BopomofoKeyboardLayout* IBMLayout();
static const BopomofoKeyboardLayout* HanyuPinyinLayout(); static const BopomofoKeyboardLayout* HanyuPinyinLayout();
BopomofoKeyboardLayout(const BopomofoKeyToComponentMap& ktcm, BopomofoKeyboardLayout(const BopomofoKeyToComponentMap& ktcm,
const std::string& name) const std::string& name)
: m_keyToComponent(ktcm), m_name(name) { : m_keyToComponent(ktcm), m_name(name) {
for (BopomofoKeyToComponentMap::const_iterator miter = for (BopomofoKeyToComponentMap::const_iterator miter =
m_keyToComponent.begin(); m_keyToComponent.begin();
miter != m_keyToComponent.end(); ++miter) miter != m_keyToComponent.end(); ++miter)
for (std::vector<BPMF::Component>::const_iterator viter = for (std::vector<BPMF::Component>::const_iterator viter =
(*miter).second.begin(); (*miter).second.begin();
viter != (*miter).second.end(); ++viter) viter != (*miter).second.end(); ++viter)
m_componentToKey[*viter] = (*miter).first; m_componentToKey[*viter] = (*miter).first;
} }
const std::string name() const { return m_name; } const std::string name() const { return m_name; }
char componentToKey(BPMF::Component component) const { char componentToKey(BPMF::Component component) const {
BopomofoComponentToKeyMap::const_iterator iter = BopomofoComponentToKeyMap::const_iterator iter =
m_componentToKey.find(component); m_componentToKey.find(component);
return (iter == m_componentToKey.end()) ? 0 : (*iter).second; return (iter == m_componentToKey.end()) ? 0 : (*iter).second;
} }
const std::vector<BPMF::Component> keyToComponents(char key) const { const std::vector<BPMF::Component> keyToComponents(char key) const {
BopomofoKeyToComponentMap::const_iterator iter = m_keyToComponent.find(key); BopomofoKeyToComponentMap::const_iterator iter = m_keyToComponent.find(key);
return (iter == m_keyToComponent.end()) ? std::vector<BPMF::Component>() return (iter == m_keyToComponent.end()) ? std::vector<BPMF::Component>()
: (*iter).second; : (*iter).second;
} }
const std::string keySequenceFromSyllable(BPMF syllable) const { const std::string keySequenceFromSyllable(BPMF syllable) const {
std::string sequence; std::string sequence;
BPMF::Component c; BPMF::Component c;
char k; char k;
#define STKS_COMBINE(component) \ #define STKS_COMBINE(component) \
if ((c = component)) { \ if ((c = component)) { \
if ((k = componentToKey(c))) sequence += std::string(1, k); \ if ((k = componentToKey(c))) sequence += std::string(1, k); \
} }
STKS_COMBINE(syllable.consonantComponent()); STKS_COMBINE(syllable.consonantComponent());
STKS_COMBINE(syllable.middleVowelComponent()); STKS_COMBINE(syllable.middleVowelComponent());
STKS_COMBINE(syllable.vowelComponent()); STKS_COMBINE(syllable.vowelComponent());
STKS_COMBINE(syllable.toneMarkerComponent()); STKS_COMBINE(syllable.toneMarkerComponent());
#undef STKS_COMBINE #undef STKS_COMBINE
return sequence; return sequence;
}
const BPMF syllableFromKeySequence(const std::string& sequence) const {
BPMF syllable;
for (std::string::const_iterator iter = sequence.begin();
iter != sequence.end(); ++iter) {
bool beforeSeqHasIorUE = sequenceContainsIorUE(sequence.begin(), iter);
bool aheadSeqHasIorUE = sequenceContainsIorUE(iter + 1, sequence.end());
std::vector<BPMF::Component> components = keyToComponents(*iter);
if (!components.size()) continue;
if (components.size() == 1) {
syllable += BPMF(components[0]);
continue;
}
BPMF head = BPMF(components[0]);
BPMF follow = BPMF(components[1]);
BPMF ending = components.size() > 2 ? BPMF(components[2]) : follow;
// apply the I/UE + E rule
if (head.vowelComponent() == BPMF::E &&
follow.vowelComponent() != BPMF::E) {
syllable += beforeSeqHasIorUE ? head : follow;
continue;
}
if (head.vowelComponent() != BPMF::E &&
follow.vowelComponent() == BPMF::E) {
syllable += beforeSeqHasIorUE ? follow : head;
continue;
}
// apply the J/Q/X + I/UE rule, only two components are allowed in the
// components vector here
if (head.belongsToJQXClass() && !follow.belongsToJQXClass()) {
if (!syllable.isEmpty()) {
if (ending != follow) syllable += ending;
} else {
syllable += aheadSeqHasIorUE ? head : follow;
}
continue;
}
if (!head.belongsToJQXClass() && follow.belongsToJQXClass()) {
if (!syllable.isEmpty()) {
if (ending != follow) syllable += ending;
} else {
syllable += aheadSeqHasIorUE ? follow : head;
}
continue;
}
// the nasty issue of only one char in the buffer
if (iter == sequence.begin() && iter + 1 == sequence.end()) {
if (head.hasVowel() || follow.hasToneMarker() ||
head.belongsToZCSRClass()) {
syllable += head;
} else {
if (follow.hasVowel() || ending.hasToneMarker()) {
syllable += follow;
} else {
syllable += ending;
}
}
continue;
}
if (!(syllable.maskType() & head.maskType()) &&
!endAheadOrAheadHasToneMarkKey(iter + 1, sequence.end())) {
syllable += head;
} else {
if (endAheadOrAheadHasToneMarkKey(iter + 1, sequence.end()) &&
head.belongsToZCSRClass() && syllable.isEmpty()) {
syllable += head;
} else if (syllable.maskType() < follow.maskType()) {
syllable += follow;
} else {
syllable += ending;
}
}
} }
// heuristics for Hsu keyboard layout const BPMF syllableFromKeySequence(const std::string& sequence) const {
if (this == HsuLayout()) { BPMF syllable;
// fix the left out L to ERR when it has sound, and GI, GUE -> JI, JUE
if (syllable.vowelComponent() == BPMF::ENG && !syllable.hasConsonant() && for (std::string::const_iterator iter = sequence.begin();
!syllable.hasMiddleVowel()) { iter != sequence.end(); ++iter) {
syllable += BPMF(BPMF::ERR); bool beforeSeqHasIorUE = sequenceContainsIorUE(sequence.begin(), iter);
} else if (syllable.consonantComponent() == BPMF::G && bool aheadSeqHasIorUE = sequenceContainsIorUE(iter + 1, sequence.end());
(syllable.middleVowelComponent() == BPMF::I ||
syllable.middleVowelComponent() == BPMF::UE)) { std::vector<BPMF::Component> components = keyToComponents(*iter);
syllable += BPMF(BPMF::J);
} if (!components.size()) continue;
if (components.size() == 1) {
syllable += BPMF(components[0]);
continue;
}
BPMF head = BPMF(components[0]);
BPMF follow = BPMF(components[1]);
BPMF ending = components.size() > 2 ? BPMF(components[2]) : follow;
// apply the I/UE + E rule
if (head.vowelComponent() == BPMF::E &&
follow.vowelComponent() != BPMF::E) {
syllable += beforeSeqHasIorUE ? head : follow;
continue;
}
if (head.vowelComponent() != BPMF::E &&
follow.vowelComponent() == BPMF::E) {
syllable += beforeSeqHasIorUE ? follow : head;
continue;
}
// apply the J/Q/X + I/UE rule, only two components are allowed in the
// components vector here
if (head.belongsToJQXClass() && !follow.belongsToJQXClass()) {
if (!syllable.isEmpty()) {
if (ending != follow) syllable += ending;
} else {
syllable += aheadSeqHasIorUE ? head : follow;
}
continue;
}
if (!head.belongsToJQXClass() && follow.belongsToJQXClass()) {
if (!syllable.isEmpty()) {
if (ending != follow) syllable += ending;
} else {
syllable += aheadSeqHasIorUE ? follow : head;
}
continue;
}
// the nasty issue of only one char in the buffer
if (iter == sequence.begin() && iter + 1 == sequence.end()) {
if (head.hasVowel() || follow.hasToneMarker() ||
head.belongsToZCSRClass()) {
syllable += head;
} else {
if (follow.hasVowel() || ending.hasToneMarker()) {
syllable += follow;
} else {
syllable += ending;
}
}
continue;
}
if (!(syllable.maskType() & head.maskType()) &&
!endAheadOrAheadHasToneMarkKey(iter + 1, sequence.end())) {
syllable += head;
} else {
if (endAheadOrAheadHasToneMarkKey(iter + 1, sequence.end()) &&
head.belongsToZCSRClass() && syllable.isEmpty()) {
syllable += head;
} else if (syllable.maskType() < follow.maskType()) {
syllable += follow;
} else {
syllable += ending;
}
}
}
// heuristics for Hsu keyboard layout
if (this == HsuLayout()) {
// fix the left out L to ERR when it has sound, and GI, GUE -> JI, JUE
if (syllable.vowelComponent() == BPMF::ENG && !syllable.hasConsonant() &&
!syllable.hasMiddleVowel()) {
syllable += BPMF(BPMF::ERR);
} else if (syllable.consonantComponent() == BPMF::G &&
(syllable.middleVowelComponent() == BPMF::I ||
syllable.middleVowelComponent() == BPMF::UE)) {
syllable += BPMF(BPMF::J);
}
}
return syllable;
} }
return syllable; protected:
} bool endAheadOrAheadHasToneMarkKey(std::string::const_iterator ahead,
std::string::const_iterator end) const {
if (ahead == end) return true;
protected: char tone1 = componentToKey(BPMF::Tone1);
bool endAheadOrAheadHasToneMarkKey(std::string::const_iterator ahead, char tone2 = componentToKey(BPMF::Tone2);
std::string::const_iterator end) const { char tone3 = componentToKey(BPMF::Tone3);
if (ahead == end) return true; char tone4 = componentToKey(BPMF::Tone4);
char tone5 = componentToKey(BPMF::Tone5);
char tone1 = componentToKey(BPMF::Tone1); if (tone1)
char tone2 = componentToKey(BPMF::Tone2); if (*ahead == tone1) return true;
char tone3 = componentToKey(BPMF::Tone3);
char tone4 = componentToKey(BPMF::Tone4);
char tone5 = componentToKey(BPMF::Tone5);
if (tone1) if (*ahead == tone2 || *ahead == tone3 || *ahead == tone4 ||
if (*ahead == tone1) return true; *ahead == tone5)
return true;
if (*ahead == tone2 || *ahead == tone3 || *ahead == tone4 || return false;
*ahead == tone5) }
return true;
return false; bool sequenceContainsIorUE(std::string::const_iterator start,
} std::string::const_iterator end) const {
char iChar = componentToKey(BPMF::I);
char ueChar = componentToKey(BPMF::UE);
bool sequenceContainsIorUE(std::string::const_iterator start, for (; start != end; ++start)
std::string::const_iterator end) const { if (*start == iChar || *start == ueChar) return true;
char iChar = componentToKey(BPMF::I); return false;
char ueChar = componentToKey(BPMF::UE); }
for (; start != end; ++start) std::string m_name;
if (*start == iChar || *start == ueChar) return true; BopomofoKeyToComponentMap m_keyToComponent;
return false; BopomofoComponentToKeyMap m_componentToKey;
}
std::string m_name;
BopomofoKeyToComponentMap m_keyToComponent;
BopomofoComponentToKeyMap m_componentToKey;
}; };
class BopomofoReadingBuffer { class BopomofoReadingBuffer {
public: public:
explicit BopomofoReadingBuffer(const BopomofoKeyboardLayout* layout) explicit BopomofoReadingBuffer(const BopomofoKeyboardLayout* layout)
: layout_(layout), pinyin_mode_(false) { : layout_(layout), pinyin_mode_(false) {
if (layout == BopomofoKeyboardLayout::HanyuPinyinLayout()) { if (layout == BopomofoKeyboardLayout::HanyuPinyinLayout()) {
pinyin_mode_ = true; pinyin_mode_ = true;
pinyin_sequence_ = ""; pinyin_sequence_ = "";
}
}
void setKeyboardLayout(const BopomofoKeyboardLayout* layout) {
layout_ = layout;
if (layout == BopomofoKeyboardLayout::HanyuPinyinLayout()) {
pinyin_mode_ = true;
pinyin_sequence_ = "";
}
}
bool isValidKey(char k) const {
if (!pinyin_mode_) {
return layout_ ? (layout_->keyToComponents(k)).size() > 0 : false;
}
char lk = tolower(k);
if (lk >= 'a' && lk <= 'z') {
// if a tone marker is already in place
if (pinyin_sequence_.length()) {
char lastc = pinyin_sequence_[pinyin_sequence_.length() - 1];
if (lastc >= '2' && lastc <= '5') {
return false;
} }
return true;
}
return true;
} }
if (pinyin_sequence_.length() && (lk >= '2' && lk <= '5')) { void setKeyboardLayout(const BopomofoKeyboardLayout* layout) {
return true; layout_ = layout;
if (layout == BopomofoKeyboardLayout::HanyuPinyinLayout()) {
pinyin_mode_ = true;
pinyin_sequence_ = "";
}
} }
return false; bool isValidKey(char k) const {
} if (!pinyin_mode_) {
return layout_ ? (layout_->keyToComponents(k)).size() > 0 : false;
}
bool combineKey(char k) { char lk = tolower(k);
if (!isValidKey(k)) return false; if (lk >= 'a' && lk <= 'z') {
// if a tone marker is already in place
if (pinyin_sequence_.length()) {
char lastc = pinyin_sequence_[pinyin_sequence_.length() - 1];
if (lastc >= '2' && lastc <= '5') {
return false;
}
return true;
}
return true;
}
if (pinyin_mode_) { if (pinyin_sequence_.length() && (lk >= '2' && lk <= '5')) {
pinyin_sequence_ += std::string(1, tolower(k)); return true;
syllable_ = BPMF::FromHanyuPinyin(pinyin_sequence_); }
return true;
return false;
} }
std::string sequence = bool combineKey(char k) {
if (!isValidKey(k)) return false;
if (pinyin_mode_) {
pinyin_sequence_ += std::string(1, tolower(k));
syllable_ = BPMF::FromHanyuPinyin(pinyin_sequence_);
return true;
}
std::string sequence =
layout_->keySequenceFromSyllable(syllable_) + std::string(1, k); layout_->keySequenceFromSyllable(syllable_) + std::string(1, k);
syllable_ = layout_->syllableFromKeySequence(sequence); syllable_ = layout_->syllableFromKeySequence(sequence);
return true; return true;
}
void clear() {
pinyin_sequence_.clear();
syllable_.clear();
}
void backspace() {
if (!layout_) return;
if (pinyin_mode_) {
if (pinyin_sequence_.length()) {
pinyin_sequence_ =
pinyin_sequence_.substr(0, pinyin_sequence_.length() - 1);
}
syllable_ = BPMF::FromHanyuPinyin(pinyin_sequence_);
return;
} }
std::string sequence = layout_->keySequenceFromSyllable(syllable_); void clear() {
if (sequence.length()) { pinyin_sequence_.clear();
sequence = sequence.substr(0, sequence.length() - 1); syllable_.clear();
syllable_ = layout_->syllableFromKeySequence(sequence);
}
}
bool isEmpty() const { return syllable_.isEmpty(); }
const std::string composedString() const {
if (pinyin_mode_) {
return pinyin_sequence_;
} }
return syllable_.composedString(); void backspace() {
} if (!layout_) return;
const BPMF syllable() const { return syllable_; } if (pinyin_mode_) {
if (pinyin_sequence_.length()) {
pinyin_sequence_ =
pinyin_sequence_.substr(0, pinyin_sequence_.length() - 1);
}
const std::string standardLayoutQueryString() const { syllable_ = BPMF::FromHanyuPinyin(pinyin_sequence_);
return BopomofoKeyboardLayout::StandardLayout()->keySequenceFromSyllable( return;
syllable_); }
}
const std::string absoluteOrderQueryString() const { std::string sequence = layout_->keySequenceFromSyllable(syllable_);
return syllable_.absoluteOrderString(); if (sequence.length()) {
} sequence = sequence.substr(0, sequence.length() - 1);
syllable_ = layout_->syllableFromKeySequence(sequence);
}
}
bool hasToneMarker() const { return syllable_.hasToneMarker(); } bool isEmpty() const { return syllable_.isEmpty(); }
protected: const std::string composedString() const {
const BopomofoKeyboardLayout* layout_; if (pinyin_mode_) {
BPMF syllable_; return pinyin_sequence_;
}
bool pinyin_mode_; return syllable_.composedString();
std::string pinyin_sequence_; }
const BPMF syllable() const { return syllable_; }
const std::string standardLayoutQueryString() const {
return BopomofoKeyboardLayout::StandardLayout()->keySequenceFromSyllable(syllable_);
}
const std::string absoluteOrderQueryString() const {
return syllable_.absoluteOrderString();
}
bool hasToneMarker() const { return syllable_.hasToneMarker(); }
protected:
const BopomofoKeyboardLayout* layout_;
BPMF syllable_;
bool pinyin_mode_;
std::string pinyin_sequence_;
}; };
} // namespace Mandarin } // namespace Mandarin
} // namespace Taiyan } // namespace Taiyan