forked from aymara/lima
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndirectDataDico.tcc
156 lines (138 loc) · 5 KB
/
IndirectDataDico.tcc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Copyright 2002-2013 CEA LIST
This file is part of LIMA.
LIMA is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LIMA is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with LIMA. If not, see <http://www.gnu.org/licenses/>
*/
/***************************************************************************
* Copyright (C) 2003 by CEA *
* author Olivier MESNARD [email protected] *
* *
* Compact dictionnary based on finite state automata implemented with *
* Boost Graph library. *
* Algorithm is described in article from Daciuk, Mihov, Watson & Watson: *
* "Incremental Construction of Minimal Acyclic Finite State Automata" *
***************************************************************************/
// For ::stat() function
#include <sys/types.h>
#include <sys/stat.h>
#ifndef WIN32
#include <unistd.h>
#endif
/*
#include "linguisticProcessing/core/Dictionary/DictionaryEntry.h"
#include "linguisticProcessing/core/Dictionary/DictionaryCode.h"
*/
namespace Lima {
namespace Common {
namespace StringMap {
template <typename accessMethod, typename contentElement>
IndirectDataDico<accessMethod, contentElement>::IndirectDataDico( const contentElement& defaultValue )
: StringMap<accessMethod, contentElement>( defaultValue ) , m_index2Data(0){
#ifdef DEBUG_CD
STRINGMAPLOGINIT;
LDEBUG << "IndirectDataDico::IndirectDataDico()";
#endif
}
template <typename accessMethod, typename contentElement>
IndirectDataDico<accessMethod, contentElement>::~IndirectDataDico() {
if( m_data != 0 )
delete [] m_data;
if( m_index2Data != 0 )
delete [] m_index2Data;
}
template <typename accessMethod, typename contentElement>
void IndirectDataDico<accessMethod, contentElement>::parseData( const std::string& dataFileName )
{
#ifdef DEBUG_CD
STRINGMAPLOGINIT;
LDEBUG << "IndirectDataDico::parseData(" << dataFileName << ")";
#endif
// create datas memory storage
struct stat sts;
uint64_t dataSize = 0;
// if( stat( dataFileName.c_str(), &sts) != 0)
stat( dataFileName.c_str(), &sts);
dataSize += sts.st_size;
std::cerr << "IndirectDataDico::parseData: sts.st_size = " << sts.st_size << std::endl;
m_data = new uint8_t [dataSize];
#ifdef DEBUG_CD
LDEBUG << "IndirectDataDico::parseData: allocate " << dataSize << " bytes";
#endif
if (m_data == NULL)
{
std::string mess = "IndirectDataDico::parseData: memory allocation error";
#ifdef DEBUG_CD
LERROR << mess;
#endif
throw( std::logic_error( mess ) );
}
// load data
FILE *dataFile = fopen(dataFileName.c_str(), "rb");
uint32_t totalDataReadSize = 0;
uint32_t readSize = 0;
if (dataFile == NULL)
{
LIMA_EXCEPTION_LOGINIT(
STRINGMAPLOGINIT,
"IndirectDataDico::parseData error cannot open data file "
<< dataFileName.c_str());
}
// fseek(dataFile, DATA_HEADER_SIZE, SEEK_SET); // skip header
#ifdef DEBUG_CD
LDEBUG << "IndirectDataDico::parseData: fread(" << totalDataReadSize
<< ", " << dataSize-totalDataReadSize << ")";
#endif
readSize = fread(m_data+totalDataReadSize, 1, dataSize-totalDataReadSize, dataFile); //_dataSize = max
#ifdef DEBUG_CD
LDEBUG << "IndirectDataDico::parseData: read " << readSize
<< " bytes from " << dataFileName;
#endif
totalDataReadSize += readSize;
fclose(dataFile);
if (totalDataReadSize != dataSize)
{
std::string mess = "IndirectDataDico::parseData: totalDataReadSize != _dataSize ";
#ifdef DEBUG_CD
LERROR << mess;
#endif
throw( std::logic_error( mess ) );
}
fillIndex2Data();
}
template <typename accessMethod, typename contentElement>
void IndirectDataDico<accessMethod, contentElement>::fillIndex2Data() {
// fill index2Data
uint64_t keyCount = this->getSize();
m_index2Data = new uint64_t[keyCount];
#ifdef DEBUG_CD
STRINGMAPLOGINIT;
LDEBUG << "IndirectDataDico::parseData: fill index_2data... ";
#endif
size_t ptrOffset = 0;
uint8_t *datasAddr = m_data;
for( uint32_t entry=0 ; entry < keyCount ; entry++ )
{
#ifdef DEBUG_CD
// LDEBUG << "IndirectDataDico::parseData: offset m_index2Data[" << entry << "]=" << ptrOffset;
#endif
ptrOffset = datasAddr - m_data;
m_index2Data[entry] = ptrOffset;
datasAddr += 2; // ???
// binaryEntry.nextField(datasAddr);
}
#ifdef DEBUG_CD
LDEBUG << "IndirectDataDico::parseData: end fill index_2data";
#endif
}
} // namespace StringMap
} // namespace Commmon
} // namespace Lima