├── Keyboard.ico ├── LICENSE ├── README.md ├── ShowPictures └── 1.gif ├── Soft-keyboard.pro ├── chineseInput ├── chineseBase │ └── chinese.db ├── xydatabaseoperation.cpp ├── xydatabaseoperation.h ├── xyinputsearchinterface.cpp ├── xyinputsearchinterface.h ├── xytranslateitem.cpp └── xytranslateitem.h ├── data ├── files.qrc └── symbol.txt ├── ico.rc ├── images ├── Keyboard.ico └── images.qrc ├── libgooglepinyin ├── dict │ ├── dict_pinyin.dat │ └── dict_pinyin_user.dat ├── googlepinyin.pri ├── include │ ├── CMakeLists.txt │ ├── atomdictbase.h │ ├── dictbuilder.h │ ├── dictdef.h │ ├── dictlist.h │ ├── dicttrie.h │ ├── lpicache.h │ ├── matrixsearch.h │ ├── mystdlib.h │ ├── ngram.h │ ├── pinyinime.h │ ├── searchutility.h │ ├── spellingtable.h │ ├── spellingtrie.h │ ├── splparser.h │ ├── sync.h │ ├── userdict.h │ ├── utf16char.h │ └── utf16reader.h ├── src │ ├── CMakeLists.txt │ ├── dictbuilder.cpp │ ├── dictlist.cpp │ ├── dicttrie.cpp │ ├── lpicache.cpp │ ├── matrixsearch.cpp │ ├── mystdlib.cpp │ ├── ngram.cpp │ ├── pinyinime.cpp │ ├── searchutility.cpp │ ├── spellingtable.cpp │ ├── spellingtrie.cpp │ ├── splparser.cpp │ ├── sync.cpp │ ├── userdict.cpp │ ├── utf16char.cpp │ └── utf16reader.cpp ├── xygooglepinyin.cpp └── xygooglepinyin.h ├── main.cpp ├── skin ├── blue.skin ├── dark.skin └── yellow.skin ├── xydragablewidget.cpp ├── xydragablewidget.h ├── xyhdragabletranslateview.cpp ├── xyhdragabletranslateview.h ├── xyinput.json ├── xyinputplugin.cpp ├── xyinputplugin.h ├── xyplatforminputcontext.cpp ├── xyplatforminputcontext.h ├── xypushbutton.cpp ├── xypushbutton.h ├── xyqstringview.cpp ├── xyqstringview.h ├── xyskin.cpp ├── xyskin.h ├── xytempwindows.cpp ├── xytempwindows.h ├── xyvdragabletranslateview.cpp ├── xyvdragabletranslateview.h ├── xyvirtualkeyboard.cpp └── xyvirtualkeyboard.h /Keyboard.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaoyanLG/Soft-keyboard/68b37f5099a1a14279e62acd3361a11ad5ee958d/Keyboard.ico -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Soft-keyboard 2 | 3 | ![](ShowPictures/1.gif) 4 | -------------------------------------------------------------------------------- /ShowPictures/1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaoyanLG/Soft-keyboard/68b37f5099a1a14279e62acd3361a11ad5ee958d/ShowPictures/1.gif -------------------------------------------------------------------------------- /Soft-keyboard.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2017-11-03T09:42:03 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui sql xml 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | INCLUDEPATH += chineseInput 12 | QMAKE_CXXFLAGS += -std=c++11 13 | 14 | lessThan(QT_MAJOR_VERSION, 5): DEFINES += Q_NULLPTR=NULL 15 | 16 | SOURCES += \ 17 | xyvirtualkeyboard.cpp \ 18 | xypushbutton.cpp \ 19 | xydragablewidget.cpp \ 20 | chineseInput/xydatabaseoperation.cpp \ 21 | chineseInput/xytranslateitem.cpp \ 22 | chineseInput/xyinputsearchinterface.cpp \ 23 | xyhdragabletranslateview.cpp \ 24 | xyvdragabletranslateview.cpp \ 25 | xyqstringview.cpp \ 26 | xyskin.cpp \ 27 | xytempwindows.cpp 28 | 29 | HEADERS += \ 30 | xyvirtualkeyboard.h \ 31 | xypushbutton.h \ 32 | xydragablewidget.h \ 33 | chineseInput/xydatabaseoperation.h \ 34 | chineseInput/xytranslateitem.h \ 35 | chineseInput/xyinputsearchinterface.h \ 36 | xyhdragabletranslateview.h \ 37 | xyvdragabletranslateview.h \ 38 | xyqstringview.h \ 39 | xyskin.h \ 40 | xytempwindows.h 41 | 42 | # 这里区分当前编译类型 43 | #DEFINES += THIS_IS_PLUGIN 44 | if(contains(DEFINES,THIS_IS_PLUGIN)){ 45 | TEMPLATE = lib 46 | QT += gui-private 47 | CONFIG += plugin 48 | 49 | SOURCES += \ 50 | xyplatforminputcontext.cpp \ 51 | xyinputplugin.cpp 52 | 53 | HEADERS += \ 54 | xyplatforminputcontext.h \ 55 | xyinputplugin.h 56 | 57 | } else { 58 | TEMPLATE = app 59 | SOURCES += \ 60 | main.cpp 61 | } 62 | 63 | msvc { 64 | greaterThan(QT_MAJOR_VERSION, 4): RC_FILE = ico.rc 65 | } 66 | 67 | RESOURCES += \ 68 | images/images.qrc \ 69 | data/files.qrc 70 | 71 | include($$PWD/libgooglepinyin/googlepinyin.pri) 72 | -------------------------------------------------------------------------------- /chineseInput/chineseBase/chinese.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaoyanLG/Soft-keyboard/68b37f5099a1a14279e62acd3361a11ad5ee958d/chineseInput/chineseBase/chinese.db -------------------------------------------------------------------------------- /chineseInput/xydatabaseoperation.h: -------------------------------------------------------------------------------- 1 | #ifndef XYDATABASEOPERATION_H 2 | #define XYDATABASEOPERATION_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class XYTranslateItem; 9 | class XYDatabaseOperation : public QObject 10 | { 11 | Q_OBJECT 12 | public: 13 | static XYDatabaseOperation *getInstance(); 14 | explicit XYDatabaseOperation(QObject *parent = Q_NULLPTR); 15 | ~XYDatabaseOperation(); 16 | 17 | bool createDatabaseFile(const QString &filePath, const QString &passwd = "", bool fource = false); 18 | bool openDatabaseFile(const QString &filePath, const QString &passwd = ""); 19 | 20 | bool createInputTable(); 21 | bool insertData(XYTranslateItem *item, const QString &table); 22 | bool insertData(const QList &list, const QString &table); 23 | bool delItem(XYTranslateItem *item); 24 | QList findData(const QString &key, 25 | const QString &number, 26 | const QString &table, 27 | bool *haveFind = Q_NULLPTR, int max = 200); 28 | 29 | private: 30 | static XYDatabaseOperation *DB; 31 | 32 | }; 33 | 34 | #define XYDB XYDatabaseOperation::getInstance() 35 | #endif // XYDATABASEOPERATION_H 36 | -------------------------------------------------------------------------------- /chineseInput/xyinputsearchinterface.h: -------------------------------------------------------------------------------- 1 | #ifndef XYINPUTSEARCHINTERFACE_H 2 | #define XYINPUTSEARCHINTERFACE_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "xytranslateitem.h" 8 | 9 | class XYInputSearchInterface : public QObject 10 | { 11 | Q_OBJECT 12 | public: 13 | static XYInputSearchInterface *getInstance(); 14 | ~XYInputSearchInterface(); 15 | bool initInputBase(const QString &path); 16 | void resetSearch(); 17 | QString getCurLetters(); 18 | 19 | signals: 20 | 21 | public slots: 22 | QList &searchTranslates(const QString &keyword); 23 | void setChinese(bool ch); 24 | 25 | // 分割拼音,比如women-->wo%'men,返回分割后的字符串(并加上%模糊查找),并带回有效的字数 26 | QString splitePinyin(const QString &pinyin, int &num); 27 | void deDuplication(QList &items, bool del = true); // 删除重复的字词 28 | XYTranslateItem *autoCreateWords(const QString &keyword); // 自动造词 29 | QList findItemsFromTemp(const QString &keyword, bool force = true); 30 | QList findPossibleMust(const QString &keyword, int max = 200); 31 | void saveItem(XYTranslateItem *item); 32 | void clearTemp(); 33 | const QStringList &getYunMuByShengMu(const QChar &shenmu); 34 | QList &completeInput(const QString &text, int index, QString &showText); 35 | QList &completeInput(const QString &text, QString &showText, XYTranslateItem *item = Q_NULLPTR); 36 | 37 | private: 38 | explicit XYInputSearchInterface(QObject *parent = Q_NULLPTR); 39 | static XYInputSearchInterface *instance; 40 | bool mbEnglish; 41 | XYTranslateItem moCompleteItem; 42 | XYTranslateItem moAutoCompleteItem; 43 | QString msCurrentKeyWords; 44 | QList curSearchedTranslates; 45 | QMap > mmapTempItems; // 存储一次输入中查询过的词组 46 | 47 | friend class XYVirtualKeyboard; 48 | }; 49 | 50 | #endif // XYINPUTSEARCHINTERFACE_H 51 | -------------------------------------------------------------------------------- /chineseInput/xytranslateitem.cpp: -------------------------------------------------------------------------------- 1 | #include "xytranslateitem.h" 2 | 3 | XYTranslateItem::XYTranslateItem(const QString &source, 4 | const QString &translate, 5 | const QString &complete, 6 | const QString &extra, 7 | int id, 8 | int times, 9 | bool stick) 10 | : msSource(source), 11 | msTranslate(translate), 12 | msComplete(complete), 13 | msExtra(extra), 14 | miID(id), 15 | miTimes(times), 16 | mbStick(stick) 17 | { 18 | 19 | } 20 | 21 | XYTranslateItem::~XYTranslateItem() 22 | { 23 | 24 | } 25 | 26 | void XYTranslateItem::clear() 27 | { 28 | msSource.clear(); 29 | msTranslate.clear(); 30 | msComplete.clear(); 31 | msExtra.clear(); 32 | miID = -1; 33 | miTimes = 1; 34 | mbStick = false; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /chineseInput/xytranslateitem.h: -------------------------------------------------------------------------------- 1 | #ifndef XYTRANSLATEITEM_H 2 | #define XYTRANSLATEITEM_H 3 | 4 | #include 5 | 6 | class XYTranslateItem 7 | { 8 | public: 9 | XYTranslateItem(const QString &source = "", // 输入源 10 | const QString &translate = "", // 存放当前想输入的类型(比如中文,或者英文) 11 | const QString &complete = "", // 完整的拼音(比如中文对应的拼音,或者完整的) 12 | const QString &extra = "", // 额外的信息(比如中文词组的字数) 13 | int id = -1, 14 | int times = 1, // 词频 15 | bool stick = false); // 是否置顶 16 | ~XYTranslateItem(); 17 | void clear(); 18 | 19 | private: 20 | QString msSource; 21 | QString msTranslate; 22 | QString msComplete; 23 | QString msExtra; 24 | int miID; 25 | int miTimes; 26 | bool mbStick; 27 | 28 | friend class XYInputSearchInterface; 29 | friend class XYVirtualKeyboard; 30 | friend class XYInput; 31 | friend class XYTranslateModel; 32 | friend class XYTranslateView; 33 | friend class XYDatabaseOperation; 34 | }; 35 | 36 | #endif // XYTRANSLATEITEM_H 37 | -------------------------------------------------------------------------------- /data/files.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | symbol.txt 4 | 5 | 6 | -------------------------------------------------------------------------------- /data/symbol.txt: -------------------------------------------------------------------------------- 1 | ,。?!:;·…~&@# 2 | ,.?!:;、……~&@# 3 | “”‘’〝〞 "'"'´' 4 | ()【】《》<>﹝﹞<> 5 | ()[]«»‹›〔〕〈〉 6 | {}[]「」{}〖〗『』 7 | ︵︷︹︿︽﹁﹃︻︗/|\ 8 | ︶︸︺﹀︾﹂﹄︼︘/|\ 9 | ①②③④⑤⑥⑦⑧⑨⑩ 10 | ⑴⑵⑶⑷⑸⑹⑺⑻⑼⑽⑾⑿ 11 | ⒈⒉⒊⒋⒌⒍⒎⒏⒐⒑⒒⒓ 12 | ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ 13 | ⅰⅱⅲⅳⅴⅵⅶⅷⅸⅹ 14 | ㈠㈡㈢㈣㈤㈥㈦㈧㈨㈩ 15 | ﹢﹣·/=﹤﹥≦≧≮≯≡ 16 | +-×÷=<>≤≥≈≒≠ 17 | ﹢﹣±∶∵∴∷㏒㏑∑∏∅ 18 | ∝∽∈∩∧⊙⌒∥∟∣∂∆ 19 | ㎎㎏μm㎜㎝㎞㎡m³㏄㏕ 20 | ¥¥£¢₠€฿£$ 21 | -------------------------------------------------------------------------------- /ico.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaoyanLG/Soft-keyboard/68b37f5099a1a14279e62acd3361a11ad5ee958d/ico.rc -------------------------------------------------------------------------------- /images/Keyboard.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaoyanLG/Soft-keyboard/68b37f5099a1a14279e62acd3361a11ad5ee958d/images/Keyboard.ico -------------------------------------------------------------------------------- /images/images.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | Keyboard.ico 4 | 5 | 6 | -------------------------------------------------------------------------------- /libgooglepinyin/dict/dict_pinyin.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaoyanLG/Soft-keyboard/68b37f5099a1a14279e62acd3361a11ad5ee958d/libgooglepinyin/dict/dict_pinyin.dat -------------------------------------------------------------------------------- /libgooglepinyin/dict/dict_pinyin_user.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaoyanLG/Soft-keyboard/68b37f5099a1a14279e62acd3361a11ad5ee958d/libgooglepinyin/dict/dict_pinyin_user.dat -------------------------------------------------------------------------------- /libgooglepinyin/googlepinyin.pri: -------------------------------------------------------------------------------- 1 | INCLUDEPATH += $$PWD \ 2 | $$PWD/include \ 3 | 4 | HEADERS += \ 5 | $$PWD/include/atomdictbase.h \ 6 | $$PWD/include/dictbuilder.h \ 7 | $$PWD/include/dictdef.h \ 8 | $$PWD/include/dictlist.h \ 9 | $$PWD/include/dicttrie.h \ 10 | $$PWD/include/lpicache.h \ 11 | $$PWD/include/matrixsearch.h \ 12 | $$PWD/include/mystdlib.h \ 13 | $$PWD/include/ngram.h \ 14 | $$PWD/include/pinyinime.h \ 15 | $$PWD/include/searchutility.h \ 16 | $$PWD/include/spellingtable.h \ 17 | $$PWD/include/spellingtrie.h \ 18 | $$PWD/include/splparser.h \ 19 | $$PWD/include/sync.h \ 20 | $$PWD/include/userdict.h \ 21 | $$PWD/include/utf16char.h \ 22 | $$PWD/include/utf16reader.h \ 23 | $$PWD/xygooglepinyin.h 24 | 25 | SOURCES += \ 26 | $$PWD/src/dictbuilder.cpp \ 27 | $$PWD/src/dictlist.cpp \ 28 | $$PWD/src/dicttrie.cpp \ 29 | $$PWD/src/lpicache.cpp \ 30 | $$PWD/src/matrixsearch.cpp \ 31 | $$PWD/src/mystdlib.cpp \ 32 | $$PWD/src/ngram.cpp \ 33 | $$PWD/src/pinyinime.cpp \ 34 | $$PWD/src/searchutility.cpp \ 35 | $$PWD/src/spellingtable.cpp \ 36 | $$PWD/src/spellingtrie.cpp \ 37 | $$PWD/src/splparser.cpp \ 38 | $$PWD/src/sync.cpp \ 39 | $$PWD/src/userdict.cpp \ 40 | $$PWD/src/utf16char.cpp \ 41 | $$PWD/src/utf16reader.cpp \ 42 | $$PWD/xygooglepinyin.cpp 43 | 44 | -------------------------------------------------------------------------------- /libgooglepinyin/include/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(GOOGLEPINYIN_HEADERS 2 | atomdictbase.h 3 | dictbuilder.h 4 | dictdef.h 5 | dictlist.h 6 | dicttrie.h 7 | lpicache.h 8 | matrixsearch.h 9 | mystdlib.h 10 | ngram.h 11 | pinyinime.h 12 | searchutility.h 13 | spellingtable.h 14 | spellingtrie.h 15 | splparser.h 16 | sync.h 17 | userdict.h 18 | utf16char.h 19 | utf16reader.h 20 | ) 21 | 22 | install(FILES ${GOOGLEPINYIN_HEADERS} DESTINATION ${includedir}) 23 | -------------------------------------------------------------------------------- /libgooglepinyin/include/atomdictbase.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /** 18 | * This class defines AtomDictBase class which is the base class for all atom 19 | * dictionaries. Atom dictionaries are managed by the decoder class 20 | * MatrixSearch. 21 | * 22 | * When the user appends a new character to the Pinyin string, all enabled atom 23 | * dictionaries' extend_dict() will be called at least once to get candidates 24 | * ended in this step (the information of starting step is also given in the 25 | * parameter). Usually, when extend_dict() is called, a MileStoneHandle object 26 | * returned by a previous calling for a earlier step is given to speed up the 27 | * look-up process, and a new MileStoneHandle object will be returned if 28 | * the extension is successful. 29 | * 30 | * A returned MileStoneHandle object should keep alive until Function 31 | * reset_milestones() is called and this object is noticed to be reset. 32 | * 33 | * Usually, the atom dictionary can use step information to manage its 34 | * MileStoneHandle objects, or it can make the objects in ascendant order to 35 | * make the reset easier. 36 | * 37 | * When the decoder loads the dictionary, it will give a starting lemma id for 38 | * this atom dictionary to map a inner id to a global id. Global ids should be 39 | * used when an atom dictionary talks to any component outside. 40 | */ 41 | #ifndef PINYINIME_INCLUDE_ATOMDICTBASE_H__ 42 | #define PINYINIME_INCLUDE_ATOMDICTBASE_H__ 43 | 44 | #include 45 | #include "./dictdef.h" 46 | #include "./searchutility.h" 47 | 48 | namespace ime_pinyin { 49 | class AtomDictBase { 50 | public: 51 | virtual ~AtomDictBase() {} 52 | 53 | /** 54 | * Load an atom dictionary from a file. 55 | * 56 | * @param file_name The file name to load dictionary. 57 | * @param start_id The starting id used for this atom dictionary. 58 | * @param end_id The end id (included) which can be used for this atom 59 | * dictionary. User dictionary will always use the last id space, so it can 60 | * ignore this paramter. All other atom dictionaries should check this 61 | * parameter. 62 | * @return True if succeed. 63 | */ 64 | virtual bool load_dict(const char *file_name, LemmaIdType start_id, 65 | LemmaIdType end_id) = 0; 66 | 67 | /** 68 | * Close this atom dictionary. 69 | * 70 | * @return True if succeed. 71 | */ 72 | virtual bool close_dict() = 0; 73 | 74 | /** 75 | * Get the total number of lemmas in this atom dictionary. 76 | * 77 | * @return The total number of lemmas. 78 | */ 79 | virtual size_t number_of_lemmas() = 0; 80 | 81 | /** 82 | * This function is called by the decoder when user deletes a character from 83 | * the input string, or begins a new input string. 84 | * 85 | * Different atom dictionaries may implement this function in different way. 86 | * an atom dictionary can use one of these two parameters (or both) to reset 87 | * its corresponding MileStoneHandle objects according its detailed 88 | * implementation. 89 | * 90 | * For example, if an atom dictionary uses step information to manage its 91 | * MileStoneHandle objects, parameter from_step can be used to identify which 92 | * objects should be reset; otherwise, if another atom dictionary does not 93 | * use the detailed step information, it only uses ascendant handles 94 | * (according to step. For the same step, earlier call, smaller handle), it 95 | * can easily reset those MileStoneHandle which are larger than from_handle. 96 | * 97 | * The decoder always reset the decoding state by step. So when it begins 98 | * resetting, it will call reset_milestones() of its atom dictionaries with 99 | * the step information, and the MileStoneHandle objects returned by the 100 | * earliest calling of extend_dict() for that step. 101 | * 102 | * If an atom dictionary does not implement incremental search, this function 103 | * can be totally ignored. 104 | * 105 | * @param from_step From which step(included) the MileStoneHandle 106 | * objects should be reset. 107 | * @param from_handle The ealiest MileStoneHandle object for step from_step 108 | */ 109 | virtual void reset_milestones(uint16 from_step, 110 | MileStoneHandle from_handle) = 0; 111 | 112 | /** 113 | * Used to extend in this dictionary. The handle returned should keep valid 114 | * until reset_milestones() is called. 115 | * 116 | * @param from_handle Its previous returned extended handle without the new 117 | * spelling id, it can be used to speed up the extending. 118 | * @param dep The paramter used for extending. 119 | * @param lpi_items Used to fill in the lemmas matched. 120 | * @param lpi_max The length of the buffer 121 | * @param lpi_num Used to return the newly added items. 122 | * @return The new mile stone for this extending. 0 if fail. 123 | */ 124 | virtual MileStoneHandle extend_dict(MileStoneHandle from_handle, 125 | const DictExtPara *dep, 126 | LmaPsbItem *lpi_items, 127 | size_t lpi_max, size_t *lpi_num) = 0; 128 | 129 | /** 130 | * Get lemma items with scores according to a spelling id stream. 131 | * This atom dictionary does not need to sort the returned items. 132 | * 133 | * @param splid_str The spelling id stream buffer. 134 | * @param splid_str_len The length of the spelling id stream buffer. 135 | * @param lpi_items Used to return matched lemma items with scores. 136 | * @param lpi_max The maximum size of the buffer to return result. 137 | * @return The number of matched items which have been filled in to lpi_items. 138 | */ 139 | virtual size_t get_lpis(const uint16 *splid_str, uint16 splid_str_len, 140 | LmaPsbItem *lpi_items, size_t lpi_max) = 0; 141 | 142 | /** 143 | * Get a lemma string (The Chinese string) by the given lemma id. 144 | * 145 | * @param id_lemma The lemma id to get the string. 146 | * @param str_buf The buffer to return the Chinese string. 147 | * @param str_max The maximum size of the buffer. 148 | * @return The length of the string, 0 if fail. 149 | */ 150 | virtual uint16 get_lemma_str(LemmaIdType id_lemma, char16 *str_buf, 151 | uint16 str_max) = 0; 152 | 153 | /** 154 | * Get the full spelling ids for the given lemma id. 155 | * If the given buffer is too short, return 0. 156 | * 157 | * @param splids Used to return the spelling ids. 158 | * @param splids_max The maximum buffer length of splids. 159 | * @param arg_valid Used to indicate if the incoming parameters have been 160 | * initialized are valid. If it is true, the splids and splids_max are valid 161 | * and there may be half ids in splids to be updated to full ids. In this 162 | * case, splids_max is the number of valid ids in splids. 163 | * @return The number of ids in the buffer. 164 | */ 165 | virtual uint16 get_lemma_splids(LemmaIdType id_lemma, uint16 *splids, 166 | uint16 splids_max, bool arg_valid) = 0; 167 | 168 | /** 169 | * Function used for prediction. 170 | * No need to sort the newly added items. 171 | * 172 | * @param last_hzs The last n Chinese chracters(called Hanzi), its length 173 | * should be less than or equal to kMaxPredictSize. 174 | * @param hzs_len specifies the length(<= kMaxPredictSize) of the history. 175 | * @param npre_items Used used to return the result. 176 | * @param npre_max The length of the buffer to return result 177 | * @param b4_used Number of prediction result (from npre_items[-b4_used]) 178 | * from other atom dictionaries. A atom ditionary can just ignore it. 179 | * @return The number of prediction result from this atom dictionary. 180 | */ 181 | virtual size_t predict(const char16 last_hzs[], uint16 hzs_len, 182 | NPredictItem *npre_items, size_t npre_max, 183 | size_t b4_used) = 0; 184 | 185 | /** 186 | * Add a lemma to the dictionary. If the dictionary allows to add new 187 | * items and this item does not exist, add it. 188 | * 189 | * @param lemma_str The Chinese string of the lemma. 190 | * @param splids The spelling ids of the lemma. 191 | * @param lemma_len The length of the Chinese lemma. 192 | * @param count The frequency count for this lemma. 193 | */ 194 | virtual LemmaIdType put_lemma(char16 lemma_str[], uint16 splids[], 195 | uint16 lemma_len, uint16 count) = 0; 196 | 197 | /** 198 | * Update a lemma's occuring count. 199 | * 200 | * @param lemma_id The lemma id to update. 201 | * @param delta_count The frequnecy count to ajust. 202 | * @param selected Indicate whether this lemma is selected by user and 203 | * submitted to target edit box. 204 | * @return The id if succeed, 0 if fail. 205 | */ 206 | virtual LemmaIdType update_lemma(LemmaIdType lemma_id, int16 delta_count, 207 | bool selected) = 0; 208 | 209 | /** 210 | * Get the lemma id for the given lemma. 211 | * 212 | * @param lemma_str The Chinese string of the lemma. 213 | * @param splids The spelling ids of the lemma. 214 | * @param lemma_len The length of the lemma. 215 | * @return The matched lemma id, or 0 if fail. 216 | */ 217 | virtual LemmaIdType get_lemma_id(char16 lemma_str[], uint16 splids[], 218 | uint16 lemma_len) = 0; 219 | 220 | /** 221 | * Get the lemma score. 222 | * 223 | * @param lemma_id The lemma id to get score. 224 | * @return The score of the lemma, or 0 if fail. 225 | */ 226 | virtual LmaScoreType get_lemma_score(LemmaIdType lemma_id) = 0; 227 | 228 | /** 229 | * Get the lemma score. 230 | * 231 | * @param lemma_str The Chinese string of the lemma. 232 | * @param splids The spelling ids of the lemma. 233 | * @param lemma_len The length of the lemma. 234 | * @return The score of the lamm, or 0 if fail. 235 | */ 236 | virtual LmaScoreType get_lemma_score(char16 lemma_str[], uint16 splids[], 237 | uint16 lemma_len) = 0; 238 | 239 | /** 240 | * If the dictionary allowed, remove a lemma from it. 241 | * 242 | * @param lemma_id The id of the lemma to remove. 243 | * @return True if succeed. 244 | */ 245 | virtual bool remove_lemma(LemmaIdType lemma_id) = 0; 246 | 247 | /** 248 | * Get the total occuring count of this atom dictionary. 249 | * 250 | * @return The total occuring count of this atom dictionary. 251 | */ 252 | virtual size_t get_total_lemma_count() = 0; 253 | 254 | /** 255 | * Set the total occuring count of other atom dictionaries. 256 | * 257 | * @param count The total occuring count of other atom dictionaies. 258 | */ 259 | virtual void set_total_lemma_count_of_others(size_t count) = 0; 260 | 261 | /** 262 | * Notify this atom dictionary to flush the cached data to persistent storage 263 | * if necessary. 264 | */ 265 | virtual void flush_cache() = 0; 266 | }; 267 | } 268 | 269 | #endif // PINYINIME_INCLUDE_ATOMDICTBASE_H__ 270 | -------------------------------------------------------------------------------- /libgooglepinyin/include/dictbuilder.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef PINYINIME_INCLUDE_DICTBUILDER_H__ 18 | #define PINYINIME_INCLUDE_DICTBUILDER_H__ 19 | 20 | #include 21 | #include "./utf16char.h" 22 | #include "./dictdef.h" 23 | #include "./dictlist.h" 24 | #include "./spellingtable.h" 25 | #include "./spellingtrie.h" 26 | #include "./splparser.h" 27 | 28 | namespace ime_pinyin { 29 | 30 | #ifdef ___BUILD_MODEL___ 31 | 32 | #define ___DO_STATISTICS___ 33 | 34 | class DictTrie; 35 | 36 | class DictBuilder { 37 | private: 38 | // The raw lemma array buffer. 39 | LemmaEntry *lemma_arr_; 40 | size_t lemma_num_; 41 | 42 | // Used to store all possible single char items. 43 | // Two items may have the same Hanzi while their spelling ids are different. 44 | SingleCharItem *scis_; 45 | size_t scis_num_; 46 | 47 | // In the tree, root's level is -1. 48 | // Lemma nodes for root, and level 0 49 | LmaNodeLE0 *lma_nodes_le0_; 50 | 51 | // Lemma nodes for layers whose levels are deeper than 0 52 | LmaNodeGE1 *lma_nodes_ge1_; 53 | 54 | // Number of used lemma nodes 55 | size_t lma_nds_used_num_le0_; 56 | size_t lma_nds_used_num_ge1_; 57 | 58 | // Used to store homophonies' ids. 59 | LemmaIdType *homo_idx_buf_; 60 | // Number of homophonies each of which only contains one Chinese character. 61 | size_t homo_idx_num_eq1_; 62 | // Number of homophonies each of which contains more than one character. 63 | size_t homo_idx_num_gt1_; 64 | 65 | // The items with highest scores. 66 | LemmaEntry *top_lmas_; 67 | size_t top_lmas_num_; 68 | 69 | SpellingTable *spl_table_; 70 | SpellingParser *spl_parser_; 71 | 72 | #ifdef ___DO_STATISTICS___ 73 | size_t max_sonbuf_len_[kMaxLemmaSize]; 74 | size_t max_homobuf_len_[kMaxLemmaSize]; 75 | 76 | size_t total_son_num_[kMaxLemmaSize]; 77 | size_t total_node_hasson_[kMaxLemmaSize]; 78 | size_t total_sonbuf_num_[kMaxLemmaSize]; 79 | size_t total_sonbuf_allnoson_[kMaxLemmaSize]; 80 | size_t total_node_in_sonbuf_allnoson_[kMaxLemmaSize]; 81 | size_t total_homo_num_[kMaxLemmaSize]; 82 | 83 | size_t sonbufs_num1_; // Number of son buffer with only 1 son 84 | size_t sonbufs_numgt1_; // Number of son buffer with more 1 son; 85 | 86 | size_t total_lma_node_num_; 87 | 88 | void stat_init(); 89 | void stat_print(); 90 | #endif 91 | 92 | public: 93 | 94 | DictBuilder(); 95 | ~DictBuilder(); 96 | 97 | // Build dictionary trie from the file fn_raw. File fn_validhzs provides 98 | // valid chars. If fn_validhzs is NULL, only chars in GB2312 will be 99 | // included. 100 | bool build_dict(const char* fn_raw, const char* fn_validhzs, 101 | DictTrie *dict_trie); 102 | 103 | private: 104 | // Fill in the buffer with id. The caller guarantees that the paramters are 105 | // vaild. 106 | void id_to_charbuf(unsigned char *buf, LemmaIdType id); 107 | 108 | // Update the offset of sons for a node. 109 | void set_son_offset(LmaNodeGE1 *node, size_t offset); 110 | 111 | // Update the offset of homophonies' ids for a node. 112 | void set_homo_id_buf_offset(LmaNodeGE1 *node, size_t offset); 113 | 114 | // Format a speling string. 115 | void format_spelling_str(char *spl_str); 116 | 117 | // Sort the lemma_arr by the hanzi string, and give each of unique items 118 | // a id. Why we need to sort the lemma list according to their Hanzi string 119 | // is to find items started by a given prefix string to do prediction. 120 | // Actually, the single char items are be in other order, for example, 121 | // in spelling id order, etc. 122 | // Return value is next un-allocated idx available. 123 | LemmaIdType sort_lemmas_by_hz(); 124 | 125 | // Build the SingleCharItem list, and fill the hanzi_scis_ids in the 126 | // lemma buffer lemma_arr_. 127 | // This function should be called after the lemma array is ready. 128 | // Return the number of unique SingleCharItem elements. 129 | size_t build_scis(); 130 | 131 | // Construct a subtree using a subset of the spelling array (from 132 | // item_star to item_end) 133 | // parent is the parent node to update the necessary information 134 | // parent can be a member of LmaNodeLE0 or LmaNodeGE1 135 | bool construct_subset(void* parent, LemmaEntry* lemma_arr, 136 | size_t item_start, size_t item_end, size_t level); 137 | 138 | 139 | // Read valid Chinese Hanzis from the given file. 140 | // num is used to return number of chars. 141 | // The return buffer is sorted and caller needs to free the returned buffer. 142 | char16* read_valid_hanzis(const char *fn_validhzs, size_t *num); 143 | 144 | 145 | // Read a raw dictionary. max_item is the maximum number of items. If there 146 | // are more items in the ditionary, only the first max_item will be read. 147 | // Returned value is the number of items successfully read from the file. 148 | size_t read_raw_dict(const char* fn_raw, const char *fn_validhzs, 149 | size_t max_item); 150 | 151 | // Try to find if a character is in hzs buffer. 152 | bool hz_in_hanzis_list(const char16 *hzs, size_t hzs_len, char16 hz); 153 | 154 | // Try to find if all characters in str are in hzs buffer. 155 | bool str_in_hanzis_list(const char16 *hzs, size_t hzs_len, 156 | const char16 *str, size_t str_len); 157 | 158 | // Get these lemmas with toppest scores. 159 | void get_top_lemmas(); 160 | 161 | // Allocate resource to build dictionary. 162 | // lma_num is the number of items to be loaded 163 | bool alloc_resource(size_t lma_num); 164 | 165 | // Free resource. 166 | void free_resource(); 167 | }; 168 | #endif // ___BUILD_MODEL___ 169 | } 170 | 171 | #endif // PINYINIME_INCLUDE_DICTBUILDER_H__ 172 | -------------------------------------------------------------------------------- /libgooglepinyin/include/dictdef.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef PINYINIME_INCLUDE_DICTDEF_H__ 18 | #define PINYINIME_INCLUDE_DICTDEF_H__ 19 | 20 | #include 21 | #include "./utf16char.h" 22 | 23 | namespace ime_pinyin { 24 | 25 | // Enable the following line when building the binary dictionary model. 26 | // #define ___BUILD_MODEL___ 27 | 28 | typedef unsigned char uint8; 29 | typedef unsigned short uint16; 30 | typedef unsigned int uint32; 31 | 32 | typedef signed char int8; 33 | typedef short int16; 34 | typedef int int32; 35 | typedef long long int64; 36 | typedef unsigned long long uint64; 37 | 38 | const bool kPrintDebug0 = false; 39 | const bool kPrintDebug1 = false; 40 | const bool kPrintDebug2 = false; 41 | 42 | // The max length of a lemma. 43 | const size_t kMaxLemmaSize = 8; 44 | 45 | // The max length of a Pinyin (spelling). 46 | const size_t kMaxPinyinSize = 6; 47 | 48 | // The number of half spelling ids. For Chinese Pinyin, there 30 half ids. 49 | // See SpellingTrie.h for details. 50 | const size_t kHalfSpellingIdNum = 29; 51 | 52 | // The maximum number of full spellings. For Chinese Pinyin, there are only 53 | // about 410 spellings. 54 | // If change this value is bigger(needs more bits), please also update 55 | // other structures like SpellingNode, to make sure than a spelling id can be 56 | // stored. 57 | // -1 is because that 0 is never used. 58 | const size_t kMaxSpellingNum = 512 - kHalfSpellingIdNum - 1; 59 | const size_t kMaxSearchSteps = 40; 60 | 61 | // One character predicts its following characters. 62 | const size_t kMaxPredictSize = (kMaxLemmaSize - 1); 63 | 64 | // LemmaIdType must always be size_t. 65 | typedef size_t LemmaIdType; 66 | const size_t kLemmaIdSize = 3; // Actually, a Id occupies 3 bytes in storage. 67 | const size_t kLemmaIdComposing = 0xffffff; 68 | 69 | typedef uint16 LmaScoreType; 70 | typedef uint16 KeyScoreType; 71 | 72 | // Number of items with highest score are kept for prediction purpose. 73 | const size_t kTopScoreLemmaNum = 10; 74 | 75 | const size_t kMaxPredictNumByGt3 = 1; 76 | const size_t kMaxPredictNumBy3 = 2; 77 | const size_t kMaxPredictNumBy2 = 2; 78 | 79 | // The last lemma id (included) for the system dictionary. The system 80 | // dictionary's ids always start from 1. 81 | const LemmaIdType kSysDictIdEnd = 500000; 82 | 83 | // The first lemma id for the user dictionary. 84 | const LemmaIdType kUserDictIdStart = 500001; 85 | 86 | // The last lemma id (included) for the user dictionary. 87 | const LemmaIdType kUserDictIdEnd = 600000; 88 | 89 | typedef struct { 90 | uint16 half_splid:5; 91 | uint16 full_splid:11; 92 | } SpellingId, *PSpellingId; 93 | 94 | 95 | /** 96 | * We use different node types for different layers 97 | * Statistical data of the building result for a testing dictionary: 98 | * root, level 0, level 1, level 2, level 3 99 | * max son num of one node: 406 280 41 2 - 100 | * max homo num of one node: 0 90 23 2 2 101 | * total node num of a layer: 1 406 31766 13516 993 102 | * total homo num of a layer: 9 5674 44609 12667 995 103 | * 104 | * The node number for root and level 0 won't be larger than 500 105 | * According to the information above, two kinds of nodes can be used; one for 106 | * root and level 0, the other for these layers deeper than 0. 107 | * 108 | * LE = less and equal, 109 | * A node occupies 16 bytes. so, totallly less than 16 * 500 = 8K 110 | */ 111 | struct LmaNodeLE0 { 112 | uint32 son_1st_off; 113 | uint32 homo_idx_buf_off; 114 | uint16 spl_idx; 115 | uint16 num_of_son; 116 | uint16 num_of_homo; 117 | }; 118 | 119 | /** 120 | * GE = great and equal 121 | * A node occupies 8 bytes. 122 | */ 123 | struct LmaNodeGE1 { 124 | uint16 son_1st_off_l; // Low bits of the son_1st_off 125 | uint16 homo_idx_buf_off_l; // Low bits of the homo_idx_buf_off_1 126 | uint16 spl_idx; 127 | unsigned char num_of_son; // number of son nodes 128 | unsigned char num_of_homo; // number of homo words 129 | unsigned char son_1st_off_h; // high bits of the son_1st_off 130 | unsigned char homo_idx_buf_off_h; // high bits of the homo_idx_buf_off 131 | }; 132 | 133 | #ifdef ___BUILD_MODEL___ 134 | struct SingleCharItem { 135 | float freq; 136 | char16 hz; 137 | SpellingId splid; 138 | }; 139 | 140 | struct LemmaEntry { 141 | LemmaIdType idx_by_py; 142 | LemmaIdType idx_by_hz; 143 | char16 hanzi_str[kMaxLemmaSize + 1]; 144 | 145 | // The SingleCharItem id for each Hanzi. 146 | uint16 hanzi_scis_ids[kMaxLemmaSize]; 147 | 148 | uint16 spl_idx_arr[kMaxLemmaSize + 1]; 149 | char pinyin_str[kMaxLemmaSize][kMaxPinyinSize + 1]; 150 | unsigned char hz_str_len; 151 | float freq; 152 | }; 153 | #endif // ___BUILD_MODEL___ 154 | 155 | } // namespace ime_pinyin 156 | 157 | #endif // PINYINIME_INCLUDE_DICTDEF_H__ 158 | -------------------------------------------------------------------------------- /libgooglepinyin/include/dictlist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef PINYINIME_INCLUDE_DICTLIST_H__ 18 | #define PINYINIME_INCLUDE_DICTLIST_H__ 19 | 20 | #include 21 | #include 22 | #include "./dictdef.h" 23 | #include "./searchutility.h" 24 | #include "./spellingtrie.h" 25 | #include "./utf16char.h" 26 | 27 | namespace ime_pinyin { 28 | 29 | class DictList { 30 | private: 31 | bool initialized_; 32 | 33 | const SpellingTrie *spl_trie_; 34 | 35 | // Number of SingCharItem. The first is blank, because id 0 is invalid. 36 | uint32 scis_num_; 37 | char16 *scis_hz_; 38 | SpellingId *scis_splid_; 39 | 40 | // The large memory block to store the word list. 41 | char16 *buf_; 42 | 43 | // Starting position of those words whose lengths are i+1, counted in 44 | // char16 45 | uint32 start_pos_[kMaxLemmaSize + 1]; 46 | 47 | uint32 start_id_[kMaxLemmaSize + 1]; 48 | 49 | int (*cmp_func_[kMaxLemmaSize])(const void *, const void *); 50 | 51 | bool alloc_resource(size_t buf_size, size_t scim_num); 52 | 53 | void free_resource(); 54 | 55 | #ifdef ___BUILD_MODEL___ 56 | // Calculate the requsted memory, including the start_pos[] buffer. 57 | size_t calculate_size(const LemmaEntry *lemma_arr, size_t lemma_num); 58 | 59 | void fill_scis(const SingleCharItem *scis, size_t scis_num); 60 | 61 | // Copy the related content to the inner buffer 62 | // It should be called after calculate_size() 63 | void fill_list(const LemmaEntry *lemma_arr, size_t lemma_num); 64 | 65 | // Find the starting position for the buffer of those 2-character Chinese word 66 | // whose first character is the given Chinese character. 67 | char16* find_pos2_startedbyhz(char16 hz_char); 68 | #endif 69 | 70 | // Find the starting position for the buffer of those words whose lengths are 71 | // word_len. The given parameter cmp_func decides how many characters from 72 | // beginning will be used to compare. 73 | char16* find_pos_startedbyhzs(const char16 last_hzs[], 74 | size_t word_Len, 75 | int (*cmp_func)(const void *, const void *)); 76 | 77 | public: 78 | 79 | DictList(); 80 | ~DictList(); 81 | 82 | bool save_list(FILE *fp); 83 | bool load_list(FILE *fp); 84 | 85 | #ifdef ___BUILD_MODEL___ 86 | // Init the list from the LemmaEntry array. 87 | // lemma_arr should have been sorted by the hanzi_str, and have been given 88 | // ids from 1 89 | bool init_list(const SingleCharItem *scis, size_t scis_num, 90 | const LemmaEntry *lemma_arr, size_t lemma_num); 91 | #endif 92 | 93 | // Get the hanzi string for the given id 94 | uint16 get_lemma_str(LemmaIdType id_hz, char16 *str_buf, uint16 str_max); 95 | 96 | void convert_to_hanzis(char16 *str, uint16 str_len); 97 | 98 | void convert_to_scis_ids(char16 *str, uint16 str_len); 99 | 100 | // last_hzs stores the last n Chinese characters history, its length should be 101 | // less or equal than kMaxPredictSize. 102 | // hzs_len specifies the length(<= kMaxPredictSize). 103 | // predict_buf is used to store the result. 104 | // buf_len specifies the buffer length. 105 | // b4_used specifies how many items before predict_buf have been used. 106 | // Returned value is the number of newly added items. 107 | size_t predict(const char16 last_hzs[], uint16 hzs_len, 108 | NPredictItem *npre_items, size_t npre_max, 109 | size_t b4_used); 110 | 111 | // If half_splid is a valid half spelling id, return those full spelling 112 | // ids which share this half id. 113 | uint16 get_splids_for_hanzi(char16 hanzi, uint16 half_splid, 114 | uint16 *splids, uint16 max_splids); 115 | 116 | LemmaIdType get_lemma_id(const char16 *str, uint16 str_len); 117 | }; 118 | } 119 | 120 | #endif // PINYINIME_INCLUDE_DICTLIST_H__ 121 | -------------------------------------------------------------------------------- /libgooglepinyin/include/dicttrie.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef PINYINIME_INCLUDE_DICTTRIE_H__ 18 | #define PINYINIME_INCLUDE_DICTTRIE_H__ 19 | 20 | #include 21 | #include "./atomdictbase.h" 22 | #include "./dictdef.h" 23 | #include "./dictlist.h" 24 | #include "./searchutility.h" 25 | 26 | namespace ime_pinyin { 27 | 28 | class DictTrie : AtomDictBase { 29 | private: 30 | struct ParsingMark { 31 | size_t node_offset:24; 32 | size_t node_num:8; // Number of nodes with this spelling id given 33 | // by spl_id. If spl_id is a Shengmu, for nodes 34 | // in the first layer of DictTrie, it equals to 35 | // SpellingTrie::shm2full_num(); but for those 36 | // nodes which are not in the first layer, 37 | // node_num < SpellingTrie::shm2full_num(). 38 | // For a full spelling id, node_num = 1; 39 | }; 40 | 41 | // Used to indicate an extended mile stone. 42 | // An extended mile stone is used to mark a partial match in the dictionary 43 | // trie to speed up further potential extending. 44 | // For example, when the user inputs "w", a mile stone is created to mark the 45 | // partial match status, so that when user inputs another char 'm', it will be 46 | // faster to extend search space based on this mile stone. 47 | // 48 | // For partial match status of "wm", there can be more than one sub mile 49 | // stone, for example, "wm" can be matched to "wanm", "wom", ..., etc, so 50 | // there may be more one parsing mark used to mark these partial matchings. 51 | // A mile stone records the starting position in the mark list and number of 52 | // marks. 53 | struct MileStone { 54 | uint16 mark_start; 55 | uint16 mark_num; 56 | }; 57 | 58 | DictList* dict_list_; 59 | 60 | const SpellingTrie *spl_trie_; 61 | 62 | LmaNodeLE0* root_; // Nodes for root and the first layer. 63 | LmaNodeGE1* nodes_ge1_; // Nodes for other layers. 64 | 65 | // An quick index from spelling id to the LmaNodeLE0 node buffer, or 66 | // to the root_ buffer. 67 | // Index length: 68 | // SpellingTrie::get_instance().get_spelling_num() + 1. The last one is used 69 | // to get the end. 70 | // All Shengmu ids are not indexed because they will be converted into 71 | // corresponding full ids. 72 | // So, given an id splid, the son is: 73 | // root_[splid_le0_index_[splid - kFullSplIdStart]] 74 | uint16 *splid_le0_index_; 75 | 76 | uint32 lma_node_num_le0_; 77 | uint32 lma_node_num_ge1_; 78 | 79 | // The first part is for homophnies, and the last top_lma_num_ items are 80 | // lemmas with highest scores. 81 | unsigned char *lma_idx_buf_; 82 | uint32 lma_idx_buf_len_; // The total size of lma_idx_buf_ in byte. 83 | uint32 total_lma_num_; // Total number of lemmas in this dictionary. 84 | uint32 top_lmas_num_; // Number of lemma with highest scores. 85 | 86 | // Parsing mark list used to mark the detailed extended statuses. 87 | ParsingMark *parsing_marks_; 88 | // The position for next available mark. 89 | uint16 parsing_marks_pos_; 90 | 91 | // Mile stone list used to mark the extended status. 92 | MileStone *mile_stones_; 93 | // The position for the next available mile stone. We use positions (except 0) 94 | // as handles. 95 | MileStoneHandle mile_stones_pos_; 96 | 97 | // Get the offset of sons for a node. 98 | inline size_t get_son_offset(const LmaNodeGE1 *node); 99 | 100 | // Get the offset of homonious ids for a node. 101 | inline size_t get_homo_idx_buf_offset(const LmaNodeGE1 *node); 102 | 103 | // Get the lemma id by the offset. 104 | inline LemmaIdType get_lemma_id(size_t id_offset); 105 | 106 | void free_resource(bool free_dict_list); 107 | 108 | bool load_dict(FILE *fp); 109 | 110 | // Given a LmaNodeLE0 node, extract the lemmas specified by it, and fill 111 | // them into the lpi_items buffer. 112 | // This function is called by the search engine. 113 | size_t fill_lpi_buffer(LmaPsbItem lpi_items[], size_t max_size, 114 | LmaNodeLE0 *node); 115 | 116 | // Given a LmaNodeGE1 node, extract the lemmas specified by it, and fill 117 | // them into the lpi_items buffer. 118 | // This function is called by inner functions extend_dict0(), extend_dict1() 119 | // and extend_dict2(). 120 | size_t fill_lpi_buffer(LmaPsbItem lpi_items[], size_t max_size, 121 | size_t homo_buf_off, LmaNodeGE1 *node, 122 | uint16 lma_len); 123 | 124 | // Extend in the trie from level 0. 125 | MileStoneHandle extend_dict0(MileStoneHandle from_handle, 126 | const DictExtPara *dep, LmaPsbItem *lpi_items, 127 | size_t lpi_max, size_t *lpi_num); 128 | 129 | // Extend in the trie from level 1. 130 | MileStoneHandle extend_dict1(MileStoneHandle from_handle, 131 | const DictExtPara *dep, LmaPsbItem *lpi_items, 132 | size_t lpi_max, size_t *lpi_num); 133 | 134 | // Extend in the trie from level 2. 135 | MileStoneHandle extend_dict2(MileStoneHandle from_handle, 136 | const DictExtPara *dep, LmaPsbItem *lpi_items, 137 | size_t lpi_max, size_t *lpi_num); 138 | 139 | // Try to extend the given spelling id buffer, and if the given id_lemma can 140 | // be successfully gotten, return true; 141 | // The given spelling ids are all valid full ids. 142 | bool try_extend(const uint16 *splids, uint16 splid_num, LemmaIdType id_lemma); 143 | 144 | #ifdef ___BUILD_MODEL___ 145 | bool save_dict(FILE *fp); 146 | #endif // ___BUILD_MODEL___ 147 | 148 | static const int kMaxMileStone = 100; 149 | static const int kMaxParsingMark = 600; 150 | static const MileStoneHandle kFirstValidMileStoneHandle = 1; 151 | 152 | friend class DictParser; 153 | friend class DictBuilder; 154 | 155 | public: 156 | 157 | DictTrie(); 158 | ~DictTrie(); 159 | 160 | #ifdef ___BUILD_MODEL___ 161 | // Construct the tree from the file fn_raw. 162 | // fn_validhzs provide the valid hanzi list. If fn_validhzs is 163 | // NULL, only chars in GB2312 will be included. 164 | bool build_dict(const char *fn_raw, const char *fn_validhzs); 165 | 166 | // Save the binary dictionary 167 | // Actually, the SpellingTrie/DictList instance will be also saved. 168 | bool save_dict(const char *filename); 169 | #endif // ___BUILD_MODEL___ 170 | 171 | void convert_to_hanzis(char16 *str, uint16 str_len); 172 | 173 | void convert_to_scis_ids(char16 *str, uint16 str_len); 174 | 175 | // Load a binary dictionary 176 | // The SpellingTrie instance/DictList will be also loaded 177 | bool load_dict(const char *filename, LemmaIdType start_id, 178 | LemmaIdType end_id); 179 | bool load_dict_fd(int sys_fd, long start_offset, long length, 180 | LemmaIdType start_id, LemmaIdType end_id); 181 | bool close_dict() {return true;} 182 | size_t number_of_lemmas() {return 0;} 183 | 184 | void reset_milestones(uint16 from_step, MileStoneHandle from_handle); 185 | 186 | MileStoneHandle extend_dict(MileStoneHandle from_handle, 187 | const DictExtPara *dep, 188 | LmaPsbItem *lpi_items, 189 | size_t lpi_max, size_t *lpi_num); 190 | 191 | size_t get_lpis(const uint16 *splid_str, uint16 splid_str_len, 192 | LmaPsbItem *lpi_items, size_t lpi_max); 193 | 194 | uint16 get_lemma_str(LemmaIdType id_lemma, char16 *str_buf, uint16 str_max); 195 | 196 | uint16 get_lemma_splids(LemmaIdType id_lemma, uint16 *splids, 197 | uint16 splids_max, bool arg_valid); 198 | 199 | size_t predict(const char16 *last_hzs, uint16 hzs_len, 200 | NPredictItem *npre_items, size_t npre_max, 201 | size_t b4_used); 202 | 203 | LemmaIdType put_lemma(char16 /*lemma_str*/[], uint16 /*splids*/[], 204 | uint16 /*lemma_len*/, uint16 /*count*/) {return 0;} 205 | 206 | LemmaIdType update_lemma(LemmaIdType /*lemma_id*/, int16 /*delta_count*/, 207 | bool /*selected*/) {return 0;} 208 | 209 | LemmaIdType get_lemma_id(char16 /*lemma_str*/[], uint16 /*splids*/[], 210 | uint16 /*lemma_len*/) {return 0;} 211 | 212 | LmaScoreType get_lemma_score(LemmaIdType /*lemma_id*/) {return 0;} 213 | 214 | LmaScoreType get_lemma_score(char16 /*lemma_str*/[], uint16 /*splids*/[], 215 | uint16 /*lemma_len*/) {return 0;} 216 | 217 | bool remove_lemma(LemmaIdType /*lemma_id*/) {return false;} 218 | 219 | size_t get_total_lemma_count() {return 0;} 220 | void set_total_lemma_count_of_others(size_t count); 221 | 222 | void flush_cache() {} 223 | 224 | LemmaIdType get_lemma_id(const char16 lemma_str[], uint16 lemma_len); 225 | 226 | // Fill the lemmas with highest scores to the prediction buffer. 227 | // his_len is the history length to fill in the prediction buffer. 228 | size_t predict_top_lmas(size_t his_len, NPredictItem *npre_items, 229 | size_t npre_max, size_t b4_used); 230 | }; 231 | } 232 | 233 | #endif // PINYINIME_INCLUDE_DICTTRIE_H__ 234 | -------------------------------------------------------------------------------- /libgooglepinyin/include/lpicache.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef PINYINIME_ANDPY_INCLUDE_LPICACHE_H__ 18 | #define PINYINIME_ANDPY_INCLUDE_LPICACHE_H__ 19 | 20 | #include 21 | #include "./searchutility.h" 22 | #include "./spellingtrie.h" 23 | 24 | namespace ime_pinyin { 25 | 26 | // Used to cache LmaPsbItem list for half spelling ids. 27 | class LpiCache { 28 | private: 29 | static LpiCache *instance_; 30 | static const int kMaxLpiCachePerId = 15; 31 | 32 | LmaPsbItem *lpi_cache_; 33 | uint16 *lpi_cache_len_; 34 | 35 | public: 36 | LpiCache(); 37 | ~LpiCache(); 38 | 39 | static LpiCache& get_instance(); 40 | 41 | // Test if the LPI list of the given splid has been cached. 42 | // If splid is a full spelling id, it returns false, because we only cache 43 | // list for half ids. 44 | bool is_cached(uint16 splid); 45 | 46 | // Put LPI list to cahce. If the length of the list, lpi_num, is longer than 47 | // the cache buffer. the list will be truncated, and function returns the 48 | // maximum length of the cache buffer. 49 | // Note: splid must be a half id, and lpi_items must be not NULL. The 50 | // caller of this function should guarantee this. 51 | size_t put_cache(uint16 splid, LmaPsbItem lpi_items[], size_t lpi_num); 52 | 53 | // Get the cached list for the given half id. 54 | // Return the length of the cached buffer. 55 | // Note: splid must be a half id, and lpi_items must be not NULL. The 56 | // caller of this function should guarantee this. 57 | size_t get_cache(uint16 splid, LmaPsbItem lpi_items[], size_t lpi_max); 58 | }; 59 | 60 | } // namespace 61 | 62 | #endif // PINYINIME_ANDPY_INCLUDE_LPICACHE_H__ 63 | -------------------------------------------------------------------------------- /libgooglepinyin/include/mystdlib.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef PINYINIME_INCLUDE_MYSTDLIB_H__ 18 | #define PINYINIME_INCLUDE_MYSTDLIB_H__ 19 | 20 | #include 21 | 22 | namespace ime_pinyin { 23 | 24 | void myqsort(void *p, size_t n, size_t es, 25 | int (*cmp)(const void *, const void *)); 26 | 27 | void *mybsearch(const void *key, const void *base, 28 | size_t nmemb, size_t size, 29 | int (*compar)(const void *, const void *)); 30 | } 31 | 32 | #endif // PINYINIME_INCLUDE_MYSTDLIB_H__ 33 | -------------------------------------------------------------------------------- /libgooglepinyin/include/ngram.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef PINYINIME_INCLUDE_NGRAM_H__ 18 | #define PINYINIME_INCLUDE_NGRAM_H__ 19 | 20 | #include 21 | #include 22 | #include "./dictdef.h" 23 | 24 | namespace ime_pinyin { 25 | 26 | typedef unsigned char CODEBOOK_TYPE; 27 | 28 | static const size_t kCodeBookSize = 256; 29 | 30 | class NGram { 31 | public: 32 | // The maximum score of a lemma item. 33 | static const LmaScoreType kMaxScore = 0x3fff; 34 | 35 | // In order to reduce the storage size, the original log value is amplified by 36 | // kScoreAmplifier, and we use LmaScoreType to store. 37 | // After this process, an item with a lower score has a higher frequency. 38 | static const int kLogValueAmplifier = -800; 39 | 40 | // System words' total frequency. It is not the real total frequency, instead, 41 | // It is only used to adjust system lemmas' scores when the user dictionary's 42 | // total frequency changes. 43 | // In this version, frequencies of system lemmas are fixed. We are considering 44 | // to make them changable in next version. 45 | static const size_t kSysDictTotalFreq = 100000000; 46 | 47 | private: 48 | 49 | static NGram* instance_; 50 | 51 | bool initialized_; 52 | uint32 idx_num_; 53 | 54 | size_t total_freq_none_sys_; 55 | 56 | // Score compensation for system dictionary lemmas. 57 | // Because after user adds some user lemmas, the total frequency changes, and 58 | // we use this value to normalize the score. 59 | float sys_score_compensation_; 60 | 61 | #ifdef ___BUILD_MODEL___ 62 | double *freq_codes_df_; 63 | #endif 64 | LmaScoreType *freq_codes_; 65 | CODEBOOK_TYPE *lma_freq_idx_; 66 | 67 | public: 68 | NGram(); 69 | ~NGram(); 70 | 71 | static NGram& get_instance(); 72 | 73 | bool save_ngram(FILE *fp); 74 | bool load_ngram(FILE *fp); 75 | 76 | // Set the total frequency of all none system dictionaries. 77 | void set_total_freq_none_sys(size_t freq_none_sys); 78 | 79 | float get_uni_psb(LemmaIdType lma_id); 80 | 81 | // Convert a probability to score. Actually, the score will be limited to 82 | // kMaxScore, but at runtime, we also need float expression to get accurate 83 | // value of the score. 84 | // After the conversion, a lower score indicates a higher probability of the 85 | // item. 86 | static float convert_psb_to_score(double psb); 87 | 88 | #ifdef ___BUILD_MODEL___ 89 | // For constructing the unigram mode model. 90 | bool build_unigram(LemmaEntry *lemma_arr, size_t num, 91 | LemmaIdType next_idx_unused); 92 | #endif 93 | }; 94 | } 95 | 96 | #endif // PINYINIME_INCLUDE_NGRAM_H__ 97 | -------------------------------------------------------------------------------- /libgooglepinyin/include/pinyinime.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef PINYINIME_INCLUDE_ANDPYIME_H__ 18 | #define PINYINIME_INCLUDE_ANDPYIME_H__ 19 | 20 | #include 21 | #include "./dictdef.h" 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | namespace ime_pinyin { 28 | 29 | /** 30 | * Open the decoder engine via the system and user dictionary file names. 31 | * 32 | * @param fn_sys_dict The file name of the system dictionary. 33 | * @param fn_usr_dict The file name of the user dictionary. 34 | * @return true if open the decoder engine successfully. 35 | */ 36 | bool im_open_decoder(const char *fn_sys_dict, const char *fn_usr_dict); 37 | 38 | /** 39 | * Open the decoder engine via the system dictionary FD and user dictionary 40 | * file name. Because on Android, the system dictionary is embedded in the 41 | * whole application apk file. 42 | * 43 | * @param sys_fd The file in which the system dictionary is embedded. 44 | * @param start_offset The starting position of the system dictionary in the 45 | * file sys_fd. 46 | * @param length The length of the system dictionary in the file sys_fd, 47 | * counted in byte. 48 | * @return true if succeed. 49 | */ 50 | bool im_open_decoder_fd(int sys_fd, long start_offset, long length, 51 | const char *fn_usr_dict); 52 | 53 | /** 54 | * Close the decoder engine. 55 | */ 56 | void im_close_decoder(); 57 | 58 | /** 59 | * Set maximum limitations for decoding. If this function is not called, 60 | * default values will be used. For example, due to screen size limitation, 61 | * the UI engine of the IME can only show a certain number of letters(input) 62 | * to decode, and a certain number of Chinese characters(output). If after 63 | * user adds a new letter, the input or the output string is longer than the 64 | * limitations, the engine will discard the recent letter. 65 | * 66 | * @param max_sps_len Maximum length of the spelling string(Pinyin string). 67 | * @max_hzs_len Maximum length of the decoded Chinese character string. 68 | */ 69 | void im_set_max_lens(size_t max_sps_len, size_t max_hzs_len); 70 | 71 | /** 72 | * Flush cached data to persistent memory. Because at runtime, in order to 73 | * achieve best performance, some data is only store in memory. 74 | */ 75 | void im_flush_cache(); 76 | 77 | /** 78 | * Use a spelling string(Pinyin string) to search. The engine will try to do 79 | * an incremental search based on its previous search result, so if the new 80 | * string has the same prefix with the previous one stored in the decoder, 81 | * the decoder will only continue the search from the end of the prefix. 82 | * If the caller needs to do a brand new search, please call im_reset_search() 83 | * first. Calling im_search() is equivalent to calling im_add_letter() one by 84 | * one. 85 | * 86 | * @param sps_buf The spelling string buffer to decode. 87 | * @param sps_len The length of the spelling string buffer. 88 | * @return The number of candidates. 89 | */ 90 | size_t im_search(const char* sps_buf, size_t sps_len); 91 | 92 | /** 93 | * Make a delete operation in the current search result, and make research if 94 | * necessary. 95 | * 96 | * @param pos The posistion of char in spelling string to delete, or the 97 | * position of spelling id in result string to delete. 98 | * @param is_pos_in_splid Indicate whether the pos parameter is the position 99 | * in the spelling string, or the position in the result spelling id string. 100 | * @return The number of candidates. 101 | */ 102 | size_t im_delsearch(size_t pos, bool is_pos_in_splid, 103 | bool clear_fixed_this_step); 104 | 105 | /** 106 | * Reset the previous search result. 107 | */ 108 | void im_reset_search(); 109 | 110 | /** 111 | * Add a Pinyin letter to the current spelling string kept by decoder. If the 112 | * decoder fails in adding the letter, it will do nothing. im_get_sps_str() 113 | * can be used to get the spelling string kept by decoder currently. 114 | * 115 | * @param ch The letter to add. 116 | * @return The number of candidates. 117 | */ 118 | size_t im_add_letter(char ch); 119 | 120 | /** 121 | * Get the spelling string kept by the decoder. 122 | * 123 | * @param decoded_len Used to return how many characters in the spelling 124 | * string is successfully parsed. 125 | * @return The spelling string kept by the decoder. 126 | */ 127 | const char *im_get_sps_str(size_t *decoded_len); 128 | 129 | /** 130 | * Get a candidate(or choice) string. 131 | * 132 | * @param cand_id The id to get a candidate. Started from 0. Usually, id 0 133 | * is a sentence-level candidate. 134 | * @param cand_str The buffer to store the candidate. 135 | * @param max_len The maximum length of the buffer. 136 | * @return cand_str if succeeds, otherwise NULL. 137 | */ 138 | char16* im_get_candidate(size_t cand_id, char16* cand_str, 139 | size_t max_len); 140 | 141 | /** 142 | * Get the segmentation information(the starting positions) of the spelling 143 | * string. 144 | * 145 | * @param spl_start Used to return the starting posistions. 146 | * @return The number of spelling ids. If it is L, there will be L+1 valid 147 | * elements in spl_start, and spl_start[L] is the posistion after the end of 148 | * the last spelling id. 149 | */ 150 | size_t im_get_spl_start_pos(const uint16 *&spl_start); 151 | 152 | /** 153 | * Choose a candidate and make it fixed. If the candidate does not match 154 | * the end of all spelling ids, new candidates will be provided from the 155 | * first unfixed position. If the candidate matches the end of the all 156 | * spelling ids, there will be only one new candidates, or the whole fixed 157 | * sentence. 158 | * 159 | * @param cand_id The id of candidate to select and make it fixed. 160 | * @return The number of candidates. If after the selection, the whole result 161 | * string has been fixed, there will be only one candidate. 162 | */ 163 | size_t im_choose(size_t cand_id); 164 | 165 | /** 166 | * Cancel the last selection, or revert the last operation of im_choose(). 167 | * 168 | * @return The number of candidates. 169 | */ 170 | size_t im_cancel_last_choice(); 171 | 172 | /** 173 | * Get the number of fixed spelling ids, or Chinese characters. 174 | * 175 | * @return The number of fixed spelling ids, of Chinese characters. 176 | */ 177 | size_t im_get_fixed_len(); 178 | 179 | /** 180 | * Cancel the input state and reset the search workspace. 181 | */ 182 | bool im_cancel_input(); 183 | 184 | /** 185 | * Get prediction candiates based on the given fixed Chinese string as the 186 | * history. 187 | * 188 | * @param his_buf The history buffer to do the prediction. It should be ended 189 | * with '\0'. 190 | * @param pre_buf Used to return prediction result list. 191 | * @return The number of predicted result string. 192 | */ 193 | size_t im_get_predicts(const char16 *his_buf, 194 | char16 (*&pre_buf)[kMaxPredictSize + 1]); 195 | 196 | /** 197 | * Enable Shengmus in ShouZiMu mode. 198 | */ 199 | void im_enable_shm_as_szm(bool enable); 200 | 201 | /** 202 | * Enable Yunmus in ShouZiMu mode. 203 | */ 204 | void im_enable_ym_as_szm(bool enable); 205 | 206 | /** 207 | * Initializes or uninitializes the user dictionary. 208 | * 209 | * @param fn_usr_dict The file name of the user dictionary. 210 | */ 211 | void im_init_user_dictionary(const char *fn_usr_dict); 212 | 213 | /** 214 | * Returns the current status of user dictinary. 215 | */ 216 | bool im_is_user_dictionary_enabled(void); 217 | } 218 | 219 | #ifdef __cplusplus 220 | } 221 | #endif 222 | 223 | #endif // PINYINIME_INCLUDE_ANDPYIME_H__ 224 | -------------------------------------------------------------------------------- /libgooglepinyin/include/searchutility.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef PINYINIME_ANDPY_INCLUDE_SEARCHCOMMON_H__ 18 | #define PINYINIME_ANDPY_INCLUDE_SEARCHCOMMON_H__ 19 | 20 | #include 21 | #include "./spellingtrie.h" 22 | 23 | namespace ime_pinyin { 24 | 25 | // Type used to identify the size of a pool, such as id pool, etc. 26 | typedef uint16 PoolPosType; 27 | 28 | // Type used to identify a parsing mile stone in an atom dictionary. 29 | typedef uint16 MileStoneHandle; 30 | 31 | // Type used to express a lemma and its probability score. 32 | typedef struct { 33 | size_t id:(kLemmaIdSize * 8); 34 | size_t lma_len:4; 35 | uint16 psb; // The score, the lower psb, the higher possibility. 36 | // For single character items, we may also need Hanzi. 37 | // For multiple characer items, ignore it. 38 | char16 hanzi; 39 | } LmaPsbItem, *PLmaPsbItem; 40 | 41 | // LmaPsbItem extended with string. 42 | typedef struct { 43 | LmaPsbItem lpi; 44 | char16 str[kMaxLemmaSize + 1]; 45 | } LmaPsbStrItem, *PLmaPsbStrItem; 46 | 47 | 48 | typedef struct { 49 | float psb; 50 | char16 pre_hzs[kMaxPredictSize]; 51 | uint16 his_len; // The length of the history used to do the prediction. 52 | } NPredictItem, *PNPredictItem; 53 | 54 | // Parameter structure used to extend in a dictionary. All dictionaries 55 | // receives the same DictExtPara and a dictionary specific MileStoneHandle for 56 | // extending. 57 | // 58 | // When the user inputs a new character, AtomDictBase::extend_dict() will be 59 | // called at least once for each dictionary. 60 | // 61 | // For example, when the user inputs "wm", extend_dict() will be called twice, 62 | // and the DictExtPara parameter are as follows respectively: 63 | // 1. splids = {w, m}; splids_extended = 1; ext_len = 1; step_no = 1; 64 | // splid_end_split = false; id_start = wa(the first id start with 'w'); 65 | // id_num = number of ids starting with 'w'. 66 | // 2. splids = {m}; splids_extended = 0; ext_len = 1; step_no = 1; 67 | // splid_end_split = false; id_start = wa; id_num = number of ids starting with 68 | // 'w'. 69 | // 70 | // For string "women", one of the cases of the DictExtPara parameter is: 71 | // splids = {wo, men}, splids_extended = 1, ext_len = 3 (length of "men"), 72 | // step_no = 4; splid_end_split = false; id_start = men, id_num = 1. 73 | // 74 | typedef struct { 75 | // Spelling ids for extending, there are splids_extended + 1 ids in the 76 | // buffer. 77 | // For a normal lemma, there can only be kMaxLemmaSize spelling ids in max, 78 | // but for a composing phrase, there can kMaxSearchSteps spelling ids. 79 | uint16 splids[kMaxSearchSteps]; 80 | 81 | // Number of ids that have been used before. splids[splids_extended] is the 82 | // newly added id for the current extension. 83 | uint16 splids_extended; 84 | 85 | // The step span of the extension. It is also the size of the string for 86 | // the newly added spelling id. 87 | uint16 ext_len; 88 | 89 | // The step number for the current extension. It is also the ending position 90 | // in the input Pinyin string for the substring of spelling ids in splids[]. 91 | // For example, when the user inputs "women", step_no = 4. 92 | // This parameter may useful to manage the MileStoneHandle list for each 93 | // step. When the user deletes a character from the string, MileStoneHandle 94 | // objects for the the steps after that character should be reset; when the 95 | // user begins a new string, all MileStoneHandle objects should be reset. 96 | uint16 step_no; 97 | 98 | // Indicate whether the newly added spelling ends with a splitting character 99 | bool splid_end_split; 100 | 101 | // If the newly added id is a half id, id_start is the first id of the 102 | // corresponding full ids; if the newly added id is a full id, id_start is 103 | // that id. 104 | uint16 id_start; 105 | 106 | // If the newly added id is a half id, id_num is the number of corresponding 107 | // ids; if it is a full id, id_num == 1. 108 | uint16 id_num; 109 | }DictExtPara, *PDictExtPara; 110 | 111 | bool is_system_lemma(LemmaIdType lma_id); 112 | bool is_user_lemma(LemmaIdType lma_id); 113 | bool is_composing_lemma(LemmaIdType lma_id); 114 | 115 | int cmp_lpi_with_psb(const void *p1, const void *p2); 116 | int cmp_lpi_with_unified_psb(const void *p1, const void *p2); 117 | int cmp_lpi_with_id(const void *p1, const void *p2); 118 | int cmp_lpi_with_hanzi(const void *p1, const void *p2); 119 | 120 | int cmp_lpsi_with_str(const void *p1, const void *p2); 121 | 122 | int cmp_hanzis_1(const void *p1, const void *p2); 123 | int cmp_hanzis_2(const void *p1, const void *p2); 124 | int cmp_hanzis_3(const void *p1, const void *p2); 125 | int cmp_hanzis_4(const void *p1, const void *p2); 126 | int cmp_hanzis_5(const void *p1, const void *p2); 127 | int cmp_hanzis_6(const void *p1, const void *p2); 128 | int cmp_hanzis_7(const void *p1, const void *p2); 129 | int cmp_hanzis_8(const void *p1, const void *p2); 130 | 131 | int cmp_npre_by_score(const void *p1, const void *p2); 132 | int cmp_npre_by_hislen_score(const void *p1, const void *p2); 133 | int cmp_npre_by_hanzi_score(const void *p1, const void *p2); 134 | 135 | 136 | size_t remove_duplicate_npre(NPredictItem *npre_items, size_t npre_num); 137 | 138 | size_t align_to_size_t(size_t size); 139 | 140 | } // namespace 141 | 142 | #endif // PINYINIME_ANDPY_INCLUDE_SEARCHCOMMON_H__ 143 | -------------------------------------------------------------------------------- /libgooglepinyin/include/spellingtable.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef PINYINIME_INCLUDE_SPELLINGTABLE_H__ 18 | #define PINYINIME_INCLUDE_SPELLINGTABLE_H__ 19 | 20 | #include 21 | #include "./dictdef.h" 22 | 23 | namespace ime_pinyin { 24 | 25 | #ifdef ___BUILD_MODEL___ 26 | 27 | const size_t kMaxSpellingSize = kMaxPinyinSize; 28 | 29 | typedef struct { 30 | char str[kMaxSpellingSize + 1]; 31 | double freq; 32 | } RawSpelling, *PRawSpelling; 33 | 34 | // This class is used to store the spelling strings 35 | // The length of the input spelling string should be less or equal to the 36 | // spelling_size_ (set by init_table). If the input string is too long, 37 | // we only keep its first spelling_size_ chars. 38 | class SpellingTable { 39 | private: 40 | static const size_t kNotSupportNum = 3; 41 | static const char kNotSupportList[kNotSupportNum][kMaxSpellingSize + 1]; 42 | 43 | bool need_score_; 44 | 45 | size_t spelling_max_num_; 46 | 47 | RawSpelling *raw_spellings_; 48 | 49 | // Used to store spelling strings. If the spelling table needs to calculate 50 | // score, an extra char after each spelling string is the score. 51 | // An item with a lower score has a higher probability. 52 | char *spelling_buf_; 53 | size_t spelling_size_; 54 | 55 | double total_freq_; 56 | 57 | size_t spelling_num_; 58 | 59 | double score_amplifier_; 60 | 61 | unsigned char average_score_; 62 | 63 | // If frozen is true, put_spelling() and contain() are not allowed to call. 64 | bool frozen_; 65 | 66 | size_t get_hash_pos(const char* spelling_str); 67 | size_t hash_pos_next(size_t hash_pos); 68 | void free_resource(); 69 | public: 70 | SpellingTable(); 71 | ~SpellingTable(); 72 | 73 | // pure_spl_size is the pure maximum spelling string size. For example, 74 | // "zhuang" is the longgest item in Pinyin, so pure_spl_size should be 6. 75 | // spl_max_num is the maximum number of spelling strings to store. 76 | // need_score is used to indicate whether the caller needs to calculate a 77 | // score for each spelling. 78 | bool init_table(size_t pure_spl_size, size_t spl_max_num, bool need_score); 79 | 80 | // Put a spelling string to the table. 81 | // It always returns false if called after arrange() withtout a new 82 | // init_table() operation. 83 | // freq is the spelling's occuring count. 84 | // If the spelling has been in the table, occuring count will accumulated. 85 | bool put_spelling(const char* spelling_str, double spl_count); 86 | 87 | // Test whether a spelling string is in the table. 88 | // It always returns false, when being called after arrange() withtout a new 89 | // init_table() operation. 90 | bool contain(const char* spelling_str); 91 | 92 | // Sort the spelling strings and put them from the begin of the buffer. 93 | // Return the pointer of the sorted spelling strings. 94 | // item_size and spl_num return the item size and number of spelling. 95 | // Because each spelling uses a '\0' as terminator, the returned item_size is 96 | // at least one char longer than the spl_size parameter specified by 97 | // init_table(). If the table is initialized to calculate score, item_size 98 | // will be increased by 1, and current_spl_str[item_size - 1] stores an 99 | // unsinged char score. 100 | // An item with a lower score has a higher probability. 101 | // Do not call put_spelling() and contains() after arrange(). 102 | const char* arrange(size_t *item_size, size_t *spl_num); 103 | 104 | float get_score_amplifier(); 105 | 106 | unsigned char get_average_score(); 107 | }; 108 | #endif // ___BUILD_MODEL___ 109 | } 110 | 111 | #endif // PINYINIME_INCLUDE_SPELLINGTABLE_H__ 112 | -------------------------------------------------------------------------------- /libgooglepinyin/include/spellingtrie.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef PINYINIME_INCLUDE_SPELLINGTRIE_H__ 18 | #define PINYINIME_INCLUDE_SPELLINGTRIE_H__ 19 | 20 | #include 21 | #include 22 | #include "./dictdef.h" 23 | 24 | namespace ime_pinyin { 25 | 26 | static const unsigned short kFullSplIdStart = kHalfSpellingIdNum + 1; 27 | 28 | // Node used for the trie of spellings 29 | struct SpellingNode { 30 | SpellingNode *first_son; 31 | // The spelling id for each node. If you need more bits to store 32 | // spelling id, please adjust this structure. 33 | uint16 spelling_idx:11; 34 | uint16 num_of_son:5; 35 | char char_this_node; 36 | unsigned char score; 37 | }; 38 | 39 | class SpellingTrie { 40 | private: 41 | static const int kMaxYmNum = 64; 42 | static const size_t kValidSplCharNum = 26; 43 | 44 | static const uint16 kHalfIdShengmuMask = 0x01; 45 | static const uint16 kHalfIdYunmuMask = 0x02; 46 | static const uint16 kHalfIdSzmMask = 0x04; 47 | 48 | // Map from half spelling id to single char. 49 | // For half ids of Zh/Ch/Sh, map to z/c/s (low case) respectively. 50 | // For example, 1 to 'A', 2 to 'B', 3 to 'C', 4 to 'c', 5 to 'D', ..., 51 | // 28 to 'Z', 29 to 'z'. 52 | // [0] is not used to achieve better efficiency. 53 | static const char kHalfId2Sc_[kFullSplIdStart + 1]; 54 | 55 | static unsigned char char_flags_[]; 56 | static SpellingTrie* instance_; 57 | 58 | // The spelling table 59 | char *spelling_buf_; 60 | 61 | // The size of longest spelling string, includes '\0' and an extra char to 62 | // store score. For example, "zhuang" is the longgest item in Pinyin list, 63 | // so spelling_size_ is 8. 64 | // Structure: The string ended with '\0' + score char. 65 | // An item with a lower score has a higher probability. 66 | uint32 spelling_size_; 67 | 68 | // Number of full spelling ids. 69 | uint32 spelling_num_; 70 | 71 | float score_amplifier_; 72 | unsigned char average_score_; 73 | 74 | // The Yunmu id list for the spelling ids (for half ids of Shengmu, 75 | // the Yunmu id is 0). 76 | // The length of the list is spelling_num_ + kFullSplIdStart, 77 | // so that spl_ym_ids_[splid] is the Yunmu id of the splid. 78 | uint8 *spl_ym_ids_; 79 | 80 | // The Yunmu table. 81 | // Each Yunmu will be assigned with Yunmu id from 1. 82 | char *ym_buf_; 83 | size_t ym_size_; // The size of longest Yunmu string, '\0'included. 84 | size_t ym_num_; 85 | 86 | // The spelling string just queried 87 | char *splstr_queried_; 88 | 89 | // The spelling string just queried 90 | char16 *splstr16_queried_; 91 | 92 | // The root node of the spelling tree 93 | SpellingNode* root_; 94 | 95 | // If a none qwerty key such as a fnction key like ENTER is given, this node 96 | // will be used to indicate that this is not a QWERTY node. 97 | SpellingNode* dumb_node_; 98 | 99 | // If a splitter key is pressed, this node will be used to indicate that this 100 | // is a splitter key. 101 | SpellingNode* splitter_node_; 102 | 103 | // Used to get the first level sons. 104 | SpellingNode* level1_sons_[kValidSplCharNum]; 105 | 106 | // The full spl_id range for specific half id. 107 | // h2f means half to full. 108 | // A half id can be a ShouZiMu id (id to represent the first char of a full 109 | // spelling, including Shengmu and Yunmu), or id of zh/ch/sh. 110 | // [1..kFullSplIdStart-1] is the arrange of half id. 111 | uint16 h2f_start_[kFullSplIdStart]; 112 | uint16 h2f_num_[kFullSplIdStart]; 113 | 114 | // Map from full id to half id. 115 | uint16 *f2h_; 116 | 117 | #ifdef ___BUILD_MODEL___ 118 | // How many node used to build the trie. 119 | size_t node_num_; 120 | #endif 121 | 122 | SpellingTrie(); 123 | 124 | void free_son_trie(SpellingNode* node); 125 | 126 | // Construct a subtree using a subset of the spelling array (from 127 | // item_star to item_end). 128 | // Member spelliing_buf_ and spelling_size_ should be valid. 129 | // parent is used to update its num_of_son and score. 130 | SpellingNode* construct_spellings_subset(size_t item_start, size_t item_end, 131 | size_t level, SpellingNode *parent); 132 | bool build_f2h(); 133 | 134 | // The caller should guarantee ch >= 'A' && ch <= 'Z' 135 | bool is_shengmu_char(char ch) const; 136 | 137 | // The caller should guarantee ch >= 'A' && ch <= 'Z' 138 | bool is_yunmu_char(char ch) const; 139 | 140 | #ifdef ___BUILD_MODEL___ 141 | // Given a spelling string, return its Yunmu string. 142 | // The caller guaratees spl_str is valid. 143 | const char* get_ym_str(const char *spl_str); 144 | 145 | // Build the Yunmu list, and the mapping relation between the full ids and the 146 | // Yunmu ids. This functin is called after the spelling trie is built. 147 | bool build_ym_info(); 148 | #endif 149 | 150 | friend class SpellingParser; 151 | friend class SmartSplParser; 152 | friend class SmartSplParser2; 153 | 154 | public: 155 | ~SpellingTrie(); 156 | 157 | inline static bool is_valid_spl_char(char ch) { 158 | return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'); 159 | } 160 | 161 | // The caller guarantees that the two chars are valid spelling chars. 162 | inline static bool is_same_spl_char(char ch1, char ch2) { 163 | return ch1 == ch2 || ch1 - ch2 == 'a' - 'A' || ch2 - ch1 == 'a' - 'A'; 164 | } 165 | 166 | // Construct the tree from the input pinyin array 167 | // The given string list should have been sorted. 168 | // score_amplifier is used to convert a possibility value into score. 169 | // average_score is the average_score of all spellings. The dumb node is 170 | // assigned with this score. 171 | bool construct(const char* spelling_arr, size_t item_size, size_t item_num, 172 | float score_amplifier, unsigned char average_score); 173 | 174 | // Test if the given id is a valid spelling id. 175 | // If function returns true, the given splid may be updated like this: 176 | // When 'A' is not enabled in ShouZiMu mode, the parsing result for 'A' is 177 | // first given as a half id 1, but because 'A' is a one-char Yunmu and 178 | // it is a valid id, it needs to updated to its corresponding full id. 179 | bool if_valid_id_update(uint16 *splid) const; 180 | 181 | // Test if the given id is a half id. 182 | bool is_half_id(uint16 splid) const; 183 | 184 | bool is_full_id(uint16 splid) const; 185 | 186 | // Test if the given id is a one-char Yunmu id (obviously, it is also a half 187 | // id), such as 'A', 'E' and 'O'. 188 | bool is_half_id_yunmu(uint16 splid) const; 189 | 190 | // Test if this char is a ShouZiMu char. This ShouZiMu char may be not enabled. 191 | // For Pinyin, only i/u/v is not a ShouZiMu char. 192 | // The caller should guarantee that ch >= 'A' && ch <= 'Z' 193 | bool is_szm_char(char ch) const; 194 | 195 | // Test If this char is enabled in ShouZiMu mode. 196 | // The caller should guarantee that ch >= 'A' && ch <= 'Z' 197 | bool szm_is_enabled(char ch) const; 198 | 199 | // Enable/disable Shengmus in ShouZiMu mode(using the first char of a spelling 200 | // to input). 201 | void szm_enable_shm(bool enable); 202 | 203 | // Enable/disable Yunmus in ShouZiMu mode. 204 | void szm_enable_ym(bool enable); 205 | 206 | // Test if this char is enabled in ShouZiMu mode. 207 | // The caller should guarantee ch >= 'A' && ch <= 'Z' 208 | bool is_szm_enabled(char ch) const; 209 | 210 | // Return the number of full ids for the given half id. 211 | uint16 half2full_num(uint16 half_id) const; 212 | 213 | // Return the number of full ids for the given half id, and fill spl_id_start 214 | // to return the first full id. 215 | uint16 half_to_full(uint16 half_id, uint16 *spl_id_start) const; 216 | 217 | // Return the corresponding half id for the given full id. 218 | // Not frequently used, low efficient. 219 | // Return 0 if fails. 220 | uint16 full_to_half(uint16 full_id) const; 221 | 222 | // To test whether a half id is compatible with a full id. 223 | // Generally, when half_id == full_to_half(full_id), return true. 224 | // But for "Zh, Ch, Sh", if fussy mode is on, half id for 'Z' is compatible 225 | // with a full id like "Zhe". (Fussy mode is not ready). 226 | bool half_full_compatible(uint16 half_id, uint16 full_id) const; 227 | 228 | static const SpellingTrie* get_cpinstance(); 229 | 230 | static SpellingTrie& get_instance(); 231 | 232 | // Save to the file stream 233 | bool save_spl_trie(FILE *fp); 234 | 235 | // Load from the file stream 236 | bool load_spl_trie(FILE *fp); 237 | 238 | // Get the number of spellings 239 | size_t get_spelling_num(); 240 | 241 | // Return the Yunmu id for the given Yunmu string. 242 | // If the string is not valid, return 0; 243 | uint8 get_ym_id(const char* ym_str); 244 | 245 | // Get the readonly Pinyin string for a given spelling id 246 | const char* get_spelling_str(uint16 splid); 247 | 248 | // Get the readonly Pinyin string for a given spelling id 249 | const char16* get_spelling_str16(uint16 splid); 250 | 251 | // Get Pinyin string for a given spelling id. Return the length of the 252 | // string, and fill-in '\0' at the end. 253 | size_t get_spelling_str16(uint16 splid, char16 *splstr16, 254 | size_t splstr16_len); 255 | }; 256 | } 257 | 258 | #endif // PINYINIME_INCLUDE_SPELLINGTRIE_H__ 259 | -------------------------------------------------------------------------------- /libgooglepinyin/include/splparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef PINYINIME_INCLUDE_SPLPARSER_H__ 18 | #define PINYINIME_INCLUDE_SPLPARSER_H__ 19 | 20 | #include "./dictdef.h" 21 | #include "./spellingtrie.h" 22 | 23 | namespace ime_pinyin { 24 | 25 | class SpellingParser { 26 | protected: 27 | const SpellingTrie *spl_trie_; 28 | 29 | public: 30 | SpellingParser(); 31 | 32 | // Given a string, parse it into a spelling id stream. 33 | // If the whole string are sucessfully parsed, last_is_pre will be true; 34 | // if the whole string is not fullly parsed, last_is_pre will return whether 35 | // the last part of the string is a prefix of a full spelling string. For 36 | // example, given string "zhengzhon", "zhon" is not a valid speling, but it is 37 | // the prefix of "zhong". 38 | // 39 | // If splstr starts with a character not in ['a'-z'] (it is a split char), 40 | // return 0. 41 | // Split char can only appear in the middle of the string or at the end. 42 | uint16 splstr_to_idxs(const char *splstr, uint16 str_len, uint16 splidx[], 43 | uint16 start_pos[], uint16 max_size, bool &last_is_pre); 44 | 45 | // Similar to splstr_to_idxs(), the only difference is that splstr_to_idxs() 46 | // convert single-character Yunmus into half ids, while this function converts 47 | // them into full ids. 48 | uint16 splstr_to_idxs_f(const char *splstr, uint16 str_len, uint16 splidx[], 49 | uint16 start_pos[], uint16 max_size, bool &last_is_pre); 50 | 51 | // Similar to splstr_to_idxs(), the only difference is that this function 52 | // uses char16 instead of char8. 53 | uint16 splstr16_to_idxs(const char16 *splstr, uint16 str_len, uint16 splidx[], 54 | uint16 start_pos[], uint16 max_size, bool &last_is_pre); 55 | 56 | // Similar to splstr_to_idxs_f(), the only difference is that this function 57 | // uses char16 instead of char8. 58 | uint16 splstr16_to_idxs_f(const char16 *splstr16, uint16 str_len, 59 | uint16 splidx[], uint16 start_pos[], 60 | uint16 max_size, bool &last_is_pre); 61 | 62 | // If the given string is a spelling, return the id, others, return 0. 63 | // If the give string is a single char Yunmus like "A", and the char is 64 | // enabled in ShouZiMu mode, the returned spelling id will be a half id. 65 | // When the returned spelling id is a half id, *is_pre returns whether it 66 | // is a prefix of a full spelling string. 67 | uint16 get_splid_by_str(const char *splstr, uint16 str_len, bool *is_pre); 68 | 69 | // If the given string is a spelling, return the id, others, return 0. 70 | // If the give string is a single char Yunmus like "a", no matter the char 71 | // is enabled in ShouZiMu mode or not, the returned spelling id will be 72 | // a full id. 73 | // When the returned spelling id is a half id, *p_is_pre returns whether it 74 | // is a prefix of a full spelling string. 75 | uint16 get_splid_by_str_f(const char *splstr, uint16 str_len, bool *is_pre); 76 | 77 | // Splitter chars are not included. 78 | bool is_valid_to_parse(char ch); 79 | 80 | // When auto-correction is not enabled, get_splid_by_str() will be called to 81 | // return the single result. When auto-correction is enabled, this function 82 | // will be called to get the results. Auto-correction is not ready. 83 | // full_id_num returns number of full spelling ids. 84 | // is_pre returns whether the given string is the prefix of a full spelling 85 | // string. 86 | // If splstr starts with a character not in [a-zA-Z] (it is a split char), 87 | // return 0. 88 | // Split char can only appear in the middle of the string or at the end. 89 | // The caller should guarantee NULL != splstr && str_len > 0 && NULL != splidx 90 | uint16 get_splids_parallel(const char *splstr, uint16 str_len, 91 | uint16 splidx[], uint16 max_size, 92 | uint16 &full_id_num, bool &is_pre); 93 | }; 94 | } 95 | 96 | #endif // PINYINIME_INCLUDE_SPLPARSER_H__ 97 | -------------------------------------------------------------------------------- /libgooglepinyin/include/sync.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef PINYINIME_INCLUDE_SYNC_H__ 18 | #define PINYINIME_INCLUDE_SYNC_H__ 19 | 20 | #define ___SYNC_ENABLED___ 21 | 22 | #ifdef ___SYNC_ENABLED___ 23 | 24 | #include "userdict.h" 25 | 26 | namespace ime_pinyin { 27 | 28 | // Class for user dictionary synchronization 29 | // This class is not thread safe 30 | // Normal invoking flow will be 31 | // begin() -> 32 | // put_lemmas() x N -> 33 | // { 34 | // get_lemmas() -> 35 | // [ get_last_got_count() ] -> 36 | // clear_last_got() -> 37 | // } x N -> 38 | // finish() 39 | class Sync { 40 | public: 41 | Sync(); 42 | ~Sync(); 43 | 44 | static const int kUserDictMaxLemmaCount = 5000; 45 | static const int kUserDictMaxLemmaSize = 200000; 46 | static const int kUserDictRatio = 20; 47 | 48 | bool begin(const char * filename); 49 | 50 | // Merge lemmas downloaded from sync server into local dictionary 51 | // lemmas, lemmas string encoded in UTF16LE 52 | // len, length of lemmas string 53 | // Return how many lemmas merged successfully 54 | int put_lemmas(char16 * lemmas, int len); 55 | 56 | // Get local new user lemmas into UTF16LE string 57 | // str, buffer ptr to store new user lemmas 58 | // size, size of buffer 59 | // Return length of returned buffer in measure of UTF16LE 60 | int get_lemmas(char16 * str, int size); 61 | 62 | // Return lemmas count in last get_lemmas() 63 | int get_last_got_count(); 64 | 65 | // Return total lemmas count need get_lemmas() 66 | int get_total_count(); 67 | 68 | // Clear lemmas got by recent get_lemmas() 69 | void clear_last_got(); 70 | 71 | void finish(); 72 | 73 | int get_capacity(); 74 | 75 | private: 76 | UserDict * userdict_; 77 | char * dictfile_; 78 | int last_count_; 79 | }; 80 | 81 | } 82 | 83 | #endif 84 | 85 | #endif // PINYINIME_INCLUDE_SYNC_H__ 86 | -------------------------------------------------------------------------------- /libgooglepinyin/include/utf16char.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef PINYINIME_INCLUDE_UTF16CHAR_H__ 18 | #define PINYINIME_INCLUDE_UTF16CHAR_H__ 19 | 20 | #include 21 | 22 | namespace ime_pinyin { 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | typedef unsigned short char16; 29 | 30 | // Get a token from utf16_str, 31 | // Returned pointer is a '\0'-terminated utf16 string, or NULL 32 | // *utf16_str_next returns the next part of the string for further tokenizing 33 | char16* utf16_strtok(char16 *utf16_str, size_t *token_size, 34 | char16 **utf16_str_next); 35 | 36 | int utf16_atoi(const char16 *utf16_str); 37 | 38 | float utf16_atof(const char16 *utf16_str); 39 | 40 | size_t utf16_strlen(const char16 *utf16_str); 41 | 42 | int utf16_strcmp(const char16 *str1, const char16 *str2); 43 | int utf16_strncmp(const char16 *str1, const char16 *str2, size_t size); 44 | 45 | char16* utf16_strcpy(char16 *dst, const char16 *src); 46 | char16* utf16_strncpy(char16 *dst, const char16 *src, size_t size); 47 | 48 | 49 | char* utf16_strcpy_tochar(char *dst, const char16 *src); 50 | 51 | #ifdef __cplusplus 52 | } 53 | #endif 54 | } 55 | 56 | #endif // PINYINIME_INCLUDE_UTF16CHAR_H__ 57 | -------------------------------------------------------------------------------- /libgooglepinyin/include/utf16reader.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef PINYINIME_INCLUDE_UTF16READER_H__ 18 | #define PINYINIME_INCLUDE_UTF16READER_H__ 19 | 20 | #include 21 | #include "./utf16char.h" 22 | 23 | namespace ime_pinyin { 24 | 25 | class Utf16Reader { 26 | private: 27 | FILE *fp_; 28 | char16 *buffer_; 29 | size_t buffer_total_len_; 30 | size_t buffer_next_pos_; 31 | 32 | // Always less than buffer_total_len_ - buffer_next_pos_ 33 | size_t buffer_valid_len_; 34 | 35 | public: 36 | Utf16Reader(); 37 | ~Utf16Reader(); 38 | 39 | // filename is the name of the file to open. 40 | // buffer_len specifies how long buffer should be allocated to speed up the 41 | // future reading 42 | bool open(const char* filename, size_t buffer_len); 43 | char16* readline(char16* read_buf, size_t max_len); 44 | bool close(); 45 | }; 46 | } 47 | 48 | #endif // PINYINIME_INCLUDE_UTF16READER_H__ 49 | -------------------------------------------------------------------------------- /libgooglepinyin/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(LIBGOOGLEPINYIN_API_VERSION "0.1.0") 2 | 3 | set(LIBGOOGLEPINYIN_ABI_VERSION "0.1.0") 4 | set(LIBGOOGLEPINYIN_ABI_MAJOR_VERSION "0") 5 | 6 | set(GOOGLEPINYIN_SRC 7 | dictbuilder.cpp 8 | dictlist.cpp 9 | dicttrie.cpp 10 | lpicache.cpp 11 | matrixsearch.cpp 12 | mystdlib.cpp 13 | ngram.cpp 14 | pinyinime.cpp 15 | searchutility.cpp 16 | spellingtable.cpp 17 | spellingtrie.cpp 18 | splparser.cpp 19 | sync.cpp 20 | userdict.cpp 21 | utf16char.cpp 22 | utf16reader.cpp 23 | ) 24 | 25 | add_library(googlepinyin SHARED ${GOOGLEPINYIN_SRC}) 26 | target_link_libraries(googlepinyin pthread ) 27 | set_target_properties(googlepinyin PROPERTIES 28 | VERSION ${LIBGOOGLEPINYIN_ABI_VERSION} 29 | SOVERSION ${LIBGOOGLEPINYIN_ABI_MAJOR_VERSION} 30 | LINK_FLAGS ${CMAKE_SHARED_LINKER_FLAGS} 31 | ) 32 | 33 | if (ENABLE_STATIC) 34 | 35 | add_library(googlepinyin-static STATIC ${GOOGLEPINYIN_SRC}) 36 | SET_TARGET_PROPERTIES(googlepinyin-static PROPERTIES 37 | OUTPUT_NAME "googlepinyin" 38 | PREFIX "lib" 39 | ) 40 | 41 | install(TARGETS googlepinyin-static 42 | LIBRARY DESTINATION ${LIB_INSTALL_DIR} 43 | ARCHIVE DESTINATION ${LIB_INSTALL_DIR} 44 | ) 45 | endif (ENABLE_STATIC) 46 | install(TARGETS googlepinyin 47 | LIBRARY DESTINATION ${LIB_INSTALL_DIR} 48 | ARCHIVE DESTINATION ${LIB_INSTALL_DIR} 49 | ) 50 | -------------------------------------------------------------------------------- /libgooglepinyin/src/lpicache.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | #include "../include/lpicache.h" 19 | 20 | namespace ime_pinyin { 21 | 22 | LpiCache* LpiCache::instance_ = NULL; 23 | 24 | LpiCache::LpiCache() { 25 | lpi_cache_ = new LmaPsbItem[kFullSplIdStart * kMaxLpiCachePerId]; 26 | lpi_cache_len_ = new uint16[kFullSplIdStart]; 27 | assert(NULL != lpi_cache_); 28 | assert(NULL != lpi_cache_len_); 29 | for (uint16 id = 0; id < kFullSplIdStart; id++) 30 | lpi_cache_len_[id] = 0; 31 | } 32 | 33 | LpiCache::~LpiCache() { 34 | if (NULL != lpi_cache_) 35 | delete [] lpi_cache_; 36 | 37 | if (NULL != lpi_cache_len_) 38 | delete [] lpi_cache_len_; 39 | } 40 | 41 | LpiCache& LpiCache::get_instance() { 42 | if (NULL == instance_) { 43 | instance_ = new LpiCache(); 44 | assert(NULL != instance_); 45 | } 46 | return *instance_; 47 | } 48 | 49 | bool LpiCache::is_cached(uint16 splid) { 50 | if (splid >= kFullSplIdStart) 51 | return false; 52 | return lpi_cache_len_[splid] != 0; 53 | } 54 | 55 | size_t LpiCache::put_cache(uint16 splid, LmaPsbItem lpi_items[], 56 | size_t lpi_num) { 57 | uint16 num = kMaxLpiCachePerId; 58 | if (num > lpi_num) 59 | num = static_cast(lpi_num); 60 | 61 | LmaPsbItem *lpi_cache_this = lpi_cache_ + splid * kMaxLpiCachePerId; 62 | for (uint16 pos = 0; pos < num; pos++) 63 | lpi_cache_this[pos] = lpi_items[pos]; 64 | 65 | lpi_cache_len_[splid] = num; 66 | return num; 67 | } 68 | 69 | size_t LpiCache::get_cache(uint16 splid, LmaPsbItem lpi_items[], 70 | size_t lpi_max) { 71 | if (lpi_max > lpi_cache_len_[splid]) 72 | lpi_max = lpi_cache_len_[splid]; 73 | 74 | LmaPsbItem *lpi_cache_this = lpi_cache_ + splid * kMaxLpiCachePerId; 75 | for (uint16 pos = 0; pos < lpi_max; pos++) { 76 | lpi_items[pos] = lpi_cache_this[pos]; 77 | } 78 | return lpi_max; 79 | } 80 | 81 | } // namespace ime_pinyin 82 | -------------------------------------------------------------------------------- /libgooglepinyin/src/mystdlib.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | 19 | namespace ime_pinyin { 20 | 21 | // For debug purpose. You can add a fixed version of qsort and bsearch functions 22 | // here so that the output will be totally the same under different platforms. 23 | 24 | void myqsort(void *p, size_t n, size_t es, 25 | int (*cmp)(const void *, const void *)) { 26 | qsort(p,n, es, cmp); 27 | } 28 | 29 | void *mybsearch(const void *k, const void *b, 30 | size_t n, size_t es, 31 | int (*cmp)(const void *, const void *)) { 32 | return bsearch(k, b, n, es, cmp); 33 | } 34 | } // namespace ime_pinyin 35 | -------------------------------------------------------------------------------- /libgooglepinyin/src/ngram.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include "../include/mystdlib.h" 23 | #include "../include/ngram.h" 24 | 25 | namespace ime_pinyin { 26 | 27 | #define ADD_COUNT 0.3 28 | 29 | int comp_double(const void *p1, const void *p2) { 30 | if (*static_cast(p1) < *static_cast(p2)) 31 | return -1; 32 | if (*static_cast(p1) > *static_cast(p2)) 33 | return 1; 34 | return 0; 35 | } 36 | 37 | inline double distance(double freq, double code) { 38 | // return fabs(freq - code); 39 | return freq * fabs(log(freq) - log(code)); 40 | } 41 | 42 | // Find the index of the code value which is nearest to the given freq 43 | int qsearch_nearest(double code_book[], double freq, int start, int end) { 44 | if (start == end) 45 | return start; 46 | 47 | if (start + 1 == end) { 48 | if (distance(freq, code_book[end]) > distance(freq, code_book[start])) 49 | return start; 50 | return end; 51 | } 52 | 53 | int mid = (start + end) / 2; 54 | 55 | if (code_book[mid] > freq) 56 | return qsearch_nearest(code_book, freq, start, mid); 57 | else 58 | return qsearch_nearest(code_book, freq, mid, end); 59 | } 60 | 61 | size_t update_code_idx(double freqs[], size_t num, double code_book[], 62 | CODEBOOK_TYPE *code_idx) { 63 | size_t changed = 0; 64 | for (size_t pos = 0; pos < num; pos++) { 65 | CODEBOOK_TYPE idx; 66 | idx = qsearch_nearest(code_book, freqs[pos], 0, kCodeBookSize - 1); 67 | if (idx != code_idx[pos]) 68 | changed++; 69 | code_idx[pos] = idx; 70 | } 71 | return changed; 72 | } 73 | 74 | double recalculate_kernel(double freqs[], size_t num, double code_book[], 75 | CODEBOOK_TYPE *code_idx) { 76 | double ret = 0; 77 | 78 | size_t *item_num = new size_t[kCodeBookSize]; 79 | assert(item_num); 80 | memset(item_num, 0, sizeof(size_t) * kCodeBookSize); 81 | 82 | double *cb_new = new double[kCodeBookSize]; 83 | assert(cb_new); 84 | memset(cb_new, 0, sizeof(double) * kCodeBookSize); 85 | 86 | for (size_t pos = 0; pos < num; pos++) { 87 | ret += distance(freqs[pos], code_book[code_idx[pos]]); 88 | 89 | cb_new[code_idx[pos]] += freqs[pos]; 90 | item_num[code_idx[pos]] += 1; 91 | } 92 | 93 | for (size_t code = 0; code < kCodeBookSize; code++) { 94 | assert(item_num[code] > 0); 95 | code_book[code] = cb_new[code] / item_num[code]; 96 | } 97 | 98 | delete [] item_num; 99 | delete [] cb_new; 100 | 101 | return ret; 102 | } 103 | 104 | void iterate_codes(double freqs[], size_t num, double code_book[], 105 | CODEBOOK_TYPE *code_idx) { 106 | size_t iter_num = 0; 107 | double delta_last = 0; 108 | do { 109 | size_t changed = update_code_idx(freqs, num, code_book, code_idx); 110 | 111 | double delta = recalculate_kernel(freqs, num, code_book, code_idx); 112 | 113 | if (kPrintDebug0) { 114 | printf("---Unigram codebook iteration: %d : %d, %.9f\n", 115 | iter_num, changed, delta); 116 | } 117 | iter_num++; 118 | 119 | if (iter_num > 1 && 120 | (delta == 0 || fabs(delta_last - delta)/fabs(delta) < 0.000000001)) 121 | break; 122 | delta_last = delta; 123 | } while (true); 124 | } 125 | 126 | 127 | NGram* NGram::instance_ = NULL; 128 | 129 | NGram::NGram() { 130 | initialized_ = false; 131 | idx_num_ = 0; 132 | lma_freq_idx_ = NULL; 133 | sys_score_compensation_ = 0; 134 | 135 | #ifdef ___BUILD_MODEL___ 136 | freq_codes_df_ = NULL; 137 | #endif 138 | freq_codes_ = NULL; 139 | } 140 | 141 | NGram::~NGram() { 142 | if (NULL != lma_freq_idx_) 143 | free(lma_freq_idx_); 144 | 145 | #ifdef ___BUILD_MODEL___ 146 | if (NULL != freq_codes_df_) 147 | free(freq_codes_df_); 148 | #endif 149 | 150 | if (NULL != freq_codes_) 151 | free(freq_codes_); 152 | } 153 | 154 | NGram& NGram::get_instance() { 155 | if (NULL == instance_) 156 | instance_ = new NGram(); 157 | return *instance_; 158 | } 159 | 160 | bool NGram::save_ngram(FILE *fp) { 161 | if (!initialized_ || NULL == fp) 162 | return false; 163 | 164 | if (0 == idx_num_ || NULL == freq_codes_ || NULL == lma_freq_idx_) 165 | return false; 166 | 167 | if (fwrite(&idx_num_, sizeof(uint32), 1, fp) != 1) 168 | return false; 169 | 170 | if (fwrite(freq_codes_, sizeof(LmaScoreType), kCodeBookSize, fp) != 171 | kCodeBookSize) 172 | return false; 173 | 174 | if (fwrite(lma_freq_idx_, sizeof(CODEBOOK_TYPE), idx_num_, fp) != idx_num_) 175 | return false; 176 | 177 | return true; 178 | } 179 | 180 | bool NGram::load_ngram(FILE *fp) { 181 | if (NULL == fp) 182 | return false; 183 | 184 | initialized_ = false; 185 | 186 | if (fread(&idx_num_, sizeof(uint32), 1, fp) != 1 ) 187 | return false; 188 | 189 | if (NULL != lma_freq_idx_) 190 | free(lma_freq_idx_); 191 | 192 | if (NULL != freq_codes_) 193 | free(freq_codes_); 194 | 195 | lma_freq_idx_ = static_cast 196 | (malloc(idx_num_ * sizeof(CODEBOOK_TYPE))); 197 | freq_codes_ = static_cast 198 | (malloc(kCodeBookSize * sizeof(LmaScoreType))); 199 | 200 | if (NULL == lma_freq_idx_ || NULL == freq_codes_) 201 | return false; 202 | 203 | if (fread(freq_codes_, sizeof(LmaScoreType), kCodeBookSize, fp) != 204 | kCodeBookSize) 205 | return false; 206 | 207 | if (fread(lma_freq_idx_, sizeof(CODEBOOK_TYPE), idx_num_, fp) != idx_num_) 208 | return false; 209 | 210 | initialized_ = true; 211 | 212 | total_freq_none_sys_ = 0; 213 | return true; 214 | } 215 | 216 | void NGram::set_total_freq_none_sys(size_t freq_none_sys) { 217 | total_freq_none_sys_ = freq_none_sys; 218 | if (0 == total_freq_none_sys_) { 219 | sys_score_compensation_ = 0; 220 | } else { 221 | double factor = static_cast(kSysDictTotalFreq) / ( 222 | kSysDictTotalFreq + total_freq_none_sys_); 223 | sys_score_compensation_ = static_cast( 224 | log(factor) * kLogValueAmplifier); 225 | } 226 | } 227 | 228 | // The caller makes sure this oject is initialized. 229 | float NGram::get_uni_psb(LemmaIdType lma_id) { 230 | return static_cast(freq_codes_[lma_freq_idx_[lma_id]]) + 231 | sys_score_compensation_; 232 | } 233 | 234 | float NGram::convert_psb_to_score(double psb) { 235 | float score = static_cast( 236 | log(psb) * static_cast(kLogValueAmplifier)); 237 | if (score > static_cast(kMaxScore)) { 238 | score = static_cast(kMaxScore); 239 | } 240 | return score; 241 | } 242 | 243 | #ifdef ___BUILD_MODEL___ 244 | bool NGram::build_unigram(LemmaEntry *lemma_arr, size_t lemma_num, 245 | LemmaIdType next_idx_unused) { 246 | if (NULL == lemma_arr || 0 == lemma_num || next_idx_unused <= 1) 247 | return false; 248 | 249 | double total_freq = 0; 250 | double *freqs = new double[next_idx_unused]; 251 | if (NULL == freqs) 252 | return false; 253 | 254 | freqs[0] = ADD_COUNT; 255 | total_freq += freqs[0]; 256 | LemmaIdType idx_now = 0; 257 | for (size_t pos = 0; pos < lemma_num; pos++) { 258 | if (lemma_arr[pos].idx_by_hz == idx_now) 259 | continue; 260 | idx_now++; 261 | 262 | assert(lemma_arr[pos].idx_by_hz == idx_now); 263 | 264 | freqs[idx_now] = lemma_arr[pos].freq; 265 | if (freqs[idx_now] <= 0) 266 | freqs[idx_now] = 0.3; 267 | 268 | total_freq += freqs[idx_now]; 269 | } 270 | 271 | double max_freq = 0; 272 | idx_num_ = idx_now + 1; 273 | assert(idx_now + 1 == next_idx_unused); 274 | 275 | for (size_t pos = 0; pos < idx_num_; pos++) { 276 | freqs[pos] = freqs[pos] / total_freq; 277 | assert(freqs[pos] > 0); 278 | if (freqs[pos] > max_freq) 279 | max_freq = freqs[pos]; 280 | } 281 | 282 | // calculate the code book 283 | if (NULL == freq_codes_df_) 284 | freq_codes_df_ = new double[kCodeBookSize]; 285 | assert(freq_codes_df_); 286 | memset(freq_codes_df_, 0, sizeof(double) * kCodeBookSize); 287 | 288 | if (NULL == freq_codes_) 289 | freq_codes_ = new LmaScoreType[kCodeBookSize]; 290 | assert(freq_codes_); 291 | memset(freq_codes_, 0, sizeof(LmaScoreType) * kCodeBookSize); 292 | 293 | size_t freq_pos = 0; 294 | for (size_t code_pos = 0; code_pos < kCodeBookSize; code_pos++) { 295 | bool found = true; 296 | 297 | while (found) { 298 | found = false; 299 | double cand = freqs[freq_pos]; 300 | for (size_t i = 0; i < code_pos; i++) 301 | if (freq_codes_df_[i] == cand) { 302 | found = true; 303 | break; 304 | } 305 | if (found) 306 | freq_pos++; 307 | } 308 | 309 | freq_codes_df_[code_pos] = freqs[freq_pos]; 310 | freq_pos++; 311 | } 312 | 313 | myqsort(freq_codes_df_, kCodeBookSize, sizeof(double), comp_double); 314 | 315 | if (NULL == lma_freq_idx_) 316 | lma_freq_idx_ = new CODEBOOK_TYPE[idx_num_]; 317 | assert(lma_freq_idx_); 318 | 319 | iterate_codes(freqs, idx_num_, freq_codes_df_, lma_freq_idx_); 320 | 321 | delete [] freqs; 322 | 323 | if (kPrintDebug0) { 324 | printf("\n------Language Model Unigram Codebook------\n"); 325 | } 326 | 327 | for (size_t code_pos = 0; code_pos < kCodeBookSize; code_pos++) { 328 | double log_score = log(freq_codes_df_[code_pos]); 329 | float final_score = convert_psb_to_score(freq_codes_df_[code_pos]); 330 | if (kPrintDebug0) { 331 | printf("code:%d, probability:%.9f, log score:%.3f, final score: %.3f\n", 332 | code_pos, freq_codes_df_[code_pos], log_score, final_score); 333 | } 334 | freq_codes_[code_pos] = static_cast(final_score); 335 | } 336 | 337 | initialized_ = true; 338 | return true; 339 | } 340 | #endif 341 | 342 | } // namespace ime_pinyin 343 | -------------------------------------------------------------------------------- /libgooglepinyin/src/pinyinime.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | #include "../include/pinyinime.h" 19 | #include "../include/dicttrie.h" 20 | #include "../include/matrixsearch.h" 21 | #include "../include/spellingtrie.h" 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | using namespace ime_pinyin; 28 | 29 | // The maximum number of the prediction items. 30 | static const size_t kMaxPredictNum = 500; 31 | 32 | // Used to search Pinyin string and give the best candidate. 33 | MatrixSearch* matrix_search = NULL; 34 | 35 | char16 predict_buf[kMaxPredictNum][kMaxPredictSize + 1]; 36 | 37 | bool im_open_decoder(const char *fn_sys_dict, const char *fn_usr_dict) { 38 | if (NULL != matrix_search) 39 | delete matrix_search; 40 | 41 | matrix_search = new MatrixSearch(); 42 | if (NULL == matrix_search) { 43 | return false; 44 | } 45 | 46 | return matrix_search->init(fn_sys_dict, fn_usr_dict); 47 | } 48 | 49 | bool im_open_decoder_fd(int sys_fd, long start_offset, long length, 50 | const char *fn_usr_dict) { 51 | if (NULL != matrix_search) 52 | delete matrix_search; 53 | 54 | matrix_search = new MatrixSearch(); 55 | if (NULL == matrix_search) 56 | return false; 57 | 58 | return matrix_search->init_fd(sys_fd, start_offset, length, fn_usr_dict); 59 | } 60 | 61 | void im_close_decoder() { 62 | if (NULL != matrix_search) { 63 | matrix_search->close(); 64 | delete matrix_search; 65 | } 66 | matrix_search = NULL; 67 | } 68 | 69 | void im_set_max_lens(size_t max_sps_len, size_t max_hzs_len) { 70 | if (NULL != matrix_search) { 71 | matrix_search->set_max_lens(max_sps_len, max_hzs_len); 72 | } 73 | } 74 | 75 | void im_flush_cache() { 76 | if (NULL != matrix_search) 77 | matrix_search->flush_cache(); 78 | } 79 | 80 | // To be updated. 81 | size_t im_search(const char* pybuf, size_t pylen) { 82 | if (NULL == matrix_search) 83 | return 0; 84 | 85 | matrix_search->search(pybuf, pylen); 86 | return matrix_search->get_candidate_num(); 87 | } 88 | 89 | size_t im_delsearch(size_t pos, bool is_pos_in_splid, 90 | bool clear_fixed_this_step) { 91 | if (NULL == matrix_search) 92 | return 0; 93 | matrix_search->delsearch(pos, is_pos_in_splid, clear_fixed_this_step); 94 | return matrix_search->get_candidate_num(); 95 | } 96 | 97 | void im_reset_search() { 98 | if (NULL == matrix_search) 99 | return; 100 | 101 | matrix_search->reset_search(); 102 | } 103 | 104 | // To be removed 105 | size_t im_add_letter(char ch) { 106 | return 0; 107 | } 108 | 109 | const char* im_get_sps_str(size_t *decoded_len) { 110 | if (NULL == matrix_search) 111 | return NULL; 112 | 113 | return matrix_search->get_pystr(decoded_len); 114 | } 115 | 116 | char16* im_get_candidate(size_t cand_id, char16* cand_str, 117 | size_t max_len) { 118 | if (NULL == matrix_search) 119 | return NULL; 120 | 121 | return matrix_search->get_candidate(cand_id, cand_str, max_len); 122 | } 123 | 124 | size_t im_get_spl_start_pos(const uint16 *&spl_start) { 125 | if (NULL == matrix_search) 126 | return 0; 127 | 128 | return matrix_search->get_spl_start(spl_start); 129 | } 130 | 131 | size_t im_choose(size_t choice_id) { 132 | if (NULL == matrix_search) 133 | return 0; 134 | 135 | return matrix_search->choose(choice_id); 136 | } 137 | 138 | size_t im_cancel_last_choice() { 139 | if (NULL == matrix_search) 140 | return 0; 141 | 142 | return matrix_search->cancel_last_choice(); 143 | } 144 | 145 | size_t im_get_fixed_len() { 146 | if (NULL == matrix_search) 147 | return 0; 148 | 149 | return matrix_search->get_fixedlen(); 150 | } 151 | 152 | // To be removed 153 | bool im_cancel_input() { 154 | return true; 155 | } 156 | 157 | 158 | size_t im_get_predicts(const char16 *his_buf, 159 | char16 (*&pre_buf)[kMaxPredictSize + 1]) { 160 | if (NULL == his_buf) 161 | return 0; 162 | 163 | size_t fixed_len = utf16_strlen(his_buf); 164 | const char16 *fixed_ptr = his_buf; 165 | if (fixed_len > kMaxPredictSize) { 166 | fixed_ptr += fixed_len - kMaxPredictSize; 167 | fixed_len = kMaxPredictSize; 168 | } 169 | 170 | pre_buf = predict_buf; 171 | return matrix_search->get_predicts(his_buf, pre_buf, kMaxPredictNum); 172 | } 173 | 174 | void im_enable_shm_as_szm(bool enable) { 175 | SpellingTrie &spl_trie = SpellingTrie::get_instance(); 176 | spl_trie.szm_enable_shm(enable); 177 | } 178 | 179 | void im_enable_ym_as_szm(bool enable) { 180 | SpellingTrie &spl_trie = SpellingTrie::get_instance(); 181 | spl_trie.szm_enable_ym(enable); 182 | } 183 | 184 | void im_init_user_dictionary(const char *fn_usr_dict) { 185 | if (!matrix_search) 186 | return; 187 | matrix_search->flush_cache(); 188 | matrix_search->init_user_dictionary(fn_usr_dict); 189 | } 190 | 191 | bool im_is_user_dictionary_enabled(void) { 192 | return NULL != matrix_search ? matrix_search->is_user_dictionary_enabled() : false; 193 | } 194 | 195 | #ifdef __cplusplus 196 | } 197 | #endif 198 | -------------------------------------------------------------------------------- /libgooglepinyin/src/searchutility.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | #include "../include/mystdlib.h" 19 | #include "../include/searchutility.h" 20 | 21 | namespace ime_pinyin { 22 | 23 | bool is_system_lemma(LemmaIdType lma_id) { 24 | return (0 < lma_id && lma_id <= kSysDictIdEnd); 25 | } 26 | 27 | bool is_user_lemma(LemmaIdType lma_id) { 28 | return (kUserDictIdStart <= lma_id && lma_id <= kUserDictIdEnd); 29 | } 30 | 31 | bool is_composing_lemma(LemmaIdType lma_id) { 32 | return (kLemmaIdComposing == lma_id); 33 | } 34 | 35 | int cmp_lpi_with_psb(const void *p1, const void *p2) { 36 | if ((static_cast(p1))->psb > 37 | (static_cast(p2))->psb) 38 | return 1; 39 | if ((static_cast(p1))->psb < 40 | (static_cast(p2))->psb) 41 | return -1; 42 | return 0; 43 | } 44 | 45 | int cmp_lpi_with_unified_psb(const void *p1, const void *p2) { 46 | const LmaPsbItem *item1 = static_cast(p1); 47 | const LmaPsbItem *item2 = static_cast(p2); 48 | 49 | // The real unified psb is psb1 / lma_len1 and psb2 * lma_len2 50 | // But we use psb1 * lma_len2 and psb2 * lma_len1 to get better 51 | // precision. 52 | size_t up1 = item1->psb * (item2->lma_len); 53 | size_t up2 = item2->psb * (item1->lma_len); 54 | if (up1 < up2) { 55 | return -1; 56 | } 57 | if (up1 > up2) { 58 | return 1; 59 | } 60 | return 0; 61 | } 62 | 63 | int cmp_lpi_with_id(const void *p1, const void *p2) { 64 | if ((static_cast(p1))->id < 65 | (static_cast(p2))->id) 66 | return -1; 67 | if ((static_cast(p1))->id > 68 | (static_cast(p2))->id) 69 | return 1; 70 | return 0; 71 | } 72 | 73 | int cmp_lpi_with_hanzi(const void *p1, const void *p2) { 74 | if ((static_cast(p1))->hanzi < 75 | (static_cast(p2))->hanzi) 76 | return -1; 77 | if ((static_cast(p1))->hanzi > 78 | (static_cast(p2))->hanzi) 79 | return 1; 80 | 81 | return 0; 82 | } 83 | 84 | int cmp_lpsi_with_str(const void *p1, const void *p2) { 85 | return utf16_strcmp((static_cast(p1))->str, 86 | (static_cast(p2))->str); 87 | } 88 | 89 | 90 | int cmp_hanzis_1(const void *p1, const void *p2) { 91 | if (*static_cast(p1) < 92 | *static_cast(p2)) 93 | return -1; 94 | 95 | if (*static_cast(p1) > 96 | *static_cast(p2)) 97 | return 1; 98 | return 0; 99 | } 100 | 101 | int cmp_hanzis_2(const void *p1, const void *p2) { 102 | return utf16_strncmp(static_cast(p1), 103 | static_cast(p2), 2); 104 | } 105 | 106 | int cmp_hanzis_3(const void *p1, const void *p2) { 107 | return utf16_strncmp(static_cast(p1), 108 | static_cast(p2), 3); 109 | } 110 | 111 | int cmp_hanzis_4(const void *p1, const void *p2) { 112 | return utf16_strncmp(static_cast(p1), 113 | static_cast(p2), 4); 114 | } 115 | 116 | int cmp_hanzis_5(const void *p1, const void *p2) { 117 | return utf16_strncmp(static_cast(p1), 118 | static_cast(p2), 5); 119 | } 120 | 121 | int cmp_hanzis_6(const void *p1, const void *p2) { 122 | return utf16_strncmp(static_cast(p1), 123 | static_cast(p2), 6); 124 | } 125 | 126 | int cmp_hanzis_7(const void *p1, const void *p2) { 127 | return utf16_strncmp(static_cast(p1), 128 | static_cast(p2), 7); 129 | } 130 | 131 | int cmp_hanzis_8(const void *p1, const void *p2) { 132 | return utf16_strncmp(static_cast(p1), 133 | static_cast(p2), 8); 134 | } 135 | 136 | int cmp_npre_by_score(const void *p1, const void *p2) { 137 | if ((static_cast(p1))->psb > 138 | (static_cast(p2))->psb) 139 | return 1; 140 | 141 | if ((static_cast(p1))->psb < 142 | (static_cast(p2))->psb) 143 | return -1; 144 | 145 | return 0; 146 | } 147 | 148 | int cmp_npre_by_hislen_score(const void *p1, const void *p2) { 149 | if ((static_cast(p1))->his_len < 150 | (static_cast(p2))->his_len) 151 | return 1; 152 | 153 | if ((static_cast(p1))->his_len > 154 | (static_cast(p2))->his_len) 155 | return -1; 156 | 157 | if ((static_cast(p1))->psb > 158 | (static_cast(p2))->psb) 159 | return 1; 160 | 161 | if ((static_cast(p1))->psb < 162 | (static_cast(p2))->psb) 163 | return -1; 164 | 165 | return 0; 166 | } 167 | 168 | int cmp_npre_by_hanzi_score(const void *p1, const void *p2) { 169 | int ret_v = (utf16_strncmp((static_cast(p1))->pre_hzs, 170 | (static_cast(p2))->pre_hzs, kMaxPredictSize)); 171 | if (0 != ret_v) 172 | return ret_v; 173 | 174 | if ((static_cast(p1))->psb > 175 | (static_cast(p2))->psb) 176 | return 1; 177 | 178 | if ((static_cast(p1))->psb < 179 | (static_cast(p2))->psb) 180 | return -1; 181 | 182 | return 0; 183 | } 184 | 185 | size_t remove_duplicate_npre(NPredictItem *npre_items, size_t npre_num) { 186 | if (NULL == npre_items || 0 == npre_num) 187 | return 0; 188 | 189 | myqsort(npre_items, npre_num, sizeof(NPredictItem), cmp_npre_by_hanzi_score); 190 | 191 | size_t remain_num = 1; // The first one is reserved. 192 | for (size_t pos = 1; pos < npre_num; pos++) { 193 | if (utf16_strncmp(npre_items[pos].pre_hzs, 194 | npre_items[remain_num - 1].pre_hzs, 195 | kMaxPredictSize) != 0) { 196 | if (remain_num != pos) { 197 | npre_items[remain_num] = npre_items[pos]; 198 | } 199 | remain_num++; 200 | } 201 | } 202 | return remain_num; 203 | } 204 | 205 | size_t align_to_size_t(size_t size) { 206 | size_t s = sizeof(size_t); 207 | return (size + s -1) / s * s; 208 | } 209 | 210 | } // namespace ime_pinyin 211 | -------------------------------------------------------------------------------- /libgooglepinyin/src/spellingtable.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include "../include/spellingtable.h" 23 | 24 | namespace ime_pinyin { 25 | 26 | #ifdef ___BUILD_MODEL___ 27 | 28 | const char SpellingTable:: 29 | kNotSupportList[kNotSupportNum][kMaxSpellingSize + 1] = {"HM", "HNG", "NG"}; 30 | 31 | // "" is the biggest, so that all empty strings will be moved to the end 32 | // _eb mean empty is biggest 33 | int compare_raw_spl_eb(const void* p1, const void* p2) { 34 | if ('\0' == (static_cast(p1))->str[0]) 35 | return 1; 36 | 37 | if ('\0' == (static_cast(p2))->str[0]) 38 | return -1; 39 | 40 | return strcmp((static_cast(p1))->str, 41 | (static_cast(p2))->str); 42 | } 43 | 44 | size_t get_odd_next(size_t value) { 45 | size_t v_next = value; 46 | while (true) { 47 | size_t v_next_sqrt = (size_t)sqrt(v_next); 48 | 49 | bool is_odd = true; 50 | for (size_t v_dv = 2; v_dv < v_next_sqrt + 1; v_dv++) { 51 | if (v_next % v_dv == 0) { 52 | is_odd = false; 53 | break; 54 | } 55 | } 56 | 57 | if (is_odd) 58 | return v_next; 59 | 60 | v_next++; 61 | } 62 | 63 | // never reach here 64 | return 0; 65 | } 66 | 67 | SpellingTable::SpellingTable() { 68 | need_score_ = false; 69 | raw_spellings_ = NULL; 70 | spelling_buf_ = NULL; 71 | spelling_num_ = 0; 72 | total_freq_ = 0; 73 | frozen_ = true; 74 | } 75 | 76 | SpellingTable::~SpellingTable() { 77 | free_resource(); 78 | } 79 | 80 | size_t SpellingTable::get_hash_pos(const char* spelling_str) { 81 | size_t hash_pos = 0; 82 | for (size_t pos = 0; pos < spelling_size_; pos++) { 83 | if ('\0' == spelling_str[pos]) 84 | break; 85 | hash_pos += (size_t)spelling_str[pos]; 86 | } 87 | 88 | hash_pos = hash_pos % spelling_max_num_; 89 | return hash_pos; 90 | } 91 | 92 | size_t SpellingTable::hash_pos_next(size_t hash_pos) { 93 | hash_pos += 123; 94 | hash_pos = hash_pos % spelling_max_num_; 95 | return hash_pos; 96 | } 97 | 98 | void SpellingTable::free_resource() { 99 | if (NULL != raw_spellings_) 100 | delete [] raw_spellings_; 101 | raw_spellings_ = NULL; 102 | 103 | if (NULL != spelling_buf_) 104 | delete [] spelling_buf_; 105 | spelling_buf_ = NULL; 106 | } 107 | 108 | bool SpellingTable::init_table(size_t pure_spl_size, size_t spl_max_num, 109 | bool need_score) { 110 | if (pure_spl_size == 0 || spl_max_num ==0) 111 | return false; 112 | 113 | need_score_ = need_score; 114 | 115 | free_resource(); 116 | 117 | spelling_size_ = pure_spl_size + 1; 118 | if (need_score) 119 | spelling_size_ += 1; 120 | spelling_max_num_ = get_odd_next(spl_max_num); 121 | spelling_num_ = 0; 122 | 123 | raw_spellings_ = new RawSpelling[spelling_max_num_]; 124 | spelling_buf_ = new char[spelling_max_num_ * (spelling_size_)]; 125 | if (NULL == raw_spellings_ || NULL == spelling_buf_) { 126 | free_resource(); 127 | return false; 128 | } 129 | 130 | memset(raw_spellings_, 0, spelling_max_num_ * sizeof(RawSpelling)); 131 | memset(spelling_buf_, 0, spelling_max_num_ * (spelling_size_)); 132 | frozen_ = false; 133 | total_freq_ = 0; 134 | return true; 135 | } 136 | 137 | bool SpellingTable::put_spelling(const char* spelling_str, double freq) { 138 | if (frozen_ || NULL == spelling_str) 139 | return false; 140 | 141 | for (size_t pos = 0; pos < kNotSupportNum; pos++) { 142 | if (strcmp(spelling_str, kNotSupportList[pos]) == 0) { 143 | return false; 144 | } 145 | } 146 | 147 | total_freq_ += freq; 148 | 149 | size_t hash_pos = get_hash_pos(spelling_str); 150 | 151 | raw_spellings_[hash_pos].str[spelling_size_ - 1] = '\0'; 152 | 153 | if (strncmp(raw_spellings_[hash_pos].str, spelling_str, 154 | spelling_size_ - 1) == 0) { 155 | raw_spellings_[hash_pos].freq += freq; 156 | return true; 157 | } 158 | 159 | size_t hash_pos_ori = hash_pos; 160 | 161 | while (true) { 162 | if (strncmp(raw_spellings_[hash_pos].str, 163 | spelling_str, spelling_size_ - 1) == 0) { 164 | raw_spellings_[hash_pos].freq += freq; 165 | return true; 166 | } 167 | 168 | if ('\0' == raw_spellings_[hash_pos].str[0]) { 169 | raw_spellings_[hash_pos].freq += freq; 170 | strncpy(raw_spellings_[hash_pos].str, spelling_str, spelling_size_ - 1); 171 | raw_spellings_[hash_pos].str[spelling_size_ - 1] = '\0'; 172 | spelling_num_++; 173 | return true; 174 | } 175 | 176 | hash_pos = hash_pos_next(hash_pos); 177 | if (hash_pos_ori == hash_pos) 178 | return false; 179 | } 180 | 181 | // never reach here 182 | return false; 183 | } 184 | 185 | bool SpellingTable::contain(const char* spelling_str) { 186 | if (NULL == spelling_str || NULL == spelling_buf_ || frozen_) 187 | return false; 188 | 189 | size_t hash_pos = get_hash_pos(spelling_str); 190 | 191 | if ('\0' == raw_spellings_[hash_pos].str[0]) 192 | return false; 193 | 194 | if (strncmp(raw_spellings_[hash_pos].str, spelling_str, spelling_size_ - 1) 195 | == 0) 196 | return true; 197 | 198 | size_t hash_pos_ori = hash_pos; 199 | 200 | while (true) { 201 | hash_pos = hash_pos_next(hash_pos); 202 | if (hash_pos_ori == hash_pos) 203 | return false; 204 | 205 | if ('\0' == raw_spellings_[hash_pos].str[0]) 206 | return false; 207 | 208 | if (strncmp(raw_spellings_[hash_pos].str, spelling_str, spelling_size_ - 1) 209 | == 0) 210 | return true; 211 | } 212 | 213 | // never reach here 214 | return false; 215 | } 216 | 217 | const char* SpellingTable::arrange(size_t *item_size, size_t *spl_num) { 218 | if (NULL == raw_spellings_ || NULL == spelling_buf_ || 219 | NULL == item_size || NULL == spl_num) 220 | return NULL; 221 | 222 | qsort(raw_spellings_, spelling_max_num_, sizeof(RawSpelling), 223 | compare_raw_spl_eb); 224 | 225 | // After sorting, only the first spelling_num_ items are valid. 226 | // Copy them to the destination buffer. 227 | for (size_t pos = 0; pos < spelling_num_; pos++) { 228 | strncpy(spelling_buf_ + pos * spelling_size_, raw_spellings_[pos].str, 229 | spelling_size_); 230 | } 231 | 232 | if (need_score_) { 233 | if (kPrintDebug0) 234 | printf("------------Spelling Possiblities--------------\n"); 235 | 236 | double max_score = 0; 237 | double min_score = 0; 238 | 239 | // After sorting, only the first spelling_num_ items are valid. 240 | for (size_t pos = 0; pos < spelling_num_; pos++) { 241 | raw_spellings_[pos].freq /= total_freq_; 242 | if (need_score_) { 243 | if (0 == pos) { 244 | max_score = raw_spellings_[0].freq; 245 | min_score = max_score; 246 | } else { 247 | if (raw_spellings_[pos].freq > max_score) 248 | max_score = raw_spellings_[pos].freq; 249 | if (raw_spellings_[pos].freq < min_score) 250 | min_score = raw_spellings_[pos].freq; 251 | } 252 | } 253 | } 254 | 255 | if (kPrintDebug0) 256 | printf("-----max psb: %f, min psb: %f\n", max_score, min_score); 257 | 258 | max_score = log(max_score); 259 | min_score = log(min_score); 260 | 261 | if (kPrintDebug0) 262 | printf("-----max log value: %f, min log value: %f\n", 263 | max_score, min_score); 264 | 265 | // The absolute value of min_score is bigger than that of max_score because 266 | // both of them are negative after log function. 267 | score_amplifier_ = 1.0 * 255 / min_score; 268 | 269 | double average_score = 0; 270 | for (size_t pos = 0; pos < spelling_num_; pos++) { 271 | double score = log(raw_spellings_[pos].freq) * score_amplifier_; 272 | assert(score >= 0); 273 | 274 | average_score += score; 275 | 276 | // Because of calculation precision issue, score might be a little bigger 277 | // than 255 after being amplified. 278 | if (score > 255) 279 | score = 255; 280 | char *this_spl_buf = spelling_buf_ + pos * spelling_size_; 281 | this_spl_buf[spelling_size_ - 1] = 282 | static_cast((unsigned char)score); 283 | 284 | if (kPrintDebug0) { 285 | printf("---pos:%d, %s, psb:%d\n", pos, this_spl_buf, 286 | (unsigned char)this_spl_buf[spelling_size_ -1]); 287 | } 288 | } 289 | average_score /= spelling_num_; 290 | assert(average_score <= 255); 291 | average_score_ = static_cast(average_score); 292 | 293 | if (kPrintDebug0) 294 | printf("\n----Score Amplifier: %f, Average Score: %d\n", score_amplifier_, 295 | average_score_); 296 | } 297 | 298 | *item_size = spelling_size_; 299 | *spl_num = spelling_num_; 300 | frozen_ = true; 301 | return spelling_buf_; 302 | } 303 | 304 | float SpellingTable::get_score_amplifier() { 305 | return static_cast(score_amplifier_); 306 | } 307 | 308 | unsigned char SpellingTable::get_average_score() { 309 | return average_score_; 310 | } 311 | 312 | #endif // ___BUILD_MODEL___ 313 | } // namespace ime_pinyin 314 | -------------------------------------------------------------------------------- /libgooglepinyin/src/splparser.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | #include "../include/splparser.h" 19 | 20 | namespace ime_pinyin { 21 | 22 | SpellingParser::SpellingParser() { 23 | spl_trie_ = SpellingTrie::get_cpinstance(); 24 | } 25 | 26 | bool SpellingParser::is_valid_to_parse(char ch) { 27 | return SpellingTrie::is_valid_spl_char(ch); 28 | } 29 | 30 | uint16 SpellingParser::splstr_to_idxs(const char *splstr, uint16 str_len, 31 | uint16 spl_idx[], uint16 start_pos[], 32 | uint16 max_size, bool &last_is_pre) { 33 | if (NULL == splstr || 0 == max_size || 0 == str_len) 34 | return 0; 35 | 36 | if (!SpellingTrie::is_valid_spl_char(splstr[0])) 37 | return 0; 38 | 39 | last_is_pre = false; 40 | 41 | const SpellingNode *node_this = spl_trie_->root_; 42 | 43 | uint16 str_pos = 0; 44 | uint16 idx_num = 0; 45 | if (NULL != start_pos) 46 | start_pos[0] = 0; 47 | bool last_is_splitter = false; 48 | 49 | while (str_pos < str_len) { 50 | char char_this = splstr[str_pos]; 51 | // all characters outside of [a, z] are considered as splitters 52 | if (!SpellingTrie::is_valid_spl_char(char_this)) { 53 | // test if the current node is endable 54 | uint16 id_this = node_this->spelling_idx; 55 | if (spl_trie_->if_valid_id_update(&id_this)) { 56 | spl_idx[idx_num] = id_this; 57 | 58 | idx_num++; 59 | str_pos++; 60 | if (NULL != start_pos) 61 | start_pos[idx_num] = str_pos; 62 | if (idx_num >= max_size) 63 | return idx_num; 64 | 65 | node_this = spl_trie_->root_; 66 | last_is_splitter = true; 67 | continue; 68 | } else { 69 | if (last_is_splitter) { 70 | str_pos++; 71 | if (NULL != start_pos) 72 | start_pos[idx_num] = str_pos; 73 | continue; 74 | } else { 75 | return idx_num; 76 | } 77 | } 78 | } 79 | 80 | last_is_splitter = false; 81 | 82 | SpellingNode *found_son = NULL; 83 | 84 | if (0 == str_pos) { 85 | if (char_this >= 'a') 86 | found_son = spl_trie_->level1_sons_[char_this - 'a']; 87 | else 88 | found_son = spl_trie_->level1_sons_[char_this - 'A']; 89 | } else { 90 | SpellingNode *first_son = node_this->first_son; 91 | // Because for Zh/Ch/Sh nodes, they are the last in the buffer and 92 | // frequently used, so we scan from the end. 93 | for (int i = 0; i < node_this->num_of_son; i++) { 94 | SpellingNode *this_son = first_son + i; 95 | if (SpellingTrie::is_same_spl_char( 96 | this_son->char_this_node, char_this)) { 97 | found_son = this_son; 98 | break; 99 | } 100 | } 101 | } 102 | 103 | // found, just move the current node pointer to the the son 104 | if (NULL != found_son) { 105 | node_this = found_son; 106 | } else { 107 | // not found, test if it is endable 108 | uint16 id_this = node_this->spelling_idx; 109 | if (spl_trie_->if_valid_id_update(&id_this)) { 110 | // endable, remember the index 111 | spl_idx[idx_num] = id_this; 112 | 113 | idx_num++; 114 | if (NULL != start_pos) 115 | start_pos[idx_num] = str_pos; 116 | if (idx_num >= max_size) 117 | return idx_num; 118 | node_this = spl_trie_->root_; 119 | continue; 120 | } else { 121 | return idx_num; 122 | } 123 | } 124 | 125 | str_pos++; 126 | } 127 | 128 | uint16 id_this = node_this->spelling_idx; 129 | if (spl_trie_->if_valid_id_update(&id_this)) { 130 | // endable, remember the index 131 | spl_idx[idx_num] = id_this; 132 | 133 | idx_num++; 134 | if (NULL != start_pos) 135 | start_pos[idx_num] = str_pos; 136 | } 137 | 138 | last_is_pre = !last_is_splitter; 139 | 140 | return idx_num; 141 | } 142 | 143 | uint16 SpellingParser::splstr_to_idxs_f(const char *splstr, uint16 str_len, 144 | uint16 spl_idx[], uint16 start_pos[], 145 | uint16 max_size, bool &last_is_pre) { 146 | uint16 idx_num = splstr_to_idxs(splstr, str_len, spl_idx, start_pos, 147 | max_size, last_is_pre); 148 | for (uint16 pos = 0; pos < idx_num; pos++) { 149 | if (spl_trie_->is_half_id_yunmu(spl_idx[pos])) { 150 | spl_trie_->half_to_full(spl_idx[pos], spl_idx + pos); 151 | if (pos == idx_num - 1) { 152 | last_is_pre = false; 153 | } 154 | } 155 | } 156 | return idx_num; 157 | } 158 | 159 | uint16 SpellingParser::splstr16_to_idxs(const char16 *splstr, uint16 str_len, 160 | uint16 spl_idx[], uint16 start_pos[], 161 | uint16 max_size, bool &last_is_pre) { 162 | if (NULL == splstr || 0 == max_size || 0 == str_len) 163 | return 0; 164 | 165 | if (!SpellingTrie::is_valid_spl_char(splstr[0])) 166 | return 0; 167 | 168 | last_is_pre = false; 169 | 170 | const SpellingNode *node_this = spl_trie_->root_; 171 | 172 | uint16 str_pos = 0; 173 | uint16 idx_num = 0; 174 | if (NULL != start_pos) 175 | start_pos[0] = 0; 176 | bool last_is_splitter = false; 177 | 178 | while (str_pos < str_len) { 179 | char16 char_this = splstr[str_pos]; 180 | // all characters outside of [a, z] are considered as splitters 181 | if (!SpellingTrie::is_valid_spl_char(char_this)) { 182 | // test if the current node is endable 183 | uint16 id_this = node_this->spelling_idx; 184 | if (spl_trie_->if_valid_id_update(&id_this)) { 185 | spl_idx[idx_num] = id_this; 186 | 187 | idx_num++; 188 | str_pos++; 189 | if (NULL != start_pos) 190 | start_pos[idx_num] = str_pos; 191 | if (idx_num >= max_size) 192 | return idx_num; 193 | 194 | node_this = spl_trie_->root_; 195 | last_is_splitter = true; 196 | continue; 197 | } else { 198 | if (last_is_splitter) { 199 | str_pos++; 200 | if (NULL != start_pos) 201 | start_pos[idx_num] = str_pos; 202 | continue; 203 | } else { 204 | return idx_num; 205 | } 206 | } 207 | } 208 | 209 | last_is_splitter = false; 210 | 211 | SpellingNode *found_son = NULL; 212 | 213 | if (0 == str_pos) { 214 | if (char_this >= 'a') 215 | found_son = spl_trie_->level1_sons_[char_this - 'a']; 216 | else 217 | found_son = spl_trie_->level1_sons_[char_this - 'A']; 218 | } else { 219 | SpellingNode *first_son = node_this->first_son; 220 | // Because for Zh/Ch/Sh nodes, they are the last in the buffer and 221 | // frequently used, so we scan from the end. 222 | for (int i = 0; i < node_this->num_of_son; i++) { 223 | SpellingNode *this_son = first_son + i; 224 | if (SpellingTrie::is_same_spl_char( 225 | this_son->char_this_node, char_this)) { 226 | found_son = this_son; 227 | break; 228 | } 229 | } 230 | } 231 | 232 | // found, just move the current node pointer to the the son 233 | if (NULL != found_son) { 234 | node_this = found_son; 235 | } else { 236 | // not found, test if it is endable 237 | uint16 id_this = node_this->spelling_idx; 238 | if (spl_trie_->if_valid_id_update(&id_this)) { 239 | // endable, remember the index 240 | spl_idx[idx_num] = id_this; 241 | 242 | idx_num++; 243 | if (NULL != start_pos) 244 | start_pos[idx_num] = str_pos; 245 | if (idx_num >= max_size) 246 | return idx_num; 247 | node_this = spl_trie_->root_; 248 | continue; 249 | } else { 250 | return idx_num; 251 | } 252 | } 253 | 254 | str_pos++; 255 | } 256 | 257 | uint16 id_this = node_this->spelling_idx; 258 | if (spl_trie_->if_valid_id_update(&id_this)) { 259 | // endable, remember the index 260 | spl_idx[idx_num] = id_this; 261 | 262 | idx_num++; 263 | if (NULL != start_pos) 264 | start_pos[idx_num] = str_pos; 265 | } 266 | 267 | last_is_pre = !last_is_splitter; 268 | 269 | return idx_num; 270 | } 271 | 272 | uint16 SpellingParser::splstr16_to_idxs_f(const char16 *splstr, uint16 str_len, 273 | uint16 spl_idx[], uint16 start_pos[], 274 | uint16 max_size, bool &last_is_pre) { 275 | uint16 idx_num = splstr16_to_idxs(splstr, str_len, spl_idx, start_pos, 276 | max_size, last_is_pre); 277 | for (uint16 pos = 0; pos < idx_num; pos++) { 278 | if (spl_trie_->is_half_id_yunmu(spl_idx[pos])) { 279 | spl_trie_->half_to_full(spl_idx[pos], spl_idx + pos); 280 | if (pos == idx_num - 1) { 281 | last_is_pre = false; 282 | } 283 | } 284 | } 285 | return idx_num; 286 | } 287 | 288 | uint16 SpellingParser::get_splid_by_str(const char *splstr, uint16 str_len, 289 | bool *is_pre) { 290 | if (NULL == is_pre) 291 | return 0; 292 | 293 | uint16 spl_idx[2]; 294 | uint16 start_pos[3]; 295 | 296 | if (splstr_to_idxs(splstr, str_len, spl_idx, start_pos, 2, *is_pre) != 1) 297 | return 0; 298 | 299 | if (start_pos[1] != str_len) 300 | return 0; 301 | return spl_idx[0]; 302 | } 303 | 304 | uint16 SpellingParser::get_splid_by_str_f(const char *splstr, uint16 str_len, 305 | bool *is_pre) { 306 | if (NULL == is_pre) 307 | return 0; 308 | 309 | uint16 spl_idx[2]; 310 | uint16 start_pos[3]; 311 | 312 | if (splstr_to_idxs(splstr, str_len, spl_idx, start_pos, 2, *is_pre) != 1) 313 | return 0; 314 | 315 | if (start_pos[1] != str_len) 316 | return 0; 317 | if (spl_trie_->is_half_id_yunmu(spl_idx[0])) { 318 | spl_trie_->half_to_full(spl_idx[0], spl_idx); 319 | *is_pre = false; 320 | } 321 | 322 | return spl_idx[0]; 323 | } 324 | 325 | uint16 SpellingParser::get_splids_parallel(const char *splstr, uint16 str_len, 326 | uint16 splidx[], uint16 max_size, 327 | uint16 &full_id_num, bool &is_pre) { 328 | if (max_size <= 0 || !is_valid_to_parse(splstr[0])) 329 | return 0; 330 | 331 | splidx[0] = get_splid_by_str(splstr, str_len, &is_pre); 332 | full_id_num = 0; 333 | if (0 != splidx[0]) { 334 | if (splidx[0] >= kFullSplIdStart) 335 | full_id_num = 1; 336 | return 1; 337 | } 338 | return 0; 339 | } 340 | 341 | } // namespace ime_pinyin 342 | -------------------------------------------------------------------------------- /libgooglepinyin/src/sync.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "../include/sync.h" 18 | #include 19 | #include 20 | 21 | #ifdef ___SYNC_ENABLED___ 22 | 23 | namespace ime_pinyin { 24 | 25 | Sync::Sync() 26 | : userdict_(NULL), 27 | dictfile_(NULL), 28 | last_count_(0) { 29 | }; 30 | 31 | Sync::~Sync() { 32 | } 33 | 34 | 35 | bool Sync::begin(const char * filename) { 36 | if (userdict_) { 37 | finish(); 38 | } 39 | 40 | if (!filename) { 41 | return false; 42 | } 43 | 44 | dictfile_ = strdup(filename); 45 | if (!dictfile_) { 46 | return false; 47 | } 48 | 49 | userdict_ = new UserDict(); 50 | if (!userdict_) { 51 | free(dictfile_); 52 | dictfile_ = NULL; 53 | return false; 54 | } 55 | 56 | if (userdict_->load_dict((const char*)dictfile_, kUserDictIdStart, 57 | kUserDictIdEnd) == false) { 58 | delete userdict_; 59 | userdict_ = NULL; 60 | free(dictfile_); 61 | dictfile_ = NULL; 62 | return false; 63 | } 64 | 65 | userdict_->set_limit(kUserDictMaxLemmaCount, kUserDictMaxLemmaSize, kUserDictRatio); 66 | 67 | return true; 68 | } 69 | 70 | int Sync::put_lemmas(char16 * lemmas, int len) { 71 | return userdict_->put_lemmas_no_sync_from_utf16le_string(lemmas, len); 72 | } 73 | 74 | int Sync::get_lemmas(char16 * str, int size) { 75 | return userdict_->get_sync_lemmas_in_utf16le_string_from_beginning(str, size, &last_count_); 76 | } 77 | 78 | int Sync::get_last_got_count() { 79 | return last_count_; 80 | } 81 | 82 | int Sync::get_total_count() { 83 | return userdict_->get_sync_count(); 84 | } 85 | 86 | void Sync::clear_last_got() { 87 | if (last_count_ < 0) { 88 | return; 89 | } 90 | userdict_->clear_sync_lemmas(0, last_count_); 91 | last_count_ = 0; 92 | } 93 | 94 | void Sync::finish() { 95 | if (userdict_) { 96 | userdict_->close_dict(); 97 | delete userdict_; 98 | userdict_ = NULL; 99 | free(dictfile_); 100 | dictfile_ = NULL; 101 | last_count_ = 0; 102 | } 103 | } 104 | 105 | int Sync::get_capacity() { 106 | UserDict::UserDictStat stat; 107 | userdict_->state(&stat); 108 | return stat.limit_lemma_count - stat.lemma_count; 109 | } 110 | 111 | } 112 | #endif 113 | -------------------------------------------------------------------------------- /libgooglepinyin/src/utf16char.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | #include "../include/utf16char.h" 19 | 20 | namespace ime_pinyin { 21 | 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif 25 | 26 | char16* utf16_strtok(char16 *utf16_str, size_t *token_size, 27 | char16 **utf16_str_next) { 28 | if (NULL == utf16_str || NULL == token_size || NULL == utf16_str_next) { 29 | return NULL; 30 | } 31 | 32 | // Skip the splitters 33 | size_t pos = 0; 34 | while ((char16)' ' == utf16_str[pos] || (char16)'\n' == utf16_str[pos] 35 | || (char16)'\t' == utf16_str[pos]) 36 | pos++; 37 | 38 | utf16_str += pos; 39 | pos = 0; 40 | 41 | while ((char16)'\0' != utf16_str[pos] && (char16)' ' != utf16_str[pos] 42 | && (char16)'\n' != utf16_str[pos] 43 | && (char16)'\t' != utf16_str[pos]) { 44 | pos++; 45 | } 46 | 47 | char16 *ret_val = utf16_str; 48 | if ((char16)'\0' == utf16_str[pos]) { 49 | *utf16_str_next = NULL; 50 | if (0 == pos) 51 | return NULL; 52 | } else { 53 | *utf16_str_next = utf16_str + pos + 1; 54 | } 55 | 56 | utf16_str[pos] = (char16)'\0'; 57 | *token_size = pos; 58 | 59 | return ret_val; 60 | } 61 | 62 | int utf16_atoi(const char16 *utf16_str) { 63 | if (NULL == utf16_str) 64 | return 0; 65 | 66 | int value = 0; 67 | int sign = 1; 68 | size_t pos = 0; 69 | 70 | if ((char16)'-' == utf16_str[pos]) { 71 | sign = -1; 72 | pos++; 73 | } 74 | 75 | while ((char16)'0' <= utf16_str[pos] && 76 | (char16)'9' >= utf16_str[pos]) { 77 | value = value * 10 + static_cast(utf16_str[pos] - (char16)'0'); 78 | pos++; 79 | } 80 | 81 | return value*sign; 82 | } 83 | 84 | float utf16_atof(const char16 *utf16_str) { 85 | // A temporary implemetation. 86 | char char8[256]; 87 | if (utf16_strlen(utf16_str) >= 256) return 0; 88 | 89 | utf16_strcpy_tochar(char8, utf16_str); 90 | return atof(char8); 91 | } 92 | 93 | size_t utf16_strlen(const char16 *utf16_str) { 94 | if (NULL == utf16_str) 95 | return 0; 96 | 97 | size_t size = 0; 98 | while ((char16)'\0' != utf16_str[size]) 99 | size++; 100 | return size; 101 | } 102 | 103 | int utf16_strcmp(const char16* str1, const char16* str2) { 104 | size_t pos = 0; 105 | while (str1[pos] == str2[pos] && (char16)'\0' != str1[pos]) 106 | pos++; 107 | 108 | return static_cast(str1[pos]) - static_cast(str2[pos]); 109 | } 110 | 111 | int utf16_strncmp(const char16 *str1, const char16 *str2, size_t size) { 112 | size_t pos = 0; 113 | while (pos < size && str1[pos] == str2[pos] && (char16)'\0' != str1[pos]) 114 | pos++; 115 | 116 | if (pos == size) 117 | return 0; 118 | 119 | return static_cast(str1[pos]) - static_cast(str2[pos]); 120 | } 121 | 122 | // we do not consider overlapping 123 | char16* utf16_strcpy(char16 *dst, const char16 *src) { 124 | if (NULL == src || NULL == dst) 125 | return NULL; 126 | 127 | char16* cp = dst; 128 | 129 | while ((char16)'\0' != *src) { 130 | *cp = *src; 131 | cp++; 132 | src++; 133 | } 134 | 135 | *cp = *src; 136 | 137 | return dst; 138 | } 139 | 140 | char16* utf16_strncpy(char16 *dst, const char16 *src, size_t size) { 141 | if (NULL == src || NULL == dst || 0 == size) 142 | return NULL; 143 | 144 | if (src == dst) 145 | return dst; 146 | 147 | char16* cp = dst; 148 | 149 | if (dst < src || (dst > src && dst >= src + size)) { 150 | while (size-- && (*cp++ = *src++)) 151 | ; 152 | } else { 153 | cp += size - 1; 154 | src += size - 1; 155 | while (size-- && (*cp-- == *src--)) 156 | ; 157 | } 158 | return dst; 159 | } 160 | 161 | // We do not handle complicated cases like overlapping, because in this 162 | // codebase, it is not necessary. 163 | char* utf16_strcpy_tochar(char *dst, const char16 *src) { 164 | if (NULL == src || NULL == dst) 165 | return NULL; 166 | 167 | char* cp = dst; 168 | 169 | while ((char16)'\0' != *src) { 170 | *cp = static_cast(*src); 171 | cp++; 172 | src++; 173 | } 174 | *cp = *src; 175 | 176 | return dst; 177 | } 178 | 179 | #ifdef __cplusplus 180 | } 181 | #endif 182 | } // namespace ime_pinyin 183 | -------------------------------------------------------------------------------- /libgooglepinyin/src/utf16reader.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2009 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "../include/utf16reader.h" 18 | 19 | namespace ime_pinyin { 20 | 21 | #define MIN_BUF_LEN 128 22 | #define MAX_BUF_LEN 65535 23 | 24 | Utf16Reader::Utf16Reader() { 25 | fp_ = NULL; 26 | buffer_ = NULL; 27 | buffer_total_len_ = 0; 28 | buffer_next_pos_ = 0; 29 | buffer_valid_len_ = 0; 30 | } 31 | 32 | Utf16Reader::~Utf16Reader() { 33 | if (NULL != fp_) 34 | fclose(fp_); 35 | 36 | if (NULL != buffer_) 37 | delete [] buffer_; 38 | } 39 | 40 | 41 | bool Utf16Reader::open(const char* filename, size_t buffer_len) { 42 | if (filename == NULL) 43 | return false; 44 | 45 | if (buffer_len < MIN_BUF_LEN) 46 | buffer_len = MIN_BUF_LEN; 47 | else if (buffer_len > MAX_BUF_LEN) 48 | buffer_len = MAX_BUF_LEN; 49 | 50 | buffer_total_len_ = buffer_len; 51 | 52 | if (NULL != buffer_) 53 | delete [] buffer_; 54 | buffer_ = new char16[buffer_total_len_]; 55 | if (NULL == buffer_) 56 | return false; 57 | 58 | if ((fp_ = fopen(filename, "rb")) == NULL) 59 | return false; 60 | 61 | // the UTF16 file header, skip 62 | char16 header; 63 | if (fread(&header, sizeof(header), 1, fp_) != 1 || header != 0xfeff) { 64 | fclose(fp_); 65 | fp_ = NULL; 66 | return false; 67 | } 68 | 69 | return true; 70 | } 71 | 72 | char16* Utf16Reader::readline(char16* read_buf, size_t max_len) { 73 | if (NULL == fp_ || NULL == read_buf || 0 == max_len) 74 | return NULL; 75 | 76 | size_t ret_len = 0; 77 | 78 | do { 79 | if (buffer_valid_len_ == 0) { 80 | buffer_next_pos_ = 0; 81 | buffer_valid_len_ = fread(buffer_, sizeof(char16), 82 | buffer_total_len_, fp_); 83 | if (buffer_valid_len_ == 0) { 84 | if (0 == ret_len) 85 | return NULL; 86 | read_buf[ret_len] = (char16)'\0'; 87 | return read_buf; 88 | } 89 | } 90 | 91 | for (size_t i = 0; i < buffer_valid_len_; i++) { 92 | if (i == max_len - 1 || 93 | buffer_[buffer_next_pos_ + i] == (char16)'\n') { 94 | if (ret_len + i > 0 && read_buf[ret_len + i - 1] == (char16)'\r') { 95 | read_buf[ret_len + i - 1] = (char16)'\0'; 96 | } else { 97 | read_buf[ret_len + i] = (char16)'\0'; 98 | } 99 | 100 | i++; 101 | buffer_next_pos_ += i; 102 | buffer_valid_len_ -= i; 103 | if (buffer_next_pos_ == buffer_total_len_) { 104 | buffer_next_pos_ = 0; 105 | buffer_valid_len_ = 0; 106 | } 107 | return read_buf; 108 | } else { 109 | read_buf[ret_len + i] = buffer_[buffer_next_pos_ + i]; 110 | } 111 | } 112 | 113 | ret_len += buffer_valid_len_; 114 | buffer_valid_len_ = 0; 115 | } while (true); 116 | 117 | // Never reach here 118 | return NULL; 119 | } 120 | 121 | bool Utf16Reader::close() { 122 | if (NULL != fp_) 123 | fclose(fp_); 124 | fp_ = NULL; 125 | 126 | if (NULL != buffer_) 127 | delete [] buffer_; 128 | buffer_ = NULL; 129 | return true; 130 | } 131 | } // namespace ime_pinyin 132 | -------------------------------------------------------------------------------- /libgooglepinyin/xygooglepinyin.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * Qt/E 4 input method and input panel 4 | * 5 | * Verification using sylixos(tm) real-time operating system 6 | */ 7 | 8 | /* 9 | * Copyright (c) 2006-2013 SylixOS Group. 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * 1. Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 3. The name of the author may not be used to endorse or promote products 21 | * derived from this software without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 24 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 25 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 26 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 27 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 28 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 32 | * OF SUCH DAMAGE. 33 | * 34 | * Author: Han.hui 35 | * 36 | */ 37 | 38 | #include "xygooglepinyin.h" 39 | #include 40 | #include 41 | #include 42 | 43 | using namespace ime_pinyin; 44 | 45 | #define DICT_PATH "dict_pinyin.dat" 46 | #define DICT_USER_PATH "dict_pinyin_user.dat" 47 | 48 | // fix cand_len 49 | static size_t fix_cand_len (size_t cand, int output_len) 50 | { 51 | #define MAX_SINGLE 40 // single hz MAX is 40 52 | size_t i; 53 | size_t decode_len; 54 | size_t single = 0; 55 | size_t multi = 0; 56 | char16 *cand_buf = new char16[output_len]; 57 | 58 | im_get_sps_str(&decode_len); 59 | if (decode_len == 1) { // only one spell char 60 | if (cand > 10) { 61 | cand = 10; // max is 10. 62 | } 63 | delete cand_buf; 64 | return cand; 65 | } 66 | 67 | for (i = 0; i < cand; i++) { 68 | im_get_candidate(i, cand_buf, output_len); 69 | if (strlen((char *)cand_buf) > 2) { 70 | multi++; 71 | } else { 72 | single++; 73 | if (single > MAX_SINGLE) { 74 | break; 75 | } 76 | } 77 | } 78 | 79 | cand = multi + single; 80 | delete cand_buf; 81 | return cand; 82 | } 83 | 84 | // constructor 85 | pinyin_im::pinyin_im(QObject *parent) : 86 | QObject(parent), enable(false) 87 | { 88 | } 89 | 90 | // input panel destructor 91 | pinyin_im::~pinyin_im() 92 | { 93 | } 94 | 95 | // init dict database 96 | bool pinyin_im::init(const QString &dir, int max_spell_len, int max_output_len) 97 | { 98 | bool ret; 99 | QString app_dir = dir; 100 | if (app_dir.isEmpty()) { 101 | app_dir = qApp->applicationDirPath() + "/dict"; 102 | } 103 | ret = im_open_decoder(QString("%1/" DICT_PATH).arg(app_dir).toLocal8Bit().data(), 104 | QString("%1/" DICT_USER_PATH).arg(app_dir).toLocal8Bit().data()); 105 | enable = ret; 106 | if (ret == false) { 107 | return ret; 108 | } 109 | 110 | im_set_max_lens(max_spell_len /* input pinyin max len */, 111 | max_output_len /* max output Chinese character string len */); 112 | 113 | reset_search(); 114 | 115 | spell_len = max_spell_len; 116 | output_len = max_output_len; 117 | 118 | return ret; 119 | } 120 | 121 | // deinit dict database 122 | void pinyin_im::deinit() 123 | { 124 | im_close_decoder(); 125 | } 126 | 127 | // flush dict cache 128 | void pinyin_im::flush_cache() 129 | { 130 | im_flush_cache(); 131 | } 132 | 133 | // add a char to search 134 | unsigned pinyin_im::search(const QString &spell) 135 | { 136 | if (!enable) 137 | { 138 | return 0; 139 | } 140 | QByteArray bytearray; 141 | char *pinyin; 142 | 143 | bytearray = spell.toUtf8(); 144 | pinyin = bytearray.data(); 145 | bytearray.size(); 146 | 147 | size_t cand = im_search(pinyin, bytearray.size()); 148 | 149 | // if (im_get_fixed_len()) { // if fixed at least 1, candidate 0(whole line) is not display. 150 | // cand--; 151 | // } 152 | 153 | cand = fix_cand_len(cand, output_len); 154 | 155 | return (unsigned)cand; 156 | } 157 | 158 | // delete a char to search 159 | unsigned pinyin_im::del_search(unsigned pos) 160 | { 161 | if (!enable) 162 | { 163 | return 0; 164 | } 165 | size_t cand = im_delsearch(pos, false, false); 166 | 167 | // if (im_get_fixed_len()) { // if fixed at least 1, candidate 0(whole line) is not display. 168 | // cand--; 169 | // } 170 | 171 | cand = fix_cand_len(cand, output_len); 172 | 173 | return (unsigned)cand; 174 | } 175 | 176 | // reset search string 177 | void pinyin_im::reset_search() 178 | { 179 | if (!enable) 180 | { 181 | return; 182 | } 183 | im_reset_search(); 184 | } 185 | 186 | // get current search position 187 | unsigned pinyin_im::cur_search_pos() 188 | { 189 | const uint16 *start_pos; 190 | size_t pos_len; 191 | size_t fixed_len = im_get_fixed_len(); 192 | 193 | pos_len = im_get_spl_start_pos(start_pos); 194 | if (fixed_len <= pos_len) { 195 | return (start_pos[fixed_len]); 196 | } 197 | 198 | return 0; 199 | } 200 | 201 | // get a candidate 202 | QString pinyin_im::get_candidate(unsigned index) 203 | { 204 | char16 *cand_buf = new char16[output_len]; 205 | char16 *cand; 206 | QString cand_str; 207 | 208 | // if (im_get_fixed_len()) { // if fixed at least 1, candidate 0(whole line) is not display. 209 | // index++; 210 | // } 211 | 212 | cand = im_get_candidate(index, cand_buf, output_len); 213 | if (cand) { 214 | cand_str = QString::fromUtf16(cand); // 'googlepinyin' output is utf16 string. 215 | if (index == 0) { 216 | cand_str.remove(0, im_get_fixed_len()); 217 | } 218 | } else { 219 | cand_str = ""; 220 | } 221 | 222 | delete cand_buf; 223 | return cand_str; 224 | } 225 | 226 | // choose a candidate 227 | unsigned pinyin_im::choose(unsigned index) 228 | { 229 | size_t left; 230 | 231 | // if (im_get_fixed_len()) { // if fixed at least 1, candidate 0(whole line) is not display. 232 | // index++; 233 | // } 234 | 235 | left = im_choose(index); 236 | 237 | left = fix_cand_len(left, output_len); 238 | if (left < 1) { 239 | return 0; // rearch over! 240 | } else { 241 | return (left - 1); 242 | } 243 | } 244 | 245 | // unchoose a candidate 246 | unsigned pinyin_im::unchoose() 247 | { 248 | size_t cand = im_cancel_last_choice(); 249 | 250 | // if (im_get_fixed_len()) { // if fixed at least 1, candidate 0 is not display. 251 | // cand--; 252 | // } 253 | 254 | cand = fix_cand_len(cand, output_len); 255 | 256 | return (unsigned)cand; 257 | } 258 | -------------------------------------------------------------------------------- /libgooglepinyin/xygooglepinyin.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * Qt/E 4 input method and input panel 4 | * 5 | * Verification using sylixos(tm) real-time operating system 6 | */ 7 | 8 | /* 9 | * Copyright (c) 2006-2013 SylixOS Group. 10 | * All rights reserved. 11 | * 12 | * Redistribution and use in source and binary forms, with or without modification, 13 | * are permitted provided that the following conditions are met: 14 | * 15 | * 1. Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 3. The name of the author may not be used to endorse or promote products 21 | * derived from this software without specific prior written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 24 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 25 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 26 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 27 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 28 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 32 | * OF SUCH DAMAGE. 33 | * 34 | * Author: Han.hui 35 | * 36 | */ 37 | 38 | #ifndef SYIMPINYIN_H 39 | #define SYIMPINYIN_H 40 | 41 | #include 42 | 43 | class pinyin_im : public QObject 44 | { 45 | Q_OBJECT 46 | public: 47 | explicit pinyin_im(QObject *parent = 0); 48 | ~pinyin_im(); 49 | 50 | public: 51 | bool init(const QString &dir = "", int max_spell_len = 64, int max_output_len = 64); 52 | void deinit(); 53 | 54 | void flush_cache(); 55 | 56 | unsigned search(const QString &); 57 | unsigned del_search(unsigned); 58 | void reset_search(); 59 | unsigned cur_search_pos(); 60 | QString get_candidate(unsigned); 61 | 62 | unsigned choose(unsigned); 63 | unsigned unchoose(); 64 | 65 | private: 66 | bool enable; 67 | int spell_len; 68 | int output_len; 69 | }; 70 | 71 | #endif // SYIMPINYIN_H 72 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "xyvirtualkeyboard.h" 3 | #include "xyskin.h" 4 | #include 5 | #include 6 | 7 | #define TEST 8 | #ifdef TEST 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #endif 16 | 17 | int main(int argc, char *argv[]) 18 | { 19 | QApplication a(argc, argv); 20 | 21 | // XYSKIN->writeSkinToFile("I:/DeskTop/test.skin"); 22 | XYSKIN->loadSkinWithFile(qApp->applicationDirPath() + "/skin/xyinput.skin"); 23 | XYVirtualKeyboard::getInstance()->initPinyinDictionary(); 24 | XYVirtualKeyboard::getInstance()->show(); 25 | 26 | #ifdef TEST 27 | // 测试代码 28 | QWidget widget; 29 | QVBoxLayout *layout = new QVBoxLayout(&widget); 30 | layout->addWidget(new QLineEdit); 31 | layout->addWidget(new QTextEdit); 32 | layout->addWidget(new QLineEdit); 33 | widget.show(); 34 | 35 | QObject::connect(qApp->inputMethod(), &QInputMethod::cursorRectangleChanged, 36 | XYVirtualKeyboard::getInstance(), [](){ 37 | QWindow *focusWindow = qApp->focusWindow(); 38 | if (focusWindow && qApp->focusWidget() && !XYVirtualKeyboard::getInstance()->isVisible()) { 39 | QRect rect = qApp->inputMethod()->cursorRectangle().toRect().translated(focusWindow->position()); 40 | QPoint pos = rect.bottomLeft() + QPoint(0, 5); 41 | QScreen *screen = qApp->screenAt(pos); 42 | if (screen == Q_NULLPTR) 43 | screen = qApp->primaryScreen(); 44 | 45 | if (pos.x() + XYVirtualKeyboard::getInstance()->width() > screen->geometry().width()) 46 | pos.setX(screen->geometry().width() - XYVirtualKeyboard::getInstance()->width()); 47 | if (pos.y() + XYVirtualKeyboard::getInstance()->height() > screen->geometry().height()) 48 | pos.setY(screen->geometry().height() - XYVirtualKeyboard::getInstance()->height()); 49 | 50 | XYVirtualKeyboard::getInstance()->move(pos); 51 | XYVirtualKeyboard::getInstance()->show(); 52 | } 53 | }); 54 | 55 | QObject::connect(XYVirtualKeyboard::getInstance(), 56 | &XYVirtualKeyboard::send_preedit, [](const QString &text){ 57 | QInputMethodEvent *event = 58 | new QInputMethodEvent(text, QList()); 59 | const QApplication *app = qApp; 60 | QWidget *focusWindow = app ? app->focusWidget() : Q_NULLPTR; 61 | if (focusWindow) { 62 | app->postEvent(focusWindow, event); 63 | } 64 | }); 65 | QObject::connect(XYVirtualKeyboard::getInstance(), 66 | &XYVirtualKeyboard::send_commit, [](const QString &text){ 67 | QInputMethodEvent *event = new QInputMethodEvent; 68 | event->setCommitString(text); 69 | const QApplication *app = qApp; 70 | QWidget *focusWindow = app ? app->focusWidget() : Q_NULLPTR; 71 | if (focusWindow) { 72 | app->postEvent(focusWindow, event); 73 | } 74 | }); 75 | QObject::connect(XYVirtualKeyboard::getInstance(), 76 | &XYVirtualKeyboard::keyClicked, [](int unicode, int key, Qt::KeyboardModifiers modifiers, bool press){ 77 | QKeyEvent *event = new QKeyEvent(press ? QEvent::KeyPress : QEvent::KeyRelease, 78 | key, 79 | modifiers, 80 | QChar(unicode)); 81 | const QApplication *app = qApp; 82 | QWidget *focusWindow = app ? app->focusWidget() : Q_NULLPTR; 83 | if (focusWindow) { 84 | app->postEvent(focusWindow, event); 85 | } 86 | }); 87 | #endif 88 | 89 | a.setQuitOnLastWindowClosed(true); 90 | a.setWindowIcon(QIcon(":/Keyboard.ico")); 91 | return a.exec(); 92 | } 93 | -------------------------------------------------------------------------------- /skin/blue.skin: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /skin/dark.skin: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /skin/yellow.skin: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /xydragablewidget.cpp: -------------------------------------------------------------------------------- 1 | #include "xydragablewidget.h" 2 | #include 3 | #include 4 | #include 5 | 6 | XYDragableWidget::XYDragableWidget(QWidget *centerWidget, XYDragableWidget::DIRECTION dire, QWidget *parent) 7 | : QWidget(parent), filterObjsPressed(false), mouseSensitivity(1), centerWidget(centerWidget), direction(dire) 8 | { 9 | this->centerWidget = centerWidget; 10 | this->centerWidget->setParent(this); 11 | this->centerWidget->installEventFilter(this); 12 | } 13 | 14 | void XYDragableWidget::resizeEvent(QResizeEvent *event) 15 | { 16 | if (direction == HORIZONTAL) 17 | { 18 | this->centerWidget->setFixedHeight(this->height()); 19 | this->centerWidget->setMinimumWidth(this->width()); 20 | } 21 | else 22 | { 23 | this->centerWidget->setMinimumHeight(this->height()); 24 | this->centerWidget->setFixedWidth(this->width()); 25 | } 26 | 27 | QWidget::resizeEvent(event); 28 | } 29 | 30 | bool XYDragableWidget::eventFilter(QObject *obj, QEvent *event) 31 | { 32 | if (centerWidget == obj) 33 | { 34 | if (event->type() == QEvent::MouseButtonPress) 35 | { 36 | QMouseEvent *mouse_event = (QMouseEvent *)event; 37 | filterObjsPressed = true; 38 | filterObjscurPressedPoint = filterObjsLastPressedPoint = mouse_event->globalPos(); 39 | } 40 | else if (event->type() == QEvent::MouseButtonRelease) 41 | { 42 | filterObjsPressed = false; 43 | filterObjsLastPressedPoint = QPoint(); 44 | } 45 | else if (event->type() == QEvent::Wheel) 46 | { 47 | QWheelEvent *wheel_event = (QWheelEvent *)event; 48 | if (direction == HORIZONTAL) 49 | { 50 | if (wheel_event->delta() < 0) 51 | { 52 | if (centerWidget->pos().x() - 1 >= width() - centerWidget->width()) 53 | { 54 | centerWidget->move(centerWidget->pos().x() - mouseSensitivity * 2, centerWidget->pos().y()); 55 | } 56 | } 57 | else if (wheel_event->delta() > 0) 58 | { 59 | if (centerWidget->pos().x() + 1 <= 0) 60 | { 61 | centerWidget->move(centerWidget->pos().x() + mouseSensitivity * 2, centerWidget->pos().y()); 62 | } 63 | } 64 | } 65 | else 66 | { 67 | if (wheel_event->delta() < 0) 68 | { 69 | if (centerWidget->pos().y() - 1 >= height() - centerWidget->height()) 70 | { 71 | centerWidget->move(centerWidget->pos().x(), centerWidget->pos().y() - mouseSensitivity * 2); 72 | } 73 | } 74 | else if (wheel_event->delta() > 0) 75 | { 76 | if (centerWidget->pos().y() + 1 <= 0) 77 | { 78 | centerWidget->move(centerWidget->pos().x(), centerWidget->pos().y() + mouseSensitivity * 2); 79 | } 80 | } 81 | } 82 | } 83 | else if (event->type() == QEvent::MouseMove && filterObjsPressed) 84 | { 85 | QMouseEvent *mouse_event = (QMouseEvent *)event; 86 | if (direction == HORIZONTAL) 87 | { 88 | if (qAbs(filterObjscurPressedPoint.x() - mouse_event->globalPos().x()) > 10) // 边界值 89 | { 90 | if (filterObjsLastPressedPoint.x() > mouse_event->globalPos().x() + 5) 91 | { 92 | if (centerWidget->pos().x() - 1 >= width() - centerWidget->width()) 93 | { 94 | centerWidget->move(centerWidget->pos().x() - mouseSensitivity, centerWidget->pos().y()); 95 | } 96 | filterObjsLastPressedPoint = mouse_event->globalPos(); 97 | } 98 | else if (filterObjsLastPressedPoint.x() < mouse_event->globalPos().x() - 5) 99 | { 100 | if (centerWidget->pos().x() + 1 <= 0) 101 | { 102 | centerWidget->move(centerWidget->pos().x() + mouseSensitivity, centerWidget->pos().y()); 103 | } 104 | filterObjsLastPressedPoint = mouse_event->globalPos(); 105 | } 106 | } 107 | } 108 | else 109 | { 110 | if (qAbs(filterObjscurPressedPoint.y() - mouse_event->globalPos().y()) > 10) // 边界值 111 | { 112 | if (filterObjsLastPressedPoint.y() > mouse_event->globalPos().y() + 5) 113 | { 114 | if (centerWidget->pos().y() - 1 >= height() - centerWidget->height()) 115 | { 116 | centerWidget->move(centerWidget->pos().x(), centerWidget->pos().y() - mouseSensitivity); 117 | } 118 | filterObjsLastPressedPoint = mouse_event->globalPos(); 119 | } 120 | else if (filterObjsLastPressedPoint.y() < mouse_event->globalPos().y() - 5) 121 | { 122 | if (centerWidget->pos().y() + 1 <= 0) 123 | { 124 | centerWidget->move(centerWidget->pos().x(), centerWidget->pos().y() + mouseSensitivity); 125 | } 126 | filterObjsLastPressedPoint = mouse_event->globalPos(); 127 | } 128 | } 129 | } 130 | } 131 | } 132 | 133 | return QWidget::eventFilter(obj, event); 134 | } 135 | 136 | int XYDragableWidget::getMouseSensitivity() const 137 | { 138 | return mouseSensitivity; 139 | } 140 | 141 | void XYDragableWidget::setMouseSensitivity(int value) 142 | { 143 | mouseSensitivity = value; 144 | } 145 | 146 | -------------------------------------------------------------------------------- /xydragablewidget.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaoyanLG/Soft-keyboard/68b37f5099a1a14279e62acd3361a11ad5ee958d/xydragablewidget.h -------------------------------------------------------------------------------- /xyhdragabletranslateview.cpp: -------------------------------------------------------------------------------- 1 | #include "xyhdragabletranslateview.h" 2 | #include "xydragablewidget.h" 3 | #include "xyskin.h" 4 | #include 5 | //#include 6 | 7 | XYHDragableTranslateView::XYHDragableTranslateView(QWidget *parent) 8 | : XYQStringView(parent) 9 | { 10 | 11 | } 12 | 13 | void XYHDragableTranslateView::paintEvent(QPaintEvent *event) 14 | { 15 | QPainter painter(this); 16 | painter.setRenderHint(QPainter::Antialiasing); 17 | painter.setFont(XYSKIN->candidateTextFont); 18 | 19 | QFontMetrics metrics(painter.font()); 20 | int cur_x = 10; 21 | int text_w = 0; 22 | QString text; 23 | QRect text_rect; 24 | dataRects.clear(); 25 | 26 | // 先绘制按下的矩形 27 | if (!pressRect.isEmpty()) 28 | { 29 | painter.setPen(XYSKIN->candidateTextPressedBKColor); 30 | painter.setBrush(XYSKIN->candidateTextPressedBKColor); 31 | painter.drawRect(pressRect); 32 | } 33 | 34 | // 绘制所有的文字 35 | painter.setPen(XYSKIN->candidateTextPen); 36 | for (int i = 0; i < dataStrings.size(); ++i) 37 | { 38 | text = dataStrings.at(i); 39 | text_w = metrics.width(text) + 15; 40 | if (unitMinWidth != -1) 41 | { 42 | text_w = qMax(unitMinWidth, text_w); 43 | } 44 | 45 | text_rect = QRect(cur_x, 0, text_w, height()); 46 | painter.drawText(text_rect, text, QTextOption(Qt::AlignCenter)); 47 | dataRects.append(text_rect); 48 | cur_x += text_w + 15; 49 | } 50 | 51 | setFixedWidth(cur_x); 52 | 53 | XYQStringView::paintEvent(event); 54 | } 55 | -------------------------------------------------------------------------------- /xyhdragabletranslateview.h: -------------------------------------------------------------------------------- 1 | #ifndef XYHDRAGABLETRANSLATEVIEW_H 2 | #define XYHDRAGABLETRANSLATEVIEW_H 3 | 4 | #include "xyqstringview.h" 5 | 6 | class XYHDragableTranslateView : public XYQStringView 7 | { 8 | Q_OBJECT 9 | public: 10 | explicit XYHDragableTranslateView(QWidget *parent = 0); 11 | 12 | protected: 13 | void paintEvent(QPaintEvent *event); 14 | 15 | friend class XYVirtualKeyboard; 16 | }; 17 | 18 | #endif // XYHDRAGABLETRANSLATEVIEW_H 19 | -------------------------------------------------------------------------------- /xyinput.json: -------------------------------------------------------------------------------- 1 | { 2 | "Keys": [ "xyinput" ] 3 | } 4 | -------------------------------------------------------------------------------- /xyinputplugin.cpp: -------------------------------------------------------------------------------- 1 | #include "xyinputplugin.h" 2 | #include "xyplatforminputcontext.h" 3 | #include 4 | 5 | QPlatformInputContext *XYInputPlugin::create(const QString &system, const QStringList ¶mList) 6 | { 7 | if (system.compare(QLatin1String("xyinput"), Qt::CaseInsensitive) == 0) { 8 | return new XYPlatformInputContext(); 9 | } 10 | return Q_NULLPTR; 11 | } 12 | -------------------------------------------------------------------------------- /xyinputplugin.h: -------------------------------------------------------------------------------- 1 | #ifndef XYINPUTPLUGIN_H 2 | #define XYINPUTPLUGIN_H 3 | 4 | #include 5 | #include 6 | 7 | class XYInputPlugin : public QPlatformInputContextPlugin 8 | { 9 | Q_OBJECT 10 | Q_PLUGIN_METADATA(IID QPlatformInputContextFactoryInterface_iid FILE "xyinput.json") 11 | 12 | public: 13 | QPlatformInputContext *create(const QString&, const QStringList&); 14 | }; 15 | 16 | #endif // XYINPUTPLUGIN_H 17 | -------------------------------------------------------------------------------- /xyplatforminputcontext.cpp: -------------------------------------------------------------------------------- 1 | #define QT_NO_DEBUG_OUTPUT 2 | 3 | #include "xyplatforminputcontext.h" 4 | #include "xyvirtualkeyboard.h" 5 | #include "xypushbutton.h" 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | XYPlatformInputContext::XYPlatformInputContext() : 12 | m_focusObject(Q_NULLPTR), 13 | m_locale(), 14 | m_filterEvent(Q_NULLPTR), 15 | m_visible(false), 16 | m_inputDirection(m_locale.textDirection()) 17 | { 18 | m_inputPanel = XYVirtualKeyboard::getInstance(); 19 | m_inputPanel->initPinyinDictionary(); 20 | 21 | QObject::connect(m_inputPanel, &XYVirtualKeyboard::send_preedit, 22 | this, [this](const QString &text){ 23 | if (focusObject()) { 24 | QInputMethodEvent event(text, QList()); 25 | m_filterEvent = &event; 26 | qApp->sendEvent(focusObject(), &event); 27 | m_filterEvent = Q_NULLPTR; 28 | } 29 | }); 30 | QObject::connect(m_inputPanel, &XYVirtualKeyboard::send_commit, 31 | this, [this](const QString &text){ 32 | if (focusObject()) { 33 | QInputMethodEvent event; 34 | event.setCommitString(text); 35 | m_filterEvent = &event; 36 | qApp->sendEvent(focusObject(), &event); 37 | m_filterEvent = Q_NULLPTR; 38 | } 39 | }); 40 | QObject::connect(m_inputPanel, &XYVirtualKeyboard::keyClicked, 41 | this, [this](int unicode, int key, Qt::KeyboardModifiers modifiers, bool press){ 42 | if (focusObject()) { 43 | QKeyEvent event(press ? QEvent::KeyPress : QEvent::KeyRelease, 44 | key, 45 | modifiers, 46 | QChar(unicode)); 47 | m_filterEvent = &event; 48 | qApp->sendEvent(focusObject(), &event); 49 | m_filterEvent = Q_NULLPTR; 50 | } 51 | }); 52 | 53 | QObject::connect(qApp->inputMethod(), &QInputMethod::anchorRectangleChanged, 54 | this, [this](){ 55 | const QGuiApplication *app = qApp; 56 | QWindow *focusWindow = app ? app->focusWindow() : Q_NULLPTR; 57 | if (focusWindow) { 58 | this->m_anchorRect = app->inputMethod()->anchorRectangle().toRect().translated(focusWindow->position()); 59 | } 60 | }); 61 | } 62 | 63 | XYPlatformInputContext::~XYPlatformInputContext() 64 | { 65 | } 66 | 67 | bool XYPlatformInputContext::isValid() const 68 | { 69 | return true; 70 | } 71 | 72 | void XYPlatformInputContext::reset() 73 | { 74 | qDebug() << Q_FUNC_INFO; 75 | } 76 | 77 | void XYPlatformInputContext::commit() 78 | { 79 | qDebug() << Q_FUNC_INFO; 80 | } 81 | 82 | void XYPlatformInputContext::update(Qt::InputMethodQueries queries) 83 | { 84 | qDebug() << Q_FUNC_INFO; 85 | } 86 | 87 | void XYPlatformInputContext::invokeAction(QInputMethod::Action action, int cursorPosition) 88 | { 89 | qDebug() << Q_FUNC_INFO; 90 | } 91 | 92 | QRectF XYPlatformInputContext::keyboardRect() const 93 | { 94 | qDebug() << Q_FUNC_INFO; 95 | return QRectF(); 96 | } 97 | 98 | bool XYPlatformInputContext::isAnimating() const 99 | { 100 | qDebug() << Q_FUNC_INFO; 101 | return false; 102 | } 103 | 104 | void XYPlatformInputContext::showInputPanel() 105 | { 106 | qDebug() << Q_FUNC_INFO; 107 | m_inputPanel->move(m_anchorRect.bottomLeft() + QPoint(0, 10)); 108 | m_inputPanel->show(); 109 | } 110 | 111 | void XYPlatformInputContext::hideInputPanel() 112 | { 113 | qDebug() << Q_FUNC_INFO; 114 | m_inputPanel->close(); 115 | } 116 | 117 | bool XYPlatformInputContext::isInputPanelVisible() const 118 | { 119 | qDebug() << Q_FUNC_INFO; 120 | return m_inputPanel ? m_inputPanel->isVisible() : false; 121 | } 122 | 123 | QLocale XYPlatformInputContext::locale() const 124 | { 125 | qDebug() << Q_FUNC_INFO; 126 | return m_locale; 127 | } 128 | 129 | void XYPlatformInputContext::setLocale(QLocale locale) 130 | { 131 | qDebug() << Q_FUNC_INFO; 132 | if (m_locale != locale) { 133 | m_locale = locale; 134 | emitLocaleChanged(); 135 | } 136 | } 137 | 138 | Qt::LayoutDirection XYPlatformInputContext::inputDirection() const 139 | { 140 | qDebug() << Q_FUNC_INFO; 141 | return m_inputDirection; 142 | } 143 | 144 | void XYPlatformInputContext::setInputDirection(Qt::LayoutDirection direction) 145 | { 146 | qDebug() << Q_FUNC_INFO; 147 | } 148 | 149 | QObject *XYPlatformInputContext::focusObject() 150 | { 151 | qDebug() << Q_FUNC_INFO; 152 | return m_focusObject; 153 | } 154 | 155 | void XYPlatformInputContext::setFocusObject(QObject *object) 156 | { 157 | qDebug() << Q_FUNC_INFO; 158 | if (m_focusObject != object) { 159 | if (m_focusObject) 160 | m_focusObject->removeEventFilter(this); 161 | m_focusObject = object; 162 | if (m_focusObject) 163 | m_focusObject->installEventFilter(this); 164 | } 165 | update(Qt::ImQueryAll); 166 | } 167 | 168 | bool XYPlatformInputContext::eventFilter(QObject *object, QEvent *event) 169 | { 170 | if (event != m_filterEvent 171 | && event->type() == QEvent::KeyPress 172 | && object == m_focusObject 173 | && m_inputPanel 174 | && XYPushButton::isChinese()) 175 | { 176 | return m_inputPanel->handleQKeyEvent(static_cast(event)); 177 | } 178 | 179 | return false; 180 | } 181 | -------------------------------------------------------------------------------- /xyplatforminputcontext.h: -------------------------------------------------------------------------------- 1 | #ifndef XYPLATFORMINPUTCONTEXT_H 2 | #define XYPLATFORMINPUTCONTEXT_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | class XYVirtualKeyboard; 10 | class XYPlatformInputContext : public QPlatformInputContext 11 | { 12 | Q_OBJECT 13 | public: 14 | explicit XYPlatformInputContext(); 15 | ~XYPlatformInputContext(); 16 | 17 | virtual bool isValid() const; 18 | 19 | virtual void reset(); 20 | virtual void commit(); 21 | virtual void update(Qt::InputMethodQueries queries); 22 | virtual void invokeAction(QInputMethod::Action action, int cursorPosition); 23 | virtual QRectF keyboardRect() const; 24 | 25 | virtual bool isAnimating() const; 26 | 27 | virtual void showInputPanel(); 28 | virtual void hideInputPanel(); 29 | virtual bool isInputPanelVisible() const; 30 | 31 | virtual QLocale locale() const; 32 | void setLocale(QLocale locale); 33 | virtual Qt::LayoutDirection inputDirection() const; 34 | void setInputDirection(Qt::LayoutDirection direction); 35 | 36 | QObject *focusObject(); 37 | virtual void setFocusObject(QObject *object); 38 | 39 | virtual bool eventFilter(QObject *object, QEvent *event); 40 | 41 | private: 42 | XYVirtualKeyboard *m_inputPanel; 43 | QRect m_anchorRect; 44 | QObject *m_focusObject; 45 | QLocale m_locale; 46 | QEvent *m_filterEvent; 47 | bool m_visible; 48 | Qt::LayoutDirection m_inputDirection; 49 | }; 50 | 51 | #endif // XYPLATFORMINPUTCONTEXT_H 52 | -------------------------------------------------------------------------------- /xypushbutton.h: -------------------------------------------------------------------------------- 1 | #ifndef XYPUSHBUTTON_H 2 | #define XYPUSHBUTTON_H 3 | 4 | #include 5 | #include 6 | 7 | class XYPushButton : public QLabel 8 | { 9 | Q_OBJECT 10 | public: 11 | explicit XYPushButton(const QString &text, int generalKey, int shiftKey = -1, bool small = false, QWidget *parent=0); 12 | ~XYPushButton(); 13 | bool isChecked(); 14 | static bool isChinese(); 15 | static QString EnText(); 16 | static QString zhongText(); 17 | QSize sizeHint() const; 18 | 19 | signals: 20 | void mousePressed(XYPushButton *btn); 21 | void mouseReleaseed(XYPushButton *btn); 22 | void clicked(); 23 | void clicked(int unicode, int key, Qt::KeyboardModifiers modifiers, bool press); 24 | void checkedChanged(bool checked = false); 25 | 26 | public slots: 27 | void setChecked(bool checked); 28 | void setCheckable(bool checkable); 29 | void startRepeatTimer(); 30 | 31 | protected: 32 | void paintEvent(QPaintEvent *event); 33 | void timerEvent(QTimerEvent *event); 34 | void mousePressEvent(QMouseEvent *event); 35 | void mouseReleaseEvent(QMouseEvent *event); 36 | void mouseMoveEvent(QMouseEvent *event); 37 | 38 | private: 39 | void mousePressedOP(); 40 | void mouseReleaseedOP(bool alwaysEmit, bool contansMouse); 41 | 42 | public: 43 | static void drawIconWithKey(QPainterPath &path, int key, const QRect &rect); 44 | 45 | private: 46 | bool checkable; 47 | bool checked; 48 | bool pressed; 49 | bool isSmallAreaKey; // 记录是否是小键盘区域的按键 50 | int repeatEmitTimer; 51 | QTimer timer; 52 | 53 | int shiftKey; 54 | int generalKey; 55 | 56 | static QList allKeyBtns; 57 | static bool capsLocked; 58 | static bool numLocked; 59 | static bool chinese; 60 | static Qt::KeyboardModifiers modifiers; 61 | 62 | friend class XYVirtualKeyboard; 63 | }; 64 | 65 | #endif // XYPUSHBUTTON_H 66 | -------------------------------------------------------------------------------- /xyqstringview.cpp: -------------------------------------------------------------------------------- 1 | #include "xyqstringview.h" 2 | #include 3 | #include 4 | //#include 5 | 6 | XYQStringView::XYQStringView(QWidget *parent) 7 | : QWidget(parent), pressed(false) 8 | { 9 | unitMinWidth = -1; 10 | unitMinHeight = -1; 11 | } 12 | int XYQStringView::getUnitMinWidth() const 13 | { 14 | return unitMinWidth; 15 | } 16 | 17 | void XYQStringView::setUnitMinWidth(int value) 18 | { 19 | unitMinWidth = value; 20 | } 21 | int XYQStringView::getUnitMinHeight() const 22 | { 23 | return unitMinHeight; 24 | } 25 | 26 | void XYQStringView::setUnitMinHeight(int value) 27 | { 28 | unitMinHeight = value; 29 | } 30 | 31 | bool XYQStringView::event(QEvent *event) 32 | { 33 | if (event->type() == QEvent::MouseButtonPress) 34 | { 35 | pressed = true; 36 | pressRect = QRect(); 37 | QMouseEvent *mouse_event = (QMouseEvent *)event; 38 | int index = findcontainsMouseRect(mouse_event->pos(), pressRect); 39 | if (index != -1) 40 | { 41 | emit stringPressed(dataStrings.at(index), 42 | mapToGlobal(QPoint(pressRect.x(), pressRect.y()))); 43 | update(pressRect); 44 | } 45 | return true; 46 | } 47 | else if (event->type() == QEvent::MouseButtonRelease) 48 | { 49 | if (pressed) 50 | { 51 | if (!pressRect.isEmpty()) 52 | { 53 | int index = dataRects.indexOf(pressRect); 54 | emit clicked(dataStrings.at(index), index); 55 | } 56 | update(pressRect); 57 | emit stringPressed("", QPoint()); 58 | } 59 | pressed = false; 60 | pressRect = QRect(); 61 | return true; 62 | } 63 | else if (pressed && event->type() == QEvent::MouseMove) 64 | { 65 | if (pressed) 66 | { 67 | QRect cur_rect; 68 | QMouseEvent *mouse_event = (QMouseEvent *)event; 69 | int index = findcontainsMouseRect(mouse_event->pos(), cur_rect); 70 | if (index == -1) 71 | { 72 | emit stringPressed("", QPoint()); 73 | pressed = false; 74 | update(pressRect); 75 | pressRect = QRect(); 76 | } 77 | } 78 | return true; 79 | } 80 | 81 | return QWidget::event(event); 82 | } 83 | 84 | int XYQStringView::findcontainsMouseRect(const QPoint &mousePos, QRect &rect) 85 | { 86 | for (int i = 0; i < dataRects.size(); ++i) 87 | { 88 | if (dataRects.at(i).contains(mousePos)) 89 | { 90 | rect = dataRects.at(i); 91 | return i; 92 | } 93 | } 94 | 95 | return -1; 96 | } 97 | -------------------------------------------------------------------------------- /xyqstringview.h: -------------------------------------------------------------------------------- 1 | #ifndef XYQSTRINGVIEW_H 2 | #define XYQSTRINGVIEW_H 3 | 4 | #include 5 | 6 | class XYQStringView : public QWidget 7 | { 8 | Q_OBJECT 9 | public: 10 | explicit XYQStringView(QWidget *parent = 0); 11 | 12 | int getUnitMinWidth() const; 13 | void setUnitMinWidth(int value); 14 | 15 | int getUnitMinHeight() const; 16 | void setUnitMinHeight(int value); 17 | 18 | signals: 19 | void stringPressed(const QString &text, const QPoint &pos); 20 | void clicked(const QString &text, int index); 21 | 22 | protected: 23 | bool event(QEvent *event); 24 | 25 | private: 26 | int findcontainsMouseRect(const QPoint &mousePos, QRect &rect); 27 | 28 | protected: 29 | QList dataRects; 30 | QStringList dataStrings; 31 | 32 | int unitMinWidth; 33 | int unitMinHeight; 34 | bool pressed; 35 | QRect pressRect; 36 | 37 | }; 38 | 39 | #endif // XYQSTRINGVIEW_H 40 | -------------------------------------------------------------------------------- /xyskin.h: -------------------------------------------------------------------------------- 1 | #ifndef XYSKIN_H 2 | #define XYSKIN_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | struct KeyButtonStyle{ 13 | KeyButtonStyle() 14 | :key(0), pressed(0) 15 | {} 16 | KeyButtonStyle(const KeyButtonStyle *other) 17 | { 18 | this->key = other->key; 19 | this->pressed = other->pressed; 20 | this->textFont = other->textFont; 21 | this->backgroundColor = other->backgroundColor; 22 | this->textPen = other->textPen; 23 | this->borderPen = other->borderPen; 24 | this->borderFont = other->borderFont; 25 | this->backImage = other->backImage; 26 | } 27 | 28 | int key; // 按钮键值 29 | int pressed; // 是否按下(0表示没有按下,非0表示按下) 30 | QFont textFont; // 文字字体 31 | QColor backgroundColor;// 背景颜色 32 | QPen textPen; // 文字画笔 33 | QPen borderPen; // 边框画笔 34 | QFont borderFont; // 边框字体 35 | QPixmap backImage; // 背景图片 36 | }; 37 | 38 | class XYSkin : public QObject 39 | { 40 | Q_OBJECT 41 | public: 42 | static XYSkin *getInstance(); 43 | ~XYSkin(); 44 | KeyButtonStyle *getStyleByKey(int key, bool pressed); 45 | void initWithNoSkinFile(); 46 | bool loadSkinWithFile(const QString &filePath); 47 | bool writeSkinToFile(const QString &filePath); 48 | 49 | signals: 50 | void skinChanged(); 51 | 52 | private: 53 | void loadIntStyle(const QDomElement &parent, int &value); 54 | void loadPixmapStyle(const QDomElement &parent, QPixmap &pixmap); 55 | void loadColorStyle(const QDomElement &parent, QColor &color); 56 | void loadFontStyle(const QDomElement &parent, QFont &font); 57 | void loadPenStyle(const QDomElement &parent, QPen &pen); 58 | void loadButtonStyle(const QDomElement &parent, KeyButtonStyle *btn); 59 | 60 | void writeIntStyle(QDomDocument &doc,QDomElement &parent, 61 | int key, 62 | const QString &title); 63 | void writePixmapStyle(QDomDocument &doc,QDomElement &parent, 64 | const QPixmap &pixmap, 65 | const QString &title); 66 | void writeColorStyle(QDomDocument &doc,QDomElement &parent, 67 | const QColor &color, 68 | const QString &title, 69 | bool isAttribute = false); 70 | void writeFontStyle(QDomDocument &doc,QDomElement &parent, 71 | const QFont &font, 72 | const QString &title); 73 | void writePenStyle(QDomDocument &doc,QDomElement &parent, 74 | const QPen &pen, 75 | const QString &title); 76 | void writeButtonStyle(QDomDocument &doc, 77 | QDomElement &parent, 78 | KeyButtonStyle *btn, 79 | const QString &title); 80 | 81 | public: 82 | KeyButtonStyle *defaultButtonStyle1; // 默认按钮1风格 83 | KeyButtonStyle *defaultButtonStyle1_pressed; // 默认按钮1按下时风格 84 | KeyButtonStyle *defaultButtonStyle2; // 默认按钮2风格 85 | KeyButtonStyle *defaultButtonStyle2_pressed; // 默认按钮2按下时风格 86 | KeyButtonStyle *popupButtonStyle; // 弹出框风格,如果没有使用默认 87 | QList userDefButtonsStyle; // 自定义按钮风格 88 | QColor topColor; // 候选词背景色 89 | QPixmap topBKPixmap; // 候选词背景图片 90 | QPen trianglePen; // 下拉按钮图标画笔 91 | QColor trianglePressedBKColor; // 下拉按钮按下时的颜色 92 | QPixmap triangleBKPixmap; // 下拉按钮图标 93 | QPixmap triangleBKPressedPixmap;// 下拉按钮按下时图标 94 | QColor bottomColor1; // 底部显示按钮时的颜色 95 | QColor bottomColor2; // 底部显示滚动窗口时候的颜色 96 | QPixmap bottomBKPixmap; // 按钮背景图片 97 | QPen borderPen; // 边框画笔 98 | QColor inputLettersColor; // 用户输入字母的颜色 99 | QFont inputLettersFont; // 用户输入字母的字体 100 | QFont candidateTextFont; // 候选词的字体 101 | QPen candidateTextPen; // 候选词的画笔 102 | QColor candidateTextPressedBKColor; // 候选词按下时候的颜色 103 | 104 | private: 105 | explicit XYSkin(QObject *parent = 0); 106 | static XYSkin *instance; 107 | }; 108 | 109 | #define XYSKIN XYSkin::getInstance() 110 | #endif // XYSKIN_H 111 | -------------------------------------------------------------------------------- /xytempwindows.cpp: -------------------------------------------------------------------------------- 1 | #include "xytempwindows.h" 2 | #include "xypushbutton.h" 3 | #include "xyskin.h" 4 | #include 5 | 6 | XYTempWindows::XYTempWindows(XYTempWindows::DIRECTION direction, QWidget *parent) 7 | : QWidget(parent), direction(direction), key(0) 8 | { 9 | this->setWindowFlags(Qt::FramelessWindowHint 10 | | Qt::WindowStaysOnTopHint 11 | | Qt::Tool); 12 | #if QT_VERSION >= 0x050000 13 | this->setWindowFlags(this->windowFlags() | Qt::WindowDoesNotAcceptFocus); 14 | #endif 15 | this->setAttribute(Qt::WA_TranslucentBackground); 16 | 17 | setFont(XYSKIN->popupButtonStyle->textFont); 18 | } 19 | 20 | void XYTempWindows::paintEvent(QPaintEvent *) 21 | { 22 | QPainter painter(this); 23 | painter.setRenderHint(QPainter::Antialiasing); 24 | KeyButtonStyle *style = XYSKIN->popupButtonStyle; 25 | 26 | painter.setFont(style->textFont); 27 | QFont font = painter.font(); 28 | QPainterPath path = getPainterPath(this->rect(), direction); 29 | 30 | painter.setPen(style->borderPen); 31 | if (!style->backImage.isNull()) 32 | { 33 | painter.setBrush(style->backImage.scaled(textRect.size())); 34 | } 35 | else 36 | { 37 | painter.setBrush(style->backgroundColor); 38 | } 39 | painter.drawPath(path); 40 | painter.setPen(style->textPen); 41 | 42 | if (key == Qt::Key_Backspace 43 | || key == Qt::Key_Return 44 | || key == Qt::Key_Enter) 45 | { 46 | QPainterPath path; 47 | XYPushButton::drawIconWithKey(path, key, textRect); 48 | painter.setPen(painter.brush().color()); 49 | painter.setBrush(style->textPen.color()); 50 | painter.drawPath(path); 51 | } 52 | else if (key == Qt::Key_Menu) 53 | { 54 | QRect left = textRect; 55 | left.setWidth(left.width() / 2); 56 | QRect right = left; 57 | right.setX(left.x() + left.width()); 58 | right.setWidth(left.width()); 59 | QString left_text, right_text; 60 | if (XYPushButton::isChinese()) 61 | { 62 | left_text = XYPushButton::EnText() + "/"; 63 | right_text = XYPushButton::zhongText(); 64 | font.setBold(false); 65 | font.setPixelSize(font.pixelSize() - 2); 66 | painter.setFont(font); 67 | painter.drawText(left, left_text, QTextOption(Qt::AlignRight|Qt::AlignVCenter)); 68 | font.setBold(true); 69 | font.setPixelSize(font.pixelSize() + 2); 70 | painter.setFont(font); 71 | painter.drawText(right, right_text, QTextOption(Qt::AlignLeft|Qt::AlignVCenter)); 72 | } 73 | else 74 | { 75 | left_text = XYPushButton::EnText(); 76 | right_text = "/" + XYPushButton::zhongText(); 77 | font.setBold(true); 78 | painter.setFont(font); 79 | painter.drawText(left, left_text, QTextOption(Qt::AlignRight|Qt::AlignVCenter)); 80 | font.setBold(false); 81 | font.setPixelSize(font.pixelSize() - 2); 82 | painter.setFont(font); 83 | painter.drawText(right, right_text, QTextOption(Qt::AlignLeft|Qt::AlignVCenter)); 84 | } 85 | } 86 | else if (key == Qt::Key_Space) 87 | { 88 | painter.drawText(textRect, "Space", QTextOption(Qt::AlignCenter)); 89 | } 90 | else 91 | { 92 | painter.drawText(textRect, text, QTextOption(Qt::AlignCenter)); 93 | } 94 | } 95 | 96 | QPainterPath XYTempWindows::getPainterPath(const QRect &rect, XYTempWindows::DIRECTION direction) 97 | { 98 | QPainterPath path; 99 | int arrow_h = 5; 100 | switch (direction) 101 | { 102 | case TOP: 103 | arrow_h = qRound(rect.height() * 0.1); 104 | textRect = QRect(rect.x() + 1, rect.y() + arrow_h + 1, 105 | rect.width() - 2, rect.height() - arrow_h - 2); 106 | path.addRoundedRect(textRect, 4, 4); 107 | path.moveTo(rect.x() + rect.width() / 2 - arrow_h / 2, rect.y() + arrow_h + 1); 108 | path.lineTo(rect.x() + rect.width() / 2, rect.y()); 109 | path.lineTo(rect.x() + rect.width() / 2 + arrow_h / 2, rect.y() + arrow_h + 1); 110 | break; 111 | case BOTTOM: 112 | arrow_h = qRound(rect.height() * 0.1); 113 | textRect = QRect(rect.x() + 1, rect.y() + 1, 114 | rect.width() - 2, rect.height() - arrow_h - 1); 115 | path.addRoundedRect(textRect, 4, 4); 116 | path.moveTo(rect.x() + rect.width() / 2 - arrow_h / 2, rect.y() + rect.height() - arrow_h); 117 | path.lineTo(rect.x() + rect.width() / 2, rect.y() + rect.height()); 118 | path.lineTo(rect.x() + rect.width() / 2 + arrow_h / 2, rect.y() + rect.height() - arrow_h); 119 | break; 120 | case LEFT: 121 | arrow_h = qRound(rect.height() * 0.1); 122 | textRect = QRect(rect.x() + arrow_h, rect.y() + 1, 123 | rect.width() - arrow_h - 2, rect.height() - 2); 124 | path.addRoundedRect(textRect, 4, 4); 125 | path.moveTo(rect.x() + arrow_h, rect.y() + rect.height() / 2 - arrow_h / 2); 126 | path.lineTo(rect.x() - 2, rect.y() + rect.height() / 2); 127 | path.lineTo(rect.x() + arrow_h, rect.y() + rect.height() / 2 + arrow_h / 2); 128 | break; 129 | case RIGHT: 130 | arrow_h = qRound(rect.height() * 0.1); 131 | textRect = QRect(rect.x() + 1, rect.y() + 1, 132 | rect.width() - arrow_h - 2, rect.height() - 2); 133 | path.addRoundedRect(textRect, 4, 4); 134 | path.moveTo(rect.x() - 1 + rect.width() - arrow_h, rect.y() + rect.height() / 2 - arrow_h / 2); 135 | path.lineTo(rect.x() - 1 + rect.width(), rect.y() + rect.height() / 2); 136 | path.lineTo(rect.x() - 1 + rect.width() - arrow_h, rect.y() + rect.height() / 2 + arrow_h / 2); 137 | break; 138 | default: 139 | break; 140 | } 141 | return path.simplified(); 142 | } 143 | 144 | int XYTempWindows::getKey() const 145 | { 146 | return key; 147 | } 148 | 149 | void XYTempWindows::setKey(int value) 150 | { 151 | key = value; 152 | } 153 | 154 | XYTempWindows::DIRECTION XYTempWindows::getDirection() const 155 | { 156 | return direction; 157 | } 158 | 159 | void XYTempWindows::setDirection(const DIRECTION &value) 160 | { 161 | direction = value; 162 | } 163 | 164 | QString XYTempWindows::getText() const 165 | { 166 | return text; 167 | } 168 | 169 | void XYTempWindows::setText(const QString &value) 170 | { 171 | text = value; 172 | } 173 | 174 | 175 | -------------------------------------------------------------------------------- /xytempwindows.h: -------------------------------------------------------------------------------- 1 | #ifndef XYTEMPWINDOWS_H 2 | #define XYTEMPWINDOWS_H 3 | 4 | #include 5 | 6 | class XYTempWindows : public QWidget 7 | { 8 | Q_OBJECT 9 | public: 10 | enum DIRECTION{BOTTOM, RIGHT, LEFT, TOP}; 11 | explicit XYTempWindows(DIRECTION direction = BOTTOM, QWidget *parent = 0); 12 | 13 | QString getText() const; 14 | void setText(const QString &value); 15 | 16 | DIRECTION getDirection() const; 17 | void setDirection(const DIRECTION &value); 18 | 19 | int getKey() const; 20 | void setKey(int value); 21 | 22 | protected: 23 | void paintEvent(QPaintEvent *event); 24 | 25 | private: 26 | QPainterPath getPainterPath(const QRect &rect, DIRECTION direction); 27 | 28 | private: 29 | DIRECTION direction; 30 | QRect textRect; 31 | 32 | int key; 33 | QString text; 34 | 35 | }; 36 | 37 | #endif // XYTEMPWINDOWS_H 38 | -------------------------------------------------------------------------------- /xyvdragabletranslateview.cpp: -------------------------------------------------------------------------------- 1 | #include "xyvdragabletranslateview.h" 2 | #include "xyskin.h" 3 | #include 4 | 5 | XYVDragableTranslateView::XYVDragableTranslateView(QWidget *parent) 6 | : XYQStringView(parent) 7 | { 8 | 9 | } 10 | 11 | void XYVDragableTranslateView::paintEvent(QPaintEvent *event) 12 | { 13 | QPainter painter(this); 14 | painter.setRenderHint(QPainter::Antialiasing); 15 | painter.setFont(XYSKIN->candidateTextFont); 16 | 17 | QFontMetrics metrics(painter.font()); 18 | int cur_x = 5; 19 | int text_w = 0; 20 | int text_h = qMax(metrics.height(), unitMinHeight); 21 | QString text; 22 | QRect text_rect; 23 | dataRects.clear(); 24 | 25 | // 先绘制按下的矩形 26 | if (!pressRect.isEmpty()) 27 | { 28 | painter.setPen(XYSKIN->candidateTextPressedBKColor); 29 | painter.setBrush(XYSKIN->candidateTextPressedBKColor); 30 | painter.drawRect(pressRect); 31 | } 32 | 33 | // 绘制所有的文字 34 | painter.setPen(XYSKIN->candidateTextPen); 35 | int cur_y = 0; 36 | for (int i = 0; i < dataStrings.size(); ++i) 37 | { 38 | text = dataStrings.at(i); 39 | text_w = metrics.width(text) + 15; 40 | if (unitMinWidth != -1) 41 | { 42 | text_w = qMax(unitMinWidth, text_w); 43 | } 44 | 45 | if (cur_x + text_w > width()) 46 | { 47 | cur_y += text_h + 5; 48 | cur_x = 5; 49 | } 50 | text_rect = QRect(cur_x, cur_y, text_w, text_h); 51 | painter.drawText(text_rect, text, QTextOption(Qt::AlignCenter)); 52 | dataRects.append(text_rect); 53 | 54 | cur_x += text_w + 13; 55 | } 56 | 57 | setFixedHeight(cur_y + text_h + 5); 58 | 59 | XYQStringView::paintEvent(event); 60 | } 61 | 62 | -------------------------------------------------------------------------------- /xyvdragabletranslateview.h: -------------------------------------------------------------------------------- 1 | #ifndef XYVDRAGABLEERTICALTRANSLATEVIEW_H 2 | #define XYVDRAGABLEERTICALTRANSLATEVIEW_H 3 | 4 | #include "xyqstringview.h" 5 | 6 | class XYVDragableTranslateView : public XYQStringView 7 | { 8 | Q_OBJECT 9 | public: 10 | explicit XYVDragableTranslateView(QWidget *parent = 0); 11 | 12 | protected: 13 | void paintEvent(QPaintEvent *event); 14 | 15 | friend class XYVirtualKeyboard; 16 | }; 17 | 18 | #endif // XYVDRAGABLEERTICALTRANSLATEVIEW_H 19 | -------------------------------------------------------------------------------- /xyvirtualkeyboard.h: -------------------------------------------------------------------------------- 1 | #ifndef XYVIRTUALKEYBOARD_H 2 | #define XYVIRTUALKEYBOARD_H 3 | 4 | #include 5 | #include 6 | 7 | class QStackedWidget; 8 | class XYHDragableTranslateView; 9 | class XYVDragableTranslateView; 10 | class XYPushButton; 11 | class XYDragableWidget; 12 | class pinyin_im; 13 | 14 | class XYMovableLabel: public QLabel 15 | { 16 | Q_OBJECT 17 | public: 18 | XYMovableLabel(QWidget *parent = Q_NULLPTR); 19 | 20 | public slots: 21 | void setText(const QString &text); 22 | void clear(); 23 | 24 | signals: 25 | void textChanged(const QString &text); 26 | 27 | protected: 28 | bool event(QEvent *event); 29 | }; 30 | 31 | class XYVirtualKeyboard : public QWidget 32 | { 33 | Q_OBJECT 34 | public: 35 | static XYVirtualKeyboard *getInstance(); 36 | void initPinyinDictionary(); 37 | void switchLanguage(); 38 | 39 | // 按键被按下 40 | signals: 41 | void triangleBtnClicked(); 42 | void keyClicked(int unicode, int key, Qt::KeyboardModifiers modifiers, bool press); 43 | 44 | public slots: 45 | bool handleQKeyEvent(QKeyEvent *event); 46 | bool keyEventHandle(int unicode, int key, Qt::KeyboardModifiers modifiers, bool press); 47 | void showLetterWidget(); 48 | void showNumberWidget(); 49 | void showSymbols(); 50 | void show(); 51 | 52 | private slots: 53 | void languageChanged(); 54 | void caseChanged(bool checked); 55 | void triangleBtnClickedOP(); 56 | void keyPressed(XYPushButton *key); 57 | void keyReleaseed(XYPushButton *key); 58 | void showTempWindow(const QString &text, const QPoint &pos); 59 | void showTempWindow(XYPushButton *key, bool press); 60 | void funcClicked(const QString &text, int index); 61 | void skinChanged(); 62 | 63 | protected: 64 | void paintEvent(QPaintEvent *event); 65 | void mousePressEvent(QMouseEvent *event); 66 | void mouseReleaseEvent(QMouseEvent *event); 67 | void mouseMoveEvent(QMouseEvent *event); 68 | bool eventFilter(QObject *obj, QEvent *event); 69 | void resizeRequest(QMouseEvent *event); 70 | 71 | private: 72 | enum ResizeType{No, Top, Bottom}; 73 | explicit XYVirtualKeyboard(QWidget *parent = Q_NULLPTR); 74 | static XYVirtualKeyboard *instance; // 单例句柄 75 | 76 | QList allShiftChangeKeys; // 字母按键 77 | XYPushButton *switchLanguageBtn; // 语言切换按钮 78 | XYDragableWidget *translateHDragableWidget; // 水平候选词拖拽控件 79 | XYDragableWidget *translateVDragableWidget; // 垂直候选词拖拽控件 80 | XYDragableWidget *symbolDragableWidget; // 符号拖拽控件 81 | XYDragableWidget *funcDragableWidget; // 功能拖拽控件 82 | QWidget *letterWidget; // 字母控件窗口 83 | QWidget *numberWidget; // 数字控件窗口 84 | QStackedWidget *stackedWidget; // 用来保存字母/数字控制的栈 85 | XYMovableLabel *letterLabel; // 输入字母显示控件,同时提供对键盘的移动操作 86 | XYHDragableTranslateView *translateHView; // 查询翻译内容展示控件 87 | XYVDragableTranslateView *translateVView; // 查询翻译内容展示控件 88 | XYVDragableTranslateView *symbolView; // 特殊符号展示控件 89 | XYHDragableTranslateView *funcHView; // 功能拖拽 90 | ResizeType resizeType; // 拉伸类型 91 | QPoint lastResizePos; // 上次拉伸位置 92 | 93 | QRect triangleBtnRect; // 记录三角按钮的矩形框 94 | bool triangleBtnPressed; // 记录三角按钮是否按下 95 | 96 | 97 | // 以下变量及接口用于中文输入 98 | signals: 99 | void send_preedit(const QString &); // send preedit string to input area 100 | void send_commit(const QString &); // send commit string to input area 101 | 102 | private slots: 103 | bool a2zkey_clicked(int unicode, int key); 104 | bool backspace_clicked(); 105 | bool space_clicked(); 106 | bool enter_clicked(); 107 | void search_begin(const QString &keywords); 108 | void search_closure(); 109 | void clear_history(); 110 | void loadLetterLabel(); 111 | QStringList loadSymbols(const QString &file); 112 | void symbolSeleted(const QString &text, int index); 113 | void userSelectChinese(const QString &text, int index); 114 | 115 | private: 116 | pinyin_im *googlePinyin; // google拼音接口类 117 | QString alreadyInputLetters; // 用户输入的所有字母 118 | QStringList alreadySelectTranslates; // 用户选择的所有中文词组 119 | 120 | }; 121 | 122 | #endif // XYVIRTUALKEYBOARD_H 123 | --------------------------------------------------------------------------------