-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheuristics.cpp
More file actions
213 lines (177 loc) · 6.1 KB
/
Copy pathheuristics.cpp
File metadata and controls
213 lines (177 loc) · 6.1 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "heuristics.h"
#include <string>
size_t Ranker::distance( Location loc1, Location loc2 )
{
return ( loc1 > loc2 ) ?
( loc1 - loc2 ) :
( loc2 - loc1 );
}
void Ranker::rarestWord( )
{
// find locations of matching doc
docEnd = endDoc->GetStartLocation( );
docLength = endDoc->GetDocumentLength( );
docBegin = docEnd - docLength;
// number of frequent word
int numFreqWord = 0;
// find rarest word
for ( int i = 0; i < numWords; i ++ )
{
int wordCount = 0;
ISRWord *tmpISR = words[ i ];
while ( tmpISR != nullptr && tmpISR->GetStartLocation( ) > docBegin && tmpISR->GetStartLocation( ) < docEnd )
{
wordCount ++;
if ( tmpISR->Next( ) == nullptr )
break;
}
if ( wordCount < rarestOccurrences )
{
rarestOccurrences = wordCount;
rarest = i;
}
// whether word freqent
if ( wordCount >= MinToBeFreq )
numFreqWord ++;
// reset isrs
words[ i ]->Seek( docBegin ); // TODO: avoid reset
endDoc->Seek(docBegin);
}
// increment numMostWordFreq
percentWordFreq = ( float ) numFreqWord / numWords;
if ( percentWordFreq >= MinRatioToBeMost )
numMostWordFreq ++;
endDoc->Seek(docBegin);
//std::cout << "rarest: " << rarest << ", occurrence: " << rarestOccurrences << std::endl;
}
void Ranker::forward( )
{
ISRWord *tmpRarestISR = words[ rarest ];
vector< Location > spanLocations( numWords ); // locations of words in a span
for ( int i = 0; i < rarestOccurrences; i ++ )
{
Location rarestWordLocation = tmpRarestISR->GetStartLocation( );
spanLocations[ rarest ] = rarestWordLocation;
// arrange other ISRs to as close as possible to the rarest word
for ( int j = 0; j < numWords; j ++ )
{
if ( j != rarest )
{
ISRWord *tmpOtherISR = words[ j ];
int minDifference = INT_MAX;
while ( tmpOtherISR != nullptr )
{
Location otherWordLocation = tmpOtherISR->GetStartLocation( );
int difference = rarestWordLocation - otherWordLocation;
// std::cout << "diff: " << difference << " other word: " << otherWordLocation << " rarest word: " << rarestWordLocation << std::endl;
if ( difference > 0 )
{
minDifference = difference;
spanLocations[ j ] = otherWordLocation;
if ( tmpOtherISR->Next( ) == nullptr )
break;
}
else
{
// the other word exceeds the rarest word
if ( - difference < minDifference )
{
// if the other word is the closest to the rarest word
spanLocations[ j ] = otherWordLocation;
minDifference = - difference;
}
break;
}
}
// reset isr j
words[ j ]->Seek( docBegin );
endDoc->Seek(docBegin);
}
}
Location nearestLocation = SIZE_MAX, farthestLocation = 0; // for heuristics
for ( int i = 0; i < numWords; i ++ )
{
// record nearest and farthest location ( for heuristics )
if ( spanLocations[ i ] < nearestLocation )
nearestLocation = spanLocations[ i ];
if ( spanLocations[ i ] > farthestLocation )
farthestLocation = spanLocations[ i ];
//std::cout << spanLocations[i] << " ";
}
//std::cout << std::endl;
//std::cout << "farthest loc: " << farthestLocation << ", nearest loc: " << nearestLocation << std::endl;
// calculate heuristics
// if short span
if ( farthestLocation - nearestLocation <= MaxToBeShort )
numShortSpan ++;
// in order span; exact phrase; most words are frequent
bool isInOrder = true, isExactPhrase = true;
int numFreqWord = 0;
for ( int i = 1; i < numWords; i ++ )
{
if ( spanLocations[ i - 1 ] > spanLocations[ i ] )
isInOrder = false;
if ( spanLocations[ i - 1 ] != spanLocations[ i ] - 1 )
isExactPhrase = false;
}
if ( isInOrder )
numInOrderSpan ++;
if ( isExactPhrase )
numExactPhrase ++;
// if span near the top
if ( farthestLocation <= MinToBeNearTop )
numTopSpan ++;
// look into next occurrence of the rarest word
if ( tmpRarestISR->Next( ) == nullptr ) // nullptr if reaching the end of posting list; TODO: memory leak?
break;
}
}
int Ranker::dynamicScore( )
{
int score = numShortSpan * shortSpanWeight +
// numInOrderSpan * inOrderSpanWeight +
numExactPhrase * exactPhraseWeight +
numTopSpan * topSpanWeight +
numMostWordFreq * mostWordFreqWeight;
return score;
}
int Ranker::staticScore( )
{
int isNiceDocLength, isShortUrl = 0;
// isShortTitle = endDoc->GetTitleLength( ) <= MaxShortTitle ? 1 : 0;
isNiceDocLength = ( docLength >= MinNiceDocLength && docLength <= MaxNiceDocLength ) ? 1 : 0;
isShortUrl = ( urlLength <= MaxShortUrl ) ? 1 : 0;
return isNiceDocLength * docLengthWeight +
isShortUrl * shortUrlWeight;
}
int Ranker::urlScore(vector<string> &tokens, string &url) {
int weight = 0;
for (const auto& s : tokens)
if (url.contains(s.c_str()))
weight += 50;
return weight;
}
int Ranker::rankingScore( )
{
rarestWord( );
forward( );
return dynamicScore( );
}
unsigned int Ranker::getNumShortSpan() {
return numShortSpan;
}
unsigned int Ranker::getNumInOrderSpan() {
return numInOrderSpan;
}
unsigned int Ranker::getNumExactPhrase() {
return numExactPhrase;
}
unsigned int Ranker::getNumTopSpan() {
return numTopSpan;
}
float Ranker::getPercentWordFreq() {
return percentWordFreq;
}
size_t Ranker::getDocLength() {
return docLength;
}