6#ifndef DEEPLIMA_MWT_LEXICON_H
7#define DEEPLIMA_MWT_LEXICON_H
18#include <unordered_map>
90 std::vector<std::string>
apply(
const std::string& surface)
const
92 std::vector<std::string> result;
95 result.push_back(surface.substr(0, surface.size() -
affix.size()) +
rewrite);
96 result.insert(result.end(),
words.begin(),
words.end());
101 result.push_back(
rewrite + surface.substr(
affix.size()));
114 void load(
const std::string& fn)
119 throw std::runtime_error(
"MwtLexicon: can't open \"" + fn +
"\"");
124 if (std::ifstream(fn +
".s2s").good())
126 m_s2s.load(fn +
".s2s");
133 m_suffix_rules.clear();
134 m_prefix_rules.clear();
136 while (std::getline(f, line))
138 if (!line.empty() &&
'\r' == line.back())
146 const std::vector<std::string> fields = split(line);
150 if (fields.size() >= 7 &&
"#rule" == fields[0]
151 && (
"suffix" == fields[1] ||
"prefix" == fields[1]) && !fields[2].empty())
154 r.
suffix = (
"suffix" == fields[1]);
159 r.
support = uint32_t(std::stoul(fields[4]));
160 r.
applicable = uint32_t(std::stoul(fields[5]));
162 catch (
const std::logic_error&)
166 r.
words.assign(fields.begin() + 6, fields.end());
172 if (fields.size() < 4 || fields[0].empty())
176 m_dict.emplace(fields[0], std::vector<std::string>(fields.begin() + 2, fields.end()));
182 return m_dict.size();
187 return m_suffix_rules.size() + m_prefix_rules.size();
192 return m_s2s.loaded();
195 void add_entry(
const std::string& surface,
const std::vector<std::string>& words)
197 m_dict.emplace(surface, words);
203 auto& rules = r.
suffix ? m_suffix_rules : m_prefix_rules;
204 auto it = rules.find(r.
affix);
205 if (rules.end() == it)
207 std::string affix = r.
affix;
208 rules.emplace(std::move(affix), std::move(r));
212 it->second = std::move(r);
217 const std::vector<std::string>*
lookup(
const std::string& surface)
const
219 const auto it = m_dict.find(surface);
220 return (m_dict.end() == it) ? nullptr : &it->second;
226 const Rule* best =
nullptr;
228 for (
size_t i = 1; i < surface.size(); ++i)
230 if (is_continuation(surface[i]))
234 const auto s = m_suffix_rules.find(surface.substr(i));
235 if (m_suffix_rules.end() != s && (
nullptr == best || s->second.
better_than(*best)))
239 const auto p = m_prefix_rules.find(surface.substr(0, i));
240 if (m_prefix_rules.end() != p && (
nullptr == best || p->second.
better_than(*best)))
251 bool expand(
const std::string& surface, std::vector<std::string>& words,
bool use_rules)
const
253 if (
const auto* w =
lookup(surface))
260 const bool s2s = m_s2s.loaded();
261 if (s2s && m_s2s.before_rules())
263 std::vector<std::string> w = m_s2s.expand(surface);
266 words = std::move(w);
272 words = r->apply(surface);
275 if (s2s && !m_s2s.before_rules())
277 std::vector<std::string> w = m_s2s.expand(surface);
280 words = std::move(w);
292 m_suffix_rules.clear();
293 m_prefix_rules.clear();
294 if (0 == min_support)
299 std::map<Rule, uint32_t, RuleOrder> support;
300 for (
const auto& [surface, words] : m_dict)
309 std::map<std::pair<bool, std::string>, uint32_t> applicable;
310 for (
const auto& [r, n] : support)
312 if (n >= min_support)
314 applicable.emplace(std::make_pair(r.suffix, r.affix), 0);
317 for (
const auto& [surface, words] : m_dict)
319 for (
size_t i = 1; i < surface.size(); ++i)
321 if (is_continuation(surface[i]))
325 auto s = applicable.find(std::make_pair(
true, surface.substr(i)));
326 if (applicable.end() != s) s->second++;
327 auto p = applicable.find(std::make_pair(
false, surface.substr(0, i)));
328 if (applicable.end() != p) p->second++;
332 for (
const auto& [r, n] : support)
340 kept.
applicable = applicable[std::make_pair(r.suffix, r.affix)];
349 for (
const auto* rules : {&m_suffix_rules, &m_prefix_rules})
351 std::map<std::string, const Rule*> sorted;
352 for (
const auto& [affix, r] : *rules)
354 sorted.emplace(affix, &r);
356 for (
const auto& [affix, r] : sorted)
358 out <<
"#rule\t" << (r->suffix ?
"suffix" :
"prefix") <<
"\t" << r->affix <<
"\t"
359 << r->rewrite <<
"\t" << r->support <<
"\t" << r->applicable;
360 for (
const auto& w : r->words)
370 static std::vector<Rule>
rules_of(
const std::string& surface,
const std::vector<std::string>& words)
372 std::vector<Rule> result;
373 if (words.size() < 2)
378 const std::string& first = words.front();
380 if (k > 0 && k < surface.size())
384 r.
affix = surface.substr(k);
386 r.
words.assign(words.begin() + 1, words.end());
387 result.push_back(std::move(r));
390 const std::string& last = words.back();
392 if (l > 0 && l < surface.size())
396 r.
affix = surface.substr(0, surface.size() - l);
397 r.
rewrite = last.substr(0, last.size() - l);
398 r.
words.assign(words.begin(), words.end() - 1);
399 result.push_back(std::move(r));
408 while (k < a.size() && k < b.size() && a[k] == b[k])
413 while (k > 0 && ((k < a.size() && is_continuation(a[k])) || (k < b.size() && is_continuation(b[k]))))
424 while (k < a.size() && k < b.size() && a[a.size() - 1 - k] == b[b.size() - 1 - k])
429 while (k > 0 && is_continuation(a[a.size() - k]))
437 static bool is_continuation(
char c)
439 return (
static_cast<unsigned char>(c) & 0xC0) == 0x80;
442 static std::vector<std::string> split(
const std::string& line)
444 std::vector<std::string> fields;
448 const size_t tab = line.find(
'\t', start);
449 fields.push_back(line.substr(start, std::string::npos == tab ? std::string::npos : tab - start));
450 if (std::string::npos == tab)
462 bool operator()(
const Rule& a,
const Rule& b)
const
464 return std::tie(a.suffix, a.affix, a.rewrite, a.words) < std::tie(b.suffix, b.affix, b.rewrite, b.words);
468 std::unordered_map<std::string, std::vector<std::string>> m_dict;
469 std::unordered_map<std::string, Rule> m_suffix_rules;
470 std::unordered_map<std::string, Rule> m_prefix_rules;
static std::vector< Rule > rules_of(const std::string &surface, const std::vector< std::string > &words)
const Rule * find_rule(const std::string &surface) const
size_t rules_size() const
void write_rules(std::ostream &out) const
void load(std::istream &f)
void add_entry(const std::string &surface, const std::vector< std::string > &words)
bool expand(const std::string &surface, std::vector< std::string > &words, bool use_rules) const
size_t learn_rules(uint32_t min_support)
const std::vector< std::string > * lookup(const std::string &surface) const
void load(const std::string &fn)
MwtLexicon(const std::string &fn)
static size_t common_suffix(const std::string &a, const std::string &b)
static size_t common_prefix(const std::string &a, const std::string &b)
Edit rules for lemmatizing words absent from the lemma dictionary.
std::vector< std::string > words
bool better_than(const Rule &o) const
std::vector< std::string > apply(const std::string &surface) const