| 1 | #include "cppjieba/TextRankExtractor.hpp" |
| 2 | #include "gtest/gtest.h" |
| 3 | #include "test_paths.h" |
| 4 | |
| 5 | using namespace cppjieba; |
| 6 | |
| 7 | TEST(TextRankExtractorTest, Test1) { |
| 8 | TextRankExtractor Extractor( |
| 9 | TEST_DATA_DIR "/extra_dict/jieba.dict.small.utf8", |
| 10 | DICT_DIR "/hmm_model.utf8", |
| 11 | DICT_DIR "/stop_words.utf8"); |
| 12 | { |
| 13 | string s("你好世界世界而且而且"); |
| 14 | string res; |
| 15 | size_t topN = 5; |
| 16 | |
| 17 | { |
| 18 | vector<string> words; |
| 19 | Extractor.Extract(s, words, topN); |
| 20 | res << words; |
| 21 | ASSERT_EQ(res, "[\"世界\", \"你好\"]"); |
| 22 | } |
| 23 | |
| 24 | { |
| 25 | vector<pair<string, double> > words; |
| 26 | Extractor.Extract(s, words, topN); |
| 27 | res << words; |
| 28 | ASSERT_EQ(res, "[世界:1, 你好:0.519787]"); |
| 29 | } |
| 30 | |
| 31 | { |
| 32 | vector<TextRankExtractor::Word> words; |
| 33 | Extractor.Extract(s, words, topN); |
| 34 | res << words; |
| 35 | ASSERT_EQ(res, "[{\"word\": \"世界\", \"offset\": [6, 12], \"weight\": 1}, {\"word\": \"你好\", \"offset\": [0], \"weight\": 0.519787}]"); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | { |
| 40 | string s("\xe6\x88\x91\xe6\x98\xaf\xe6\x8b\x96\xe6\x8b\x89\xe6\x9c\xba\xe5\xad\xa6\xe9\x99\xa2\xe6\x89\x8b\xe6\x89\xb6\xe6\x8b\x96\xe6\x8b\x89\xe6\x9c\xba\xe4\xb8\x93\xe4\xb8\x9a\xe7\x9a\x84\xe3\x80\x82\xe4\xb8\x8d\xe7\x94\xa8\xe5\xa4\x9a\xe4\xb9\x85\xef\xbc\x8c\xe6\x88\x91\xe5\xb0\xb1\xe4\xbc\x9a\xe5\x8d\x87\xe8\x81\x8c\xe5\x8a\xa0\xe8\x96\xaa\xef\xbc\x8c\xe5\xbd\x93\xe4\xb8\x8a CEO\xef\xbc\x8c\xe8\xb5\xb0\xe4\xb8\x8a\xe4\xba\xba\xe7\x94\x9f\xe5\xb7\x85\xe5\xb3\xb0"); |
| 41 | string res; |
| 42 | vector<TextRankExtractor::Word> wordweights; |
| 43 | size_t topN = 5; |
| 44 | Extractor.Extract(s, wordweights, topN); |
| 45 | res << wordweights; |
| 46 | ASSERT_EQ(res, "[{\"word\": \"当上\", \"offset\": [87], \"weight\": 1}, {\"word\": \"不用\", \"offset\": [48], \"weight\": 0.989848}, {\"word\": \"多久\", \"offset\": [54], \"weight\": 0.985126}, {\"word\": \"加薪\", \"offset\": [78], \"weight\": 0.983046}, {\"word\": \"升职\", \"offset\": [72], \"weight\": 0.980278}]"); |
| 47 | //ASSERT_EQ(res, "[{\"word\": \"专业\", \"offset\": [36], \"weight\": 1}, {\"word\": \"CEO\", \"offset\": [94], \"weight\": 0.95375}, {\"word\": \"手扶拖拉机\", \"offset\": [21], \"weight\": 0.801701}, {\"word\": \"当上\", \"offset\": [87], \"weight\": 0.798968}, {\"word\": \"走上\", \"offset\": [100], \"weight\": 0.775505}]"); |
| 48 | } |
| 49 | |
| 50 | { |
| 51 | string s("一部iPhone6"); |
| 52 | string res; |
| 53 | vector<TextRankExtractor::Word> wordweights; |
| 54 | size_t topN = 5; |
| 55 | Extractor.Extract(s, wordweights, topN); |
| 56 | res << wordweights; |
| 57 | ASSERT_EQ(res, "[{\"word\": \"一部\", \"offset\": [0], \"weight\": 1}, {\"word\": \"iPhone6\", \"offset\": [6], \"weight\": 0.996126}]"); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | TEST(TextRankExtractorTest, Test2) { |
| 62 | TextRankExtractor Extractor( |
| 63 | TEST_DATA_DIR "/extra_dict/jieba.dict.small.utf8", |
| 64 | DICT_DIR "/hmm_model.utf8", |
| 65 | DICT_DIR "/stop_words.utf8", |
| 66 | TEST_DATA_DIR "/userdict.utf8"); |
| 67 | |
| 68 | { |
| 69 | string s("\xe8\x93\x9d\xe7\xbf\x94\xe4\xbc\x98\xe7\xa7\x80\xe6\xaf\x95\xe4\xb8\x9a\xe7\x94\x9f"); |
| 70 | string res; |
| 71 | vector<TextRankExtractor::Word> wordweights; |
| 72 | size_t topN = 5; |
| 73 | Extractor.Extract(s, wordweights, topN); |
| 74 | res << wordweights; |
| 75 | ASSERT_EQ(res, "[{\"word\": \"蓝翔\", \"offset\": [0], \"weight\": 1}, {\"word\": \"毕业生\", \"offset\": [12], \"weight\": 0.996685}, {\"word\": \"优秀\", \"offset\": [6], \"weight\": 0.992994}]"); |
| 76 | } |
| 77 | |
| 78 | { |
| 79 | string s("一部iPhone6"); |
| 80 | string res; |
| 81 | vector<TextRankExtractor::Word> wordweights; |
| 82 | size_t topN = 5; |
| 83 | Extractor.Extract(s, wordweights, topN); |
| 84 | res << wordweights; |
| 85 | ASSERT_EQ(res, "[{\"word\": \"一部\", \"offset\": [0], \"weight\": 1}, {\"word\": \"iPhone6\", \"offset\": [6], \"weight\": 0.996126}]"); |
| 86 | } |
| 87 | } |