| 1 |
#include "varnam_utils.h" |
| 2 |
|
| 3 |
extern "C" { |
| 4 |
#include <libgovarnam/libgovarnam.h> |
| 5 |
} |
| 6 |
|
| 7 |
namespace fcitx { |
| 8 |
|
| 9 |
const ::fcitx ::LogCategory &VARNAM() { |
| 10 |
static const ::fcitx ::LogCategory category("varnam"); |
| 11 |
return category; |
| 12 |
} |
| 13 |
|
| 14 |
bool isWordBreak(FcitxKeySym keyVal, bool inscriptMode) { |
| 15 |
switch (keyVal) { |
| 16 |
case FcitxKey_space: |
| 17 |
case FcitxKey_comma: |
| 18 |
case FcitxKey_period: |
| 19 |
case FcitxKey_question: |
| 20 |
case FcitxKey_exclam: |
| 21 |
case FcitxKey_parenleft: |
| 22 |
case FcitxKey_parenright: |
| 23 |
return true; |
| 24 |
default: |
| 25 |
break; |
| 26 |
} |
| 27 |
if (!inscriptMode) { |
| 28 |
switch (keyVal) { |
| 29 |
case FcitxKey_semicolon: |
| 30 |
case FcitxKey_apostrophe: |
| 31 |
case FcitxKey_quotedbl: |
| 32 |
return true; |
| 33 |
default: |
| 34 |
break; |
| 35 |
} |
| 36 |
} |
| 37 |
return false; |
| 38 |
} |
| 39 |
|
| 40 |
const std::string getWordBreakChar(FcitxKeySym keyVal, bool inscriptMode) { |
| 41 |
switch (keyVal) { |
| 42 |
case FcitxKey_space: |
| 43 |
return " "; |
| 44 |
case FcitxKey_comma: |
| 45 |
return ","; |
| 46 |
case FcitxKey_period: |
| 47 |
return "."; |
| 48 |
case FcitxKey_question: |
| 49 |
return "?"; |
| 50 |
case FcitxKey_exclam: |
| 51 |
return "!"; |
| 52 |
case FcitxKey_parenleft: |
| 53 |
return "("; |
| 54 |
case FcitxKey_parenright: |
| 55 |
return ")"; |
| 56 |
default: |
| 57 |
break; |
| 58 |
} |
| 59 |
|
| 60 |
if (!inscriptMode) { |
| 61 |
switch (keyVal) { |
| 62 |
case FcitxKey_semicolon: |
| 63 |
return ";"; |
| 64 |
case FcitxKey_apostrophe: |
| 65 |
return "'"; |
| 66 |
case FcitxKey_quotedbl: |
| 67 |
return "\""; |
| 68 |
default: |
| 69 |
break; |
| 70 |
} |
| 71 |
} |
| 72 |
return ""; |
| 73 |
} |
| 74 |
|
| 75 |
int getNumOfUTFCharUnits(char32_t code_point) { |
| 76 |
if (code_point <= 0x7F) { |
| 77 |
return 1; |
| 78 |
} else if (code_point <= 0x7FF) { |
| 79 |
return 2; |
| 80 |
} else if (code_point <= 0xFFFF) { |
| 81 |
return 3; |
| 82 |
} else if (code_point <= 0x10FFFF) { |
| 83 |
return 4; |
| 84 |
} else { |
| 85 |
// Invalid Unicode code point |
| 86 |
return 0; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
void varnam_learn_word(int varnam_handle_id, const std::string &word_, |
| 91 |
int weight) { |
| 92 |
char *word = const_cast<char *>(word_.c_str()); |
| 93 |
int rv = varnam_learn(varnam_handle_id, word, weight); |
| 94 |
if (rv != VARNAM_SUCCESS) { |
| 95 |
VARNAM_WARN() << "Failed to learn word:" << word; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
void varnam_unlearn_word(int varnam_handle_id, const std::string &word_) { |
| 100 |
char *word = const_cast<char *>(word_.c_str()); |
| 101 |
int rv = varnam_unlearn(varnam_handle_id, word); |
| 102 |
if (rv != VARNAM_SUCCESS) { |
| 103 |
VARNAM_WARN() << "Failed to unlearn word:" << word; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
} // namespace fcitx |