LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
FsaAccess16.h
Go to the documentation of this file.
1// Copyright 2002-2013 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6/***************************************************************************
7 compactDict.h - description
8 -------------------
9 begin : mer mai 28 2003
10 copyright : (C) 2003 by Olivier Mesnard
11 email : olivier.mesnard@cea.fr
12 ***************************************************************************/
13
14/***************************************************************************
15 * *
16 * Compact dictionnary based on finite state automata implemented with *
17 * Boost Graph library. *
18 * Algorithm is described in article from Daciuk, Mihov, Watson & Watson: *
19 * "Incremental Construction of Minimal Acyclic Finite State Automata" *
20 * How to use it to compute hash code is explained in 'perfect hashing' *
21 * of document http://odur.let.rug.nl/alfa/fsa_stuff/#PerfHash *
22 * *
23 ***************************************************************************/
24
25#ifndef FSA_ACCESS_16_H
26#define FSA_ACCESS_16_H
27
31#include "FsaExceptions.h"
32#include "FsaAccessHeader.h"
33#include "FsaAccessIOHandler.h"
34#include "PrefixIterator.h"
35
36// From standard library
37#include <fstream>
38#include <map>
39#include <algorithm>
40#include <iostream>
41#include <vector>
42#include <string>
43
44// // From boost library
45#include <boost/config.hpp>
46
47// // From ICU
48#define U16_IS_LEAD(c) (((c)&0xfffffc00)==0xd800)
49#define U16_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00)
50#define U16_SURROGATE_OFFSET ((0xd800<<10UL)+0xdc00-0x10000)
51#define U16_GET_SUPPLEMENTARY(lead, trail) \
52(((char32_t)(lead)<<10UL)+(char32_t)(trail)-U16_SURROGATE_OFFSET)
53
54#define U16_LEAD(supplementary) (char32_t)(((supplementary)>>10)+0xd7c0)
55#define U16_TRAIL(supplementary) (char32_t)(((supplementary)&0x3ff)|0xdc00)
56
57#define U_IS_SURROGATE(c) (((c)&0xfffff800)==0xd800)
58#define U16_IS_SINGLE(c) !U_IS_SURROGATE(c)
59#define U16_IS_SURROGATE(c) U_IS_SURROGATE(c)
60#define U16_IS_SURROGATE_LEAD(c) (((c)&0x400)==0)
61
62#define U16_NEXT(s, i, length, c) { \
63(c)=(s)[(i)++].unicode(); \
64if(U16_IS_LEAD(c)) { \
65 uint16_t __c2; \
66 if((i)<(length) && U16_IS_TRAIL(__c2=(s)[(i)].unicode())) { \
67 ++(i); \
68 (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
69 } \
70 } \
71 }
72enum vertex_text_t { vertex_text = 4000 };
73namespace boost {BOOST_INSTALL_PROPERTY(vertex, text);}
75namespace boost {BOOST_INSTALL_PROPERTY(vertex, count);}
76
77namespace Lima {
78namespace Common {
79namespace FsaAccess {
80
81// TODO: use boost::tuple to implement state property (FINAL,HEAD_OF_CLASS,CONFLUENT,HASH_NUM)
82
84 FINAL_16= 0x20000000,
85 HEAD_OF_CLASS_16=0x40000000,
86 SET_16= 0x80000000,
87 QUALITY_16= 0xE0000000,
88 TEXT_POS_16= 0x1FFFFFFF } ;
89
90template <typename graphType>
92
93 // type of the 'vertex_name' property (with const_type modifier!!)
94 // used to store number of characters of text whose codepoint can be
95 // stored on UTF-16 word
96 // The 3 most significatnt bits are reserved to store status of vertex:
97 // final, confluent or head of class
98 typedef typename boost::property_map<graphType,boost::vertex_name_t>::const_type
100 typedef typename boost::property_map<graphType,boost::vertex_name_t>::type
102
103 // type of the 'vertex_text' property used to store
104 // character (ordered according to codepoint) in relation with transition
105 typedef typename boost::property_map<graphType,vertex_text_t>::const_type
107 typedef typename boost::property_map<graphType,vertex_text_t>::type
109
110 // type of the 'vertex_count' property used to store
111 // number of elements in subautomata to compute hash function
112 typedef typename boost::property_map<graphType,vertex_count_t>::const_type
114 typedef typename boost::property_map<graphType,vertex_count_t>::type
116};
117
119{
122 typedef boost::property< vertex_text_t, LimaString >
128 typedef boost::property< boost::vertex_name_t, VERTEX_PROPERTY_16, dicoVertexTextProperty>
132 typedef boost::property< vertex_count_t, std::vector<int>, dicoVertexStatusProperty>
136 typedef boost::adjacency_list<boost::vecS,
137 boost::vecS,
138 boost::bidirectionalS,
141
145 typedef boost::adjacency_list<boost::vecS,
146 boost::listS,
147// boost::vecS,
148 boost::bidirectionalS,
151};
152
153// Helpers to print graph (for trace and debug) used by Graphviz functions
154template <typename vname_map_type, typename vtext_map_type, typename vcount_map_type,
155 typename dicoVertex> class dicoVertexWriter16 {
156 public:
157 dicoVertexWriter16(vname_map_type vname_map,
158 vtext_map_type vtext_map, vcount_map_type vcount_map);
159 void operator()(std::ostream& out, const dicoVertex& v) const;
160
161 private:
162 vname_map_type m_vname_map;
163 vtext_map_type m_vtext_map;
164 vcount_map_type m_vcount_map;
165 };
166
167template <typename dicoEdge> class dicoEdgeWriter16 {
168 public:
170 void operator()(std::ostream& out, const dicoEdge& edge ) const {
171 LIMA_UNUSED(out)
172 LIMA_UNUSED(edge)
173 }
174 private:
175 };
176
177#define dicoVertexType typename boost::graph_traits<graphType>::vertex_descriptor
178
179template <typename graphType>
181
182public:
183
186 typedef typename boost::graph_traits<graphType>::edge_descriptor dicoEdgeType;
187
188 typedef typename boost::graph_traits<graphType>::degree_size_type dico_degree_size_type;
189
191
192 FsaAccess16(bool trie_direction_fwd);
193 virtual ~FsaAccess16() {}
194// friend std::ostream& operator << <graphType>(std::ostream& os, const FsaAccess16& dico);
195 virtual void print( std::ostream &os ) const;
196 virtual void printGraph( std::ostream &os ) const;
197 void pack();
198
200 void checkIntegrity( dicoVertexType from ) const;
201
202protected:
203 // For Builder and BuilderRandom
217 std::pair<const dicoVertexType,bool> findEquivalentInRegister( dicoVertexType tempState );
218
221 bool equivalent( dicoVertexType referenceState, dicoVertexType candidateState ) const;
222
226
233 dicoVertexType from );
236
237
241 const uint64_t offset = 0) const;
242
248 PrefixIterator* prefixIt ) const;
249
250 virtual void addSuffix( dicoVertexType from,
251 PrefixIterator* prefixIt );
252
253 graphType m_graph;
256
257 uint64_t m_size;
258
259private:
260 void print( std::ostream &os, dicoVertexType from,
261 LimaString &prefix ) const;
262
264 dicoVertexType find0degreeVertex(dicoVertexType from ) const;
265protected:
273 int32_t findEdge( const char32_t searchChar,
274 const LimaString& textString,
275 int32_t min, int range, int nb_unit_for_char ) const;
276
282 int32_t findOffsetToInsertBefore( const char32_t searchChar,
283 const LimaString& textString,
284 int32_t min, int range, int nb_unit_per_char ) const;
285};
286
287} // namespace FsaAccess
288} // namespace Common
289} // namespace Lima
290
291#include "common/FsaAccess/FsaAccess16.tcc"
292
293#endif //FSA_ACCESS_16_H
vertex_text_t
Definition FsaAccess16.h:72
@ vertex_text
Definition FsaAccess16.h:72
#define dicoVertexType
vertex_count_t
Definition FsaAccess16.h:74
@ vertex_count
Definition FsaAccess16.h:74
#define LIMA_UNUSED(x)
Definition LimaCommon.h:224
virtual void addSuffix(dicoVertexType from, PrefixIterator *prefixIt)
void checkIntegrity(dicoVertexType from) const
check integrity of subgraph
void getPrefix(dicoVertexType &from, PrefixIterator *prefixIt) const
Recursively goes through the graph from from, following edges labelled by the prefix iterator chars.
int32_t findEdge(const char32_t searchChar, const LimaString &textString, int32_t min, int range, int nb_unit_for_char) const
find the right offset in the vector of out_edge: search for the character currentChar in the string t...
virtual void print(std::ostream &os) const
bool equivalent(dicoVertexType referenceState, dicoVertexType candidateState) const
are both state equivalent? We assume that edges are ordered
void writeBody(AbstractFsaAccessOStreamWrapper &ow)
std::pair< const dicoVertexType, bool > findEquivalentInRegister(dicoVertexType tempState)
Search for equivalent state in register.
virtual void printGraph(std::ostream &os) const
void readBody(AbstractFsaAccessIStreamWrapper &iw)
FsaAccess16(bool trie_direction_fwd)
PrefixIterator * getPrefixIterator(const LimaString &word, const uint64_t offset=0) const
For all navigation Factory of prefixIterator (prefixIt depends on direction: forward/reverse)
selected_graph_types16::dicoVertexStatusProperty dicoVertexProperty
boost::graph_traits< graphType >::edge_descriptor dicoEdgeType
type of vertex descriptor type of edge descriptor
void writeVertices(AbstractFsaAccessOStreamWrapper &ow, FsaAccessIOHandler< graphType > *iOHandler, dicoVertexType from)
Parcours recursif du graphe avec creation d'un tableau de conversion ptr -> Id On renomme les noeuds ...
int32_t findOffsetToInsertBefore(const char32_t searchChar, const LimaString &textString, int32_t min, int range, int nb_unit_per_char) const
find where to insert currentChar in the string text using dichotomy search (assume characters are ord...
virtual FsaAccessIOHandler< graphType > * getFsaAccessIOHandler() const =0
For IO Factory of IO Handler: Handler depends on graphType: with mapping or not.
boost::graph_traits< graphType >::degree_size_type dico_degree_size_type
FsaDictionaries (FsaDictBuilder and FsaDictSpare) share the same binary file format:
void operator()(std::ostream &out, const dicoEdge &edge) const
void operator()(std::ostream &out, const dicoVertex &v) const
dicoVertexWriter16(vname_map_type vname_map, vtext_map_type vtext_map, vcount_map_type vcount_map)
NAUTITIA.
QString LimaString
Definition LimaString.h:33
BOOST_INSTALL_PROPERTY(vertex, text)
boost::property_map< graphType, boost::vertex_name_t >::const_type vname_map_type
Definition FsaAccess16.h:99
boost::property_map< graphType, vertex_count_t >::type nconst_vcount_map_type
boost::property_map< graphType, vertex_count_t >::const_type vcount_map_type
boost::property_map< graphType, vertex_text_t >::type nconst_vtext_map_type
boost::property_map< graphType, vertex_text_t >::const_type vtext_map_type
boost::property_map< graphType, boost::vertex_name_t >::type nconst_vname_map_type
boost::adjacency_list< boost::vecS, boost::listS, boost::bidirectionalS, dicoVertexCountProperty > builderGraphType
Graph used for FsaDictBuilder container types are chosen for their efficiency in insertion bidirectio...
boost::property< boost::vertex_name_t, VERTEX_PROPERTY_16, dicoVertexTextProperty > dicoVertexStatusProperty
Declare a property (vertex_name) of type uint8_t to store vertex quality : (qualifer & 1) == 1: final...
boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, dicoVertexCountProperty > spareGraphType
Graph used for FsaDictSpare container types are chosen for their minimal size.
boost::property< vertex_count_t, std::vector< int >, dicoVertexStatusProperty > dicoVertexCountProperty
Declare a property (vertex_count) of type std::vector<int> to store count of sub automata (to compute...
boost::property< vertex_text_t, LimaString > dicoVertexTextProperty
Declare a property (vertex_text) of type LimaString to store more efficiently edge label.