LCOV - code coverage report
Current view: top level - cluster - cosine_sim.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 16 19 84.2 %
Date: 2019-06-30 05:20:33 Functions: 1 2 50.0 %
Branches: 15 24 62.5 %

           Branch data     Line data    Source code
       1                 :            : /** @file cosine_sim.cc
       2                 :            :  *  @brief Cosine similarity calculation between documents
       3                 :            :  */
       4                 :            : /* Copyright (C) 2016 Richhiey Thomas
       5                 :            :  *
       6                 :            :  * This program is free software; you can redistribute it and/or
       7                 :            :  * modify it under the terms of the GNU General Public License as
       8                 :            :  * published by the Free Software Foundation; either version 2 of the
       9                 :            :  * License, or (at your option) any later version.
      10                 :            :  *
      11                 :            :  * This program is distributed in the hope that it will be useful,
      12                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14                 :            :  * GNU General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU General Public License
      17                 :            :  * along with this program; if not, write to the Free Software
      18                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
      19                 :            :  * USA
      20                 :            :  */
      21                 :            : 
      22                 :            : #include <config.h>
      23                 :            : 
      24                 :            : #include "xapian/cluster.h"
      25                 :            : 
      26                 :            : #include "debuglog.h"
      27                 :            : 
      28                 :            : #include <cmath>
      29                 :            : 
      30                 :            : using namespace std;
      31                 :            : using namespace Xapian;
      32                 :            : 
      33                 :            : string
      34                 :          0 : CosineDistance::get_description() const
      35                 :            : {
      36         [ #  # ]:          0 :     return "CosineDistance()";
      37                 :            : }
      38                 :            : 
      39                 :            : double
      40                 :        360 : CosineDistance::similarity(const PointType &a, const PointType &b) const
      41                 :            : {
      42                 :            :     LOGCALL(API, double, "CosineDistance::similarity", a | b);
      43                 :        360 :     double denom_a = a.get_magnitude();
      44                 :        360 :     double denom_b = b.get_magnitude();
      45                 :        360 :     double inner_product = 0;
      46                 :            : 
      47 [ +  + ][ -  + ]:        360 :     if (denom_a == 0 || denom_b == 0)
      48                 :        348 :         return 0.0;
      49                 :            : 
      50 [ +  - ][ +  - ]:         48 :     for (TermIterator it = a.termlist_begin(); it != a.termlist_end(); ++it) {
                 [ +  + ]
      51         [ +  - ]:         36 :         const string &term = *it;
      52         [ +  - ]:         36 :         double a_weight = a.get_weight(term);
      53         [ -  + ]:         36 :         if (a_weight == 0)
      54                 :          0 :             continue;
      55         [ +  - ]:         36 :         double b_weight = b.get_weight(term);
      56         [ +  + ]:         36 :         if (b_weight == 0)
      57                 :         18 :             continue;
      58         [ +  + ]:         36 :         inner_product += a_weight * b_weight;
      59                 :         30 :     }
      60                 :            : 
      61                 :        360 :     return 1 - (inner_product / (sqrt(denom_a * denom_b)));
      62                 :            : }

Generated by: LCOV version 1.11