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(int32_t size, float64_t w)
00021 : CStringKernel<char>(size), width(w)
00022 {
00023     set_normalizer(new CSqrtDiagKernelNormalizer());
00024 }
00025 
00026 CGaussianMatchStringKernel::CGaussianMatchStringKernel(
00027     CStringFeatures<char>* l, CStringFeatures<char>* r, float64_t w)
00028 : CStringKernel<char>(10), width(w)
00029 {
00030     set_normalizer(new CSqrtDiagKernelNormalizer());
00031     init(l, r);
00032 }
00033 
00034 CGaussianMatchStringKernel::~CGaussianMatchStringKernel()
00035 {
00036     cleanup();
00037 }
00038 
00039 bool CGaussianMatchStringKernel::init(CFeatures* l, CFeatures* r)
00040 {
00041     CStringKernel<char>::init(l, r);
00042     return init_normalizer();
00043 }
00044 
00045 void CGaussianMatchStringKernel::cleanup()
00046 {
00047     CKernel::cleanup();
00048 }
00049 
00050 float64_t CGaussianMatchStringKernel::compute(int32_t idx_a, int32_t idx_b)
00051 {
00052     int32_t i, alen, blen ;
00053     bool free_avec, free_bvec;
00054 
00055     char* avec = ((CStringFeatures<char>*) lhs)->get_feature_vector(idx_a, alen, free_avec);
00056     char* bvec = ((CStringFeatures<char>*) rhs)->get_feature_vector(idx_b, blen, free_bvec);
00057 
00058     float64_t result=0;
00059 
00060     ASSERT(alen==blen);
00061 
00062     for (i = 0;  i<alen; i++) 
00063         result+=(avec[i]==bvec[i]) ? 0:4;
00064 
00065     result=exp(-result/width);
00066 
00067 
00068     ((CStringFeatures<char>*) lhs)->free_feature_vector(avec, idx_a, free_avec);
00069     ((CStringFeatures<char>*) rhs)->free_feature_vector(bvec, idx_b, free_bvec);
00070     return result;
00071 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation