aGrUM  0.13.2
gum::learning::DBTranslator4LabelizedVariable< ALLOC > Class Template Reference

The databases' cell translators for labelized variables. More...

#include <agrum/learning/database/DBTranslator4LabelizedVariable.h>

+ Inheritance diagram for gum::learning::DBTranslator4LabelizedVariable< ALLOC >:
+ Collaboration diagram for gum::learning::DBTranslator4LabelizedVariable< ALLOC >:

Public Member Functions

Constructors / Destructors
template<template< typename > class XALLOC>
 DBTranslator4LabelizedVariable (const std::vector< std::string, XALLOC< std::string > > &missing_symbols, std::size_t max_dico_entries=std::numeric_limits< std::size_t >::max(), const allocator_type &alloc=allocator_type())
 default constructor without any initial variable More...
 
 DBTranslator4LabelizedVariable (std::size_t max_dico_entries=std::numeric_limits< std::size_t >::max(), const allocator_type &alloc=allocator_type())
 default constructor without any initial variable nor missing symbols More...
 
template<template< typename > class XALLOC>
 DBTranslator4LabelizedVariable (const LabelizedVariable &var, const std::vector< std::string, XALLOC< std::string > > &missing_symbols, const bool editable_dictionary=false, std::size_t max_dico_entries=std::numeric_limits< std::size_t >::max(), const allocator_type &alloc=allocator_type())
 default constructor with a labelized variable as translator More...
 
 DBTranslator4LabelizedVariable (const LabelizedVariable &var, const bool editable_dictionary=false, std::size_t max_dico_entries=std::numeric_limits< std::size_t >::max(), const allocator_type &alloc=allocator_type())
 default constructor with a labelized variable as translator but without missing symbols More...
 
 DBTranslator4LabelizedVariable (const DBTranslator4LabelizedVariable< ALLOC > &from)
 copy constructor More...
 
 DBTranslator4LabelizedVariable (const DBTranslator4LabelizedVariable< ALLOC > &from, const allocator_type &alloc)
 copy constructor with a given allocator More...
 
 DBTranslator4LabelizedVariable (DBTranslator4LabelizedVariable< ALLOC > &&from)
 move constructor More...
 
 DBTranslator4LabelizedVariable (DBTranslator4LabelizedVariable< ALLOC > &&from, const allocator_type &alloc)
 move constructor with a given allocator More...
 
virtual DBTranslator4LabelizedVariable< ALLOC > * clone () const
 virtual copy constructor More...
 
virtual DBTranslator4LabelizedVariable< ALLOC > * clone (const allocator_type &alloc) const
 virtual copy constructor with a given allocator More...
 
virtual ~DBTranslator4LabelizedVariable ()
 destructor More...
 
Operators
DBTranslator4LabelizedVariable< ALLOC > & operator= (const DBTranslator4LabelizedVariable< ALLOC > &from)
 copy operator More...
 
DBTranslator4LabelizedVariable< ALLOC > & operator= (DBTranslator4LabelizedVariable< ALLOC > &&from)
 move operator More...
 
Accessors / Modifiers
virtual DBTranslatedValue translate (const std::string &str) final
 returns the translation of a string More...
 
virtual std::string translateBack (const DBTranslatedValue translated_val) const final
 returns the original value for a given translation More...
 
virtual std::size_t domainSize () const final
 returns the domain size of a variable corresponding to the translations More...
 
virtual bool needsReordering () const final
 indicates whether a reordering is needed to make the translations sorted More...
 
virtual HashTable< std::size_t, std::size_t, ALLOC< std::pair< std::size_t, std::size_t > > > reorder () final
 performs a reordering of the dictionary and returns a mapping from the old translated values to the new ones. More...
 
virtual const LabelizedVariablevariable () const final
 returns the variable stored into the translator More...
 
Operators
DBTranslatedValue operator<< (const std::string &str)
 alias for method translate More...
 
std::string operator>> (const DBTranslatedValue translated_val)
 alias for method translateBack More...
 
Accessors / Modifiers
virtual bool hasEditableDictionary () const
 indicates whether the translator has an editable dictionary or not More...
 
virtual void setEditableDictionaryMode (bool new_mode)
 sets/unset the editable dictionary mode More...
 
const Set< std::string, ALLOC< std::string > > & missingSymbols () const
 returns the set of missing symbols taken into account by the translator More...
 
bool isMissingSymbol (const std::string &str) const
 indicates whether a string corresponds to a missing symbol More...
 
void setVariableName (const std::string &str) const
 sets the name of the variable stored into the translator More...
 
void setVariableDescription (const std::string &str) const
 sets the name of the variable stored into the translator More...
 
DBTranslatedValueType getValType () const
 returns the type of values handled by the translator More...
 
allocator_type getAllocator () const
 returns the allocator used by the translator More...
 
bool isMissingValue (const DBTranslatedValue &val) const
 indicates whether a translated value corresponds to a missing value More...
 

Public Types

using allocator_type = typename DBTranslator< ALLOC >::allocator_type
 type for the allocators passed in arguments of methods More...
 

Protected Attributes

bool _is_dictionary_dynamic
 indicates whether the dictionary can be updated or not More...
 
std::size_t _max_dico_entries
 the maximum number of entries that the dictionary is allowed to contain More...
 
Set< std::string, ALLOC< std::string > > _missing_symbols
 the set of missing symbols More...
 
Bijection< std::size_t, std::string, ALLOC< std::pair< float, std::string > > > _back_dico
 the bijection relating back translated values and their original strings. More...
 
DBTranslatedValueType _val_type
 the type of the values translated by the translator More...
 

Detailed Description

template<template< typename > class ALLOC = std::allocator>
class gum::learning::DBTranslator4LabelizedVariable< ALLOC >

The databases' cell translators for labelized variables.

Translators are used by DatabaseTable instances to transform datasets' strings into DBTranslatedValue instances. The point is that strings are not adequate for fast learning, they need to be preprocessed into a type that can be analyzed quickly (the so-called DBTranslatedValue type).

A DBTranslator4LabelizedVariable is a translator that contains and exploits a LabelizedVariable for translations. Each time a string needs be translated, we ask the LabelizedVariable to provide the index of the label corresponding to the string. This index, when encoded into a DBTranslatedValue, is precisely the translation of the string.

Here is an example of how to use this class:
// create the translator, with possible missing symbols: "N/A" and "???"
// i.e., each time the translator reads a "N/A" or a "???" string, it
// won't translate it into a number but into a missing value.
std::vector<std::string> missing { "N/A", "???" };
// gets the DBTranslatedValue corresponding to some strings:
auto val1 = translator.translate("xxx");
auto val2 = translator.translate("zzz");
auto val3 = translator << "yyy";
auto val2bis = translator.translate( "zzz" );
// In the first assignment, the translator initially contains an empty
// domain LabelizedVariable and it is by default in editable mode. So
// we add a new label "xxx" to the LabelizedVariable contained in the
// translator, and the index of this label is 0. Therefore, we have that
// val1 = DBTranslatedValue {std::size_t(0)}. Similarly, the assignments of
// val2 and val3 induce the additions of labels "zzz" and "yyy" into the
// LabelizedVariable. As a result, val2 = DBTranslatedValue {std::size_t(1)}
// and val3 = DBTranslatedValue {std::size_t(2)}. In the assigment of
// val2bis, label "zzz" already exists and its index is equal to 1. So
// val2bis = DBTranslatedValue {std::size_t(1)}.
// add the numbers assigned to val1, val2, val3
std::size_t sum = val1.discr_val + val2.discr_val + val3.discr_val;
// translate missing values: val4 and val5 will be equal to:
// DBTranslatedValue { std::numeric_limits<std::size_t>::max () }
auto val4 = translator << "N/A";
auto val5 = translator.translate ( "???" );
// given a DBTranslatedValue that is supposed to contain a label's index,
// get the corresponding label.
std::string str;
str = translator.translateBack ( val1 ); // str = "xxx"
str = translator >> val2; // str = "zzz"
str = translator >> gum::learning::DBTranslatedValue {std::size_t(1)};
// str = "zzz"
// if there is no such label, Exception NotFound is raised:
str = translator >> gum::learning::DBTranslatedValue {std::size_t(4)};
// translate back missing values: the string will corresponds to one of
// the missing symbols known to the translator
str = translator >> val4; // str = "N/A" or "???"
str = translator >> val5; // str = "N/A" or "???"
// get the domain size of the variable stored into the translator
std::size_t size = translator.domainSize (); // size = 3
// get the variable stored within the translator
dynamic_cast<const gum::LabelizedVariable*> ( translator.variable () );
// it is possible to create a translator for an already known variable.
// In this case, by default, the translator is not in editable mode, but
// this behavior can be changed passing the right arguments to the
// constructor of the translator, or using the setEditableDictionaryMode
// method.
gum::LabelizedVariable var ( "X1", "", 0 );
var.addLabel ( "toto" );
var.addLabel ( "titi" );
var.addLabel ( "tutu" );
std::size_t index1 = translator2.translate ( "toto" ).discr_val; // = 0
std::size_t index2 = translator2.translate ( "tutu" ).discr_val; // = 2
std::size_t index3 = translator2.translate ( "N/A" ).discr_val;
// here index3 corresponds to the index of a missing value, hence it is
// equal to std::numeric_limits<std::size_t>::max ()
// trying to translate a string which is not a label of var will raise
// Exception NotFound
translator2.translate ( "xxx" ); // NotFound

Definition at line 132 of file DBTranslator4LabelizedVariable.h.

Member Typedef Documentation

template<template< typename > class ALLOC = std::allocator>
using gum::learning::DBTranslator4LabelizedVariable< ALLOC >::allocator_type = typename DBTranslator< ALLOC >::allocator_type

type for the allocators passed in arguments of methods

Definition at line 135 of file DBTranslator4LabelizedVariable.h.

Constructor & Destructor Documentation

template<template< typename > class ALLOC = std::allocator>
template<template< typename > class XALLOC>
gum::learning::DBTranslator4LabelizedVariable< ALLOC >::DBTranslator4LabelizedVariable ( const std::vector< std::string, XALLOC< std::string > > &  missing_symbols,
std::size_t  max_dico_entries = std::numeric_limits< std::size_t >::max(),
const allocator_type alloc = allocator_type() 
)

default constructor without any initial variable

When using this constructor, it is assumed implicitly that the dictionary contained into the translator is editable. So, when reading the database, if we observe a label that has not been encountered before, we add it into the dictionary of the translator (hence into the variable contained by the translator).

Parameters
missing_symbolsthe set of symbols in the database representing missing values
max_dico_entriesthe max number of entries that the dictionary can contain. If we try to add new entries in the dictionary, this will be considered as an error and a SizeError exception will be raised
allocThe allocator used to allocate memory for all the fields of the DBTranslator4LabelizedVariable
template<template< typename > class ALLOC = std::allocator>
gum::learning::DBTranslator4LabelizedVariable< ALLOC >::DBTranslator4LabelizedVariable ( std::size_t  max_dico_entries = std::numeric_limits< std::size_t >::max(),
const allocator_type alloc = allocator_type() 
)

default constructor without any initial variable nor missing symbols

When using this constructor, it is assumed implicitly that the dictionary contained into the translator is editable. So, when reading the database, if we observe a label that has not been encountered before, we add it into the dictionary of the translator (hence into the variable contained by the translator).

Parameters
max_dico_entriesthe max number of entries that the dictionary can contain. If we try to add new entries in the dictionary, this will be considered as an error and a SizeError exception will be raised
allocThe allocator used to allocate memory for all the fields of the DBTranslator4LabelizedVariable
template<template< typename > class ALLOC = std::allocator>
template<template< typename > class XALLOC>
gum::learning::DBTranslator4LabelizedVariable< ALLOC >::DBTranslator4LabelizedVariable ( const LabelizedVariable var,
const std::vector< std::string, XALLOC< std::string > > &  missing_symbols,
const bool  editable_dictionary = false,
std::size_t  max_dico_entries = std::numeric_limits< std::size_t >::max(),
const allocator_type alloc = allocator_type() 
)

default constructor with a labelized variable as translator

Parameters
vara labelized variable whose labels will be used for translations. The translator keeps a copy of this variable
missing_symbolsthe set of symbols in the database representing missing values
editable_dictionarythe mode in which the translator will perform translations: when false (the default), the translation of a string that does not correspond to a label of var will raise a NotFound exception; when true, the translator will try to add the string as a new label into var (and therefore into the dictionary)
max_dico_entriesthe max number of entries that the dictionary can contain. If we try to add new entries in the dictionary, this will be considered as an error and a SizeError exception will be raised
allocThe allocator used to allocate memory for all the fields of the DBTranslator4LabelizedVariable
Warning
If the variable contained into the translator has a label equal to a missing value symbol, the label will be taken into account in the translations, not the missing value.
template<template< typename > class ALLOC = std::allocator>
gum::learning::DBTranslator4LabelizedVariable< ALLOC >::DBTranslator4LabelizedVariable ( const LabelizedVariable var,
const bool  editable_dictionary = false,
std::size_t  max_dico_entries = std::numeric_limits< std::size_t >::max(),
const allocator_type alloc = allocator_type() 
)

default constructor with a labelized variable as translator but without missing symbols

Parameters
vara labelized variable whose labels will be used for translations. The translator keeps a copy of this variable
editable_dictionarythe mode in which the translator will perform translations: when false (the default), the translation of a string that does not correspond to a label of var will raise a NotFound exception; when true, the translator will try to add the string as a new label into var (and therefore into the dictionary)
max_dico_entriesthe max number of entries that the dictionary can contain. If we try to add new entries in the dictionary, this will be considered as an error and a SizeError exception will be raised
allocThe allocator used to allocate memory for all the fields of the DBTranslator4LabelizedVariable
Warning
If the variable contained into the translator has a label equal to a missing value symbol, the label will be taken into account in the translations, not the missing value.
template<template< typename > class ALLOC = std::allocator>
gum::learning::DBTranslator4LabelizedVariable< ALLOC >::DBTranslator4LabelizedVariable ( const DBTranslator4LabelizedVariable< ALLOC > &  from)

copy constructor

template<template< typename > class ALLOC = std::allocator>
gum::learning::DBTranslator4LabelizedVariable< ALLOC >::DBTranslator4LabelizedVariable ( const DBTranslator4LabelizedVariable< ALLOC > &  from,
const allocator_type alloc 
)

copy constructor with a given allocator

template<template< typename > class ALLOC = std::allocator>
gum::learning::DBTranslator4LabelizedVariable< ALLOC >::DBTranslator4LabelizedVariable ( DBTranslator4LabelizedVariable< ALLOC > &&  from)

move constructor

template<template< typename > class ALLOC = std::allocator>
gum::learning::DBTranslator4LabelizedVariable< ALLOC >::DBTranslator4LabelizedVariable ( DBTranslator4LabelizedVariable< ALLOC > &&  from,
const allocator_type alloc 
)

move constructor with a given allocator

template<template< typename > class ALLOC = std::allocator>
virtual gum::learning::DBTranslator4LabelizedVariable< ALLOC >::~DBTranslator4LabelizedVariable ( )
virtual

destructor

Member Function Documentation

template<template< typename > class ALLOC = std::allocator>
virtual DBTranslator4LabelizedVariable< ALLOC >* gum::learning::DBTranslator4LabelizedVariable< ALLOC >::clone ( ) const
virtual

virtual copy constructor

Implements gum::learning::DBTranslator< ALLOC >.

template<template< typename > class ALLOC = std::allocator>
virtual DBTranslator4LabelizedVariable< ALLOC >* gum::learning::DBTranslator4LabelizedVariable< ALLOC >::clone ( const allocator_type alloc) const
virtual

virtual copy constructor with a given allocator

Implements gum::learning::DBTranslator< ALLOC >.

template<template< typename > class ALLOC = std::allocator>
virtual std::size_t gum::learning::DBTranslator4LabelizedVariable< ALLOC >::domainSize ( ) const
finalvirtual

returns the domain size of a variable corresponding to the translations

Assume that the translator has been fed with the observed values of a random variable. Then it has produced a set of translated values. The latter define the domain of the variable. The domainSize is the size of this domain. In other words, this corresponds to the number of labels of the LabelizedVariable contained in the translator. Note that missing values are not taken into account in the domain sizes.

Implements gum::learning::DBTranslator< ALLOC >.

template<template< typename > class ALLOC = std::allocator>
allocator_type gum::learning::DBTranslator< ALLOC >::getAllocator ( ) const
inherited

returns the allocator used by the translator

template<template< typename > class ALLOC = std::allocator>
DBTranslatedValueType gum::learning::DBTranslator< ALLOC >::getValType ( ) const
inherited

returns the type of values handled by the translator

Returns
either DBTranslatedValueType::DISCRETE if the translator includes a discrete variable or DBTranslatedValueType::CONTINUOUS if it contains a continuous variable. This is convenient to know how to interpret the DBTranslatedValue instances produced by the DBTranslator: either using their discr_val field or their cont_val field.
template<template< typename > class ALLOC = std::allocator>
virtual bool gum::learning::DBTranslator< ALLOC >::hasEditableDictionary ( ) const
virtualinherited

indicates whether the translator has an editable dictionary or not

Reimplemented in gum::learning::DBTranslator4DiscretizedVariable< ALLOC >.

template<template< typename > class ALLOC = std::allocator>
bool gum::learning::DBTranslator< ALLOC >::isMissingSymbol ( const std::string &  str) const
inherited

indicates whether a string corresponds to a missing symbol

template<template< typename > class ALLOC = std::allocator>
bool gum::learning::DBTranslator< ALLOC >::isMissingValue ( const DBTranslatedValue val) const
inherited

indicates whether a translated value corresponds to a missing value

template<template< typename > class ALLOC = std::allocator>
const Set< std::string, ALLOC< std::string > >& gum::learning::DBTranslator< ALLOC >::missingSymbols ( ) const
inherited

returns the set of missing symbols taken into account by the translator

template<template< typename > class ALLOC = std::allocator>
virtual bool gum::learning::DBTranslator4LabelizedVariable< ALLOC >::needsReordering ( ) const
finalvirtual

indicates whether a reordering is needed to make the translations sorted

If the strings represented by the translations are only numbers, translations are considered to be sorted if and only if they are sorted by increasing number. If the strings do not only represent numbers, then translations are considered to be sorted if and only if they are sorted lexicographically.

When constructing dynamically its dictionary, the translator may assign wrong DBTranslatedValue values to strings. For instance, a translator reading sequentially integer strings 4, 1, 3, may map 4 into DBTranslatedValue{std::size_t(0)}, 1 into DBTranslatedValue{std::size_t(1)} and 3 into DBTranslatedValue{std::size_t(2)}, resulting in random variables having domain {4,1,3}. The user may prefer having domain {1,3,4}, i.e., a domain specified with increasing values. This requires a reordering. Method needsReodering() returns a Boolean indicating whether such a reordering should be performed or whether the current order is OK.

Implements gum::learning::DBTranslator< ALLOC >.

template<template< typename > class ALLOC = std::allocator>
DBTranslatedValue gum::learning::DBTranslator< ALLOC >::operator<< ( const std::string &  str)
inherited

alias for method translate

template<template< typename > class ALLOC = std::allocator>
DBTranslator4LabelizedVariable< ALLOC >& gum::learning::DBTranslator4LabelizedVariable< ALLOC >::operator= ( const DBTranslator4LabelizedVariable< ALLOC > &  from)

copy operator

template<template< typename > class ALLOC = std::allocator>
DBTranslator4LabelizedVariable< ALLOC >& gum::learning::DBTranslator4LabelizedVariable< ALLOC >::operator= ( DBTranslator4LabelizedVariable< ALLOC > &&  from)

move operator

template<template< typename > class ALLOC = std::allocator>
std::string gum::learning::DBTranslator< ALLOC >::operator>> ( const DBTranslatedValue  translated_val)
inherited

alias for method translateBack

template<template< typename > class ALLOC = std::allocator>
virtual HashTable< std::size_t, std::size_t, ALLOC< std::pair< std::size_t, std::size_t > > > gum::learning::DBTranslator4LabelizedVariable< ALLOC >::reorder ( )
finalvirtual

performs a reordering of the dictionary and returns a mapping from the old translated values to the new ones.

When a reordering is needed, i.e., string values must be translated differently. Method reorder() computes how the translations should be changed. It updates accordingly the dictionary and returns the mapping that enables changing the old dictionary values into the new ones.

Warning
If there is no reordering to perform, the method returns an empty hashtable.

Implements gum::learning::DBTranslator< ALLOC >.

template<template< typename > class ALLOC = std::allocator>
virtual void gum::learning::DBTranslator< ALLOC >::setEditableDictionaryMode ( bool  new_mode)
virtualinherited

sets/unset the editable dictionary mode

Reimplemented in gum::learning::DBTranslator4DiscretizedVariable< ALLOC >.

template<template< typename > class ALLOC = std::allocator>
void gum::learning::DBTranslator< ALLOC >::setVariableDescription ( const std::string &  str) const
inherited

sets the name of the variable stored into the translator

template<template< typename > class ALLOC = std::allocator>
void gum::learning::DBTranslator< ALLOC >::setVariableName ( const std::string &  str) const
inherited

sets the name of the variable stored into the translator

template<template< typename > class ALLOC = std::allocator>
virtual DBTranslatedValue gum::learning::DBTranslator4LabelizedVariable< ALLOC >::translate ( const std::string &  str)
finalvirtual

returns the translation of a string

This method tries to translate a given string into the DBTranslatedValue that should be stored into a DatabaseTable. If the translator cannot find the translation in its current dictionary, then two situations can obtain:

  1. if the translator is not in an editable dictionary mode, then the translator raises a NotFound exception.
  2. if the translator is in an editable dictionary mode, i.e., it is allowed to update its dictionary, then it tries to add the string as a new value in the dictionary (or equivalently as a new label into its labelized variable). Upon success, it returns the translated value, otherwise, it raises a SizeError exception if the number of entries in the dictionary has already reached its maximum.
Warning
Note that missing values (i.e., string encoded as missing symbols) are translated as std::numeric_limits<std::size_t>::max ().
If the variable contained into the translator has a label equal to a missing value symbol, the label will be taken into account in the translation, not the missing value.
Parameters
strthe string that the translator will try to translate
Returns
the translated value of the string to be stored into a DatabaseTable
Exceptions
UnknownLabelInDatabaseis raised if the translation cannot be found and the translator is not in an editable dictionary mode.
SizeErroris raised if the number of entries in the dictionary has already reached its maximum.

Implements gum::learning::DBTranslator< ALLOC >.

template<template< typename > class ALLOC = std::allocator>
virtual std::string gum::learning::DBTranslator4LabelizedVariable< ALLOC >::translateBack ( const DBTranslatedValue  translated_val) const
finalvirtual

returns the original value for a given translation

Returns
the string that was translated into a given DBTranslatedValue.
Parameters
translated_vala DBTranslatedValue which is supposed to contain the index of a label of the LabelizedVariable contained in the translator. The method should return this label
Exceptions
UnknownLabelInDatabaseis raised if this original value cannot be found

Implements gum::learning::DBTranslator< ALLOC >.

template<template< typename > class ALLOC = std::allocator>
virtual const LabelizedVariable* gum::learning::DBTranslator4LabelizedVariable< ALLOC >::variable ( ) const
finalvirtual

returns the variable stored into the translator

Implements gum::learning::DBTranslator< ALLOC >.

Member Data Documentation

template<template< typename > class ALLOC = std::allocator>
Bijection< std::size_t, std::string, ALLOC< std::pair< float, std::string > > > gum::learning::DBTranslator< ALLOC >::_back_dico
mutableprotectedinherited

the bijection relating back translated values and their original strings.

Note that the translated values considered here are of type std::size_t because only the values for discrete variables need be stored, those for continuous variables are actually identity mappings.

Warning
only the values of the random variable are stored into this bijection. Missing values are not considered here.

Definition at line 390 of file DBTranslator.h.

template<template< typename > class ALLOC = std::allocator>
bool gum::learning::DBTranslator< ALLOC >::_is_dictionary_dynamic
protectedinherited

indicates whether the dictionary can be updated or not

Definition at line 373 of file DBTranslator.h.

template<template< typename > class ALLOC = std::allocator>
std::size_t gum::learning::DBTranslator< ALLOC >::_max_dico_entries
protectedinherited

the maximum number of entries that the dictionary is allowed to contain

Definition at line 376 of file DBTranslator.h.

template<template< typename > class ALLOC = std::allocator>
Set< std::string, ALLOC< std::string > > gum::learning::DBTranslator< ALLOC >::_missing_symbols
protectedinherited

the set of missing symbols

Definition at line 379 of file DBTranslator.h.

template<template< typename > class ALLOC = std::allocator>
DBTranslatedValueType gum::learning::DBTranslator< ALLOC >::_val_type
protectedinherited

the type of the values translated by the translator

Definition at line 393 of file DBTranslator.h.


The documentation for this class was generated from the following file: