SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
List of all members | Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes
CHashedDocConverter Class Reference

Detailed Description

This class can be used to convert a document collection contained in a CStringFeatures<char> object where each document is stored as a single vector into a hashed Bag-of-Words representation. Like in the standard Bag-of-Words representation, this class considers each document as a collection of tokens, which are then hashed into a new feature space of a specified dimension. This class is very flexible and allows the user to specify the tokenizer used to tokenize each document, specify whether the results should be normalized with regards to the sqrt of the document size, as well as to specify whether he wants to combine different tokens. The latter implements a k-skip n-grams approach, meaning that you can combine up to n tokens, while skipping up to k. Eg. for the tokens ["a", "b", "c", "d"], with n_grams = 2 and skips = 2, one would get the following combinations : ["a", "ab", "ac" (skipped 1), "ad" (skipped 2), "b", "bc", "bd" (skipped 1), "c", "cd", "d"].

Definition at line 39 of file HashedDocConverter.h.

Inheritance diagram for CHashedDocConverter:
Inheritance graph
[legend]

Public Member Functions

 CHashedDocConverter ()
 
 CHashedDocConverter (int32_t hash_bits, bool normalize=false, int32_t n_grams=1, int32_t skips=0)
 
 CHashedDocConverter (CTokenizer *tzer, int32_t hash_bits, bool normalize=false, int32_t n_grams=1, int32_t skips=0)
 
virtual ~CHashedDocConverter ()
 
virtual CFeaturesapply (CFeatures *features)
 
SGSparseVector< float64_tapply (SGVector< char > document)
 
virtual const char * get_name () const
 
void set_normalization (bool normalize)
 
void set_k_skip_n_grams (int32_t k, int32_t n)
 
virtual CSGObjectshallow_copy () const
 
virtual CSGObjectdeep_copy () const
 
virtual bool is_generic (EPrimitiveType *generic) const
 
template<class T >
void set_generic ()
 
template<>
void set_generic ()
 
template<>
void set_generic ()
 
template<>
void set_generic ()
 
template<>
void set_generic ()
 
template<>
void set_generic ()
 
template<>
void set_generic ()
 
template<>
void set_generic ()
 
template<>
void set_generic ()
 
template<>
void set_generic ()
 
template<>
void set_generic ()
 
template<>
void set_generic ()
 
template<>
void set_generic ()
 
template<>
void set_generic ()
 
template<>
void set_generic ()
 
template<>
void set_generic ()
 
void unset_generic ()
 
virtual void print_serializable (const char *prefix="")
 
virtual bool save_serializable (CSerializableFile *file, const char *prefix="")
 
virtual bool load_serializable (CSerializableFile *file, const char *prefix="")
 
void set_global_io (SGIO *io)
 
SGIOget_global_io ()
 
void set_global_parallel (Parallel *parallel)
 
Parallelget_global_parallel ()
 
void set_global_version (Version *version)
 
Versionget_global_version ()
 
SGStringList< char > get_modelsel_names ()
 
void print_modsel_params ()
 
char * get_modsel_param_descr (const char *param_name)
 
index_t get_modsel_param_index (const char *param_name)
 
void build_gradient_parameter_dictionary (CMap< TParameter *, CSGObject * > *dict)
 
virtual void update_parameter_hash ()
 
virtual bool parameter_hash_changed ()
 
virtual bool equals (CSGObject *other, float64_t accuracy=0.0, bool tolerant=false)
 
virtual CSGObjectclone ()
 

Static Public Member Functions

static index_t generate_ngram_hashes (SGVector< uint32_t > &hashes, index_t hashes_start, index_t len, SGVector< index_t > &ngram_hashes, int32_t num_bits, int32_t ngrams, int32_t tokens_to_skip)
 

Public Attributes

SGIOio
 
Parallelparallel
 
Versionversion
 
Parameterm_parameters
 
Parameterm_model_selection_parameters
 
Parameterm_gradient_parameters
 
uint32_t m_hash
 

Protected Member Functions

void init (CTokenizer *tzer, int32_t d, bool normalize, int32_t n_grams, int32_t skips)
 
int32_t count_distinct_indices (CDynamicArray< uint32_t > &hashed_indices)
 
SGSparseVector< float64_tcreate_hashed_representation (CDynamicArray< uint32_t > &hashed_indices)
 
virtual void load_serializable_pre () throw (ShogunException)
 
virtual void load_serializable_post () throw (ShogunException)
 
virtual void save_serializable_pre () throw (ShogunException)
 
virtual void save_serializable_post () throw (ShogunException)
 

Protected Attributes

int32_t num_bits
 
CTokenizertokenizer
 
bool should_normalize
 
int32_t ngrams
 
int32_t tokens_to_skip
 

Constructor & Destructor Documentation

Default constructor

Definition at line 23 of file HashedDocConverter.cpp.

CHashedDocConverter ( int32_t  hash_bits,
bool  normalize = false,
int32_t  n_grams = 1,
int32_t  skips = 0 
)

Constructor Creates tokens on whitespace

Parameters
hash_bitsthe number of bits of the hash. Means a dimension of size 2^(hash_bits).
normalizewhether to normalize vectors or not
n_gramsthe max number of tokens to consider when combining tokens
skipsthe max number of tokens to skip when combining tokens

Definition at line 28 of file HashedDocConverter.cpp.

CHashedDocConverter ( CTokenizer tzer,
int32_t  hash_bits,
bool  normalize = false,
int32_t  n_grams = 1,
int32_t  skips = 0 
)

Constructor

Parameters
tzerthe tokenizer to use
hash_bitsthe number of bits of the hash. Means a dimension of size 2^(hash_bits).
normalizewhether to normalize vectors or not
n_gramsthe max number of tokens to consider when combining tokens
skipsthe max number of tokens to skip when combining tokens

Definition at line 34 of file HashedDocConverter.cpp.

~CHashedDocConverter ( )
virtual

Destructor

Definition at line 40 of file HashedDocConverter.cpp.

Member Function Documentation

CFeatures * apply ( CFeatures features)
virtual

Hashes each string contained in features

Parameters
featuresthe strings to be hashed. Must be an instance of CStringFeatures.
Returns
a CSparseFeatures object containing the hashes of the strings.

Implements CConverter.

Definition at line 81 of file HashedDocConverter.cpp.

SGSparseVector< float64_t > apply ( SGVector< char >  document)

Hashes the tokens contained in document

Parameters
documentthe char vector to tokenize and hash
Returns
a SGSparseVector with the hashed representation of the document

the array will contain all the hashes generated from the tokens

this vector will maintain the current n+k active tokens in a circular manner

the combinations generated from the current active tokens will be stored here to avoid creating new objects

Reading n+s-1 tokens

Reading token and storing index to hashed_indices

For remaining combinations

Normalizing vector

Definition at line 101 of file HashedDocConverter.cpp.

void build_gradient_parameter_dictionary ( CMap< TParameter *, CSGObject * > *  dict)
inherited

Builds a dictionary of all parameters in SGObject as well of those of SGObjects that are parameters of this object. Dictionary maps parameters to the objects that own them.

Parameters
dictdictionary of parameters to be built.

Definition at line 597 of file SGObject.cpp.

CSGObject * clone ( )
virtualinherited

Creates a clone of the current object. This is done via recursively traversing all parameters, which corresponds to a deep copy. Calling equals on the cloned object always returns true although none of the memory of both objects overlaps.

Returns
an identical copy of the given object, which is disjoint in memory. NULL if the clone fails. Note that the returned object is SG_REF'ed

Definition at line 714 of file SGObject.cpp.

int32_t count_distinct_indices ( CDynamicArray< uint32_t > &  hashed_indices)
protected

This method takes a dynamic array as an argument, sorts it and returns the number of the distinct elements(indices here) in the array.

Parameters
hashed_indicesthe array to sort and count elements
Returns
the number of distinct elements

Counting nnz features

Definition at line 228 of file HashedDocConverter.cpp.

SGSparseVector< float64_t > create_hashed_representation ( CDynamicArray< uint32_t > &  hashed_indices)
protected

This method takes the dynamic array containing all the hashed indices of a document and returns a compact sparse representation with each index found and with the count of such index

Parameters
hashed_indicesthe array containing the hashed indices
Returns
the compact hashed document representation

Definition at line 184 of file HashedDocConverter.cpp.

CSGObject * deep_copy ( ) const
virtualinherited

A deep copy. All the instance variables will also be copied.

Definition at line 198 of file SGObject.cpp.

bool equals ( CSGObject other,
float64_t  accuracy = 0.0,
bool  tolerant = false 
)
virtualinherited

Recursively compares the current SGObject to another one. Compares all registered numerical parameters, recursion upon complex (SGObject) parameters. Does not compare pointers!

May be overwritten but please do with care! Should not be necessary in most cases.

Parameters
otherobject to compare with
accuracyaccuracy to use for comparison (optional)
tolerantallows linient check on float equality (within accuracy)
Returns
true if all parameters were equal, false if not

Definition at line 618 of file SGObject.cpp.

index_t generate_ngram_hashes ( SGVector< uint32_t > &  hashes,
index_t  hashes_start,
index_t  len,
SGVector< index_t > &  ngram_hashes,
int32_t  num_bits,
int32_t  ngrams,
int32_t  tokens_to_skip 
)
static

Generates all the k-skip n-grams combinations for the pre-hashed tokens in hashes, starting from hashes[hashes_start] and going up to hashes[1+len] in a circular manner. The generated tokens (maximun (n-1)(k+1)+1) are stored in ngram_hashes. The number of created tokens is returned in case fewer tokens are generated (due to a smaller len than the size of hashes). See class description for more information on k-skip n-grams.

Parameters
hashesthe hashes of the tokens to combine as k-skip n-grams
hashes_startthe index in hashes to consider as the starting point
lenthe maximum allowed number of tokens to reach in a circular manner, starting from hashes_start
ngram_hashesthe vector where to store the generated hashes. Must have been created to be able to store at most (n-1)(k+1)+1 tokens.
num_bitsthe dimension in which to limit the hashed indices (means a dimension of size 2^num_bits)
ngramsthe n in k-skip n-grams or the max number of tokens to combine
tokens_to_skipthe k in k-skip n-grams or the max number of tokens to skip when combining
Returns
the number of generated tokens

Definition at line 205 of file HashedDocConverter.cpp.

SGIO * get_global_io ( )
inherited

get the io object

Returns
io object

Definition at line 235 of file SGObject.cpp.

Parallel * get_global_parallel ( )
inherited

get the parallel object

Returns
parallel object

Definition at line 277 of file SGObject.cpp.

Version * get_global_version ( )
inherited

get the version object

Returns
version object

Definition at line 290 of file SGObject.cpp.

SGStringList< char > get_modelsel_names ( )
inherited
Returns
vector of names of all parameters which are registered for model selection

Definition at line 498 of file SGObject.cpp.

char * get_modsel_param_descr ( const char *  param_name)
inherited

Returns description of a given parameter string, if it exists. SG_ERROR otherwise

Parameters
param_namename of the parameter
Returns
description of the parameter

Definition at line 522 of file SGObject.cpp.

index_t get_modsel_param_index ( const char *  param_name)
inherited

Returns index of model selection parameter with provided index

Parameters
param_namename of model selection parameter
Returns
index of model selection parameter with provided name, -1 if there is no such

Definition at line 535 of file SGObject.cpp.

const char * get_name ( ) const
virtual
Returns
object name

Reimplemented from CConverter.

Definition at line 76 of file HashedDocConverter.cpp.

void init ( CTokenizer tzer,
int32_t  d,
bool  normalize,
int32_t  n_grams,
int32_t  skips 
)
protected

init

Definition at line 45 of file HashedDocConverter.cpp.

bool is_generic ( EPrimitiveType *  generic) const
virtualinherited

If the SGSerializable is a class template then TRUE will be returned and GENERIC is set to the type of the generic.

Parameters
genericset to the type of the generic if returning TRUE
Returns
TRUE if a class template.

Definition at line 296 of file SGObject.cpp.

bool load_serializable ( CSerializableFile file,
const char *  prefix = "" 
)
virtualinherited

Load this object from file. If it will fail (returning FALSE) then this object will contain inconsistent data and should not be used!

Parameters
filewhere to load from
prefixprefix for members
Returns
TRUE if done, otherwise FALSE

Definition at line 369 of file SGObject.cpp.

void load_serializable_post ( )
throw (ShogunException
)
protectedvirtualinherited

Can (optionally) be overridden to post-initialize some member variables which are not PARAMETER::ADD'ed. Make sure that at first the overridden method BASE_CLASS::LOAD_SERIALIZABLE_POST is called.

Exceptions
ShogunExceptionwill be thrown if an error occurs.

Reimplemented in CKernel, CWeightedDegreePositionStringKernel, CList, CAlphabet, CLinearHMM, CGaussianKernel, CInverseMultiQuadricKernel, CCircularKernel, and CExponentialKernel.

Definition at line 426 of file SGObject.cpp.

void load_serializable_pre ( )
throw (ShogunException
)
protectedvirtualinherited

Can (optionally) be overridden to pre-initialize some member variables which are not PARAMETER::ADD'ed. Make sure that at first the overridden method BASE_CLASS::LOAD_SERIALIZABLE_PRE is called.

Exceptions
ShogunExceptionwill be thrown if an error occurs.

Reimplemented in CDynamicArray< T >, CDynamicArray< float64_t >, CDynamicArray< float32_t >, CDynamicArray< int32_t >, CDynamicArray< char >, CDynamicArray< bool >, and CDynamicObjectArray.

Definition at line 421 of file SGObject.cpp.

bool parameter_hash_changed ( )
virtualinherited
Returns
whether parameter combination has changed since last update

Definition at line 262 of file SGObject.cpp.

void print_modsel_params ( )
inherited

prints all parameter registered for model selection and their type

Definition at line 474 of file SGObject.cpp.

void print_serializable ( const char *  prefix = "")
virtualinherited

prints registered parameters out

Parameters
prefixprefix for members

Definition at line 308 of file SGObject.cpp.

bool save_serializable ( CSerializableFile file,
const char *  prefix = "" 
)
virtualinherited

Save this object to file.

Parameters
filewhere to save the object; will be closed during returning if PREFIX is an empty string.
prefixprefix for members
Returns
TRUE if done, otherwise FALSE

Definition at line 314 of file SGObject.cpp.

void save_serializable_post ( )
throw (ShogunException
)
protectedvirtualinherited

Can (optionally) be overridden to post-initialize some member variables which are not PARAMETER::ADD'ed. Make sure that at first the overridden method BASE_CLASS::SAVE_SERIALIZABLE_POST is called.

Exceptions
ShogunExceptionwill be thrown if an error occurs.

Reimplemented in CKernel.

Definition at line 436 of file SGObject.cpp.

void save_serializable_pre ( )
throw (ShogunException
)
protectedvirtualinherited

Can (optionally) be overridden to pre-initialize some member variables which are not PARAMETER::ADD'ed. Make sure that at first the overridden method BASE_CLASS::SAVE_SERIALIZABLE_PRE is called.

Exceptions
ShogunExceptionwill be thrown if an error occurs.

Reimplemented in CKernel, CDynamicArray< T >, CDynamicArray< float64_t >, CDynamicArray< float32_t >, CDynamicArray< int32_t >, CDynamicArray< char >, CDynamicArray< bool >, and CDynamicObjectArray.

Definition at line 431 of file SGObject.cpp.

void set_generic ( )
inherited

Definition at line 41 of file SGObject.cpp.

void set_generic ( )
inherited

Definition at line 46 of file SGObject.cpp.

void set_generic ( )
inherited

Definition at line 51 of file SGObject.cpp.

void set_generic ( )
inherited

Definition at line 56 of file SGObject.cpp.

void set_generic ( )
inherited

Definition at line 61 of file SGObject.cpp.

void set_generic ( )
inherited

Definition at line 66 of file SGObject.cpp.

void set_generic ( )
inherited

Definition at line 71 of file SGObject.cpp.

void set_generic ( )
inherited

Definition at line 76 of file SGObject.cpp.

void set_generic ( )
inherited

Definition at line 81 of file SGObject.cpp.

void set_generic ( )
inherited

Definition at line 86 of file SGObject.cpp.

void set_generic ( )
inherited

Definition at line 91 of file SGObject.cpp.

void set_generic ( )
inherited

Definition at line 96 of file SGObject.cpp.

void set_generic ( )
inherited

Definition at line 101 of file SGObject.cpp.

void set_generic ( )
inherited

Definition at line 106 of file SGObject.cpp.

void set_generic ( )
inherited

Definition at line 111 of file SGObject.cpp.

void set_generic ( )
inherited

set generic type to T

void set_global_io ( SGIO io)
inherited

set the io object

Parameters
ioio object to use

Definition at line 228 of file SGObject.cpp.

void set_global_parallel ( Parallel parallel)
inherited

set the parallel object

Parameters
parallelparallel object to use

Definition at line 241 of file SGObject.cpp.

void set_global_version ( Version version)
inherited

set the version object

Parameters
versionversion object to use

Definition at line 283 of file SGObject.cpp.

void set_k_skip_n_grams ( int32_t  k,
int32_t  n 
)

Method used to specify the parameters for the quadratic approach of k-skip n-grams. See class description for more details and an example.

Parameters
kthe max number of allowed skips
nthe max number of tokens to combine

Definition at line 251 of file HashedDocConverter.cpp.

void set_normalization ( bool  normalize)

specify whether hashed vector should be normalized or not

Parameters
normalizewhether to normalize

Definition at line 246 of file HashedDocConverter.cpp.

CSGObject * shallow_copy ( ) const
virtualinherited

A shallow copy. All the SGObject instance variables will be simply assigned and SG_REF-ed.

Reimplemented in CGaussianKernel.

Definition at line 192 of file SGObject.cpp.

void unset_generic ( )
inherited

unset generic type

this has to be called in classes specializing a template class

Definition at line 303 of file SGObject.cpp.

void update_parameter_hash ( )
virtualinherited

Updates the hash of current parameter combination

Definition at line 248 of file SGObject.cpp.

Member Data Documentation

SGIO* io
inherited

io

Definition at line 369 of file SGObject.h.

Parameter* m_gradient_parameters
inherited

parameters wrt which we can compute gradients

Definition at line 384 of file SGObject.h.

uint32_t m_hash
inherited

Hash of parameter values

Definition at line 387 of file SGObject.h.

Parameter* m_model_selection_parameters
inherited

model selection parameters

Definition at line 381 of file SGObject.h.

Parameter* m_parameters
inherited

parameters

Definition at line 378 of file SGObject.h.

int32_t ngrams
protected

the number of consecutives tokens for quadratic

Definition at line 154 of file HashedDocConverter.h.

int32_t num_bits
protected

the number of bits of the hash

Definition at line 145 of file HashedDocConverter.h.

Parallel* parallel
inherited

parallel

Definition at line 372 of file SGObject.h.

bool should_normalize
protected

whether to normalize or not

Definition at line 151 of file HashedDocConverter.h.

CTokenizer* tokenizer
protected

the tokenizer

Definition at line 148 of file HashedDocConverter.h.

int32_t tokens_to_skip
protected

the number of tokens to skip

Definition at line 157 of file HashedDocConverter.h.

Version* version
inherited

version

Definition at line 375 of file SGObject.h.


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

SHOGUN Machine Learning Toolbox - Documentation