PolyMatchWordStringKernel.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-2008 Soeren Sonnenburg
00008  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include "lib/common.h"
00012 #include "lib/io.h"
00013 #include "kernel/PolyMatchWordStringKernel.h"
00014 #include "kernel/SqrtDiagKernelNormalizer.h"
00015 #include "features/Features.h"
00016 #include "features/StringFeatures.h"
00017 
00018 CPolyMatchWordStringKernel::CPolyMatchWordStringKernel(int32_t size, int32_t d, bool i)
00019 : CStringKernel<uint16_t>(size),degree(d),inhomogene(i)
00020 {
00021     set_normalizer(new CSqrtDiagKernelNormalizer());
00022 }
00023 
00024 CPolyMatchWordStringKernel::CPolyMatchWordStringKernel(
00025     CStringFeatures<uint16_t>* l, CStringFeatures<uint16_t>* r, int32_t d, bool i)
00026 : CStringKernel<uint16_t>(10),degree(d),inhomogene(i)
00027 {
00028     set_normalizer(new CSqrtDiagKernelNormalizer());
00029     init(l, r);
00030 }
00031 
00032 CPolyMatchWordStringKernel::~CPolyMatchWordStringKernel()
00033 {
00034     cleanup();
00035 }
00036 
00037 bool CPolyMatchWordStringKernel::init(CFeatures* l, CFeatures* r)
00038 {
00039     CStringKernel<uint16_t>::init(l,r);
00040     return init_normalizer();
00041 }
00042 
00043 void CPolyMatchWordStringKernel::cleanup()
00044 {
00045     CKernel::cleanup();
00046 }
00047 
00048 bool CPolyMatchWordStringKernel::load_init(FILE* src)
00049 {
00050     return false;
00051 }
00052 
00053 bool CPolyMatchWordStringKernel::save_init(FILE* dest)
00054 {
00055     return false;
00056 }
00057 
00058 float64_t CPolyMatchWordStringKernel::compute(int32_t idx_a, int32_t idx_b)
00059 {
00060     int32_t alen, blen;
00061 
00062     uint16_t* avec=((CStringFeatures<uint16_t>*) lhs)->get_feature_vector(idx_a, alen);
00063     uint16_t* bvec=((CStringFeatures<uint16_t>*) rhs)->get_feature_vector(idx_b, blen);
00064 
00065     ASSERT(alen==blen);
00066 
00067     int32_t sum=0;
00068 
00069     for (int32_t i=0; i<alen; i++)
00070         sum+= (avec[i]==bvec[i]) ? 1 : 0;
00071 
00072     if (inhomogene)
00073         sum+=1;
00074 
00075     float64_t result=sum;
00076 
00077     for (int32_t j=1; j<degree; j++)
00078         result*=sum;
00079 
00080     return result;
00081 }

SHOGUN Machine Learning Toolbox - Documentation