diff --git a/.gitignore b/.gitignore index de7f0f3..8e10fbd 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,5 @@ *.out *.app build/ +modules/ +data/dict_pinyin.dat diff --git a/CMakeLists.txt b/CMakeLists.txt index 13273ba..8d69090 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,16 @@ cmake_minimum_required(VERSION 2.6) option(ENABLE_STATIC "Build static library" True) +# 添加模块gflag、glog、GTest +find_package(gflags REQUIRED) +include_directories (${gflags_INCLUDE_DIR}) + +find_package(glog REQUIRED) +include_directories(${GLOG_INCLUDE_DIRS}) + +find_package(GTest REQUIRED) +include_directories(${GTEST_INCLUDE_DIRS}) + # uninstall target configure_file( "${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in" diff --git a/setupenv.sh b/setupenv.sh new file mode 100644 index 0000000..57f71cc --- /dev/null +++ b/setupenv.sh @@ -0,0 +1,56 @@ +#!/bin/bash + + +# Helper for adding a directory to the stack and echoing the result +enterDir() { + echo "Entering $1" + pushd $1 > /dev/null +} + +# Helper for popping a directory off the stack and echoing the result +leaveDir() { + echo "Leaving `pwd`" + popd > /dev/null +} + + +function clone_proj { + local proj_url=$1 + local local_dir=$2 + local branch=$3 + git clone $proj_url $local_dir + enterDir $local_dir + git checkout $branch + leaveDir +} + +function build_install_module { + local module_dir=$1 + local module_build_dir=$module_dir/_build + mkdir $module_build_dir + enterDir $module_dir/_build + cmake .. && make && make install + leaveDir +} + +if [ ! -d "modules" ]; then + mkdir modules +fi + +if [ ! -d "modules/gflags" ]; then + clone_proj "https://github.com/gflags/gflags.git" "modules/gflags" "v2.2.2" +fi +build_install_module "modules/gflags" + +if [ ! -d "modules/gtest" ]; then + clone_proj "https://github.com/google/glog.git" "modules/glog" "v0.4.0" +fi +build_install_module "modules/glog" + +if [ ! -d "modules/googletest" ]; then + clone_proj "https://github.com/google/googletest.git" "modules/googletest" "release-1.8.1" +fi +build_install_module "modules/googletest" + + + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5b7b9a6..e549333 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -30,6 +30,8 @@ set_target_properties(googlepinyin PROPERTIES LINK_FLAGS ${CMAKE_SHARED_LINKER_FLAGS} ) +# 设置为静态链接 +set(ENABLE_STATIC 1) if (ENABLE_STATIC) add_library(googlepinyin-static STATIC ${GOOGLEPINYIN_SRC}) diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 374d7a2..e50c23e 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -3,5 +3,15 @@ set(DICTBUILDER_SRCS ) add_executable(dictbuilder ${DICTBUILDER_SRCS}) -target_link_libraries(dictbuilder googlepinyin) +set(LINKER_LIBS "") +list(APPEND LINKER_LIBS glog::glog) +list(APPEND LINKER_LIBS googlepinyin-static) +list(APPEND LINKER_LIBS ${GTEST_BOTH_LIBRARIES}) +target_link_libraries(dictbuilder ${LINKER_LIBS}) + +# target_link_libraries(dictbuilder googlepinyin) + +# 添加拼音转换工具 +add_executable(pinyin_conv pinyin_convertor.cpp) +target_link_libraries(pinyin_conv ${LINKER_LIBS}) \ No newline at end of file diff --git a/tools/pinyin_convertor.cpp b/tools/pinyin_convertor.cpp new file mode 100644 index 0000000..aad4484 --- /dev/null +++ b/tools/pinyin_convertor.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include "../include/pinyinime.h" +#include +#include + +#ifdef WIN32 +#include +#include +#endif + +#include +#include +#include +#include "glog/logging.h" +#include "glog/raw_logging.h" + +using namespace ime_pinyin; + +int main(int argc, char* argv[]) +{ + setlocale(LC_ALL, ""); + char buffer[1024] = {0}; + char *cwd = getwd(buffer); + LOG(INFO)<<"current dir is:"<= 3) { + szSysDict = argv[1]; + szUserDict = argv[2]; + } + + bool ret = im_open_decoder(szSysDict, szUserDict); // 加载 + assert(ret); + im_set_max_lens(32, 16); + char szLine[256]; + + while (true) { + printf("\n >"); +#ifdef WIN32 + gets_s(szLine, 256); +#else + fgets(szLine, 256, stdin); +#endif + // 剪掉结尾的回车符号 + for(auto i=0; i 5) + break; +#ifdef WIN32 + const wchar_t* szCand = (const wchar_t*)str; + wprintf(L"%s\n", szCand); +#else + std::wstring_convert, char16_t> convert; + std::string strCand = convert.to_bytes((char16_t*)str); + std::cout << i << "." << strCand << " "; +#endif + } + if(nr>5) + printf("..."); + printf(" 共%lu个候选\n", nr); + } + + im_close_decoder(); // 关闭 + + return 0; +} diff --git a/tools/pinyinime_dictbuilder.cpp b/tools/pinyinime_dictbuilder.cpp index a79b9eb..cc550ff 100644 --- a/tools/pinyinime_dictbuilder.cpp +++ b/tools/pinyinime_dictbuilder.cpp @@ -20,6 +20,8 @@ #include #include #include "../include/dicttrie.h" +#include "glog/logging.h" +#include "glog/raw_logging.h" using namespace ime_pinyin; @@ -28,13 +30,21 @@ using namespace ime_pinyin; * in dictdef.h. */ int main(int argc, char* argv[]) { + FLAGS_logtostderr = 1; // 输出到控制台 + + char buffer[1024] = {0}; + char *cwd = getwd(buffer); + LOG(INFO)<<"current dir is:"<= 3) success = dict_trie->build_dict(argv[1], argv[2]); else - success = dict_trie->build_dict("../data/rawdict_utf16_65105_freq.txt", - "../data/valid_utf16.txt"); + success = dict_trie->build_dict("../../../data/rawdict_utf16_65105_freq.txt", + "../../../data/valid_utf16.txt"); if (success) { printf("Build dictionary successfully.\n"); @@ -43,10 +53,11 @@ int main(int argc, char* argv[]) { return -1; } + // 将输出路径改为:libgooglepinyin/build/data/dict_pinyin.dat if (argc >= 4) success = dict_trie->save_dict(argv[3]); else - success = dict_trie->save_dict("dict_pinyin.dat"); + success = dict_trie->save_dict("../../data/dict_pinyin.dat"); if (success) { printf("Save dictionary successfully.\n");