ModErn Text Analysis
META Enumerates Textual Applications
mapping.h
Go to the documentation of this file.
1 
10 #ifndef META_UTIL_MAPPING_H_
11 #define META_UTIL_MAPPING_H_
12 
13 #include <fstream>
14 #include <vector>
15 #include <string>
16 
17 #include "util/invertible_map.h"
18 
19 namespace meta
20 {
21 namespace map
22 {
23 
31 template <class Key, class Value, class... Args,
32  template <class, class, class...> class Map>
33 Value safe_at(const Map<Key, Value, Args...>& map, const Key& key)
34 {
35  auto it = map.find(key);
36  if (it == map.end())
37  return Value{};
38  return it->second;
39 }
40 
46 template <class Key, class Value>
47 void save_mapping(const util::invertible_map<Key, Value>& map,
48  const std::string& filename)
49 {
50  std::ofstream outfile{filename};
51  for (auto& p : map)
52  outfile << p.first << " " << p.second << "\n";
53 }
54 
61 template <class T>
62 void save_mapping(const std::vector<T>& vec, const std::string& filename)
63 {
64  std::ofstream outfile{filename};
65  for (auto& v : vec)
66  outfile << v << "\n";
67 }
68 
73 template <class Key, class Value>
74 void load_mapping(util::invertible_map<Key, Value>& map,
75  const std::string& filename)
76 {
77  std::ifstream input{filename};
78  Key k;
79  Value v;
80  while ((input >> k) && (input >> v))
81  map.insert(std::make_pair(k, v));
82 }
83 
89 template <class T>
90 void load_mapping(std::vector<T>& vec, const std::string& filename)
91 {
92  std::ifstream input{filename};
93  T val;
94  while (input >> val)
95  vec.push_back(val);
96 }
97 }
98 }
99 
100 #endif
The ModErn Text Analysis toolkit is a suite of natural language processing, classification, information retreival, data mining, and other applications of text processing.
Definition: analyzer.h:24
std::string filename(const std::string &path)
Definition: unit_test.h:114