6#ifndef DEEPLIMA_TOKEN_SEQUENCE_ANALYZER
7#define DEEPLIMA_TOKEN_SEQUENCE_ANALYZER
15#include <unordered_map>
16#include <unordered_set>
18#include <unicode/unistr.h>
19#include <unicode/ustream.h>
20#include "unicode/utypes.h"
37template<>
struct std::hash<
deeplima::morph_model::morph_feats_t> {
60 typedef std::function < void (std::shared_ptr< StringIndex > stridx,
62 const std::vector<StringIndex::idx_t>& lemmata,
69 virtual const std::vector<std::vector<std::string>>&
get_classes()
const = 0;
73 virtual std::shared_ptr<StringIndex>
get_stridx()
const = 0;
77 virtual void operator()(
const std::vector<segmentation::token_pos>& tokens, uint32_t len) = 0;
80template <
typename TaggingAuxScalar=
float>
88 const std::vector<StringIndex::idx_t> m_lemm_buffer;
89 std::shared_ptr< StdMatrix<uint8_t> > m_classes;
96 const std::vector<StringIndex::idx_t>& lemm_buffer,
98 : m_stridx(stridx), m_buffer(buffer), m_lemm_buffer(lemm_buffer), m_classes(classes),
99 m_current(0), m_offset(offset), m_end(
end - offset)
101 assert(
end >= offset + 1);
106 return m_current >= m_end;
112 return m_buffer[m_current].m_flags;
117 return m_buffer[m_current].m_offset;
122 return m_buffer[m_current].m_len;
129 return m_buffer[m_current].m_mwt_len;
134 return m_stridx.get_str(m_buffer[m_current].m_mwt_surface_idx).c_str();
141 return m_buffer[m_current].m_mwt_surface_idx;
146 return m_buffer[m_current].m_form_idx;
151 return m_lemm_buffer[m_current];
154 inline const char*
form()
const
157 const std::string& f = m_stridx.get_str(m_buffer[m_current].m_form_idx);
164 const std::string& f = m_stridx.get_str(m_lemm_buffer[m_current]);
170 throw std::runtime_error(
"TokenSequenceAnalyzer<T> does not implement head");
201 uint8_t val = m_classes->get(m_current + m_offset, cls_idx);
241 inline const std::string&
form()
const
258 : m_data(data), m_token(stridx) { }
262 return m_data.size();
308 const std::string& lemm_model_fn,
309 const std::string& lemm_dict_fn,
310 const std::string& fixed_ini_fn,
311 const std::string& lower_ini_fn,
312 const std::string& fixed_lemm_fn,
313 const std::string& lemm_rules_fn,
331 assert(num_buffers > 0);
337 m_cls.load(model_fn, path_resolver);
342 std::fill(b.begin(), b.end(),
m_unk_idx);
346 for (uint32_t i = 0; i < buff.size(); ++i)
348 buff[i].m_form_idx = i;
354 if (lemm_model_fn.size() > 0)
358 m_lemm.load(lemm_model_fn, path_resolver);
360 catch (
const std::runtime_error& e)
362 std::cerr <<
"TokenSequenceAnalyzer failed to load lemmatization model " << lemm_model_fn << std::endl;
366 m_lemm.init(128,
m_cls.get_output_str_dicts_names(),
m_cls.get_output_str_dicts());
368 m_cls.register_handler([
this](
370 size_t begin,
size_t end,
size_t slot_idx){
387 m_cls.register_handler([
this](
389 size_t begin,
size_t end,
size_t slot_idx)
404 if (lemm_dict_fn.size() > 0)
408 if (fixed_ini_fn.size() > 0)
412 if (lower_ini_fn.size() > 0)
416 if (fixed_lemm_fn.size() > 0)
420 if (lemm_rules_fn.size() > 0)
427 std::vector<std::string>& classes_names,
428 std::vector<std::vector<std::string>>& classes)
430 m_cls.get_classes_from_fn(fn, classes_names, classes);
443 virtual const std::vector<std::vector<std::string>>&
get_classes()
const override
445 return m_cls.get_output_str_dicts();
450 return m_cls.get_output_str_dicts_names();
453 virtual std::shared_ptr<StringIndex>
get_stridx()
const override
480 m_cls.send_all_results();
487 virtual void operator()(
const std::vector<deeplima::segmentation::token_pos>& tokens, uint32_t len)
override
494 for (
size_t i = 0; i < len; i++)
535 while (next_buffer.
locked())
537 m_cls.send_next_results();
539 assert(!next_buffer.
locked());
565 return std::hash<StringIndex::idx_t>()(arg.first) ^ arg.second.hash();
572 std::ifstream f(fn, std::ios::in);
574 std::cerr <<
"load_lemm_cache failed to open file " << fn <<
".\n";
575 throw std::runtime_error(std::string(
"load_lemm_cache failed to open file ") + fn);
578 while (std::getline(f, line))
580 if (line.size() == 0)
584 const std::vector<std::string> v =
utils::split(line,
'\t');
587 throw std::runtime_error(std::string(
"Can't decode dict line \"") + line +
"\"");
590 const std::vector<std::string> upos_feats =
utils::split(v[1],
' ');
591 if (upos_feats.size() != 2)
593 throw std::runtime_error(std::string(
"Can't decode upos and feats in dict line \"") + line +
"\"");
596 std::map<std::string, std::set<std::string>> feats;
599 throw std::runtime_error(std::string(
"Can't parse feats in dict line \"") + line +
"\"");
611 throw std::runtime_error(std::string(
"Duplicate keys in dict: \"") + line +
"\"");
626 std::unordered_set<morph_model::morph_feats_t>
load_pos_cache(
const std::string& fn)
628 std::unordered_set<morph_model::morph_feats_t> result;
630 std::ifstream f(fn, std::ios::in);
632 std::cerr <<
"load_pos_cache failed to open file " << fn <<
".\n";
633 throw std::runtime_error(std::string(
"load_pos_cache failed to open file ") + fn);
636 while (std::getline(f, line))
638 if (line.size() == 0 || line[0] ==
'#')
643 std::map<std::string, std::set<std::string>> feats;
646 result.insert(encoded_feats);
660 std::ifstream f(fn, std::ios::in);
663 std::cerr <<
"load_lemm_rules failed to open file " << fn <<
".\n";
664 throw std::runtime_error(std::string(
"load_lemm_rules failed to open file ") + fn);
669 std::map<std::string, std::set<std::string>> feats;
676 catch (
const std::logic_error&)
690 std::cerr <<
"load_lemm_rules: " << skipped <<
" rules of " << fn
691 <<
" skipped (UPOS or features unknown to the lemmatization model)" << std::endl;
709 std::vector<StringIndex::idx_t>& lemm_buffer,
711 size_t offset,
size_t end)
713 std::u32string target;
714 const auto& lang_morph_model =
m_lemm.get_morph_model();
715 for (
size_t i = 0; i < end - offset; ++i)
718 if (
m_lemm.is_fixed(classes, i + offset))
720 lemm_buffer[i] = form_idx;
729 lemm_buffer[i] = it->second;
734 const bool sentence_begin = (i == 0 || buffer[i-1].eos());
738 lemm_buffer[i] = form_idx;
748 lemm_buffer[i] = form_idx;
752 const std::u32string& f =
m_stridx.get_ustr(form_idx);
753 if (!
m_lemm_rules.lemmatize(morph_feats_i, upos, f, target))
755 m_lemm.predict(f, classes, i + offset, target);
759 lemm_buffer[i] = target.empty() ? form_idx :
m_stridx.get_idx(target);
778 std::unordered_map<lemm_cache_key_t, StringIndex::idx_t, lemm_cache_key_hash>
m_lemm_cache;
static bool parse_feats(const std::string &s, std::map< std::string, std::set< std::string > > &_feats)
Handle multithread processing of token sequence.
std::function< void(std::shared_ptr< StringIndex > stridx, const token_buffer_t<> &tokens, const std::vector< StringIndex::idx_t > &lemmata, std::shared_ptr< StdMatrix< uint8_t > > classes, size_t begin, size_t end) > output_callback_t
virtual void finalize()=0
virtual void register_handler(const output_callback_t fn)=0
virtual std::shared_ptr< StringIndex > get_stridx() const =0
virtual const std::vector< std::vector< std::string > > & get_classes() const =0
virtual void operator()(const std::vector< segmentation::token_pos > &tokens, uint32_t len)=0
virtual const std::vector< std::string > & get_class_names() const =0
TokenIterator(const StringIndex &stridx, const token_buffer_t<> &buffer, const std::vector< StringIndex::idx_t > &lemm_buffer, std::shared_ptr< StdMatrix< uint8_t > > classes, size_t offset, size_t end)
uint32_t form_idx() const
uint32_t lemma_idx() const
const char * form() const
const char * deprel() const
const char * mwt_surface() const
uint16_t token_len() const
uint16_t token_offset() const
uint32_t mwt_surface_idx() const
token_flags_t flags() const
uint8_t token_class(size_t cls_idx) const
const char * lemma() const
void reset(size_t position=0)
const enriched_token_t & operator[](size_t idx) const
enriched_token_buffer_t(const token_buffer_t<> &data, const StringIndex &stridx)
token_buffer_t ::size_type size() const
enriched_token_t(const StringIndex &stridx)
friend class TokenSequenceAnalyzer
const StringIndex & m_stridx
void set_token(const token_buffer_t<>::token_t *p)
const std::string & form() const
const token_buffer_t ::token_t * m_ptoken
token_flags_t flags() const
StringIndex::idx_t m_unk_idx
void start_analysis(size_t buffer_idx, int count=-1)
std::unordered_set< morph_model::morph_feats_t > m_fixed_lemm_cache
std::vector< token_buffer_t<> > m_buffers
std::unordered_map< lemm_cache_key_t, StringIndex::idx_t, lemm_cache_key_hash > m_lemm_cache
std::unordered_set< morph_model::morph_feats_t > m_lower_ini_cache
std::pair< StringIndex::idx_t, morph_model::morph_feats_t > lemm_cache_key_t
lemmatization::impl::LemmatizationImpl m_lemm
virtual const std::vector< std::vector< std::string > > & get_classes() const override
tagging::impl::TaggingImpl< TaggingAuxScalar > m_cls
virtual void finalize() override
TokenIterator output_iterator_t
std::vector< std::vector< StringIndex::idx_t > > m_lemm_buffers
size_t m_current_timepoint
virtual void register_handler(const output_callback_t fn) override
output_callback_t m_output_callback
virtual const std::vector< std::string > & get_class_names() const override
std::unordered_set< morph_model::morph_feats_t > load_pos_cache(const std::string &fn)
void get_classes_from_fn(const std::string &fn, std::vector< std::string > &classes_names, std::vector< std::vector< std::string > > &classes)
void process_buffer(size_t buffer_idx)
std::shared_ptr< StringIndex > m_stridx_ptr
virtual void operator()(const std::vector< deeplima::segmentation::token_pos > &tokens, uint32_t len) override
void load_lemm_cache(const std::string &fn)
std::shared_ptr< StdMatrix< uint8_t > > m_classes
TokenSequenceAnalyzer(const std::string &model_fn, const std::string &lemm_model_fn, const std::string &lemm_dict_fn, const std::string &fixed_ini_fn, const std::string &lower_ini_fn, const std::string &fixed_lemm_fn, const std::string &lemm_rules_fn, const PathResolver &path_resolver, size_t buffer_size, size_t num_buffers)
virtual std::shared_ptr< StringIndex > get_stridx() const override
void lemmatize(const token_buffer_t<> &buffer, std::vector< StringIndex::idx_t > &lemm_buffer, std::shared_ptr< StdMatrix< uint8_t > > classes, size_t offset, size_t end)
Lemmatizes buffer[0, end - offset).
void load_lemm_rules(const std::string &fn)
Loads the edit rules (deeplima-gen-lemm-dict –rules) used for the words missing from the lemma dictio...
std::unordered_set< morph_model::morph_feats_t > m_fixed_ini_cache
lemmatization::lemm_rules_t< morph_model::morph_feats_t > m_lemm_rules
virtual ~TokenSequenceAnalyzer()
Rules ready for lookup, keyed by K, the analyzer's encoding of UPOS(+FEATS) (morph_feats_t in TokenSe...
Encoding on one 64 bits integer of the set of morphological features for one token.
Helper class for morphology data (upos, features) binarization.
bool can_convert(const std::string &upos, const std::map< std::string, std::set< std::string > > &feats) const
True when convert(upos, feats) can encode this UPOS and these features: convert only asserts it,...
morph_feats_t convert(const std::string &upos, const std::map< std::string, std::set< std::string > > &feats) const
Class implementing the tagger, used as member in TokenSequenceAnalyzer, the main tagger class Son of ...
const std::string & any_feats()
The feats value of the rule used when no rule matches the exact features.
std::u32string to_lower(const std::u32string &src)
Lowercases code point by code point (ICU simple case mapping, any script).
void read_rules(std::istream &in, const std::function< void(const rule_line_t &)> &fn)
Reads a rules file, calling fn(const rule_line_t&) on each rule.
std::vector< std::string > split(const std::string &str, char delim)
Edit rules for lemmatizing words absent from the lemma dictionary.
TStringIndex< std::string, std::u32string, uint32_t > StringIndex
std::size_t operator()(const lemm_cache_key_t &arg) const
uint32_t m_mwt_surface_idx
One line of a rules file, as read_rules() hands it over.
const char * m_mwt_surface_pch
uint16_t m_mwt_surface_len
std::size_t operator()(deeplima::morph_model::morph_feats_t const &s) const noexcept