Debian Codegraph @

plugins/jieba/deps/cppjieba/test/unittest/unicode_path_test.cpp

raw symbols SCIP
1 #include <cerrno>
2 #include <cstdio>
3 #include <fstream>
4
5 #ifdef _WIN32
6 #include <direct.h>
7 #else
8 #include <sys/stat.h>
9 #include <sys/types.h>
10 #endif
11
12 #include "cppjieba/UnicodeFile.hpp"
13 #include "cppjieba/Jieba.hpp"
14 #include "cppjieba/KeywordExtractor.hpp"
15 #include "cppjieba/TextRankExtractor.hpp"
16 #include "gtest/gtest.h"
17 #include "test_paths.h"
18
19 using namespace cppjieba;
20
21 namespace {
22
23 string TestPathJoin(const string& lhs, const string& rhs) {
24 return lhs + "/" + rhs;
25 }
26
27 void MakeDir(const string& path) {
28 #ifdef _WIN32
29 std::wstring widePath;
30 ASSERT_TRUE(Utf8ToWidePath(path, widePath)) << path;
31 ASSERT_TRUE(_wmkdir(widePath.c_str()) == 0 || errno == EEXIST) << path;
32 #else
33 ASSERT_TRUE(mkdir(path.c_str(), 0755) == 0 || errno == EEXIST) << path;
34 #endif
35 }
36
37 void OpenOutputFile(std::ofstream& ofs, const string& path) {
38 #ifdef _WIN32
39 std::wstring widePath;
40 ASSERT_TRUE(Utf8ToWidePath(path, widePath)) << path;
41 ofs.open(widePath.c_str(), std::ios::binary);
42 #else
43 ofs.open(path.c_str(), std::ios::binary);
44 #endif
45 }
46
47 void CopyFile(const string& src, const string& dst) {
48 std::ifstream ifs;
49 OpenInputFile(ifs, src);
50 ASSERT_TRUE(ifs.is_open()) << src;
51 std::ofstream ofs;
52 OpenOutputFile(ofs, dst);
53 ASSERT_TRUE(ofs.is_open()) << dst;
54 ofs << ifs.rdbuf();
55 ASSERT_TRUE(ofs.good()) << dst;
56 }
57
58 string PrepareUnicodeDictDir() {
59 const string baseDir = "./cppjieba_unicode_path_test";
60 const string unicodeDir = TestPathJoin(baseDir, "中文路径");
61 const string dictDir = TestPathJoin(unicodeDir, "dict");
62 MakeDir(baseDir);
63 MakeDir(unicodeDir);
64 MakeDir(dictDir);
65
66 CopyFile(DICT_DIR "/jieba.dict.utf8", TestPathJoin(dictDir, "jieba.dict.utf8"));
67 CopyFile(DICT_DIR "/hmm_model.utf8", TestPathJoin(dictDir, "hmm_model.utf8"));
68 CopyFile(DICT_DIR "/user.dict.utf8", TestPathJoin(dictDir, "user.dict.utf8"));
69 CopyFile(DICT_DIR "/idf.utf8", TestPathJoin(dictDir, "idf.utf8"));
70 CopyFile(DICT_DIR "/stop_words.utf8", TestPathJoin(dictDir, "stop_words.utf8"));
71 CopyFile(TEST_DATA_DIR "/extra_dict/jieba.dict.small.utf8", TestPathJoin(dictDir, "jieba.dict.small.utf8"));
72 return dictDir;
73 }
74
75 } // namespace
76
77 TEST(UnicodePathTest, LoadsResourcesFromUnicodePath) {
78 const string dictDir = PrepareUnicodeDictDir();
79
80 {
81 cppjieba::Jieba jieba(TestPathJoin(dictDir, "jieba.dict.utf8"),
82 TestPathJoin(dictDir, "hmm_model.utf8"),
83 TestPathJoin(dictDir, "user.dict.utf8"),
84 TestPathJoin(dictDir, "idf.utf8"),
85 TestPathJoin(dictDir, "stop_words.utf8"));
86 vector<string> words;
87 string result;
88 jieba.Cut("他来到了网易杭研大厦", words);
89 result << words;
90 ASSERT_EQ("[\"他\", \"来到\", \"了\", \"网易\", \"杭研\", \"大厦\"]", result);
91 }
92
93 {
94 KeywordExtractor extractor(TestPathJoin(dictDir, "jieba.dict.small.utf8"),
95 TestPathJoin(dictDir, "hmm_model.utf8"),
96 TestPathJoin(dictDir, "idf.utf8"),
97 TestPathJoin(dictDir, "stop_words.utf8"));
98 vector<string> words;
99 extractor.Extract("你好世界世界而且而且", words, 5);
100 ASSERT_EQ(2u, words.size());
101 ASSERT_EQ("世界", words[0]);
102 ASSERT_EQ("你好", words[1]);
103 }
104
105 {
106 TextRankExtractor extractor(TestPathJoin(dictDir, "jieba.dict.small.utf8"),
107 TestPathJoin(dictDir, "hmm_model.utf8"),
108 TestPathJoin(dictDir, "stop_words.utf8"));
109 vector<string> words;
110 extractor.Extract("你好世界世界而且而且", words, 5);
111 ASSERT_EQ(2u, words.size());
112 ASSERT_EQ("世界", words[0]);
113 ASSERT_EQ("你好", words[1]);
114 }
115 }