Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #3

Merged
merged 4 commits into from
Aug 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@
*.out
*.app
build/
modules/
data/dict_pinyin.dat
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
56 changes: 56 additions & 0 deletions setupenv.sh
Original file line number Diff line number Diff line change
@@ -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"



2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
12 changes: 11 additions & 1 deletion tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
97 changes: 97 additions & 0 deletions tools/pinyin_convertor.cpp
Original file line number Diff line number Diff line change
@@ -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 <stdlib.h>
#include <stdio.h>
#include "../include/pinyinime.h"
#include <cassert>
#include <memory.h>

#ifdef WIN32
#include <Windows.h>
#include <tchar.h>
#endif

#include <locale.h>
#include <iostream>
#include <codecvt>
#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:"<<cwd;

char const *szSysDict = "../../data/dict_pinyin.dat";
char const *szUserDict = "";
if (argc >= 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<strlen(szLine); i++){
if (szLine[i] == '\n')
szLine[i] = '\0';
}
if (strlen(szLine) == 0)
break;

im_reset_search();
size_t nr = im_search(szLine, strlen(szLine)); // 查询
size_t size = 0;
printf("输入串:%s\n候选串:", im_get_sps_str(&size)); // 返回拼音串
char16 str[64] = { 0 };
for (auto i = 0; i < nr; i++)
{
im_get_candidate(i, str, 32); // 获取查询候选
if(i > 5)
break;
#ifdef WIN32
const wchar_t* szCand = (const wchar_t*)str;
wprintf(L"%s\n", szCand);
#else
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, 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;
}
17 changes: 14 additions & 3 deletions tools/pinyinime_dictbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <time.h>
#include <unistd.h>
#include "../include/dicttrie.h"
#include "glog/logging.h"
#include "glog/raw_logging.h"

using namespace ime_pinyin;

Expand All @@ -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:"<<cwd;

// 将输入的默认路径改为:libgooglepinyin/data/rawdict_utf16_65105_freq.txt
// 和 libgooglepinyin/data/valid_utf16.txt
DictTrie* dict_trie = new DictTrie();
bool success;
if (argc >= 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");
Expand All @@ -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");
Expand Down