LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
token_sequence_analyzer.h
Go to the documentation of this file.
1// Copyright 2021 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6#ifndef DEEPLIMA_TOKEN_SEQUENCE_ANALYZER
7#define DEEPLIMA_TOKEN_SEQUENCE_ANALYZER
8
9#include <fstream>
10#include <iostream>
11#include <map>
12#include <memory>
13#include <set>
14#include <string>
15#include <unordered_map>
16#include <unordered_set>
17
18#include <unicode/unistr.h>
19#include <unicode/ustream.h>
20#include "unicode/utypes.h"
21#include <iostream>
22
23// #include "segmentation.h"
25#include "utils/str_index.h"
26#include "token_type.h"
27#include "ner.h"
28#include "conllu/line.h"
34#include "deeplima/token_type.h"
35
36
37template<> struct std::hash<deeplima::morph_model::morph_feats_t> {
38 std::size_t operator()(deeplima::morph_model::morph_feats_t const& s) const noexcept {
39 return s.hash();
40 }
41};
42
43namespace deeplima
44{
45
58{
59public:
60 typedef std::function < void (std::shared_ptr< StringIndex > stridx,
61 const token_buffer_t<>& tokens,
62 const std::vector<StringIndex::idx_t>& lemmata,
63 std::shared_ptr< StdMatrix<uint8_t> > classes,
64 size_t begin,
65 size_t end) > output_callback_t;
66
67 virtual void register_handler(const output_callback_t fn) = 0;
68
69 virtual const std::vector<std::vector<std::string>>& get_classes() const = 0;
70
71 virtual const std::vector<std::string>& get_class_names() const = 0;
72
73 virtual std::shared_ptr<StringIndex> get_stridx() const = 0;
74
75 virtual void finalize() = 0;
76
77 virtual void operator()(const std::vector<segmentation::token_pos>& tokens, uint32_t len) = 0;
78};
79
80template <typename TaggingAuxScalar=float>
82{
83public:
85 {
86 const StringIndex& m_stridx;
87 const token_buffer_t<>& m_buffer;
88 const std::vector<StringIndex::idx_t> m_lemm_buffer;
89 std::shared_ptr< StdMatrix<uint8_t> > m_classes;
90 size_t m_current;
91 size_t m_offset;
92 size_t m_end;
93
94 public:
95 TokenIterator(const StringIndex& stridx, const token_buffer_t<>& buffer,
96 const std::vector<StringIndex::idx_t>& lemm_buffer,
97 std::shared_ptr< StdMatrix<uint8_t> > classes, size_t offset, size_t end)
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)
100 {
101 assert(end >= offset + 1);
102 }
103
104 inline bool end() const
105 {
106 return m_current >= m_end;
107 }
108
109 inline token_flags_t flags() const
110 {
111 assert(! end());
112 return m_buffer[m_current].m_flags;
113 }
114
115 inline uint16_t token_offset() const
116 {
117 return m_buffer[m_current].m_offset;
118 }
119
120 inline uint16_t token_len() const
121 {
122 return m_buffer[m_current].m_len;
123 }
124
125 // MWT: number of sub-words if this token starts an expanded multiword token
126 // (0 otherwise), and the surface form for the "N-M surface" range line.
127 inline uint8_t mwt_len() const
128 {
129 return m_buffer[m_current].m_mwt_len;
130 }
131
132 inline const char* mwt_surface() const
133 {
134 return m_stridx.get_str(m_buffer[m_current].m_mwt_surface_idx).c_str();
135 }
136
137 // Raw StringIndex id of the surface form; the dependency parser shares this
138 // StringIndex, so the id can be carried over directly when copying tokens.
139 inline uint32_t mwt_surface_idx() const
140 {
141 return m_buffer[m_current].m_mwt_surface_idx;
142 }
143
144 inline uint32_t form_idx() const
145 {
146 return m_buffer[m_current].m_form_idx;
147 }
148
149 inline uint32_t lemma_idx() const
150 {
151 return m_lemm_buffer[m_current];
152 }
153
154 inline const char* form() const
155 {
156 assert(! end());
157 const std::string& f = m_stridx.get_str(m_buffer[m_current].m_form_idx);
158 return f.c_str();
159 }
160
161 inline const char* lemma() const
162 {
163 assert(! end());
164 const std::string& f = m_stridx.get_str(m_lemm_buffer[m_current]);
165 return f.c_str();
166 }
167
168 inline uint32_t head() const
169 {
170 throw std::runtime_error("TokenSequenceAnalyzer<T> does not implement head");
171 return 0;
172 }
173
174 // The tagger iterator carries no dependency information; provided only so the
175 // CoNLL-U dumper template can be instantiated. Never reached with hasDeps=true.
176 inline const char* deprel() const
177 {
178 return "dep";
179 }
180
181 inline void next()
182 {
183 m_current++;
184 }
185
186 inline void reset(size_t position = 0)
187 {
188 // std::cerr << "TokenSequenceAnalyzer::reset" << std::endl;
189 m_current = position;
190 }
191
192 inline size_t position() const
193 {
194 return m_current;
195 }
196
197 inline uint8_t token_class(size_t cls_idx) const
198 {
199 // std::cerr << "time: " << m_current + m_offset << "\n";
200 // std::cerr << "cls_idx: " << cls_idx << "\n";
201 uint8_t val = m_classes->get(m_current + m_offset, cls_idx);
202 return val;
203 }
204 };
205
207
208protected:
209
211 {
213
214 protected:
217
219 {
220 m_ptoken = p;
221 }
222 public:
223
225 : m_stridx(stridx),
226 m_ptoken(nullptr)
227 { }
228
229 inline token_flags_t flags() const
230 {
231 assert(nullptr != m_ptoken);
232 return m_ptoken->m_flags;
233 }
234
235 inline bool eos() const
236 {
237 assert(nullptr != m_ptoken);
239 }
240
241 inline const std::string& form() const
242 {
243 assert(nullptr != m_ptoken);
244 const std::string& f = m_stridx.get_str(m_ptoken->m_form_idx);
245 return f;
246 }
247 };
248
250 {
251 const token_buffer_t<>& m_data;
252 mutable enriched_token_t m_token; // WARNING: only one iterator is supported
253
254 public:
256
258 : m_data(data), m_token(stridx) { }
259
261 {
262 return m_data.size();
263 }
264
265 inline const enriched_token_t& operator[](size_t idx) const
266 {
267 m_token.set_token(m_data.data() + idx);
268 return m_token;
269 }
270 };
271
272 //typedef tagging::impl::FeaturesVectorizer<
273 // enriched_token_buffer_t,
274 // typename enriched_token_buffer_t::token_t> FeaturesVectorizer;
275
276 //typedef tagging::impl::FeaturesVectorizerWithCache<
277 // enriched_token_buffer_t,
278 // typename enriched_token_buffer_t::token_t> FeaturesVectorizer;
279
280 // typedef tagging::impl::EntityTaggingClassifier<TaggingAuxScalar> Classifier;
281
282 // typedef tagging::impl::FeaturesVectorizerWithPrecomputing<
283 // Classifier,
284 // enriched_token_buffer_t,
285 // typename enriched_token_buffer_t::token_t> FeaturesVectorizer;
286
287 // typedef tagging::impl::TaggingImpl< Classifier,
288 // FeaturesVectorizer,
289 // Matrix > EntityTaggingModule;
290
291 // typedef DictEmbdVectorizer<EmbdUInt64FloatHolder, EmbdUInt64Float,
292 // eigen_wrp::EigenMatrixXf> EmbdVectorizer;
293 // typedef lemmatization::impl::LemmatizationImpl< RnnSeq2Seq, EmbdVectorizer,
294 // Matrix> LemmatizationModule;
295
296public:
298 m_buffer_size(0),
301 m_stridx_ptr(std::make_shared<StringIndex>()),
303 m_classes(std::make_shared<StdMatrix<uint8_t>>())
304 {
305 }
306
307 TokenSequenceAnalyzer(const std::string& model_fn,
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,
314 const PathResolver& path_resolver,
315 size_t buffer_size,
316 size_t num_buffers)
317 : m_buffer_size(buffer_size),
320 m_stridx_ptr(std::make_shared<StringIndex>()),
322 m_cls(),
323 m_classes(std::make_shared<StdMatrix<uint8_t>>())
324{
325 // std::cerr << "TokenSequenceAnalyzer::TokenSequenceAnalyzer " << model_fn << ", "
326 // << lemm_model_fn << ", " << lemm_dict_fn << ", "
327 // << fixed_ini_fn << ", " << lower_ini_fn << ", "
328 // << fixed_lemm_fn
329 // << std::endl;
330 assert(m_buffer_size > 0);
331 assert(num_buffers > 0);
332 m_buffers.resize(num_buffers);
333 m_lemm_buffers.resize(num_buffers);
334 for ( token_buffer_t<>& b : m_buffers ) b.resize(m_buffer_size);
335 for ( auto& b : m_lemm_buffers ) b.resize(m_buffer_size);
336
337 m_cls.load(model_fn, path_resolver);
338 m_cls.init(1, num_buffers, buffer_size, m_stridx);
339
340 m_unk_idx = m_stridx.get_idx("_");
341 for ( auto& b : m_lemm_buffers )
342 std::fill(b.begin(), b.end(), m_unk_idx);
343
344 {
345 token_buffer_t<> buff(m_stridx.size());
346 for (uint32_t i = 0; i < buff.size(); ++i)
347 {
348 buff[i].m_form_idx = i;
349 }
350
351 m_cls.precompute_inputs(tagging::impl::enriched_token_buffer_t(buff, m_stridx));
352 }
353
354 if (lemm_model_fn.size() > 0)
355 {
356 try
357 {
358 m_lemm.load(lemm_model_fn, path_resolver);
359 }
360 catch (const std::runtime_error& e)
361 {
362 std::cerr << "TokenSequenceAnalyzer failed to load lemmatization model " << lemm_model_fn << std::endl;
363 throw;
364 }
365 // TODO replace value 128 below (max_input_word_len) by an optimizable parameter
366 m_lemm.init(128, m_cls.get_output_str_dicts_names(), m_cls.get_output_str_dicts());
367
368 m_cls.register_handler([this](
369 std::shared_ptr< StdMatrix<uint8_t> > classes,
370 size_t begin, size_t end, size_t slot_idx){
371 // std::cerr << "handler called: " << slot_idx << std::endl;
372
373 lemmatize(m_buffers[slot_idx], m_lemm_buffers[slot_idx], classes, begin, end);
374
376 m_buffers[slot_idx],
377 m_lemm_buffers[slot_idx],
378 classes,
379 begin,
380 end);
381
382 m_buffers[slot_idx].unlock();
383 });
384 }
385 else
386 {
387 m_cls.register_handler([this](
388 std::shared_ptr< StdMatrix<uint8_t> > classes,
389 size_t begin, size_t end, size_t slot_idx)
390 {
391 // std::cerr << "handler called: " << slot_idx << std::endl;
392 m_classes = classes;
394 m_buffers[slot_idx],
395 m_lemm_buffers[slot_idx],
396 m_classes,
397 begin,
398 end);
399
400 m_buffers[slot_idx].unlock();
401 });
402 }
403
404 if (lemm_dict_fn.size() > 0)
405 {
406 load_lemm_cache(lemm_dict_fn);
407 }
408 if (fixed_ini_fn.size() > 0)
409 {
410 m_fixed_ini_cache = load_pos_cache(fixed_ini_fn);
411 }
412 if (lower_ini_fn.size() > 0)
413 {
414 m_lower_ini_cache = load_pos_cache(lower_ini_fn);
415 }
416 if (fixed_lemm_fn.size() > 0)
417 {
418 m_fixed_lemm_cache = load_pos_cache(fixed_lemm_fn);
419 }
420 if (lemm_rules_fn.size() > 0)
421 {
422 load_lemm_rules(lemm_rules_fn);
423 }
424 }
425
426 void get_classes_from_fn(const std::string& fn,
427 std::vector<std::string>& classes_names,
428 std::vector<std::vector<std::string>>& classes)
429 {
430 m_cls.get_classes_from_fn(fn, classes_names, classes);
431 }
432
434 {
435 }
436
437 virtual void register_handler(const output_callback_t fn) override
438 {
439 // std::cerr << "TokenSequenceAnalyzer::register_handler" << std::endl;
441 }
442
443 virtual const std::vector<std::vector<std::string>>& get_classes() const override
444 {
445 return m_cls.get_output_str_dicts();
446 }
447
448 virtual const std::vector<std::string>& get_class_names() const override
449 {
450 return m_cls.get_output_str_dicts_names();
451 }
452
453 virtual std::shared_ptr<StringIndex> get_stridx() const override
454 {
455 return m_stridx_ptr;
456 }
457
458 /*
459 * The logic is contiguous and the final stop and release of locks are
460 * handled by finalize() and no_more_data().
461 * Contiguous means that only the 100% full buffer triggers processing.
462 * Partially full buffers will wait. Finalization triggers the dispatch of
463 * the remaining data to the pipeline.
464 */
465 virtual void finalize() override
466 {
467 // std::cerr << "TokenSequenceAnalyzer::finalize" << std::endl;
468 if (m_current_timepoint > 0)
469 {
471 {
473 }
474 else
475 {
476 m_cls.no_more_data(m_current_buffer);
477 }
478 }
479
480 m_cls.send_all_results();
483
484 m_cls.reset();
485 }
486
487 virtual void operator()(const std::vector<deeplima::segmentation::token_pos>& tokens, uint32_t len) override
488 {
490 {
492 }
493
494 for (size_t i = 0; i < len; i++)
495 {
497 assert(m_current_buffer < m_buffers.size());
498
500 const segmentation::token_pos& src = tokens[i];
501 token.m_offset = src.m_offset;
502 token.m_len = src.m_len;
503 token.m_form_idx = m_stridx.get_idx(src.m_pch, src.m_len);
504 token.m_flags = token_flags_t(src.m_flags);
505 // Carry MWT metadata: on the first sub-word of an expanded surface token,
506 // intern the surface form so the dumper can emit the "N-M surface" line.
507 token.m_mwt_len = src.m_mwt_len;
508 token.m_mwt_surface_idx =
509 (src.m_mwt_len > 0 && nullptr != src.m_mwt_surface_pch)
511 : 0;
512
515 {
517 if (i < len - 1)
518 {
519 // if we can't wait until next call
521 }
522 }
523 }
524 }
525
526protected:
527
529 {
530 // std::cerr << "acquire_buffer" << std::endl;
531 size_t next_buffer_idx = (m_current_buffer + 1 < m_buffers.size()) ? (m_current_buffer + 1) : 0;
532 const token_buffer_t<>& next_buffer = m_buffers[next_buffer_idx];
533//
534 // wait for buffer
535 while (next_buffer.locked())
536 {
537 m_cls.send_next_results();
538 }
539 assert(!next_buffer.locked());
540
541 m_current_buffer = next_buffer_idx;
543 }
544
545 void start_analysis(size_t buffer_idx, int count = -1)
546 {
547 // std::cerr << "TokenSequenceAnalyzer::start_analysis " << buffer_idx << ", " << count << std::endl;
548 assert(!m_buffers[buffer_idx].locked());
549 m_buffers[buffer_idx].lock();
550
551 const token_buffer_t<>& current_buffer = m_buffers[buffer_idx];
552 m_cls.handle_token_buffer(buffer_idx, tagging::impl::enriched_token_buffer_t(current_buffer, m_stridx), count);
553 }
554
555 void process_buffer(size_t buffer_idx)
556 {
557 const token_buffer_t<>& current_buffer = m_buffers[buffer_idx];
558 m_cls.handle_token_buffer(buffer_idx, tagging::impl::enriched_token_buffer_t(current_buffer, m_stridx));
559 }
560
561 typedef std::pair<StringIndex::idx_t, morph_model::morph_feats_t> lemm_cache_key_t;
563 {
564 std::size_t operator() (const lemm_cache_key_t &arg) const {
565 return std::hash<StringIndex::idx_t>()(arg.first) ^ arg.second.hash();
566 }
567 };
568
569 void load_lemm_cache(const std::string& fn)
570 {
571 const morph_model::morph_model_t& mm = m_lemm.get_morph_model();
572 std::ifstream f(fn, std::ios::in);
573 if (!f) {
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);
576 }
577 std::string line;
578 while (std::getline(f, line))
579 {
580 if (line.size() == 0)
581 {
582 continue;
583 }
584 const std::vector<std::string> v = utils::split(line, '\t');
585 if (v.size() != 3)
586 {
587 throw std::runtime_error(std::string("Can't decode dict line \"") + line + "\"");
588 }
589
590 const std::vector<std::string> upos_feats = utils::split(v[1], ' ');
591 if (upos_feats.size() != 2)
592 {
593 throw std::runtime_error(std::string("Can't decode upos and feats in dict line \"") + line + "\"");
594 }
595
596 std::map<std::string, std::set<std::string>> feats;
597 if (!deeplima::CoNLLU::CoNLLULine::parse_feats(upos_feats[1], feats))
598 {
599 throw std::runtime_error(std::string("Can't parse feats in dict line \"") + line + "\"");
600 }
601
602 morph_model::morph_feats_t encoded_feats = mm.convert(upos_feats[0], feats);
603
604 const StringIndex::idx_t form_idx = m_stridx.get_idx(v[0]);
605
606 const lemm_cache_key_t form_key(form_idx, encoded_feats);
607
608 const auto it = m_lemm_cache.find(form_key);
609 if (m_lemm_cache.end() != it)
610 {
611 throw std::runtime_error(std::string("Duplicate keys in dict: \"") + line + "\"");
612 }
613
614 m_lemm_cache[form_key] = m_stridx.get_idx(v[2]);
615 }
616
617 }
618
619 // struct fixed_lemm_cache_key_hash
620 // {
621 // std::size_t operator() (const morph_model::morph_feats_t &arg) const {
622 // return arg.hash();
623 // }
624 // };
625
626 std::unordered_set<morph_model::morph_feats_t> load_pos_cache(const std::string& fn)
627 {
628 std::unordered_set<morph_model::morph_feats_t> result;
629 const morph_model::morph_model_t& mm = m_lemm.get_morph_model();
630 std::ifstream f(fn, std::ios::in);
631 if (!f) {
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);
634 }
635 std::string line;
636 while (std::getline(f, line))
637 {
638 if (line.size() == 0 || line[0] == '#')
639 {
640 continue;
641 }
642
643 std::map<std::string, std::set<std::string>> feats;
644 morph_model::morph_feats_t encoded_feats = mm.convert(line, feats);
645 // std::cerr << "load_pos_cache add " << line << " " << encoded_feats.toBaseType() << std::endl;
646 result.insert(encoded_feats);
647 }
648 return result;
649 }
650
651
657 void load_lemm_rules(const std::string& fn)
658 {
659 const morph_model::morph_model_t& mm = m_lemm.get_morph_model();
660 std::ifstream f(fn, std::ios::in);
661 if (!f)
662 {
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);
665 }
666 size_t skipped = 0;
668 {
669 std::map<std::string, std::set<std::string>> feats;
670 bool ok = true;
671 try
672 {
675 }
676 catch (const std::logic_error&)
677 {
678 ok = false;
679 }
680 if (!ok || !mm.can_convert(r.m_upos, feats))
681 {
682 ++skipped;
683 return;
684 }
685 m_lemm_rules.add(mm.convert(r.m_upos, feats), r.m_suffix, r.m_rule, r.m_count,
687 });
688 if (skipped > 0)
689 {
690 std::cerr << "load_lemm_rules: " << skipped << " rules of " << fn
691 << " skipped (UPOS or features unknown to the lemmatization model)" << std::endl;
692 }
693 }
694
708 void lemmatize(const token_buffer_t<>& buffer,
709 std::vector<StringIndex::idx_t>& lemm_buffer,
710 std::shared_ptr< StdMatrix<uint8_t> > classes,
711 size_t offset, size_t end)
712 {
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)
716 {
717 const StringIndex::idx_t form_idx = buffer[i].m_form_idx;
718 if (m_lemm.is_fixed(classes, i + offset))
719 {
720 lemm_buffer[i] = form_idx;
721 continue;
722 }
723
724 const morph_model::morph_feats_t morph_feats_i = m_lemm.get_morph_feats(classes, i + offset);
725 const lemm_cache_key_t form_key(form_idx, morph_feats_i);
726 const auto it = m_lemm_cache.find(form_key);
727 if (m_lemm_cache.end() != it)
728 {
729 lemm_buffer[i] = it->second;
730 continue;
731 }
732
733 const auto upos = morph_model::morph_feats_t(lang_morph_model.decode_upos(morph_feats_i));
734 const bool sentence_begin = (i == 0 || buffer[i-1].eos());
735 // 3 and 4 depend on the position or are free: not cached
736 if (sentence_begin && m_fixed_ini_cache.end() != m_fixed_ini_cache.find(upos))
737 {
738 lemm_buffer[i] = form_idx;
739 continue;
740 }
741 if (sentence_begin && m_lower_ini_cache.end() != m_lower_ini_cache.find(upos))
742 {
743 lemm_buffer[i] = m_stridx.get_idx(lemmatization::to_lower(m_stridx.get_ustr(form_idx)));
744 continue;
745 }
746 if (m_fixed_lemm_cache.end() != m_fixed_lemm_cache.find(upos))
747 {
748 lemm_buffer[i] = form_idx;
749 continue;
750 }
751
752 const std::u32string& f = m_stridx.get_ustr(form_idx);
753 if (!m_lemm_rules.lemmatize(morph_feats_i, upos, f, target))
754 {
755 m_lemm.predict(f, classes, i + offset, target);
756 }
757 // Never emit an empty lemma (invalid CoNLL-U): if the model produced
758 // nothing (e.g. the form reaching it was empty), keep the surface form.
759 lemm_buffer[i] = target.empty() ? form_idx : m_stridx.get_idx(target);
760 m_lemm_cache[form_key] = lemm_buffer[i];
761 }
762 }
763
767
768 std::shared_ptr<StringIndex> m_stridx_ptr;
771 std::vector<token_buffer_t<>> m_buffers;
772 std::vector<std::vector<StringIndex::idx_t>> m_lemm_buffers; // access control is sync with m_buffers
773
776 std::shared_ptr< StdMatrix<uint8_t> > m_classes;
777
778 std::unordered_map<lemm_cache_key_t, StringIndex::idx_t, lemm_cache_key_hash> m_lemm_cache;
779
780 // The three caches below has been added to allow to force copying the
781 // (possibly lowercased) token as lemma, for a fixed list of pos tags for a given language
782 // for sentence initial (fixed ini and lower ini) and for other tokens (lower lemm)
783 std::unordered_set<morph_model::morph_feats_t> m_fixed_ini_cache;
784 std::unordered_set<morph_model::morph_feats_t> m_lower_ini_cache;
785 std::unordered_set<morph_model::morph_feats_t> m_fixed_lemm_cache;
787
789};
790
791} // namespace deeplima
792
793#endif
static bool parse_feats(const std::string &s, std::map< std::string, std::set< std::string > > &_feats)
Definition line.cpp:142
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 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)
enriched_token_buffer_t(const token_buffer_t<> &data, const StringIndex &stridx)
void set_token(const token_buffer_t<>::token_t *p)
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
std::vector< std::vector< StringIndex::idx_t > > m_lemm_buffers
virtual void register_handler(const output_callback_t fn) override
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)
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
Rules ready for lookup, keyed by K, the analyzer's encoding of UPOS(+FEATS) (morph_feats_t in TokenSe...
Definition lemm_rules.h:440
Encoding on one 64 bits integer of the set of morphological features for one token.
Definition morph_model.h:36
Helper class for morphology data (upos, features) binarization.
Definition morph_model.h:93
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.
Definition lemm_rules.h:253
std::u32string to_lower(const std::u32string &src)
Lowercases code point by code point (ICU simple case mapping, any script).
Definition lemm_rules.h:72
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.
Definition lemm_rules.h:376
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
Definition str_index.h:119
@ sentence_brk
Definition token_type.h:21
STL namespace.
std::size_t operator()(const lemm_cache_key_t &arg) const
token_flags_t m_flags
Definition token_type.h:44
One line of a rules file, as read_rules() hands it over.
Definition lemm_rules.h:349
std::size_t operator()(deeplima::morph_model::morph_feats_t const &s) const noexcept