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,
25 };
26
27 int8_t m_offset; // from the current character
28 uint8_t m_length;
30
31 ngram_descr_t(int8_t offset = 0, uint8_t length = 0, ngram_type_t type = char_ngram)
32 : m_offset(offset), m_length(length), m_type(type) {}
33
34 explicit ngram_descr_t(const std::string& serialized_form)
36 {
37 from_string(serialized_form);
38 }
39
40 friend inline bool operator<(const ngram_descr_t& l, const ngram_descr_t& r)
41 {
42 return std::tie(l.m_type, l.m_length, l.m_offset) < std::tie(r.m_type, r.m_length, r.m_offset);
43 }
44
45 inline bool operator==(const ngram_descr_t& other)
46 {
47 return (other.m_type == m_type) && (other.m_length == m_length) && (other.m_offset == m_offset);
48 }
49
50 std::string to_string() const
51 {
52 std::ostringstream s;
53 s << (int)m_type << " " << (int)m_length << " " << (int)m_offset;
54 return s.str();
55 }
56
57 void from_string(const std::string& str)
58 {
59 std::istringstream s(str);
60 int temp = 0;
61
62 s >> temp;
63 assert(temp >= 0);
64 assert(temp < ngram_descr_t::max_ngram_type);
65 m_type = ngram_type_t(temp);
66
67 s >> temp;
68 assert(temp > 0);
69 assert(temp < std::numeric_limits<uint8_t>::max());
70 m_length = (uint8_t)temp;
71
72 s >> temp;
73 assert(temp > std::numeric_limits<int8_t>::min());
74 assert(temp < std::numeric_limits<int8_t>::max());
75 m_offset = (int8_t)temp;
76 }
77};
78
79} // namespace impl
80} // namespace segmentation
81} // namespace deeplima
82
83#endif
ngram_descr_t(int8_t offset=0, uint8_t length=0, ngram_type_t type=char_ngram)
Definition char_ngram.h:31
bool operator==(const ngram_descr_t &other)
Definition char_ngram.h:45
ngram_descr_t(const std::string &serialized_form)
Definition char_ngram.h:34
friend bool operator<(const ngram_descr_t &l, const ngram_descr_t &r)
Definition char_ngram.h:40
void from_string(const std::string &str)
Definition char_ngram.h:57