-
Notifications
You must be signed in to change notification settings - Fork 3
/
document.cpp
151 lines (134 loc) · 3.81 KB
/
document.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
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
#include "document.h"
/************************************
* CONSTRUCTOR *
************************************/
Document::Document()
{
occurrence_ = 1;
}
Document::Document(const string &docname)
{
name_ = docname;
occurrence_ = 1;
}
/************************************
* GET THE DOCUMENT NAME *
************************************/
string Document::name()
{
return name_;
}
/************************************
* SET THE DOCUMENT NAME *
************************************/
void Document::name(const string &docname)
{
name_ = docname;
}
/***************************************************
* GET THE KEYWORD FREQUENCY IN THE DOCUMENT *
***************************************************/
int Document::occurrence()
{
return occurrence_;
}
/***************************************************
* SET THE KEYWORD FREQUENCY IN THE DOCUMENT *
***************************************************/
void Document::occurrence(int occurrence)
{
occurrence_ = occurrence;
}
void Document::increaseOccurrence(int n)
{
occurrence_ += n;
}
/*****************************************************
* COMPARE TWO DOCUMENTS' NAME: use for sorting *
*****************************************************/
bool Document::docNameComp(Document doc1, Document doc2)
{
return (doc1.name_.compare(doc2.name_) <= 0);
}
bool Document::docFreqComp(Document doc1, Document doc2)
{
return (doc1.occurrence() > doc2.occurrence());
}
/*****************************************************************************
* MERGE TWO LIST OF DOCUMENTS, KEEP ONLY COMMON ONES *
*****************************************************************************/
vector<Document> Document::conjunct(vector<Document> docs1, vector<Document> docs2)
{
unsigned int o1, o2;
vector<Document>::iterator it1end = docs1.end(), it2end = docs2.end();
vector<Document>::iterator it1 = docs1.begin(), it2 = docs2.begin();
Document temp;
vector<Document> result;
while (it1 < it1end && it2 < it2end)
{
if (it1->name_ == it2->name_)
{
temp.name(it1->name_);
o1 = it1->occurrence_;
o2 = it2->occurrence_;
temp.occurrence( (o1 < o2) ? o1 : o2 );
result.push_back( temp );
it1++;
it2++;
}
else if (it1->name_ < it2->name_)
{
it1++;
}
else
{
it2++;
}
}
return result;
}
/*****************************************************************************
* MERGE TWO LIST OF DOCUMENTS, KEEP ALL DIFFERENCES AND COMMON ONES *
*****************************************************************************/
vector<Document> Document::disjunct(vector<Document> docs1, vector<Document> docs2)
{
unsigned int o1, o2;
vector<Document>::iterator it1end = docs1.end(), it2end = docs2.end();
vector<Document>::iterator it1 = docs1.begin(), it2 = docs2.begin();
Document temp;
vector<Document> result;
while (it1 != it1end && it2 != it2end)
{
if (it1->name_ == it2->name_)
{
temp.name( it1->name_ );
o1 = it1->occurrence();
o2 = it2->occurrence();
temp.occurrence( (o1 > o2) ? o1 : o2 );
result.push_back( temp );
it1++;
it2++;
}
else if (it1->name_ < it2->name_)
{
result.push_back( *it1 );
it1++;
}
else
{
result.push_back( *it2 );
it2++;
}
}
while (it1 != it1end)
{
result.push_back( *it1 );
it1++;
}
while (it2 != it2end)
{
result.push_back( *it2 );
it2++;
}
return result;
}