LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
char_seq_vectorizer.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_SRC_INCLUDE_SEGMENTATION_TRAIN_CHAR_SEQ_VECTORIZER_H
7#define DEEPLIMA_SRC_INCLUDE_SEGMENTATION_TRAIN_CHAR_SEQ_VECTORIZER_H
8
9#include <vector>
10#include <string>
11
14
15namespace deeplima
16{
17namespace segmentation
18{
19namespace train
20{
21
22template <class InputEncoder, class Matrix, class Adapter>
23class CharSeqVectorizerImpl : public Adapter
24{
25public:
26 CharSeqVectorizerImpl(const std::vector<impl::ngram_descr_t>& ngram_descr)
27 : Adapter(),
28 m_input_encoder(ngram_descr)
29 {
30 }
31
32 // The word list of the lexicon feature (not owned), before process
33 void set_lexicon(const impl::SegmLexicon* lexicon)
34 {
35 m_input_encoder.set_lexicon(lexicon);
36 }
37
38 std::shared_ptr<Matrix> process(const std::string& text, int64_t len = -1)
39 {
40 assert(len > 0); // TODO: calculate length of text in characters in case len == -1
41 return read_string(text, len);
42 }
43
44protected:
45 std::shared_ptr<Matrix> read_string(const std::string& text, uint64_t len)
46 {
47 auto target = std::make_shared<Matrix>(len, m_input_encoder.size());
48
49 uint64_t current_timepoint = 0;
50 int32_t pos = 0;
51 m_input_encoder.reset();
52
53 while (! m_input_encoder.ready_to_generate())
54 {
55 m_input_encoder.warmup((const uint8_t*)text.data(), &pos, text.size());
56 }
57
58 while (size_t(pos) < text.size())
59 {
60 if (m_input_encoder.parse((const uint8_t*)text.data(), &pos, text.size()) > 0)
61 {
62 handle_timepoint(*target, current_timepoint);
63 }
64 }
65
66 char final_spaces[] = " ";
67 for (size_t i = 0; i < m_input_encoder.get_lookahead(); i++)
68 {
69 int32_t pos = 0;
70 if (m_input_encoder.parse((uint8_t*)final_spaces, &pos, 1) > 0)
71 {
72 handle_timepoint(*target, current_timepoint);
73 }
74 else
75 {
76 throw std::runtime_error("Something wrong.");
77 }
78 }
79
80 return target;
81 }
82
83 inline void handle_timepoint(Matrix& target, uint64_t& current_timepoint)
84 {
85 for (size_t i = 0; i < m_input_encoder.size(); i++)
86 {
87 uint64_t v = m_input_encoder.get_feat(i);
88 Adapter::set(target, current_timepoint, i, v);
89 }
90 current_timepoint++;
91 if (current_timepoint == std::numeric_limits<uint64_t>::max())
92 {
93 throw std::overflow_error("Too much characters in training set.");
94 }
95 }
96
97 InputEncoder m_input_encoder;
98};
99
100} // namespace train
101} // namespace segmentation
102} // namespace deeplima
103
104#endif
Word list for the segmenter's lexicon feature (ngram_descr_t::lexicon_ngram).
std::shared_ptr< Matrix > read_string(const std::string &text, uint64_t len)
void set_lexicon(const impl::SegmLexicon *lexicon)
std::shared_ptr< Matrix > process(const std::string &text, int64_t len=-1)
void handle_timepoint(Matrix &target, uint64_t &current_timepoint)
CharSeqVectorizerImpl(const std::vector< impl::ngram_descr_t > &ngram_descr)
Edit rules for lemmatizing words absent from the lemma dictionary.