3ed_query.h
上传用户:qdkongtiao
上传日期:2022-06-29
资源大小:356k
文件大小:4k
源码类别:

书籍源码

开发平台:

Visual C++

  1. /*
  2.  * This file contains code from "C++ Primer, Fourth Edition", by Stanley B.
  3.  * Lippman, Jose Lajoie, and Barbara E. Moo, and is covered under the
  4.  * copyright and warranty notices given in that book:
  5.  * 
  6.  * "Copyright (c) 2005 by Objectwrite, Inc., Jose Lajoie, and Barbara E. Moo."
  7.  * 
  8.  * 
  9.  * "The authors and publisher have taken care in the preparation of this book,
  10.  * but make no expressed or implied warranty of any kind and assume no
  11.  * responsibility for errors or omissions. No liability is assumed for
  12.  * incidental or consequential damages in connection with or arising out of the
  13.  * use of the information or programs contained herein."
  14.  * 
  15.  * Permission is granted for this code to be used for educational purposes in
  16.  * association with the book, given proper citation if and when posted or
  17.  * reproduced.Any commercial use of this code requires the explicit written
  18.  * permission of the publisher, Addison-Wesley Professional, a division of
  19.  * Pearson Education, Inc. Send your request for permission, stating clearly
  20.  * what code you would like to use, and in what specific way, to the following
  21.  * address: 
  22.  * 
  23.  *  Pearson Education, Inc.
  24.  *  Rights and Contracts Department
  25.  *  75 Arlington Street, Suite 300
  26.  *  Boston, MA 02216
  27.  *  Fax: (617) 848-7047
  28. */ 
  29. #ifndef TEXTQUERY_H
  30. #define TEXTQUERY_H
  31. #include <string>
  32. #include <vector>
  33. #include <map>
  34. #include <set>
  35. #include <iostream>
  36. #include <fstream>
  37. #include <cctype>
  38. #include <cstring>
  39. /*
  40.  * This version of the TextQuery class is more complicated than
  41.  * the one presented in the 4th Edition and is derived from the
  42.  * version presented in the 3rd Edition.
  43.  *
  44.  * It keeps track of character position as well as line number.
  45.  * It also processes the text and query words to:
  46.  *    strips suffixes and 
  47.  *    ignore common words that occur too frequently to bother with.
  48.  *
  49.  * However, we have updated this version to use the standard library
  50.  * facilities, thus making is simpler than the version presented in
  51.  * the 3rd edition of the Primer.  Its basic structure is similar to
  52.  * the version presented in the current edition.
  53. */
  54. class TextQuery {
  55. public:
  56.     // typedef to make declarations easier
  57.     typedef std::string::size_type str_size;
  58.     typedef std::vector<std::string>::size_type line_no;
  59.     typedef std::pair<line_no,str_size> location;
  60.     /* interface:
  61.      *    read_file builds internal data structures for the given file
  62.      *    run_query finds the given word and returns set of lines on which it appears
  63.      *    text_line returns a requested line from the input file
  64.     */
  65.     void read_file(std::ifstream &is) 
  66.                { store_file(is); build_map(); }
  67.     std::vector<location> run_query(const std::string&); 
  68.     std::string text_line(line_no line) { return lines_of_text[line]; }
  69.     str_size size() const { return lines_of_text.size(); }
  70.     void display_map();                  // debugging aid: print the map
  71. private:
  72.     void store_file(std::ifstream&);  // read and store input file
  73.     void build_map();                  // build map of each word in file
  74.     // used by store words, ignore adjacent whitespace
  75.     str_size skip_whitespace(const std::string&, str_size);
  76.     // test word and if not an excluded word update map 
  77.     void test_insert(const std::string&, str_size, str_size, line_no);
  78.     bool exclude_word(const std::string&); // test for excluded words
  79.     void strip_caps(std::string&);         // make lower case
  80.     void strip_punct(std::string&);        // remove punctuation
  81.     void strip_suffixes(std::string&);     // remove common suffixes
  82.     void suffix_s(std::string&);           // remove suffixes ending in s
  83.     int chk_ending(const std::string&, const char*); //used by suffix_s
  84.     // populate exclusion_set with words to ignoare
  85.     // Chapter 13 discusses static class members
  86.     static std::set<std::string> build_exclusion_set(); 
  87. private:
  88.     // remember the whole input file
  89.     std::vector<std::string> lines_of_text; 
  90.     // map word to vector of all the line/char positions on which it occurs
  91.     std::map< std::string, std::vector<location> > word_map;  
  92.     // set of words to ignore
  93.     static std::set<std::string> exclusion_set;
  94.     // characters that constitute whitespace
  95.     static std::string whitespace_chars;     
  96. };
  97. #endif