-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument.cpp
More file actions
151 lines (126 loc) · 3.41 KB
/
Copy pathdocument.cpp
File metadata and controls
151 lines (126 loc) · 3.41 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
#include "include/document.h"
#include "include/text_processor.h"
#include "include/porter2_stemmer.h"
#include "include/util.h"
#include <stack>
#include <algorithm>
#include <cstdio>
#include <list>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <map>
#include <cmath>
#include <set>
#include <chrono> // this is for check execution time
#include <ctime> // this is for check execution time
TextProcessor Document::textProcessor;
string Document::outputDirectory = "";
int Document::documentNumber = 0;
map<string, int> Document::wordIds = {{"asd", 0}};
vector<Term*> Document::wordList;
int Document::wordId = 0;
Term* getWord(string word);
Term::Term() {
df = 0;
cf = 0;
indexStart = 0;
}
Term::Term(string* tokens) {
id = stoi(tokens[0]);
str = tokens[1];
df = stoi(tokens[2]);
cf = stoi(tokens[3]);
indexStart = stoull(tokens[4]);
}
Index::Index(string indexFileLine) {
termId = stoi(indexFileLine.substr(0, 6));
docId = stoi(indexFileLine.substr(6, 6));
tf = stoi(indexFileLine.substr(12, 5));
weight = stof(indexFileLine.substr(17, 7));
}
Document::Document(string _docno, string headline, string text) {
id = ++documentNumber;
docno = _docno;
textProcessor.removePunctuation(headline);
textProcessor.removePunctuation(text);
headlineWords = textProcessor.tokenize(headline);
textWords = textProcessor.tokenize(text);
}
Document::Document(int _id, string _docno, float _denominator) {
id = _id;
docno = _docno;
denominator = _denominator;
}
Document::Document(string* tokens) {
id = stoi(tokens[0]);
docno = tokens[1];
util::trim(docno);
size = stoi(tokens[2]);
weightSum = stof(tokens[3]);
}
int Document::getDocumentNumber() {
return documentNumber;
}
void Document::increaseDocumentFrequency() {
set<Term*>::iterator iter = words.begin();
while( iter != words.end()) {
(*iter)->df++;
iter++;
}
}
void Document::transform() {
textProcessor.removeNumberWords(headlineWords);
textProcessor.removeNumberWords(textWords);
stem(headlineStems, headlineWords);
stem(textStems, textWords);
//headlineWords.unique();
//textStems.unique();
}
void Document::stem(list<string> &stemList, list<string> words) {
list<string>::iterator iter = words.begin();
while( iter != words.end()) {
string word = *iter;
util::trim(word);
Porter2Stemmer::stem(word);
if(!word.empty()) {
stemList.push_back(word);
Term *temp = getWord(word);
temp->cf++;;
temp->tf[id]++;
this->words.insert(temp);
}
iter++;
}
}
Term* getWord(string word) {
if(Document::wordIds[word] == 0) {
Term *temp = new Term;
temp->id = ++Document::wordId;
Document::wordIds[word] = Document::wordId;
temp->str = word;
Document::wordList.push_back(temp);
return temp;
} else {
return Document::wordList[Document::wordIds[word] - 1];
}
}
void Document::writeDocInfoFile() {
ofstream outputFile (outputDirectory + "/doc.dat", ios_base::app);
outputFile.close();
}
string Document::preFileString() {
return to_string(id) + "\t" + docno + "\t" + to_string(words.size()) + "\t" + to_string(denominator);
}
string Document::toString() {
return to_string(id) + "\t" + docno + "\t" + to_string(words.size());
}
void Document::calculateDenominator(float dValue) { // tf idf formula's denominator
set<Term*>::iterator iter = words.begin();
while( iter != words.end()) {
denominator += pow((log((*iter)->tf[id]) + 1.0f) * dValue, 2.0f);
iter++;
}
denominator = sqrt(denominator);
}