LocalityImprovedStringKernel.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 Gunnar Raetsch
00008  * Written (W) 1999-2008 Soeren Sonnenburg
00009  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
00010  */
00011 
00012 #include "lib/common.h"
00013 #include "lib/io.h"
00014 #include "kernel/LocalityImprovedStringKernel.h"
00015 #include "features/StringFeatures.h"
00016 
00017 CLocalityImprovedStringKernel::CLocalityImprovedStringKernel(
00018     int32_t size, int32_t l, int32_t id, int32_t od)
00019 : CStringKernel<char>(size), length(l), inner_degree(id), outer_degree(od)
00020 {
00021     SG_INFO( "LIK with parms: l=%d, id=%d, od=%d created!\n", l, id, od);
00022 }
00023 
00024 CLocalityImprovedStringKernel::CLocalityImprovedStringKernel(
00025     CStringFeatures<char>* l, CStringFeatures<char>* r, int32_t len,
00026     int32_t id, int32_t od)
00027 : CStringKernel<char>(10), length(len), inner_degree(id), outer_degree(od)
00028 {
00029     SG_INFO( "LIK with parms: l=%d, id=%d, od=%d created!\n", len, id, od);
00030 
00031     init(l, r);
00032 }
00033 
00034 CLocalityImprovedStringKernel::~CLocalityImprovedStringKernel()
00035 {
00036     cleanup();
00037 }
00038 
00039 bool CLocalityImprovedStringKernel::init(CFeatures* l, CFeatures* r)
00040 {
00041     CStringKernel<char>::init(l,r);
00042     return init_normalizer();
00043 }
00044 
00045 bool CLocalityImprovedStringKernel::load_init(FILE* src)
00046 {
00047     return false;
00048 }
00049 
00050 bool CLocalityImprovedStringKernel::save_init(FILE* dest)
00051 {
00052     return false;
00053 }
00054 
00055 float64_t CLocalityImprovedStringKernel::compute(int32_t idx_a, int32_t idx_b)
00056 {
00057     int32_t alen, blen;
00058 
00059     char* avec = ((CStringFeatures<char>*) lhs)->get_feature_vector(idx_a, alen);
00060     char* bvec = ((CStringFeatures<char>*) rhs)->get_feature_vector(idx_b, blen);
00061     // can only deal with strings of same length
00062     ASSERT(alen==blen && alen>0);
00063 
00064     int32_t i,t;
00065     float64_t* match=new float64_t[alen];
00066 
00067     // initialize match table 1 -> match;  0 -> no match
00068     for (i = 0; i<alen; i++)
00069         match[i] = (avec[i] == bvec[i])? 1 : 0;
00070 
00071     float64_t outer_sum = 0;
00072 
00073     for (t = 0; t<alen-length; t++)
00074     {
00075         float64_t sum = 0;
00076         for (i = 0; i<length && t+i+length+1<alen; i++)
00077             sum += (i+1)*match[t+i]+(length-i)*match[t+i+length+1];
00078         //add middle element + normalize with sum_i=0^2l+1 i = (2l+1)(l+1)
00079         float64_t inner_sum = (sum + (length+1)*match[t+length]) / ((2*length+1)*(length+1));
00080         inner_sum = pow(inner_sum, inner_degree + 1);
00081         outer_sum += inner_sum;
00082     }
00083     delete[] match;
00084     return pow(outer_sum, outer_degree + 1);
00085 }

SHOGUN Machine Learning Toolbox - Documentation