| 1 | #include "cppjieba/KeywordExtractor.hpp" |
| 2 | #include "gtest/gtest.h" |
| 3 | #include "test_paths.h" |
| 4 | |
| 5 | using namespace cppjieba; |
| 6 | |
| 7 | TEST(KeywordExtractorTest, Test1) { |
| 8 | KeywordExtractor Extractor(TEST_DATA_DIR "/extra_dict/jieba.dict.small.utf8", |
| 9 | DICT_DIR "/hmm_model.utf8", |
| 10 | DICT_DIR "/idf.utf8", |
| 11 | DICT_DIR "/stop_words.utf8"); |
| 12 | |
| 13 | { |
| 14 | string s("你好世界世界而且而且"); |
| 15 | string res; |
| 16 | size_t topN = 5; |
| 17 | |
| 18 | { |
| 19 | vector<string> words; |
| 20 | Extractor.Extract(s, words, topN); |
| 21 | res << words; |
| 22 | ASSERT_EQ(res, "[\"世界\", \"你好\"]"); |
| 23 | } |
| 24 | |
| 25 | { |
| 26 | vector<pair<string, double> > words; |
| 27 | Extractor.Extract(s, words, topN); |
| 28 | res << words; |
| 29 | ASSERT_EQ(res, "[世界:8.73506, 你好:7.95788]"); |
| 30 | } |
| 31 | |
| 32 | { |
| 33 | vector<KeywordExtractor::Word> words; |
| 34 | Extractor.Extract(s, words, topN); |
| 35 | res << words; |
| 36 | ASSERT_EQ(res, "[{\"word\": \"\xE4\xB8\x96\xE7\x95\x8C\", \"offset\": [6, 12], \"weight\": 8.73506}, {\"word\": \"\xE4\xBD\xA0\xE5\xA5\xBD\", \"offset\": [0], \"weight\": 7.95788}]"); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | { |
| 41 | string s("我是拖拉机学院手扶拖拉机专业的。不用多久,我就会升职加薪,当上CEO,走上人生巅峰。"); |
| 42 | string res; |
| 43 | vector<KeywordExtractor::Word> wordweights; |
| 44 | size_t topN = 5; |
| 45 | Extractor.Extract(s, wordweights, topN); |
| 46 | res << wordweights; |
| 47 | ASSERT_EQ(res, "[{\"word\": \"CEO\", \"offset\": [93], \"weight\": 11.7392}, {\"word\": \"\xE5\x8D\x87\xE8\x81\x8C\", \"offset\": [72], \"weight\": 10.8562}, {\"word\": \"\xE5\x8A\xA0\xE8\x96\xAA\", \"offset\": [78], \"weight\": 10.6426}, {\"word\": \"\xE6\x89\x8B\xE6\x89\xB6\xE6\x8B\x96\xE6\x8B\x89\xE6\x9C\xBA\", \"offset\": [21], \"weight\": 10.0089}, {\"word\": \"\xE5\xB7\x85\xE5\xB3\xB0\", \"offset\": [111], \"weight\": 9.49396}]"); |
| 48 | } |
| 49 | |
| 50 | { |
| 51 | string s("一部iPhone6"); |
| 52 | string res; |
| 53 | vector<KeywordExtractor::Word> wordweights; |
| 54 | size_t topN = 5; |
| 55 | Extractor.Extract(s, wordweights, topN); |
| 56 | res << wordweights; |
| 57 | ASSERT_EQ(res, "[{\"word\": \"iPhone6\", \"offset\": [6], \"weight\": 11.7392}, {\"word\": \"\xE4\xB8\x80\xE9\x83\xA8\", \"offset\": [0], \"weight\": 6.47592}]"); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | TEST(KeywordExtractorTest, Test2) { |
| 62 | KeywordExtractor Extractor(TEST_DATA_DIR "/extra_dict/jieba.dict.small.utf8", |
| 63 | DICT_DIR "/hmm_model.utf8", |
| 64 | DICT_DIR "/idf.utf8", |
| 65 | DICT_DIR "/stop_words.utf8", |
| 66 | TEST_DATA_DIR "/userdict.utf8"); |
| 67 | |
| 68 | { |
| 69 | string s("蓝翔优秀毕业生"); |
| 70 | string res; |
| 71 | vector<KeywordExtractor::Word> wordweights; |
| 72 | size_t topN = 5; |
| 73 | Extractor.Extract(s, wordweights, topN); |
| 74 | res << wordweights; |
| 75 | ASSERT_EQ(res, "[{\"word\": \"\xE8\x93\x9D\xE7\xBF\x94\", \"offset\": [0], \"weight\": 11.7392}, {\"word\": \"\xE6\xAF\x95\xE4\xB8\x9A\xE7\x94\x9F\", \"offset\": [12], \"weight\": 8.13549}, {\"word\": \"\xE4\xBC\x98\xE7\xA7\x80\", \"offset\": [6], \"weight\": 6.78347}]"); |
| 76 | } |
| 77 | |
| 78 | { |
| 79 | string s("一部iPhone6"); |
| 80 | string res; |
| 81 | vector<KeywordExtractor::Word> wordweights; |
| 82 | size_t topN = 5; |
| 83 | Extractor.Extract(s, wordweights, topN); |
| 84 | res << wordweights; |
| 85 | ASSERT_EQ(res, "[{\"word\": \"iPhone6\", \"offset\": [6], \"weight\": 11.7392}, {\"word\": \"\xE4\xB8\x80\xE9\x83\xA8\", \"offset\": [0], \"weight\": 6.47592}]"); |
| 86 | } |
| 87 | } |