43 typedef std::function<void(
const std::vector<segmentation::token_pos>&, uint32_t)>
callback_t;
67 m_require_flag = require_flag;
70 void operator()(
const std::vector<segmentation::token_pos>& tokens, uint32_t len)
75 for (uint32_t i = 0; i < len; ++i)
79 const auto it = m_dict.find(surface);
81 if (m_dict.end() == it || (m_require_flag && !flagged))
89 const std::vector<std::string>& words = it->second;
95 m_storage.push_back(surface);
96 const char* surf_pch = m_storage.back().c_str();
98 for (
size_t k = 0; k < words.size(); ++k)
100 m_storage.push_back(words[k]);
103 sub.
m_pch = m_storage.back().c_str();
109 sub.
m_flags = (k + 1 == words.size())
118 m_out.push_back(sub);
124 m_next(m_out, uint32_t(m_out.size()));
129 void load(
const std::string& fn)
134 throw std::runtime_error(
"MwtExpander: can't open dictionary \"" + fn +
"\"");
138 while (std::getline(f, line))
140 if (line.empty() ||
'#' == line[0])
146 std::vector<std::string> fields;
150 const size_t tab = line.find(
'\t', start);
151 fields.push_back(line.substr(start, std::string::npos == tab ? std::string::npos : tab - start));
152 if (std::string::npos == tab)
159 if (fields.size() < 4)
164 const std::string& surface = fields[0];
165 std::vector<std::string> words(fields.begin() + 2, fields.end());
166 if (surface.empty() || words.size() < 2)
170 m_dict.emplace(surface, std::move(words));
174 std::unordered_map<std::string, std::vector<std::string>> m_dict;
176 bool m_require_flag =
true;
179 std::vector<segmentation::token_pos> m_out;
180 std::deque<std::string> m_storage;