ModErn Text Analysis
META Enumerates Textual Applications
make_index.h
Go to the documentation of this file.
1 
11 #ifndef META_MAKE_INDEX_H_
12 #define META_MAKE_INDEX_H_
13 
14 #include "cpptoml.h"
15 #include "meta/caching/all.h"
16 #include "meta/config.h"
19 #include "meta/io/filesystem.h"
20 
21 namespace meta
22 {
23 namespace index
24 {
25 
26 class inverted_index;
27 class forward_index;
28 
32 
35 
39 
43 
46 
61 template <class Index, class... Args>
62 std::shared_ptr<Index> make_index(const cpptoml::table& config,
63  corpus::corpus& docs, Args&&... args)
64 {
65  if (!config.contains("index"))
66  {
67  throw typename Index::exception{
68  "index name missing from configuration file"};
69  }
70 
71  // below is needed so that make_shared can find a public ctor to invoke
72  struct make_shared_enabler : public Index
73  {
74  make_shared_enabler(const cpptoml::table& config, Args&&... args)
75  : Index(config, std::forward<Args>(args)...)
76  {
77  // nothing
78  }
79  };
80 
81  auto idx = std::make_shared<make_shared_enabler>(
82  config, std::forward<Args>(args)...);
83 
84  // if index has already been made, load it
85  if (filesystem::exists(idx->index_name()) && idx->valid())
86  {
87  idx->load_index();
88  }
89  else
90  {
91  filesystem::remove_all(idx->index_name());
92  idx->create_index(config, docs);
93  }
94 
95  return idx;
96 }
97 
101 template <class Index, class... Args>
102 std::shared_ptr<Index> make_index(const cpptoml::table& config, Args&&... args)
103 {
104  if (!config.contains("index"))
105  {
106  throw typename Index::exception{
107  "index name missing from configuration file"};
108  }
109 
110  // below is needed so that make_shared can find a public ctor to invoke
111  struct make_shared_enabler : public Index
112  {
113  make_shared_enabler(const cpptoml::table& config, Args&&... args)
114  : Index(config, std::forward<Args>(args)...)
115  {
116  // nothing
117  }
118  };
119 
120  auto idx = std::make_shared<make_shared_enabler>(
121  config, std::forward<Args>(args)...);
122 
123  // if index has already been made, load it
124  if (filesystem::exists(idx->index_name()) && idx->valid())
125  {
126  idx->load_index();
127  }
128  else
129  {
130  filesystem::remove_all(idx->index_name());
131  auto docs = corpus::make_corpus(config);
132  idx->create_index(config, *docs);
133  }
134 
135  return idx;
136 }
137 
157 template <class Index, template <class, class> class Cache, class... Args>
158 std::shared_ptr<cached_index<Index, Cache>>
159 make_index(const cpptoml::table& config, Args&&... args)
160 {
161  return make_index<cached_index<Index, Cache>>(config,
162  std::forward<Args>(args)...);
163 }
164 }
165 }
166 
167 #endif
Decorator class for wrapping indexes with a cache.
Definition: cached_index.h:32
cached_index< inverted_index, caching::default_dblru_cache > dblru_inverted_index
Inverted index using default DBLRU cache.
Definition: make_index.h:31
Provides interface to with multiple corpus input formats.
Definition: corpus.h:58
std::unique_ptr< corpus > make_corpus(const cpptoml::table &config)
Convenience method for creating a corpus using the factory.
Definition: corpus_factory.cpp:29
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
std::shared_ptr< Index > make_index(const cpptoml::table &config, corpus::corpus &docs, Args &&... args)
Factory method for creating indexes.
Definition: make_index.h:62