13#include "linguisticProcessing/core/Dictionary/DictionaryCode.h"
19#include "common/misc/FileUtils.h"
20#include "common/misc/strwstrtools.h"
23#include "linguisticProcessing/core/Dictionary/ParseBinary.h"
33#define CODE_HEADER_SIZE 12
37using namespace Lima::Common::LinguisticData;
41namespace LinguisticProcessing
48DictionaryCode::DictionaryCode() :
50 _mask(new uint64_t[LinguisticData::single().getNbCategories()]),
51 _catBit(new uint64_t[LinguisticData::single().getNbCategories()]),
58DictionaryCode::DictionaryCode(
const DictionaryCode& dico) :
65 m_language(
std::numeric_limits<unsigned char>::max())
70void DictionaryCode::init(
82 LDEBUG <<
"DictionaryCode::init";
84 m_language=manager->getInitializationParameters().language;
85 std::string resourcesPath=Common::LinguisticData::LinguisticData::single().getResourcesPath();
105 LERROR <<
"no param 'codeFile' in DictionaryCode group for language " << (int) m_language;
109 parse(codeFileName.toUtf8().constData());
113DictionaryCode::~DictionaryCode()
115 delete[] _mask; _mask = 0;
116 delete[] _catBit; _catBit = 0;
117 delete[] _codeHAccess; _codeHAccess = 0;
118 delete[] _codes; _codes = 0;
127void DictionaryCode::parse(
const std::string& codeFileName)
132 ParseBinary parseCode(codeFileName.c_str());
133 parseCode.advancePointer(4);
135 if (parseCode.getNumber(4) != S2_DICTIONARY_VERSION)
140 _codes =
new unsigned char[codeSize];
144 codeFile = fopen(codeFileName.c_str(),
"rb");
145 if (codeFile == NULL)
147 std::string mes =
"Open file error: " + codeFileName;
151 readSize = fread(_codes, 1, codeSize, codeFile);
153 if (readSize != codeSize)
throw ReadErrorException(
"readSize != codeSize in DictionaryCode::parse");
156 unsigned char *ptr = _codes;
157 uint64_t num = getEncodedNumber(ptr);
158 unsigned char *endCategory = num + ptr;
159 while (ptr < endCategory)
161 uint64_t catEnum = getEncodedNumber(ptr);
162 if (catEnum > LinguisticData::single().getNbCategories()-1)
164 _mask[catEnum] = getEncodedNumber(ptr);
165 _catBit[catEnum] = getEncodedNumber(ptr);
169 size_t maxLinguisticCode = LinguisticData::single().languageData(m_language).getNbMicroCategories();
170 _codeHAccess =
new CodeHAccess[maxLinguisticCode + 1];
171 for (uint64_t i=0; i<CODE_H_ACCESS_TABLE_SIZE; i++)
175 uint64_t num = getEncodedNumber(ptr);
176 unsigned char *endCode = num + ptr;
177 while (ptr < endCode)
180 uint64_t codeValue = getEncodedNumber(ptr);
181 if (codeEnum > maxLinguisticCode)
183 _codeHAccess[codeEnum]._enum = codeEnum;
184 _codeHAccess[codeEnum]._value = codeValue;
185 _codeHAccess[codeEnum]._stringAddr = ptr;
186 _codeHAccess[codeEnum]._next = _hTable[codeValue%CODE_H_ACCESS_TABLE_SIZE];
187 _hTable[codeValue%CODE_H_ACCESS_TABLE_SIZE] = &_codeHAccess[codeEnum];
194bool DictionaryCode::isThisCode(
const uint64_t property,
195 const LinguisticCategory category,
198 if (category <= L_NONE || category > (LinguisticData::single().getNbCategories()-1))
200 if (code <= NONE_1 || code >= LinguisticData::single().languageData(m_language).getNbMicroCategories())
202 return (_codeHAccess[code]._value == (property & _mask[category]));
207 const LinguisticCategory category)
209 if (category <= L_NONE || category >= LinguisticData::single().languageData(m_language).getNbMicroCategories())
211 if (code <= NONE_1 || code > LinguisticData::single().languageData(m_language).getNbMicroCategories())
213 return (_codeHAccess[code]._value) - (_catBit[category]);
216uint64_t DictionaryCode::setCategory(
217 const uint64_t property,
219 const LinguisticCategory category)
221 uint64_t newCategoryValue = categoryValue(code, category);
222 uint64_t newProperty = (
property & (!_mask[category]) ) & newCategoryValue;
228 const LinguisticCategory category)
const
230 if (category <= L_NONE || category > (LinguisticData::single().getNbCategories()-1))
232 uint64_t codeValue =
property & (_mask[category]) | (_catBit[category]);
233 CodeHAccess *codeHAccess = _hTable[codeValue%CODE_H_ACCESS_TABLE_SIZE];
234 while (codeHAccess != NULL)
236 if (codeHAccess->_value == codeValue)
237 return codeHAccess->_enum;
238 codeHAccess = codeHAccess->_next;
245LinguisticCode DictionaryCode::unpositionedCode(
const uint64_t property,
246 const LinguisticCategory category)
const
248 if (category <= L_NONE || category > (LinguisticData::single().getNbCategories()-1))
250 uint64_t codeValue = property;
251 uint64_t mask = _mask[category];
252 while ((mask & 1) == 0)
257 codeValue |= _catBit[category];
258 CodeHAccess *codeHAccess = _hTable[codeValue%CODE_H_ACCESS_TABLE_SIZE];
259 while (codeHAccess != NULL)
261 if (codeHAccess->_value == codeValue)
262 return codeHAccess->_enum;
263 codeHAccess = codeHAccess->_next;
269LimaString DictionaryCode::stringCode(
const uint64_t property,
270 const LinguisticCategory category)
const
272 if (category <= L_NONE || category > (LinguisticData::single().getNbCategories()-1))
274 uint64_t codeValue =
property & _mask[category] | (_catBit[category]);
275 CodeHAccess *codeHAccess = _hTable[codeValue%CODE_H_ACCESS_TABLE_SIZE];
276 while (codeHAccess != NULL)
278 if (codeHAccess->_value == codeValue)
279 return getStringNoMove(codeHAccess->_stringAddr);
280 codeHAccess = codeHAccess->_next;
288 if (code > LinguisticData::single().languageData(m_language).getNbMicroCategories())
throw BoundsErrorException();
289 if ((code ==
NONE_1) || (_codeHAccess[code]._stringAddr == 0))
291 LimaString res = getStringNoMove(_codeHAccess[code]._stringAddr);
339LinguisticCode DictionaryCode::code(
const std::string& strCode)
const
341 return LinguisticData::single().languageData(m_language).getMicroCategory(strCode);
#define DICTIONARYLOGINIT
Defines a Factory to create Object of type Base.
std::string & getParamsValueAtKey(const std::string &key)
return a message when a 'param' was not found
Use this exception to signal an error in one of the configuration files.
QString findFileInPaths(const QString &paths, const QString &fileName, const QChar &separator)
Find the given file in the given paths.
SimpleFactory< AbstractResource, DictionaryCode > dictionaryCodeFactory(DICTIONARYCODE_CLASSID)
launch exception related to the configuration file parsing