PolyMatchStringKernel.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 <shogun/lib/common.h>
00012 #include <shogun/io/SGIO.h>
00013 #include <shogun/kernel/string/PolyMatchStringKernel.h>
00014 #include <shogun/kernel/normalizer/SqrtDiagKernelNormalizer.h>
00015 #include <shogun/features/Features.h>
00016 #include <shogun/features/StringFeatures.h>
00017 
00018 using namespace shogun;
00019 
00020 CPolyMatchStringKernel::CPolyMatchStringKernel()
00021 : CStringKernel<char>()
00022 {
00023     init();
00024 }
00025 
00026 CPolyMatchStringKernel::CPolyMatchStringKernel(int32_t size, int32_t d, bool i)
00027 : CStringKernel<char>(size)
00028 {
00029     init();
00030 
00031     degree=d;
00032     inhomogene=i;
00033 }
00034 
00035 CPolyMatchStringKernel::CPolyMatchStringKernel(
00036     CStringFeatures<char>* l, CStringFeatures<char>* r, int32_t d, bool i)
00037 : CStringKernel<char>(10)
00038 {
00039     init();
00040 
00041     degree=d;
00042     inhomogene=i;
00043 
00044     init(l, r);
00045 }
00046 
00047 CPolyMatchStringKernel::~CPolyMatchStringKernel()
00048 {
00049     cleanup();
00050 }
00051 
00052 bool CPolyMatchStringKernel::init(CFeatures* l, CFeatures* r)
00053 {
00054     CStringKernel<char>::init(l, r);
00055     return init_normalizer();
00056 }
00057 
00058 void CPolyMatchStringKernel::cleanup()
00059 {
00060     CKernel::cleanup();
00061 }
00062 
00063 float64_t CPolyMatchStringKernel::compute(int32_t idx_a, int32_t idx_b)
00064 {
00065     int32_t i, alen, blen, sum;
00066     bool free_avec, free_bvec;
00067 
00068     char* avec = ((CStringFeatures<char>*) lhs)->get_feature_vector(idx_a, alen, free_avec);
00069     char* bvec = ((CStringFeatures<char>*) rhs)->get_feature_vector(idx_b, blen, free_bvec);
00070 
00071     ASSERT(alen==blen);
00072     for (i = 0, sum = inhomogene; i<alen; i++)
00073     {
00074         if (avec[i]==bvec[i])
00075             sum++;
00076     }
00077     float64_t result = ((float64_t) sum);
00078 
00079     if (rescaling)
00080         result/=alen;
00081 
00082     ((CStringFeatures<char>*) lhs)->free_feature_vector(avec, idx_a, free_avec);
00083     ((CStringFeatures<char>*) rhs)->free_feature_vector(bvec, idx_b, free_bvec);
00084     return CMath::pow(result , degree);
00085 }
00086 
00087 void CPolyMatchStringKernel::init()
00088 {
00089     degree=0;
00090     inhomogene=false;
00091     rescaling=false;
00092     set_normalizer(new CSqrtDiagKernelNormalizer());
00093 
00094     SG_ADD(&degree, "degree", "Degree of poly-kernel.", MS_AVAILABLE);
00095     SG_ADD(&inhomogene, "inhomogene", "True for inhomogene poly-kernel.",
00096         MS_NOT_AVAILABLE);
00097     SG_ADD(&rescaling, "rescaling",
00098         "True to rescale kernel with string length.", MS_AVAILABLE);
00099 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation