GaussianMatchStringKernel.cpp

Go to the documentation of this file.
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) 1999-2009 Soeren Sonnenburg
00008  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include "lib/common.h"
00012 #include "lib/io.h"
00013 #include "kernel/GaussianMatchStringKernel.h"
00014 #include "kernel/SqrtDiagKernelNormalizer.h"
00015 #include "features/Features.h"
00016 #include "features/StringFeatures.h"
00017 
00018 using namespace shogun;
00019 
00020 CGaussianMatchStringKernel::CGaussianMatchStringKernel(void)
00021 : CStringKernel<char>(0), width(0.0)
00022 {
00023     SG_UNSTABLE("CGaussianMatchStringKernel::"
00024                 "CGaussianMatchStringKernel(void)", "\n");
00025 
00026     set_normalizer(new CSqrtDiagKernelNormalizer());
00027 }
00028 
00029 CGaussianMatchStringKernel::CGaussianMatchStringKernel(int32_t size, float64_t w)
00030 : CStringKernel<char>(size), width(w)
00031 {
00032     set_normalizer(new CSqrtDiagKernelNormalizer());
00033 }
00034 
00035 CGaussianMatchStringKernel::CGaussianMatchStringKernel(
00036     CStringFeatures<char>* l, CStringFeatures<char>* r, float64_t w)
00037 : CStringKernel<char>(10), width(w)
00038 {
00039     set_normalizer(new CSqrtDiagKernelNormalizer());
00040     init(l, r);
00041 }
00042 
00043 CGaussianMatchStringKernel::~CGaussianMatchStringKernel()
00044 {
00045     cleanup();
00046 }
00047 
00048 bool CGaussianMatchStringKernel::init(CFeatures* l, CFeatures* r)
00049 {
00050     CStringKernel<char>::init(l, r);
00051     return init_normalizer();
00052 }
00053 
00054 void CGaussianMatchStringKernel::cleanup()
00055 {
00056     CKernel::cleanup();
00057 }
00058 
00059 float64_t CGaussianMatchStringKernel::compute(int32_t idx_a, int32_t idx_b)
00060 {
00061     int32_t i, alen, blen ;
00062     bool free_avec, free_bvec;
00063 
00064     char* avec = ((CStringFeatures<char>*) lhs)->get_feature_vector(idx_a, alen, free_avec);
00065     char* bvec = ((CStringFeatures<char>*) rhs)->get_feature_vector(idx_b, blen, free_bvec);
00066 
00067     float64_t result=0;
00068 
00069     ASSERT(alen==blen);
00070 
00071     for (i = 0;  i<alen; i++) 
00072         result+=(avec[i]==bvec[i]) ? 0:4;
00073 
00074     result=exp(-result/width);
00075 
00076 
00077     ((CStringFeatures<char>*) lhs)->free_feature_vector(avec, idx_a, free_avec);
00078     ((CStringFeatures<char>*) rhs)->free_feature_vector(bvec, idx_b, free_bvec);
00079     return result;
00080 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation