LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
char_ngram.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_SEGMENTATION_IMPL_CHAR_NGRAM_H
7#define DEEPLIMA_SEGMENTATION_IMPL_CHAR_NGRAM_H
8
9namespace deeplima
10{
11namespace segmentation
12{
13namespace impl
14{
15
17{
18 enum ngram_type_t : uint8_t
19 {
20 char_ngram = 0x00,
21 type_ngram = 0x01,
22 len_ngram = 0x02,
24 lexicon_ngram = 0x04, // words of a lexicon ending here (SegmLexicon)
26 };
27
28 int8_t m_offset; // from the current character
29 uint8_t m_length;
31
32 ngram_descr_t(int8_t offset = 0, uint8_t length = 0, ngram_type_t type = char_ngram)
33 : m_offset(offset), m_length(length), m_type(type) {}
34
35 explicit ngram_descr_t(const std::string& serialized_form)
37 {
38 from_string(serialized_form);
39 }
40
41 friend inline bool operator<(const ngram_descr_t& l, const ngram_descr_t& r)
42 {
43 return std::tie(l.m_type, l.m_length, l.m_offset) < std::tie(r.m_type, r.m_length, r.m_offset);
44 }
45
46 inline bool operator==(const ngram_descr_t& other)
47 {
48 return (other.m_type == m_type) && (other.m_length == m_length) && (other.m_offset == m_offset);
49 }
50
51 std::string to_string() const
52 {
53 std::ostringstream s;
54 s << (int)m_type << " " << (int)m_length << " " << (int)m_offset;
55 return s.str();
56 }
57
58 void from_string(const std::string& str)
59 {
60 std::istringstream s(str);
61 int temp = 0;
62
63 s >> temp;
64 assert(temp >= 0);
65 assert(temp < ngram_descr_t::max_ngram_type);
66 m_type = ngram_type_t(temp);
67
68 s >> temp;
69 assert(temp > 0);
70 assert(temp < std::numeric_limits<uint8_t>::max());
71 m_length = (uint8_t)temp;
72
73 s >> temp;
74 assert(temp > std::numeric_limits<int8_t>::min());
75 assert(temp < std::numeric_limits<int8_t>::max());
76 m_offset = (int8_t)temp;
77 }
78};
79
80} // namespace impl
81} // namespace segmentation
82} // namespace deeplima
83
84#endif
Edit rules for lemmatizing words absent from the lemma dictionary.
ngram_descr_t(int8_t offset=0, uint8_t length=0, ngram_type_t type=char_ngram)
Definition char_ngram.h:32
bool operator==(const ngram_descr_t &other)
Definition char_ngram.h:46
ngram_descr_t(const std::string &serialized_form)
Definition char_ngram.h:35
friend bool operator<(const ngram_descr_t &l, const ngram_descr_t &r)
Definition char_ngram.h:41
void from_string(const std::string &str)
Definition char_ngram.h:58