std::unique_ptr<analyzer> load(const cpptoml::table& config) { using namespace analyzers; std::vector<std::unique_ptr<analyzer>> toks; auto analyzers = config.get_table_array("analyzers"); for (auto group : analyzers->get()) { auto method = group->get_as<std::string>("method"); if (!method) throw analyzer_exception{"failed to find analyzer method"}; toks.emplace_back( analyzer_factory::get().create(*method, config, *group)); } return make_unique<multi_analyzer>(std::move(toks)); }
metadata::schema_type metadata_schema(const cpptoml::table& config) { metadata::schema_type schema; if (auto metadata = config.get_table_array("metadata")) { const auto& arr = metadata->get(); schema.reserve(arr.size()); for (const auto& table : arr) { auto name = table->get_as<std::string>("name"); auto type = table->get_as<std::string>("type"); if (!name) throw metadata_exception{"name needed for metadata field"}; if (!type) throw metadata_exception{"type needed for metadata field"}; metadata::field_type ftype; if (*type == "int") { ftype = metadata::field_type::SIGNED_INT; } else if (*type == "uint") { ftype = metadata::field_type::UNSIGNED_INT; } else if (*type == "double") { ftype = metadata::field_type::DOUBLE; } else if (*type == "string") { ftype = metadata::field_type::STRING; } else { throw metadata_exception{"invalid metadata type: \"" + *type + "\""}; } schema.emplace_back(*name, ftype); } } return schema; }
std::unique_ptr<token_stream> load_filters(const cpptoml::table& global, const cpptoml::table& config) { auto check = config.get_as<std::string>("filter"); if (check) { if (*check == "default-chain") return default_filter_chain(global); else if (*check == "default-unigram-chain") return default_unigram_chain(global); else throw analyzer_exception{"unknown filter option: " + *check}; } auto filters = config.get_table_array("filter"); if (!filters) throw analyzer_exception{"analyzer group missing filter configuration"}; std::unique_ptr<token_stream> result; for (const auto filter : filters->get()) result = load_filter(std::move(result), *filter); return result; }