00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2011 Shashwat Lal Das 00008 * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society 00009 */ 00010 #ifndef __STREAMING_FILEFROMSTRING_H__ 00011 #define __STREAMING_FILEFROMSTRING_H__ 00012 00013 #include <shogun/io/streaming/StreamingFileFromFeatures.h> 00014 #include <shogun/features/StringFeatures.h> 00015 00016 namespace shogun 00017 { 00022 template <class T> class CStreamingFileFromStringFeatures: public CStreamingFileFromFeatures 00023 { 00024 public: 00028 CStreamingFileFromStringFeatures(); 00029 00035 CStreamingFileFromStringFeatures(CStringFeatures<T>* feat); 00036 00043 CStreamingFileFromStringFeatures(CStringFeatures<T>* feat, float64_t* lab); 00044 00048 virtual ~CStreamingFileFromStringFeatures(); 00049 00058 virtual void get_string(T* &vec, int32_t &len); 00059 00069 virtual void get_string_and_label(T* &vec, int32_t &len, float64_t &label); 00070 00076 void reset_stream() 00077 { 00078 vector_num = 0; 00079 } 00080 00082 virtual const char* get_name() const 00083 { 00084 return "StreamingFileFromStringFeatures"; 00085 00086 } 00087 00088 private: 00092 void init(); 00093 00094 protected: 00095 00097 CStringFeatures<T>* features; 00098 00100 int32_t vector_num; 00101 00102 }; 00103 00104 template <class T> 00105 CStreamingFileFromStringFeatures<T>::CStreamingFileFromStringFeatures() 00106 : CStreamingFileFromFeatures() 00107 { 00108 init(); 00109 } 00110 00111 template <class T> 00112 CStreamingFileFromStringFeatures<T>::CStreamingFileFromStringFeatures(CStringFeatures<T>* feat) 00113 : CStreamingFileFromFeatures(feat) 00114 { 00115 init(); 00116 } 00117 00118 template <class T> 00119 CStreamingFileFromStringFeatures<T>::CStreamingFileFromStringFeatures(CStringFeatures<T>* feat, float64_t* lab) 00120 : CStreamingFileFromFeatures(feat,lab) 00121 { 00122 init(); 00123 } 00124 00125 template <class T> 00126 CStreamingFileFromStringFeatures<T>::~CStreamingFileFromStringFeatures() 00127 { 00128 } 00129 00130 template <class T> 00131 void CStreamingFileFromStringFeatures<T>::init() 00132 { 00133 vector_num=0; 00134 } 00135 00136 /* Functions to return the vector from the StringFeatures object */ 00137 template <class T> 00138 void CStreamingFileFromStringFeatures<T>::get_string(T*& vector, int32_t& num_feat) 00139 { 00140 if (vector_num >= features->get_num_vectors()) 00141 { 00142 vector=NULL; 00143 num_feat=-1; 00144 return; 00145 } 00146 00147 SGVector<T> sg_vector= 00148 features->get_feature_vector(vector_num); 00149 00150 vector = sg_vector.vector; 00151 num_feat = sg_vector.vlen;; 00152 vector_num++; 00153 00154 } 00155 00156 /* Functions to return the vector from the StringFeatures object with label */ 00157 template <class T> 00158 void CStreamingFileFromStringFeatures<T>::get_string_and_label 00159 (T*& vector, int32_t& num_feat, float64_t& label) 00160 { 00161 if (vector_num >= features->get_num_vectors()) 00162 { 00163 vector=NULL; 00164 num_feat=-1; 00165 return; 00166 } 00167 00168 SGVector<T> sg_vector 00169 =features->get_feature_vector(vector_num); 00170 00171 vector = sg_vector.vector; 00172 num_feat = sg_vector.vlen; 00173 label = labels[vector_num]; 00174 00175 vector_num++; 00176 } 00177 00178 00179 } 00180 #endif //__STREAMING_FILEFROMSTRING_H__