/** * Initialize. * Returns true if there were no problems; false if the initialization * failed. */ bool PageHandler::initialize(const std::string& resource_root) { template_cache_ = boost::shared_ptr<FileCache>(new FileCache(resource_root)); page_buffer_ = shared_array<char>(new char[page_buffer_size_]); // Create the descriptions of lists of words to keep track of. shared_ptr<WordIndexDescription> j_words( new WordIndexDescription("j_words", "J words", "^.*J.*$")); shared_ptr<WordIndexDescription> q_words( new WordIndexDescription("q_words", "Q words", ".*Q.*")); shared_ptr<WordIndexDescription> q_witout_u_words( new WordIndexDescription("q_withoutt_u_words", "Q without U words", "^(.*Q[^U].*)|(.*Q)$")); shared_ptr<WordIndexDescription> x_words( new WordIndexDescription("x_words", "X words", "^.*X.*$")); shared_ptr<WordIndexDescription> z_words( new WordIndexDescription("z_words", "Z words", "^.*Z.*$")); shared_ptr<WordIndexDescription> consonants( new WordIndexDescription("consonants", "Consonants Only", "^[^AEIOU]*$")); shared_ptr<WordIndexDescription> all_vowels_but_one( new WordIndexDescription("all_vowels_but_one", "All vowels but one", "^[AEIOU]*[^AEIOU][AEIOU]*$")); shared_ptr<WordIndexDescription> out_words( new WordIndexDescription("out_words", "OUT- words", "^OUT.*$")); shared_ptr<WordIndexDescription> re_words( new WordIndexDescription("re_words", "RE- words", "^RE.*$")); index_descriptions_.push_back(j_words); index_descriptions_.push_back(q_words); index_descriptions_.push_back(q_witout_u_words); index_descriptions_.push_back(x_words); index_descriptions_.push_back(z_words); index_descriptions_.push_back(consonants); index_descriptions_.push_back(all_vowels_but_one); index_descriptions_.push_back(out_words); index_descriptions_.push_back(re_words); //Create the word picker. word_picker_ = shared_ptr<WordPicker>(new WordPicker(index_descriptions_)); bool has_initialized_word_picker = word_picker_->initialize(resource_root + "dictionaries/owl2.txt"); //Build vairous templates. main_page_template_ = this->build_main_page_template(); about_page_ = this->insert_into_main_layout("templates/about.html"); fine_print_page_ = this->insert_into_main_layout("templates/fine-print.html"); not_found_template_ = this->insert_into_main_layout("templates/404.html"); //Attach to the server. server_->add_url_handler("/", &main_page, (void*) this); server_->add_url_handler("/about/?", &about, (void*) this); server_->add_url_handler("/fine_print/?", &fine_print, (void*) this); server_->add_url_handler("/words/[a-z0-9/_]+", &words, (void*) this); server_->set_not_found_handler(¬_found, this); return has_initialized_word_picker; }
int main(int argc, char **argv, char **envp) { char ts[1000]; FILE *fp; size_t len; int cnt; char *bm = calloc(1, 0xFFFFFFFF / 0x8); /* about 512MB */ #if 1 cnt = 0; if ((fp = fopen("dict.txt", "r"))) { while (fgets(ts, 1000, fp)) { len = strlen(ts)-2; ts[len] = '\0'; if (len <= MAX_PHRASE_S_LEN) { cnt++; word_set(bm, ts); } } printf("%d words seted\n", cnt); } else { printf("open dict.txt failed\n"); return 1; } #endif #if 0 word_set(bm, "专场"); word_set(bm, "专机"); #endif if (argc > 1) { memset(words, sizeof(words), 0x0); wpos = 0; struct timeval tv_s, tv_e; unsigned long usec; gettimeofday(&tv_s, NULL); word_split(bm, argv[1], strlen(argv[1]), on_words); gettimeofday(&tv_e, NULL); usec = (tv_e.tv_sec - tv_s.tv_sec) * 1000000ul + (tv_e.tv_usec - tv_s.tv_usec); printf("parse finished in %.6f seconds\n", (double)(usec / 1000000.0)); printf("words:\n"); out_words(); } else { unsigned int i; cnt = 0; for (i = 0; i < 0xFFFFFFFF; i++) { if (BITMAP_GET(bm, i)) cnt++; } printf("%d distinct word\n", cnt); } free(bm); return 0; }