LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
LingInfoEntry.cpp
Go to the documentation of this file.
1// Copyright 2002-2020 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6// NAUTITIA
7//
8// jys 1-OCT-2002
9//
10// LingInfoEntry is the way to access dictionary linguistic infos datas.
11// LingInfoEntry manages recurent calls to lists of linguistic properties..
12// Dictionaries in memory are specified in NA2002.r2128 JYS
13// Dictionaries iterators are specified in NA2002.r2129 JYS
15// <lingInfoEntry> ::= <Len4> <lemma> <normalized> <LingPropertiesOffset>
16// <lemma> ::= <0> | <StringOffset>
17// <normalized> ::= <0> | <StringOffset>
18// <lingProperties> ::= <Len7> { <LingPropertyEntry> }
19
20#include "LingInfoEntry.h"
21
23
24using namespace Lima::Common::Misc;
25
26namespace Lima {
27namespace LinguisticProcessing {
28namespace Dictionary {
29
32 m_key(),
33 m_stringStartAddr(NULL),
34 m_lingPropertiesStartAddr(NULL),
35 m_startAddr(NULL),
36 m_endAddr(NULL),
37 m_lingProperty(NULL)
38{
39}
40
41// copy constructor. Copies the specified LingInfoEntry
42// into self. Internal iteration pointers of self are reset
45 m_key(entry.m_key),
46 m_stringStartAddr(entry.m_stringStartAddr),
47 m_lingPropertiesStartAddr(entry.m_lingPropertiesStartAddr),
48 m_startAddr(entry.m_startAddr),
49 m_endAddr(NULL),
50 m_lingProperty(NULL)
51{
52 reset();
53}
54
56 unsigned char *stringStartAddr,
57 unsigned char *lingPropertiesStartAddr,
58 unsigned char *startAddr) :
60 m_key(key),
61 m_stringStartAddr(stringStartAddr),
62 m_lingPropertiesStartAddr(lingPropertiesStartAddr),
63 m_startAddr(startAddr),
64 m_endAddr(NULL),
65 m_lingProperty(NULL)
66{
67 reset();
68}
69
70// Destroys LingInfoEntry
74
75// Assignment operator. Copies the specified LingInfoEntry
76// into self. Returns a reference to self.
77// After assignment, internal iteration pointers of self are reset
79{
80 m_key = lingInfo.m_key;
81 m_stringStartAddr = lingInfo.m_stringStartAddr;
82 m_lingPropertiesStartAddr = lingInfo.m_lingPropertiesStartAddr;
83 m_startAddr = lingInfo.m_startAddr;
84 reset();
85 return *this;
86}
87
88// Reset iteration pointer
89// <lingInfoEntry> ::= <Len4> <lemma> <normalized> <LingPropertiesOffset>
90// <lemma> ::= <0> | <StringOffset>
91// <normalized> ::= <0> | <StringOffset>
92// <lingProperties> ::= <Len7> { <LingPropertyEntry> }
94{
95 if (m_startAddr != NULL)
96 {
97 unsigned char *ptr = m_startAddr;
98 uint64_t num = getEncodedNumber(ptr);
99 m_endAddr = num + ptr; //<Len4>
100 getEncodedNumber(ptr); // skip <lemma>
101 getEncodedNumber(ptr); // skip <normalized>
102 uint64_t offset = getEncodedNumber(ptr); //<LingPropertiesOffset>
103 ptr = m_lingPropertiesStartAddr + offset;
104 if (offset == 0) // when no ling properties
105 {
106 m_endLingProperty = ptr;
107 }
108 else
109 {
110 uint64_t num = getEncodedNumber(ptr);
111 m_endLingProperty = num + ptr; //<Len7>
112 }
113 m_lingProperty = ptr;
114 }
115 else {
116 m_endAddr = NULL;
117 m_lingProperty = NULL;
118 }
119 return *this;
120}
121
122// Returns true if self is empty
124{
125 return (m_startAddr == NULL);
126}
127
128// Returns lemma string
129// If self is empty, EmptyEntryException is raised
130// <lingInfoEntry> ::= <Len4> <lemma> <normalized> <LingPropertiesOffset>
131// <lemma> ::= <0> | <StringOffset>
133{
134 if (m_startAddr == NULL)
135 {
136 throw EmptyEntryException();
137 }
138 unsigned char *ptr = m_startAddr;
139 getEncodedNumber(ptr); //skip <Len4>
140 uint64_t offset = getEncodedNumber(ptr);
141 if (offset != 0)
142 {
143 return getUtf8StringNoMove(m_stringStartAddr + offset); //lemma
144 }
145 return m_key; //key when lemma=0
146}
147
148// Returns normalized string
149// If self is empty, EmptyEntryException is raised
150// <lingInfoEntry> ::= <Len4> <lemma> <normalized> <LingPropertiesOffset>
151// <lemma> ::= <0> | <StringOffset>
152// <normalized> ::= <0> | <StringOffset>
154{
155 if (m_startAddr == NULL)
156 {
157 throw EmptyEntryException();
158 }
159 unsigned char *ptr = m_startAddr;
160 getEncodedNumber(ptr); //skip <Len4>
161 unsigned char *ptrLemma = ptr;
162 getEncodedNumber(ptr); //skip lemma
163 uint64_t offset = getEncodedNumber(ptr);
164 if (offset != 0)
165 {
166 return getUtf8StringNoMove(m_stringStartAddr + offset); //normalized
167 }
168 offset = getEncodedNumber(ptrLemma);
169 if (offset != 0)
170 {
171 return getUtf8StringNoMove(m_stringStartAddr + offset);//lemma when normalized=0
172 }
173 return m_key; //key when normalized=lemma=0
174}
175
176// Gets the next LingPropertyEntry if exists
177// This call "increments" the internal LingPropertyEntry pointer.
178// If there is no LingPropertyEntry anymore, an empty LingPropertyEntry is returned
179// If self is empty, EmptyEntryException is raised
181{
182 if (m_startAddr == NULL)
183 {
184 throw EmptyEntryException();
185 }
186 if (m_lingProperty < m_endLingProperty)
187 {
189 }
190 else
191 {
192 return LingPropertyEntry();
193 }
194}
195
196} // closing namespace Dictionary
197} // closing namespace LinguisticProcessing
198} // closing namespace Lima
static LinguisticCode decodeFromBinary(std::istream &is)
Definition StdBitset.h:197
uint64_t getEncodedNumber(unsigned char *&ptr) const
Lima::LimaString getUtf8StringNoMove(unsigned char *ptr) const
LingInfoEntry & operator=(const LingInfoEntry &)
NAUTITIA.
QString LimaString
Definition LimaString.h:33