-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex_maker.cpp
More file actions
161 lines (138 loc) · 5.55 KB
/
Copy pathindex_maker.cpp
File metadata and controls
161 lines (138 loc) · 5.55 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
#include "include/document.h"
#include "include/util.h"
#include "include/text_processor.h"
#include "include/index_maker.h"
#include <stack>
#include <list>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <map>
#include <cmath>
#include <iomanip>
using namespace util;
using namespace std;
using namespace std::chrono;
void transformFilesInFolder(FilePaths *filePaths, string type, vector<Document> &documentVector) {
for(int year = 1998; year <= 2000; year++) {
for(int month = 1; month <= 12; month++) {
for(int day = 1; day <= 31; day++) {
transformFile(filePaths->articleFile(type, year, month, day), documentVector);
}
}
}
}
void transformFile(string filePath, vector<Document> &documentVector) {
string fileString = getFileIntoString(filePath);
if(fileString != "") {
startTimer();
parseToDocuments(fileString, documentVector);
endTimerAndPrint("Refine complete...");
}
}
void parseToDocuments(string fileString, vector<Document> &documentVector) {
int docTagStartPosition = findDOCTagPosition(fileString, 0);
while(docTagStartPosition != string::npos) {
Document document = parseToDocument(fileString, docTagStartPosition);
document.transform();
document.increaseDocumentFrequency();
documentVector.push_back(document);
docTagStartPosition = findDOCTagPosition(fileString, docTagStartPosition);
}
}
// return npos if doc doens't exist
int findDOCTagPosition(string fileString, int startPosition) {
string startTag = "<DOC>";
int result = fileString.find(startTag, startPosition);
return result != string::npos ? result + startTag.length() : string::npos;
}
Document parseToDocument(string file, int docTagStartPosition) {
string docno = extractContentInTag(file, "DOCNO", docTagStartPosition);
string headline = extractContentInTag(file, "HEADLINE", docTagStartPosition);
string text = extractContentInTag(file, "TEXT", docTagStartPosition);
return Document (docno, headline, text);
}
string extractContentInTag(string fileString, string tag, int docTagStartPosition) {
string startTag = "<" + tag + ">";
string endTag = "</" + tag + ">";
string result;
int startPosition = fileString.find(startTag, docTagStartPosition) + startTag.size();
int length = fileString.find(endTag, docTagStartPosition) - startPosition;
result = fileString.substr(startPosition, length);
return result;
}
void writeDocDataFile(FilePaths *filePaths, vector<Document> &documentVector) {
ofstream documentFile (filePaths->docFile);
vector<Document>::iterator documentIterator = documentVector.begin();
while( documentIterator != documentVector.end()) {
float denominator = 0.0f;
set<Term*>::iterator wordIterator = documentIterator->words.begin();
while(wordIterator != documentIterator->words.end()) {
float dValue = log((float)Document::getDocumentNumber() / (float)(*wordIterator)->df);
denominator += pow((log((*wordIterator)->tf[documentIterator->id]) + 1.0f) * dValue, 2.0f);
wordIterator++;
}
documentIterator->denominator = sqrt(denominator);
documentFile << documentIterator->toString() << endl;
documentIterator++;
}
documentFile.close();
}
void writeIndexFile(FilePaths *filePaths, vector<Document> documentVector) { // and term.dat
cout << "Document size : " << documentVector.size() << endl;
ofstream tfFile (filePaths->tfFile);
ofstream termFile (filePaths->termFile);
ofstream indexFile (filePaths->indexFile);
int lineCount = 0;
int size = Document::wordList.size();
unsigned long long pre_df = 0;
unsigned long long index_line = 26;
for(int i = 0; i < Document::wordList.size(); i++) {
if(i % 500 == 0)
startTimer();
Term temp = *Document::wordList[i];
int documentFrequency = temp.df;
float dValue = log((float)Document::getDocumentNumber() / (float)documentFrequency);
if(dValue == 0)
continue;
map<int, int>::iterator tfIterator = temp.tf.begin();
while(tfIterator != temp.tf.end()) {
int termFrequency = tfIterator->second;
float numerator = (log((float)termFrequency) + 1.0f) * dValue; //get doc.data and denominator
float weight = numerator / documentVector[tfIterator->first - 1].denominator; // is denominator
if(documentVector[tfIterator->first - 1].denominator == 0) {
cout << "denominator is zero" << endl;
}
// get figure of weight
int temp_i = (int)weight;
int count = 0;
do {
temp_i = int(temp_i / 10);
count++;
} while(temp_i > 0);
tfFile << temp.id << '\t' << temp.str << '\t' << temp.df << '\t' << temp.cf << endl;
indexFile << setfill('0') << setw(6) << temp.id << setfill('0') << setw(6) << to_string(documentVector[tfIterator->first - 1].id) << setfill('0') << setw(5) << termFrequency << setfill('0') << setw(count) << fixed << setprecision(7 - count) << weight << endl;
documentVector[tfIterator->first - 1].weightSum += pow(weight, 2);
tfIterator++;
}
termFile << temp.id << '\t' << temp.str << '\t' << temp.df << '\t' << temp.cf << '\t' << pre_df * index_line << endl;;
pre_df += temp.df;
if(i % 500 == 0) {
cout << temp.id << " / " << size << " " << (float)((float)i / (float)size) * 100 << "% proceeding" << endl;
if(endTimerAndGetMinute() > 0) {
cout << "Word / Minute speed : " << (int)(i / endTimerAndGetMinute()) << endl;
}
}
}
indexFile.close();
termFile.close();
tfFile.close();
ofstream documentFile (filePaths->docFile);
vector<Document>::iterator documentIterator = documentVector.begin();
while( documentIterator != documentVector.end()) {
documentFile << documentIterator->toString() << "\t" << sqrt(documentIterator->weightSum) << endl;
documentIterator++;
}
documentFile.close();
}