WordFreq
parser.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include <map> // we want an ordered map
5 #include <string>
6 #include <vector>
7 
13 class Parser {
14 private:
15  static constexpr char delimeters[] = " \".,:;!?<>{}[]()_=+^%*&#";
16  std::string mOutputFilePath;
17  std::string mInputFilePath;
18  std::map<std::string, size_t> *mGlobalFreqencyMap;
19  std::map<std::string, size_t> mLocalFreqencyMap;
20  void addToMap(std::string word, std::map<std::string, size_t> &freqencyMap);
21  void addToMap(std::string word);
22  void parseLine(const std::string &line);
23 
24 public:
25  Parser(const std::string &path,
26  std::map<std::string, size_t> *globalFreqencyMap = nullptr);
27  bool parse();
28  void exportToFile();
29  friend std::ostream &operator<<(std::ostream &out, const Parser &aDis);
30 };
31 
38  std::map<std::string, size_t> globalFreqencyMap;
39  std::vector<Parser> parsers;
40  void exportGlobal(std::ostream &out = std::cout);
41 };
42 
46 class StringHelper {
47  StringHelper() = delete;
48 
49 public:
50  static void toLower(std::string &word);
51 };
52 
53 template <typename K, typename V>
54 std::ostream &operator<<(std::ostream &os, const std::map<K, V> &m) {
55  for (const std::pair<K, V> &p : m) {
56  os << "{" << p.first << ": " << p.second << "}\n";
57  }
58  return os;
59 }
60 
61 inline std::ostream &operator<<(std::ostream &out, const Parser &parser) {
62  out << parser.mLocalFreqencyMap;
63  return out;
64 }
Parser class that tokenizes words and removes special characters.
Definition: parser.h:13
friend std::ostream & operator<<(std::ostream &out, const Parser &aDis)
Definition: parser.h:61
bool parse()
A function that creates a file stream from a path and parses it for word frequency.
Definition: parser.cpp:23
Parser(const std::string &path, std::map< std::string, size_t > *globalFreqencyMap=nullptr)
Initializes a Parser with an input and export file.
Definition: parser.cpp:11
void exportToFile()
A function that exports the per parser frequency map.
Definition: parser.cpp:106
StringHelper provides static helper functions to manipulate strings.
Definition: parser.h:46
static void toLower(std::string &word)
A function that lowers all characters in a string.
Definition: parser.cpp:63
std::ostream & operator<<(std::ostream &os, const std::map< K, V > &m)
Definition: parser.h:54
Common data structures needed to support cross file frequency analysis.
Definition: parser.h:37
std::map< std::string, size_t > globalFreqencyMap
Definition: parser.h:38
std::vector< Parser > parsers
Definition: parser.h:39
void exportGlobal(std::ostream &out=std::cout)
A function that exports the combined frequency map.
Definition: parser.cpp:119