MatchWordStringKernel.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/mathematics/Math.h>
00013 #include <shogun/io/SGIO.h>
00014 #include <shogun/kernel/MatchWordStringKernel.h>
00015 #include <shogun/kernel/AvgDiagKernelNormalizer.h>
00016 #include <shogun/features/StringFeatures.h>
00017 
00018 using namespace shogun;
00019 
00020 CMatchWordStringKernel::CMatchWordStringKernel() : CStringKernel<uint16_t>()
00021 {
00022     init();
00023 }
00024 
00025 CMatchWordStringKernel::CMatchWordStringKernel(int32_t size, int32_t d)
00026 : CStringKernel<uint16_t>(size)
00027 {
00028     init();
00029     degree=d;
00030 }
00031 
00032 CMatchWordStringKernel::CMatchWordStringKernel(
00033         CStringFeatures<uint16_t>* l, CStringFeatures<uint16_t>* r, int32_t d)
00034 : CStringKernel<uint16_t>()
00035 {
00036     init();
00037     degree=d;
00038     init(l, r);
00039 }
00040 
00041 CMatchWordStringKernel::~CMatchWordStringKernel()
00042 {
00043     cleanup();
00044 }
00045 
00046 bool CMatchWordStringKernel::init(CFeatures* l, CFeatures* r)
00047 {
00048     CStringKernel<uint16_t>::init(l, r);
00049     return init_normalizer();
00050 }
00051 
00052 float64_t CMatchWordStringKernel::compute(int32_t idx_a, int32_t idx_b)
00053 {
00054     int32_t alen, blen;
00055     bool free_avec, free_bvec;
00056 
00057     uint16_t* avec=((CStringFeatures<uint16_t>*) lhs)->get_feature_vector(idx_a, alen, free_avec);
00058     uint16_t* bvec=((CStringFeatures<uint16_t>*) rhs)->get_feature_vector(idx_b, blen, free_bvec);
00059     // can only deal with strings of same length
00060     ASSERT(alen==blen);
00061 
00062     float64_t sum=0;
00063     for (int32_t i=0; i<alen; i++)
00064         sum+= (avec[i]==bvec[i]) ? 1 : 0;
00065 
00066     ((CStringFeatures<uint16_t>*) lhs)->free_feature_vector(avec, idx_a, free_avec);
00067     ((CStringFeatures<uint16_t>*) rhs)->free_feature_vector(bvec, idx_b, free_bvec);
00068 
00069     return CMath::pow(sum, degree);
00070 }
00071 
00072 void CMatchWordStringKernel::init()
00073 {
00074     degree=0;
00075     set_normalizer(new CAvgDiagKernelNormalizer());
00076     m_parameters->add(&degree, "degree", "Degree of poly kernel");
00077 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation