Repository navigation
Expand file tree
/
Copy pathsparse_matrix.cpp
More file actions
175 lines (145 loc) · 5.49 KB
/
Copy pathsparse_matrix.cpp
File metadata and controls
175 lines (145 loc) · 5.49 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
#include "sparse_matrix.hpp"
Wordnode::Wordnode(std::string new_word, int pos, int new_bow, float new_tfidf){
word = new_word;
all_words_pos = pos;
bow = new_bow;
tfidf = new_tfidf;
best_words_pos = -1;
next = NULL;
}
Wordlist::Wordlist(){
head = NULL;
size = 0;
}
Wordlist::~Wordlist(){
while(head != NULL){
Wordnode* temp = head;
head = head->next;
delete temp;
}
}
void Wordlist::decrease_size(){
size--;
}
unsigned int Wordlist::get_size(){
return size;
}
void Wordlist::push(std::string word, int pos, int bow, float tfidf){
Wordnode *n = new Wordnode(word, pos, bow, tfidf);
n->next = head;
head = n;
size++;
}
void Wordlist::push(std::string word, int pos, int best_pos, int bow, float tfidf){
Wordnode *n = new Wordnode(word, pos, bow, tfidf);
n->best_words_pos = best_pos;
n->next = head;
head = n;
size++;
}
Wordnode* Wordlist::pop() {
Wordnode* temp;
temp = head;
head = head->next;
size--;
return temp;
}
SM::SM(unsigned int new_size){
size = new_size;
file_vector = new Wordlist*[size];
for(unsigned int i = 0 ; i < size ; i++){
file_vector[i] = new Wordlist();
}
}
SM::~SM(){
for(unsigned int i = 0 ; i < size ; i++){
delete file_vector[i];
}
delete[] file_vector;
}
SM::SM(Clique* list_of_entries, std::string* all_words_vector, float* tfidf_sum_vector, float* idf_vector, Dict* all_words){
size = list_of_entries->get_size();
file_vector = new Wordlist*[size];
Cliquenode* temp = list_of_entries->head; //get head of entries list for parsing
for(unsigned int j = 0 ; j < size ; j++){ //for all entries
file_vector[j] = new Wordlist(); //init wordlist of entry
Entry* e;
std::string word;
int word_loc; //location of word in all words vector
int word_counter = 0; //counter for words in entry specs
e = temp->data; //get the entry
e->loc = j; //save row number as loc for entry
int counter_array[all_words->get_size()] = { 0 }; //init counter array for this entry
std::istringstream iss(e->specs_words);
while(iss){ // for each word in the specs
word_counter++; //update word counter
iss >> word;
word_loc = all_words->find_loc(all_words->root, word); //find it's location in the all words vector
counter_array[word_loc] = counter_array[word_loc] + 1; //update parallel location in counter array
}
for(unsigned int i = 0 ; i < all_words->get_size() ; i++) { //go through counter array
if(counter_array[i] != 0){ //if you find a word that exists in specs
int bow = counter_array[i]; //calculate it's bow
float tfidf = (float)bow/(float)word_counter; //calculate it's tfidf
file_vector[j]->push(all_words_vector[i], i, bow, tfidf); //add it to the wordlist
tfidf_sum_vector[i] = tfidf_sum_vector[i] + tfidf; //increase tfidf sum vector
}
}
temp = temp->next;
}
}
void SM::remove_not_best(Dict* best_words){
for(unsigned int i = 0 ; i < size ; i++){ //for all entries
Wordnode* curr_word_node = file_vector[i]->head;
Wordnode* prev_word_node;
while(curr_word_node != NULL){
Dictnode * temp_dict_node = best_words->find_node(best_words->root ,curr_word_node->word);
if(temp_dict_node != NULL){
curr_word_node->best_words_pos = temp_dict_node->best_words_loc;
prev_word_node = curr_word_node;
curr_word_node = curr_word_node->next;
}
else{
Wordnode* temp_word_node;
if(curr_word_node != file_vector[i]->head){
temp_word_node = curr_word_node;
prev_word_node->next = curr_word_node->next;
curr_word_node = curr_word_node->next;
}
else{
temp_word_node = curr_word_node;
file_vector[i]->head = file_vector[i]->head->next;
curr_word_node = file_vector[i]->head;
}
delete temp_word_node;
file_vector[i]->decrease_size();
}
}
}
}
void SM::get_bow_vector(unsigned int entry_loc, int* bow_vector){
if(entry_loc > size){
std::cout << "Invalid entry!" << std::endl;
exit(1);
}
else{
Wordnode* curr_word_node = file_vector[entry_loc]->head;
while(curr_word_node != NULL){
bow_vector[curr_word_node->best_words_pos] = curr_word_node->bow;
curr_word_node = curr_word_node->next;
}
}
}
void SM::get_tfidf_vector(unsigned int entry_loc, float* tfidf_vector){
if(entry_loc > size){
std::cout << "Invalid entry!" << std::endl;
exit(1);
}
else{
Wordnode* curr_word_node = file_vector[entry_loc]->head;
while(curr_word_node != NULL){
tfidf_vector[curr_word_node->best_words_pos] = curr_word_node->tfidf;
curr_word_node = curr_word_node->next;
}
}
}