LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
mwt_expander.h
Go to the documentation of this file.
1// Copyright 2021 CEA LIST
2// SPDX-FileCopyrightText: 2026 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6#ifndef DEEPLIMA_MWT_EXPANDER_H
7#define DEEPLIMA_MWT_EXPANDER_H
8
9#include <cstdint>
10#include <deque>
11#include <fstream>
12#include <functional>
13#include <stdexcept>
14#include <string>
15#include <unordered_map>
16#include <vector>
17
20#include "deeplima/token_type.h"
21
22namespace deeplima
23{
24
43{
44public:
45 typedef std::function<void(const std::vector<segmentation::token_pos>&, uint32_t)> callback_t;
46
47 explicit MwtExpander(const std::string& dict_fn)
48 : m_lexicon(dict_fn)
49 {
50 }
51
53 {
54 m_next = fn;
55 }
56
57 size_t size() const
58 {
59 return m_lexicon.size();
60 }
61
62 size_t rules_size() const
63 {
64 return m_lexicon.rules_size();
65 }
66
67 bool has_seq2seq() const
68 {
69 return m_lexicon.has_seq2seq();
70 }
71
72 // When true (the default), only surface tokens the segmenter flagged as
73 // multiword (token_flags_t::multiword) are expanded — the model decides, in
74 // context, which ambiguous forms (e.g. French "des") are contractions. Set
75 // false for a non-MWT-aware tokenizer to fall back to expanding every dict
76 // match (the original dictionary-only behaviour).
77 void set_require_flag(bool require_flag)
78 {
79 m_require_flag = require_flag;
80 }
81
82 void operator()(const std::vector<segmentation::token_pos>& tokens, uint32_t len)
83 {
84 m_out.clear();
85 m_storage.clear();
86
87 for (uint32_t i = 0; i < len; ++i)
88 {
89 const segmentation::token_pos& t = tokens[i];
90 const bool flagged = (t.m_flags & token_flags_t::multiword);
91 const std::string surface(t.m_pch, t.m_len);
92 // When gating on the model, only flagged surfaces are expanded, from the
93 // dictionary then by the rules; otherwise, every dictionary match.
94 if ((m_require_flag && !flagged) || !m_lexicon.expand(surface, m_words, m_require_flag)
95 || m_words.size() < 2)
96 {
97 // Not a multiword token: pass through unchanged.
98 m_out.push_back(t);
99 continue;
100 }
101
102 const std::vector<std::string>& words = m_words;
103
104 // Own the surface bytes for the range line. std::deque keeps element
105 // pointers/references valid across push_back, so m_pch stays valid until
106 // the downstream analyzer has interned everything (it does so
107 // synchronously inside m_next, before we clear m_storage on the next call).
108 m_storage.push_back(surface);
109 const char* surf_pch = m_storage.back().c_str();
110
111 for (size_t k = 0; k < words.size(); ++k)
112 {
113 m_storage.push_back(words[k]);
114
116 sub.m_pch = m_storage.back().c_str();
117 sub.m_len = uint16_t(words[k].size());
118 // The surface's leading whitespace attaches to the first sub-word; a
119 // sentence/paragraph break of the surface attaches to the last. The
120 // multiword flag belongs to the surface only, never to its sub-words.
121 sub.m_offset = (0 == k) ? t.m_offset : 0;
122 sub.m_flags = (k + 1 == words.size())
125 if (0 == k)
126 {
127 sub.m_mwt_len = uint8_t(words.size());
128 sub.m_mwt_surface_pch = surf_pch;
129 sub.m_mwt_surface_len = uint16_t(surface.size());
130 }
131 m_out.push_back(sub);
132 }
133 }
134
135 if (m_next)
136 {
137 m_next(m_out, uint32_t(m_out.size()));
138 }
139 }
140
141private:
142 MwtLexicon m_lexicon;
143 callback_t m_next;
144 bool m_require_flag = true;
145
146 // Scratch reused on every call.
147 std::vector<std::string> m_words;
148 std::vector<segmentation::token_pos> m_out;
149 std::deque<std::string> m_storage; // owns sub-word/surface bytes; pointer-stable
150};
151
152} // namespace deeplima
153
154#endif
size_t rules_size() const
void set_require_flag(bool require_flag)
void operator()(const std::vector< segmentation::token_pos > &tokens, uint32_t len)
MwtExpander(const std::string &dict_fn)
bool has_seq2seq() const
size_t size() const
void register_handler(const callback_t &fn)
std::function< void(const std::vector< segmentation::token_pos > &, uint32_t)> callback_t
Multiword-token (MWT) lexicon: how to expand a surface token (French "du") into its syntactic words (...
Definition mwt_lexicon.h:64
Edit rules for lemmatizing words absent from the lemma dictionary.