Skip to content

Commit bbe1f01

Browse files
authored
Merge pull request #3 from palanceli/dev
Dev
2 parents 1317f63 + c2ad732 commit bbe1f01

7 files changed

+192
-4
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@
3131
*.out
3232
*.app
3333
build/
34+
modules/
35+
data/dict_pinyin.dat

CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ cmake_minimum_required(VERSION 2.6)
33

44
option(ENABLE_STATIC "Build static library" True)
55

6+
# 添加模块gflag、glog、GTest
7+
find_package(gflags REQUIRED)
8+
include_directories (${gflags_INCLUDE_DIR})
9+
10+
find_package(glog REQUIRED)
11+
include_directories(${GLOG_INCLUDE_DIRS})
12+
13+
find_package(GTest REQUIRED)
14+
include_directories(${GTEST_INCLUDE_DIRS})
15+
616
# uninstall target
717
configure_file(
818
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in"

setupenv.sh

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
3+
4+
# Helper for adding a directory to the stack and echoing the result
5+
enterDir() {
6+
echo "Entering $1"
7+
pushd $1 > /dev/null
8+
}
9+
10+
# Helper for popping a directory off the stack and echoing the result
11+
leaveDir() {
12+
echo "Leaving `pwd`"
13+
popd > /dev/null
14+
}
15+
16+
17+
function clone_proj {
18+
local proj_url=$1
19+
local local_dir=$2
20+
local branch=$3
21+
git clone $proj_url $local_dir
22+
enterDir $local_dir
23+
git checkout $branch
24+
leaveDir
25+
}
26+
27+
function build_install_module {
28+
local module_dir=$1
29+
local module_build_dir=$module_dir/_build
30+
mkdir $module_build_dir
31+
enterDir $module_dir/_build
32+
cmake .. && make && make install
33+
leaveDir
34+
}
35+
36+
if [ ! -d "modules" ]; then
37+
mkdir modules
38+
fi
39+
40+
if [ ! -d "modules/gflags" ]; then
41+
clone_proj "https://github.com/gflags/gflags.git" "modules/gflags" "v2.2.2"
42+
fi
43+
build_install_module "modules/gflags"
44+
45+
if [ ! -d "modules/gtest" ]; then
46+
clone_proj "https://github.com/google/glog.git" "modules/glog" "v0.4.0"
47+
fi
48+
build_install_module "modules/glog"
49+
50+
if [ ! -d "modules/googletest" ]; then
51+
clone_proj "https://github.com/google/googletest.git" "modules/googletest" "release-1.8.1"
52+
fi
53+
build_install_module "modules/googletest"
54+
55+
56+

src/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ set_target_properties(googlepinyin PROPERTIES
3030
LINK_FLAGS ${CMAKE_SHARED_LINKER_FLAGS}
3131
)
3232

33+
# 设置为静态链接
34+
set(ENABLE_STATIC 1)
3335
if (ENABLE_STATIC)
3436

3537
add_library(googlepinyin-static STATIC ${GOOGLEPINYIN_SRC})

tools/CMakeLists.txt

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,15 @@ set(DICTBUILDER_SRCS
33
)
44

55
add_executable(dictbuilder ${DICTBUILDER_SRCS})
6-
target_link_libraries(dictbuilder googlepinyin)
76

7+
set(LINKER_LIBS "")
8+
list(APPEND LINKER_LIBS glog::glog)
9+
list(APPEND LINKER_LIBS googlepinyin-static)
10+
list(APPEND LINKER_LIBS ${GTEST_BOTH_LIBRARIES})
11+
target_link_libraries(dictbuilder ${LINKER_LIBS})
12+
13+
# target_link_libraries(dictbuilder googlepinyin)
14+
15+
# 添加拼音转换工具
16+
add_executable(pinyin_conv pinyin_convertor.cpp)
17+
target_link_libraries(pinyin_conv ${LINKER_LIBS})

tools/pinyin_convertor.cpp

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}

tools/pinyinime_dictbuilder.cpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <time.h>
2121
#include <unistd.h>
2222
#include "../include/dicttrie.h"
23+
#include "glog/logging.h"
24+
#include "glog/raw_logging.h"
2325

2426
using namespace ime_pinyin;
2527

@@ -28,13 +30,21 @@ using namespace ime_pinyin;
2830
* in dictdef.h.
2931
*/
3032
int main(int argc, char* argv[]) {
33+
FLAGS_logtostderr = 1; // 输出到控制台
34+
35+
char buffer[1024] = {0};
36+
char *cwd = getwd(buffer);
37+
LOG(INFO)<<"current dir is:"<<cwd;
38+
39+
// 将输入的默认路径改为:libgooglepinyin/data/rawdict_utf16_65105_freq.txt
40+
// 和 libgooglepinyin/data/valid_utf16.txt
3141
DictTrie* dict_trie = new DictTrie();
3242
bool success;
3343
if (argc >= 3)
3444
success = dict_trie->build_dict(argv[1], argv[2]);
3545
else
36-
success = dict_trie->build_dict("../data/rawdict_utf16_65105_freq.txt",
37-
"../data/valid_utf16.txt");
46+
success = dict_trie->build_dict("../../../data/rawdict_utf16_65105_freq.txt",
47+
"../../../data/valid_utf16.txt");
3848

3949
if (success) {
4050
printf("Build dictionary successfully.\n");
@@ -43,10 +53,11 @@ int main(int argc, char* argv[]) {
4353
return -1;
4454
}
4555

56+
// 将输出路径改为:libgooglepinyin/build/data/dict_pinyin.dat
4657
if (argc >= 4)
4758
success = dict_trie->save_dict(argv[3]);
4859
else
49-
success = dict_trie->save_dict("dict_pinyin.dat");
60+
success = dict_trie->save_dict("../../data/dict_pinyin.dat");
5061

5162
if (success) {
5263
printf("Save dictionary successfully.\n");

0 commit comments

Comments
 (0)