6#ifndef DEEPLIMA_DEPENDENCY_PARSER
7#define DEEPLIMA_DEPENDENCY_PARSER
87 inline bool eos()
const
93 inline uint32_t
cls(
size_t idx)
const
99 inline const std::string&
form()
const
116 : m_data(data), m_token(stridx) { }
118 inline typename tokens_with_analysis_t::size_type
size()
const
120 return m_data.size();
133 const std::vector<token_with_analysis_t>& m_buffer;
134 std::shared_ptr< StdMatrix<uint32_t> > m_heads;
138 const std::vector<std::string>* m_rel_names;
142 const std::vector<token_with_analysis_t>& buffer,
146 const std::vector<std::string>* rel_names =
nullptr)
147 : m_stridx(stridx), m_buffer(buffer), m_heads(heads),
148 m_current(0), m_offset(offset), m_end(
end - offset), m_rel_names(rel_names)
150 assert(
end > offset + 1);
155 return m_current >= m_end;
161 return m_buffer[m_current].m_flags;
166 return m_buffer[m_current].m_offset;
171 return m_buffer[m_current].m_len;
176 return m_buffer[m_current].m_form_idx;
181 throw std::runtime_error(
"TokenIterator::lemma_idx");
187 return m_heads->get(m_offset + m_current, 0);
191 inline uint32_t
rel()
const
193 return (m_heads->size() >= 2) ? m_heads->get(m_offset + m_current, 1) : 0;
199 if (
nullptr != m_rel_names &&
rel() < m_rel_names->size())
201 return (*m_rel_names)[
rel()].c_str();
206 inline const char*
form()
const
209 const std::string& f = m_stridx.get_str(m_buffer[m_current].m_form_idx);
216 const std::string& f = m_stridx.get_str(m_buffer[m_current].m_lemm_idx);
224 return m_buffer[m_current].m_mwt_len;
229 return m_stridx.get_str(m_buffer[m_current].m_mwt_surface_idx).c_str();
249 assert(m_offset == 0);
250 uint8_t val = m_buffer[m_current].m_classes[cls_idx];
255 typedef std::function < void (
const StringIndex& stridx,
256 const std::vector<token_with_analysis_t>& tokens,
263 std::shared_ptr< StringIndex > stridx,
264 const std::vector<std::string>& input_class_names,
276 assert(num_buffers > 0);
279 m_impl.load(model_fn, path_resolver);
284 size_t eff_threads = std::max<size_t>(1, std::min(threads, num_buffers));
287 for (
size_t i = 0; i < num_buffers; ++i)
292 m_impl.register_handler([
this](
294 size_t begin,
size_t end,
size_t slot_idx) {
320 return m_impl.get_rel_class_names();
328 void set_classes(
size_t idx,
const std::string& class_name,
const std::vector<std::string>& data)
345 if (feat_name ==
"upos" || feat_name ==
"xpos" || feat_name ==
"eos")
364 size_t first_timepoint_idx = 0;
382 bool insert_root = (0 == pending);
383 bool sentence_closed =
true;
432 for (
size_t i = 0; i <
m_classes.size(); ++i)
486 std::this_thread::sleep_for(std::chrono::milliseconds(500));
510 m_impl.send_all_results();
521 while (next_buffer.
locked())
524 m_impl.send_next_results();
526 assert(!next_buffer.
locked());
541 const size_t open_len =
m_lengths.back();
552 for (
size_t i = 0; i < open_len; ++i)
562 size_t first_timepoint_idx,
563 const std::vector<size_t>& lengths,
572 m_impl.handle_token_buffer(buffer_idx,
581 size_t current_iter_pos = iter.
position();
582 size_t tokens_counter = 0;
590 throw std::runtime_error(
"No place for new tokens in this buffer");
595 this_sentence_tokens++;
610 lengths.push_back(this_sentence_tokens);
611 tokens_counter += this_sentence_tokens;
615 this_sentence_tokens = 0;
621 this_sentence_tokens = 0;
624 this_sentence_tokens = 1;
637 if (tokens_counter > 0)
639 this_sentence_tokens = 0;
647 if (this_sentence_tokens > 0)
649 lengths.push_back(this_sentence_tokens);
650 tokens_counter += this_sentence_tokens;
653 iter.
reset(current_iter_pos);
656 return tokens_counter;
673#ifdef DP_VECTORIZER_WITH_PRECOMPUTING
674 using Vectorizer = FeaturesVectorizerWithPrecomputing<
678#elif DP_VECTORIZER_WITH_CACHE
679 using Vectorizer = FeaturesVectorizerWithCache<
715 auto p = std::make_shared<EmbdUInt64Float>();
726 for (
size_t i = 0; i < this->get_input_str_dicts().size(); ++i)
732 m_fastText->load(path_resolver.
resolve(
"embd", deeplima::graph_dp::impl::GraphDependencyParser::get_embd_fn(0), {
"bin",
"ftz"}));
737 size_t buffer_size_per_thread,
739 const std::vector<std::string>& class_names)
741 m_fastText->get_words([&stridx](
const std::string& word){ stridx.
get_idx(word); });
743 std::vector<typename Vectorizer::feature_descr_t> feats;
754 const auto& dp_input_names =
755 deeplima::graph_dp::impl::GraphDependencyParser::get_input_str_dicts_names();
756 for (
size_t j = 0; j + 1 < dp_input_names.size(); ++j)
758 const std::string& feat_name = dp_input_names[j + 1];
760 size_t tagger_col = Vectorizer::uint_feat_extractor_t::NO_COLUMN;
761 for (
size_t i = 0; i < class_names.size(); ++i)
763 if (class_names[i] == feat_name)
770 if (Vectorizer::uint_feat_extractor_t::NO_COLUMN == tagger_col)
772 std::cerr <<
"Warning: tagger does not produce feature '" << feat_name
773 <<
"' expected by the dependency parser; using UNK." << std::endl;
777 m_vectorizer.get_uint_feat_extractor().add_feature(feat_name, tagger_col);
801 0, num_buffers, buffer_size_per_thread, threads,
867 while (lock_count > 1)
905 while (lock_count > 1)
950 while (lock_count > 1)
976 size_t first_timepoint_idx,
978 const std::vector<size_t>& lengths,
979 int timepoints_to_analyze = -1)
989 size_t count = (timepoints_to_analyze > 0) ? timepoints_to_analyze : buffer.size();
996 (void) first_timepoint_idx;
998 for (
size_t i = 0; i < count; i++)
1039 std::shared_ptr<FastTextVectorizer<eigen_wrp::EigenMatrixXf::matrix_t, Eigen::Index>>
m_fastText;
virtual void load(const std::string &fn, const PathResolver &path_resolver)
virtual void handle_token_buffer(size_t slot_no, size_t first_timepoint_idx, const typename Vectorizer::dataset_t &buffer, const std::vector< size_t > &lengths, int timepoints_to_analyze=-1)
void precompute_inputs(const typename Vectorizer::dataset_t &buffer)
int32_t m_last_completed_slot
void send_results(int32_t slot_idx)
int32_t m_current_slot_no
virtual void register_handler(const tagging_callback_t fn)
uint32_t m_current_slot_timepoints
uint64_t m_current_timepoint
void send_results_if_available()
std::shared_ptr< EmbdUInt64Float > convert(const EmbdStrFloat &src)
std::shared_ptr< FastTextVectorizer< eigen_wrp::EigenMatrixXf::matrix_t, Eigen::Index > > m_fastText
void increment_timepoint(uint64_t &timepoint)
std::function< void(std::shared_ptr< StdMatrix< uint32_t > > classes, size_t begin, size_t end, size_t slot_idx) > tagging_callback_t
tagging_callback_t m_callback
void init(size_t threads, size_t num_buffers, size_t buffer_size_per_thread, StringIndex &stridx, const std::vector< std::string > &class_names)
void no_more_data(size_t slot_no)
std::vector< std::shared_ptr< FeatureVectorizerBase< Eigen::Index > > > m_featVectorizers
void acquire_slot(size_t slot_no)
const char * form() const
void reset(size_t position=0)
const char * mwt_surface() const
uint8_t token_class(size_t cls_idx) const
uint16_t token_offset() const
token_flags_t flags() const
const char * deprel() const
uint16_t token_len() const
uint32_t lemma_idx() const
const char * lemma() const
TokenIterator(const StringIndex &stridx, const std::vector< token_with_analysis_t > &buffer, std::shared_ptr< StdMatrix< uint32_t > > heads, size_t offset, size_t end, const std::vector< std::string > *rel_names=nullptr)
uint32_t form_idx() const
const enriched_token_t & operator[](size_t idx) const
tokens_with_analysis_t::size_type size() const
enriched_token_buffer_t(const tokens_with_analysis_t &data, const StringIndex &stridx)
const tokens_with_analysis_t::token_t * m_ptoken
const StringIndex & m_stridx
friend class DependencyParser
enriched_token_t(const StringIndex &stridx)
uint32_t cls(size_t idx) const
void set_token(const typename tokens_with_analysis_t::token_t *p)
const std::string & form() const
token_flags_t flags() const
std::shared_ptr< StringIndex > m_stridx_ptr
const std::vector< std::string > & get_rel_class_names() const
std::vector< std::string > m_class_names
void setStringIndex(std::shared_ptr< deeplima::StringIndex > stringIndexPtr)
void register_handler(const output_callback_t fn)
void set_classes(size_t idx, const std::string &class_name, const std::vector< std::string > &data)
StringIndex::idx_t m_unk_idx
std::vector< std::vector< std::string > > m_classes
void start_analysis(size_t buffer_idx, size_t first_timepoint_idx, const std::vector< size_t > &lengths, int count=-1)
output_callback_t m_output_callback
size_t m_current_timepoint
void operator()(TokenSequenceAnalyzer<>::TokenIterator &iter)
size_t count_max_tokens_until_eos(TokenSequenceAnalyzer<>::TokenIterator &iter, std::vector< size_t > &lengths)
eigen_wrp::EigenMatrixXf Matrix
DependencyParser(const std::string &model_fn, const PathResolver &path_resolver, std::shared_ptr< StringIndex > stridx, const std::vector< std::string > &input_class_names, size_t buffer_size, size_t num_buffers, size_t threads=1)
std::vector< tokens_with_analysis_t > m_buffers
std::vector< size_t > m_lengths
std::function< void(const StringIndex &stridx, const std::vector< token_with_analysis_t > &tokens, std::shared_ptr< StdMatrix< uint32_t > > heads, size_t begin, size_t end) > output_callback_t
void carry_open_sentence(size_t first_timepoint_idx)
deeplima::graph_dp::eigen_impl::FeaturesVectorizer< enriched_token_buffer_t, typename enriched_token_buffer_t::token_t > Vectorizer
const M & get_tensor() const
std::string resolve(const std::string &prefix, const std::string &path, const std::vector< std::string > &accepted_ext={}) const
bool get_slot_started(uint32_t idx) const
int32_t next_slot(uint32_t idx)
uint64_t get_slot_end(uint32_t idx) const
virtual void init(uint32_t max_feat, uint32_t overlap, uint32_t num_slots, uint32_t slot_len, uint32_t num_threads, bool precomputed_input=false)
void load(const std::string &fn)
void increment_timepoint(uint64_t &timepoint)
uint8_t get_lock_count(uint32_t idx) const
uint64_t get_slot_base(uint32_t idx) const
std::shared_ptr< StdMatrix< uint32_t > > get_output()
void set_slot_begin(uint32_t idx, uint64_t slot_begin)
void set_slot_end(uint32_t idx, uint64_t slot_end)
void increment_lock_count(uint32_t idx, uint8_t v=1)
void decrement_lock_count(uint32_t idx)
uint64_t get_slot_begin(uint32_t idx) const
void start_job(uint32_t idx, bool no_more_data=false)
void set_slot_lengths(uint32_t idx, const std::vector< size_t > &lengths)
void wait_for_slot(uint32_t idx)
uint64_t get_start_timepoint() const
idx_t get_idx(const char *p, size_t len)
uint32_t form_idx() const
uint32_t lemma_idx() 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
void reset(size_t position=0)
const Eigen::MatrixXf & get_tensor() const
A kind of RnnSequenceClassifier, used for named entities tagging (?), but also the parent of TaggingI...
EigenMatrix< Eigen::MatrixXf > EigenMatrixXf
RnnSequenceClassifier< Model, BaseMatrix, uint32_t > GraphDependencyParser
Edit rules for lemmatizing words absent from the lemma dictionary.
TStringIndex< std::string, std::u32string, uint32_t > StringIndex
EmbdDict< std::string, Eigen::MatrixXf, Eigen::Index > EmbdStrFloat
virtual ~token_with_analysis_t()=default
token_with_analysis_t(size_t num_classes)
token_with_analysis_t(const token_with_analysis_t &t)=default
std::vector< uint8_t > m_classes
token_with_analysis_t & operator=(const token_with_analysis_t &t)=default
token_buffer_t< token_with_analysis_t > Parent
tokens_with_analysis_t(size_t size, size_t num_classes)
token_with_analysis_t token_t
uint32_t m_mwt_surface_idx
token_t(uint16_t offset=0, uint16_t len=0, uint32_t idx=0, token_flags_t flags=token_flags_t::none)