|
| 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 <stdlib.h> |
| 18 | +#include <stdio.h> |
| 19 | +#include "../include/pinyinime.h" |
| 20 | +#include <cassert> |
| 21 | +#include <memory.h> |
| 22 | + |
| 23 | +#ifdef WIN32 |
| 24 | +#include <Windows.h> |
| 25 | +#include <tchar.h> |
| 26 | +#endif |
| 27 | + |
| 28 | +#include <locale.h> |
| 29 | +#include <iostream> |
| 30 | +#include <codecvt> |
| 31 | +#include "glog/logging.h" |
| 32 | +#include "glog/raw_logging.h" |
| 33 | + |
| 34 | +using namespace ime_pinyin; |
| 35 | + |
| 36 | +int main(int argc, char* argv[]) |
| 37 | +{ |
| 38 | + setlocale(LC_ALL, ""); |
| 39 | + char buffer[1024] = {0}; |
| 40 | + char *cwd = getwd(buffer); |
| 41 | + LOG(INFO)<<"current dir is:"<<cwd; |
| 42 | + |
| 43 | + char const *szSysDict = "../../data/dict_pinyin.dat"; |
| 44 | + char const *szUserDict = ""; |
| 45 | + if (argc >= 3) { |
| 46 | + szSysDict = argv[1]; |
| 47 | + szUserDict = argv[2]; |
| 48 | + } |
| 49 | + |
| 50 | + bool ret = im_open_decoder(szSysDict, szUserDict); // 加载 |
| 51 | + assert(ret); |
| 52 | + im_set_max_lens(32, 16); |
| 53 | + char szLine[256]; |
| 54 | + |
| 55 | + while (true) { |
| 56 | + printf("\n >"); |
| 57 | +#ifdef WIN32 |
| 58 | + gets_s(szLine, 256); |
| 59 | +#else |
| 60 | + fgets(szLine, 256, stdin); |
| 61 | +#endif |
| 62 | + // 剪掉结尾的回车符号 |
| 63 | + for(auto i=0; i<strlen(szLine); i++){ |
| 64 | + if (szLine[i] == '\n') |
| 65 | + szLine[i] = '\0'; |
| 66 | + } |
| 67 | + if (strlen(szLine) == 0) |
| 68 | + break; |
| 69 | + |
| 70 | + im_reset_search(); |
| 71 | + size_t nr = im_search(szLine, strlen(szLine)); // 查询 |
| 72 | + size_t size = 0; |
| 73 | + printf("输入串:%s\n候选串:", im_get_sps_str(&size)); // 返回拼音串 |
| 74 | + char16 str[64] = { 0 }; |
| 75 | + for (auto i = 0; i < nr; i++) |
| 76 | + { |
| 77 | + im_get_candidate(i, str, 32); // 获取查询候选 |
| 78 | + if(i > 5) |
| 79 | + break; |
| 80 | +#ifdef WIN32 |
| 81 | + const wchar_t* szCand = (const wchar_t*)str; |
| 82 | + wprintf(L"%s\n", szCand); |
| 83 | +#else |
| 84 | + std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert; |
| 85 | + std::string strCand = convert.to_bytes((char16_t*)str); |
| 86 | + std::cout << i << "." << strCand << " "; |
| 87 | +#endif |
| 88 | + } |
| 89 | + if(nr>5) |
| 90 | + printf("..."); |
| 91 | + printf(" 共%lu个候选\n", nr); |
| 92 | + } |
| 93 | + |
| 94 | + im_close_decoder(); // 关闭 |
| 95 | + |
| 96 | + return 0; |
| 97 | +} |
0 commit comments