BitMagic-C++: bmalgo_similarity.h Source File

BitMagic-C++

bmalgo_similarity.h
Go to the documentation of this file.
1 #ifndef BMALGO_SIMILARITY__H__INCLUDED__
2 #define BMALGO_SIMILARITY__H__INCLUDED__
3 /*
4 Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17
18 For more information please visit: http://bitmagic.io
19 */
20
21 #include <algorithm>
22 #include <functional>
23 #include <vector>
24
25 #ifndef BM__H__INCLUDED__
26 // BitMagic utility headers do not include main "bm.h" declaration
27 // #include "bm.h" or "bm64.h" explicitly
28 # error missing include (bm.h or bm64.h)
29 #endif
30
31
32 #include "bmfunc.h"
33 #include "bmdef.h"
34
35 #include "bmalgo_impl.h"
36
37 namespace bm
38 {
39
40 /*! Similarity descriptor between two objects (bit vectors, blocks, etc)
41 \internal
42 */
43 template <typename SO, unsigned DMD_SZ, typename IDX_VALUE, typename SValue, typename SFunc>
44 class similarity_descriptor
45 {
46 public:
47 typedef SO similarity_object_type;
48 typedef SValue similarity_value_type;
49 typedef SFunc similarity_functor;
50 public:
51 similarity_descriptor()
52 : so1_(0), so2_(0), so1_idx_(0), so2_idx_(0)
53 {
54 similarity_ = 0;
55 }
56
57 similarity_descriptor(const SO* so1, const SO* so2,
58 const distance_metric_descriptor* dmd_ptr)
59 :so1_(so1),
60 so2_(so2),
61 so1_idx_(0), so2_idx_(0)
62 {
63 for (size_t i = 0; i < DMD_SZ; ++i)
64 dmd_[i] = dmd_ptr[i];
65 similarity_ = 0;
66 }
67
68 similarity_descriptor(const SO* so1, IDX_VALUE i1,
69 const SO* so2, IDX_VALUE i2,
70 const distance_metric_descriptor* dmd_ptr)
71 :so1_(so1), so2_(so2), so1_idx_(i1), so2_idx_(i2)
72 {
73 for (size_t i = 0; i < DMD_SZ; ++i)
74 dmd_[i] = dmd_ptr[i];
75 similarity_ = 0;
76 }
77
78 similarity_descriptor(const similarity_descriptor& sd)
79 : similarity_(sd.similarity_),
80 so1_(sd.so1_),
81 so2_(sd.so2_),
82 so1_idx_(sd.so1_idx_),
83 so2_idx_(sd.so2_idx_)
84 {
85 for (size_t i = 0; i < DMD_SZ; ++i)
86 dmd_[i] = sd.dmd_[i];
87 }
88 similarity_descriptor& operator=(const similarity_descriptor& sd)
89 {
90 similarity_ = sd.similarity_;
91 so1_ = sd.so1_; so2_ = sd.so2_;
92 so1_idx_ = sd.so1_idx_; so2_idx_ = sd.so2_idx_;
93 for (size_t i = 0; i < DMD_SZ; ++i)
94 dmd_[i] = sd.dmd_[i];
95 return *this;
96 }
97
98 bool operator > (const similarity_descriptor& sd) const
99 {
100 return similarity_ > sd.similarity_;
101 }
102
103 SValue similarity() const { return similarity_; }
104 void set_similarity(SValue s) { similarity_ = s;}
105
106 const SO* get_first() const { return so1_; }
107 const SO* get_second() const { return so2_; }
108
109 IDX_VALUE get_first_idx() const { return so1_idx_; }
110 IDX_VALUE get_second_idx() const { return so2_idx_; }
111
112 distance_metric_descriptor* distance_begin() { return &dmd_[0]; }
113 distance_metric_descriptor* distance_end() { return &dmd_[DMD_SZ]; }
114
115 void set_metric(size_t i, distance_metric metric)
116 {
117 BM_ASSERT(i < DMD_SZ);
118 dmd_[i].metric = metric;
119 }
120
121
122 protected:
123 SValue similarity_; //< final similarity product
124 const SO* so1_; //< object 1 for similarity comparison
125 const SO* so2_; //< object 2 for similarity comparison
126 IDX_VALUE so1_idx_; //< index of object 1
127 IDX_VALUE so2_idx_; //< index of object 2
128 distance_metric_descriptor dmd_[DMD_SZ]; //< array of distance operations defined on objects
129 };
130
131 /*!
132 Batch of objects for similarity measurement
133 \internal
134 */
135 template<class SDESCR>
136 struct similarity_batch
137 {
138 typedef SDESCR similaruty_descriptor_type;
139 typedef typename SDESCR::similarity_object_type similarity_object_type;
140 typedef typename SDESCR::similarity_value_type similarity_value_type;
141 typedef typename SDESCR::similarity_functor similarity_functor;
142 typedef std::vector<SDESCR> vector_type;
143
144 /// run the similarity calculation using distance metrics engine
145 void calculate()
146 {
147 for( size_t i = 0; i < descr_vect_.size(); ++i)
148 {
149 similaruty_descriptor_type& sdescr = descr_vect_[i];
150
151 const similarity_object_type* so1 = sdescr.get_first();
152 const similarity_object_type* so2 = sdescr.get_second();
153
154 distance_metric_descriptor* dit = sdescr.distance_begin();
155 distance_metric_descriptor* dit_end = sdescr.distance_end();
156 bm::distance_operation(*so1, *so2, dit, dit_end);
157
158 // reduce: use functor to compute final similarity metric
159 similarity_functor func;
160 similarity_value_type d = func(dit, dit_end);
161 sdescr.set_similarity(d);
162 } // for i
163 }
164
165 void sort()
166 {
167 std::sort(descr_vect_.begin(), descr_vect_.end(), std::greater<SDESCR>());
168 }
169
170 void reserve(size_t cap)
171 {
172 descr_vect_.reserve(cap);
173 }
174
175 void push_back(const similaruty_descriptor_type& sdt)
176 {
177 descr_vect_.push_back(sdt);
178 }
179
180 public:
181 std::vector<SDESCR> descr_vect_;
182 };
183
184
185 /**
186 Utility function to build jaccard similarity batch for sparse_vector<>
187 \internal
188 */
189 template<class SIMBATCH, class SV>
190 void build_jaccard_similarity_batch(SIMBATCH& sbatch, const SV& sv)
191 {
192
193 size_t plains = sv.plains();
194 sbatch.reserve((plains * plains) / 2);
195
196 bm::distance_metric_descriptor dmd[2];
197 dmd[0].metric = bm::COUNT_AND;
198 dmd[1].metric = bm::COUNT_OR;
199
200 // build a batch for triangular distance matrix
201 //
202 for (unsigned i = 0; i < plains; ++i)
203 {
204 const typename SV::bvector_type* bv1 = sv.get_plain(i);
205 if (bv1)
206 {
207 for (unsigned j = i+1; j < plains; ++j)
208 {
209 const typename SV::bvector_type* bv2 = sv.get_plain(j);
210 if (bv2 && bv1 != bv2)
211 {
212 sbatch.push_back(typename SIMBATCH::similaruty_descriptor_type(bv1, i, bv2, j, &dmd[0]));
213 }
214 } // for j
215 }
216
217 } // for i
218 }
219
220
221
222 } // namespace bm
223
224 #include "bmundef.h"
225
226 #endif
bm::similarity_descriptor::so2_
const SO * so2_
Definition: bmalgo_similarity.h:125
bm::similarity_descriptor::set_metric
void set_metric(size_t i, distance_metric metric)
Definition: bmalgo_similarity.h:115
bm::similarity_batch::similarity_object_type
SDESCR::similarity_object_type similarity_object_type
Definition: bmalgo_similarity.h:139
bmfunc.h
Bit manipulation primitives (internal)
bm::similarity_descriptor::get_second_idx
IDX_VALUE get_second_idx() const
Definition: bmalgo_similarity.h:110
bm::distance_metric_descriptor
Distance metric descriptor, holds metric code and result.
Definition: bmalgo_impl.h:87
bm::similarity_descriptor::so1_
const SO * so1_
Definition: bmalgo_similarity.h:124
bm::similarity_descriptor::similarity_descriptor
similarity_descriptor()
Definition: bmalgo_similarity.h:51
bm::similarity_descriptor::distance_begin
distance_metric_descriptor * distance_begin()
Definition: bmalgo_similarity.h:112
bm::similarity_descriptor::operator=
similarity_descriptor & operator=(const similarity_descriptor &sd)
Definition: bmalgo_similarity.h:88
bm::similarity_descriptor::similarity_object_type
SO similarity_object_type
Definition: bmalgo_similarity.h:47
bm::similarity_descriptor::set_similarity
void set_similarity(SValue s)
Definition: bmalgo_similarity.h:104
bm::similarity_descriptor::similarity_descriptor
similarity_descriptor(const SO *so1, const SO *so2, const distance_metric_descriptor *dmd_ptr)
Definition: bmalgo_similarity.h:57
bm::similarity_descriptor::similarity_descriptor
similarity_descriptor(const SO *so1, IDX_VALUE i1, const SO *so2, IDX_VALUE i2, const distance_metric_descriptor *dmd_ptr)
Definition: bmalgo_similarity.h:68
bm::similarity_batch::vector_type
std::vector< SDESCR > vector_type
Definition: bmalgo_similarity.h:142
bm::build_jaccard_similarity_batch
void build_jaccard_similarity_batch(SIMBATCH &sbatch, const SV &sv)
Utility function to build jaccard similarity batch for sparse_vector<>
Definition: bmalgo_similarity.h:190
bm::similarity_descriptor
Definition: bmalgo_similarity.h:44
bm::similarity_batch::similarity_functor
SDESCR::similarity_functor similarity_functor
Definition: bmalgo_similarity.h:141
bm::COUNT_OR
@ COUNT_OR
(A | B).count()
Definition: bmalgo_impl.h:61
bm::similarity_batch::reserve
void reserve(size_t cap)
Definition: bmalgo_similarity.h:170
bmundef.h
pre-processor un-defines to avoid global space pollution (internal)
bm::similarity_batch::similarity_value_type
SDESCR::similarity_value_type similarity_value_type
Definition: bmalgo_similarity.h:140
bm::similarity_descriptor::so2_idx_
IDX_VALUE so2_idx_
Definition: bmalgo_similarity.h:127
bm::similarity_descriptor::similarity_value_type
SValue similarity_value_type
Definition: bmalgo_similarity.h:48
bm::similarity_batch::push_back
void push_back(const similaruty_descriptor_type &sdt)
Definition: bmalgo_similarity.h:175
BM_ASSERT
#define BM_ASSERT
Definition: bmdef.h:130
bm::similarity_descriptor::similarity
SValue similarity() const
Definition: bmalgo_similarity.h:103
bm::similarity_batch::descr_vect_
std::vector< SDESCR > descr_vect_
Definition: bmalgo_similarity.h:181
bvector_type
bm::bvector bvector_type
Definition: strsvsample01.cpp:39
bm::similarity_descriptor::get_first
const SO * get_first() const
Definition: bmalgo_similarity.h:106
bmdef.h
Definitions(internal)
bm::similarity_batch::sort
void sort()
Definition: bmalgo_similarity.h:165
bm::similarity_batch::similaruty_descriptor_type
SDESCR similaruty_descriptor_type
Definition: bmalgo_similarity.h:138
bm::similarity_descriptor::similarity_
SValue similarity_
Definition: bmalgo_similarity.h:123
bm::similarity_descriptor::operator>
bool operator>(const similarity_descriptor &sd) const
Definition: bmalgo_similarity.h:98
bm::similarity_descriptor::get_second
const SO * get_second() const
Definition: bmalgo_similarity.h:107
bm::distance_metric_descriptor::metric
distance_metric metric
Definition: bmalgo_impl.h:95
bm::similarity_descriptor::get_first_idx
IDX_VALUE get_first_idx() const
Definition: bmalgo_similarity.h:109
bm::similarity_descriptor::similarity_functor
SFunc similarity_functor
Definition: bmalgo_similarity.h:49
bm::similarity_descriptor::so1_idx_
IDX_VALUE so1_idx_
Definition: bmalgo_similarity.h:126
bmalgo_impl.h
Algorithms for bvector<>
bm
Definition: bm.h:76
bm::distance_operation
void distance_operation(const BV &bv1, const BV &bv2, distance_metric_descriptor *dmit, distance_metric_descriptor *dmit_end) BMNOEXCEPT
Distance computing template function.
Definition: bmalgo_impl.h:702
bm::similarity_batch::calculate
void calculate()
run the similarity calculation using distance metrics engine
Definition: bmalgo_similarity.h:145
bm::COUNT_AND
@ COUNT_AND
(A & B).count()
Definition: bmalgo_impl.h:59
bm::similarity_descriptor::dmd_
distance_metric_descriptor dmd_[DMD_SZ]
Definition: bmalgo_similarity.h:128
bm::similarity_descriptor::distance_end
distance_metric_descriptor * distance_end()
Definition: bmalgo_similarity.h:113
bm::similarity_descriptor::similarity_descriptor
similarity_descriptor(const similarity_descriptor &sd)
Definition: bmalgo_similarity.h:78
bm::distance_metric
distance_metric
Distance metrics codes defined for vectors A and B.
Definition: bmalgo_impl.h:57
bm::similarity_batch
Definition: bmalgo_similarity.h:136