| 1 | #include <iostream> |
| 2 | #include <ctime> |
| 3 | #include <fstream> |
| 4 | #include "cppjieba/MPSegment.hpp" |
| 5 | #include "cppjieba/HMMSegment.hpp" |
| 6 | #include "cppjieba/MixSegment.hpp" |
| 7 | #include "cppjieba/KeywordExtractor.hpp" |
| 8 | #include "test_paths.h" |
| 9 | |
| 10 | using namespace cppjieba; |
| 11 | |
| 12 | void Cut(size_t times = 50) { |
| 13 | MixSegment seg(DICT_DIR "/jieba.dict.utf8", DICT_DIR "/hmm_model.utf8"); |
| 14 | vector<string> res; |
| 15 | string doc; |
| 16 | ifstream ifs(TEST_DATA_DIR "/synthetic_doc.utf8"); |
| 17 | assert(ifs); |
| 18 | doc << ifs; |
| 19 | long beginTime = clock(); |
| 20 | for (size_t i = 0; i < times; i ++) { |
| 21 | printf("process [%3.0lf %%]\r", 100.0*(i+1)/times); |
| 22 | fflush(stdout); |
| 23 | res.clear(); |
| 24 | seg.Cut(doc, res); |
| 25 | } |
| 26 | printf("\n"); |
| 27 | long endTime = clock(); |
| 28 | printf("Cut: [%.3lf seconds]time consumed.\n", double(endTime - beginTime)/CLOCKS_PER_SEC); |
| 29 | } |
| 30 | |
| 31 | void Extract(size_t times = 400) { |
| 32 | KeywordExtractor Extractor(DICT_DIR "/jieba.dict.utf8", |
| 33 | DICT_DIR "/hmm_model.utf8", |
| 34 | DICT_DIR "/idf.utf8", |
| 35 | DICT_DIR "/stop_words.utf8"); |
| 36 | vector<string> words; |
| 37 | string doc; |
| 38 | ifstream ifs(TEST_DATA_DIR "/review.100"); |
| 39 | assert(ifs); |
| 40 | doc << ifs; |
| 41 | long beginTime = clock(); |
| 42 | for (size_t i = 0; i < times; i ++) { |
| 43 | printf("process [%3.0lf %%]\r", 100.0*(i+1)/times); |
| 44 | fflush(stdout); |
| 45 | words.clear(); |
| 46 | Extractor.Extract(doc, words, 5); |
| 47 | } |
| 48 | printf("\n"); |
| 49 | long endTime = clock(); |
| 50 | printf("Extract: [%.3lf seconds]time consumed.\n", double(endTime - beginTime)/CLOCKS_PER_SEC); |
| 51 | } |
| 52 | |
| 53 | int main(int argc, char ** argv) { |
| 54 | Cut(); |
| 55 | Extract(); |
| 56 | return EXIT_SUCCESS; |
| 57 | } |