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 <string>
15 #include <vector>
16 
17 #include "meta/config.h"
19 
20 namespace meta
21 {
22 namespace map
23 {
24 
32 template <class Key, class Value, class... Args,
33  template <class, class, class...> class Map>
34 Value safe_at(const Map<Key, Value, Args...>& map, const Key& key)
35 {
36  auto it = map.find(key);
37  if (it == map.end())
38  return Value{};
39  return it->second;
40 }
41 
47 template <class Key, class Value>
49  const std::string& filename)
50 {
51  std::ofstream outfile{filename};
52  for (auto& p : map)
53  outfile << p.first << " " << p.second << "\n";
54 }
55 
62 template <class T>
63 void save_mapping(const std::vector<T>& vec, const std::string& filename)
64 {
65  std::ofstream outfile{filename};
66  for (auto& v : vec)
67  outfile << v << "\n";
68 }
69 
74 template <class Key, class Value>
76  const std::string& filename)
77 {
78  std::ifstream input{filename};
79  Key k;
80  Value v;
81  while ((input >> k) && (input >> v))
82  map.insert(std::make_pair(k, v));
83 }
84 
90 template <class T>
91 void load_mapping(std::vector<T>& vec, const std::string& filename)
92 {
93  std::ifstream input{filename};
94  T val;
95  while (input >> val)
96  vec.push_back(val);
97 }
98 }
99 }
100 
101 #endif
void load_mapping(util::invertible_map< Key, Value > &map, const std::string &filename)
Definition: mapping.h:75
The ModErn Text Analysis toolkit is a suite of natural language processing, classification, information retrieval, data mining, and other applications of text processing.
Definition: analyzer.h:25
void save_mapping(const util::invertible_map< Key, Value > &map, const std::string &filename)
Saves any arbitrary mapping to the disk.
Definition: mapping.h:48
void insert(const Key &key, const Value &value)
Inserts a (key, value) pair into the invertible map.
Definition: invertible_map.tcc:69
This data structure indexes by keys as well as values, allowing constant amortized lookup time by key...
Definition: invertible_map.h:32
Value safe_at(const Map< Key, Value, Args... > &map, const Key &key)
This safe_at allows the use of a hash function to be specified.
Definition: mapping.h:34