-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompvector.cpp
executable file
·73 lines (66 loc) · 1.56 KB
/
compvector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "compvector.h"
#include <math.h>
CompVector::CompVector(int _id) : id(_id)
{
}
int CompVector::GetID() const
{
return id;
}
int CompVector::TermAt(int _index) const
{
return terms.at(_index);
}
int CompVector::FrequencyAt(int _index) const
{
return frequencies.at(_index);
}
int CompVector::Length() const
{
return terms.length();
}
int CompVector::Lookup(int _term) const
{
int Index = terms.indexOf(_term, 0);
if(Index < 0){
return 0;
}
return frequencies.at(Index);
}
double CompVector::Sim(const CompVector &Vector1, const CompVector &Vector2)
{
double Result = 0;
int Numer = 0;
for(int i = 0 ; i < Vector1.Length() ; i++){
int Term = Vector1.TermAt(i);
int Freq1 = Vector1.FrequencyAt(i);
int Freq2 = Vector2.Lookup(Term);
Numer += Freq1 * Freq2;
}
if(Numer == 0){
return 0;
}
int Denom1 = 0, Denom2 = 0, Denom = 0;
for(int i = 0 ; i < Vector1.Length() ; i++){
Denom1 += Vector1.frequencies.at(i)*Vector1.frequencies.at(i);
}
for(int i = 0 ; i < Vector2.Length() ; i++){
Denom2 += Vector2.frequencies.at(i)*Vector2.frequencies.at(i);
}
Denom = Denom1 * Denom2;
Result = ((double)Numer) / sqrt(Denom);
if(Result > 1.0 || Result < 0.0){
qDebug("Inconsistent SIM Result: %f", Result);
}
return Result;
}
void CompVector::AddTerm(int _term)
{
int Index = terms.indexOf(_term);
if(Index == -1){
terms.append(_term);
frequencies.append(1);
}else{
frequencies[Index]++;
}
}