-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpmStore.cpp
182 lines (162 loc) · 3.74 KB
/
cpmStore.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
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
/*
* cpmStore.cpp
*
* Created on: Apr 27, 2015
* Author: prashant mathur
*/
#pragma once
#define MAXW 50
#define MAXSIZE 1000
#include "cpmStore.h"
#include<map>
cpmStore *cpmStore::instance=NULL;
cpmStore::cpmStore(char *filename, int window, unsigned long DUP, bool prune) {
// TODO Auto-generated constructor stub
instance = this;
m_context_size = window;
m_dup=DUP;
m_prune=prune;
LoadTrainingCorpus(filename);
}
cpmStore::~cpmStore() {
}
bool cpmStore::LoadTrainingCorpus(char* filename){
// std::cerr<<filename<<std::endl;
boost::unordered_map<std::string, unsigned long> freqMap;
// do one pass ... encode all words .. prune to top N words ..
std::ifstream file(filename);
std::string line;
std::vector<std::string> text;
if(file.is_open())
{
std::string temp("</s>");
freqMap[temp]=1;
while(file >> line){
text.push_back(line);
freqMap[line]++;
}
}
file.close();
// Encoding
for(boost::unordered_map<std::string, unsigned long>::iterator itr=freqMap.begin();
itr!=freqMap.end(); itr++){
if(m_prune){
if(itr->second >= 5){
Code* c=EncodeWord(itr->first);
c->SetCount(itr->second);
}
} else {
Code* c=EncodeWord(itr->first);
c->SetCount(itr->second);
}
}
// storing context
std::ifstream file1(filename);
if(file1.is_open())
{
for(long k=0; k<long(text.size()); k++){
// for(size_t i=0; i<vecStr.size(); i++){
Code *c = getCode(text[k]);
if(c!=NULL){
Context *con = (Context*)calloc(1, sizeof(Context));
con = new Context();
for(int j = k - (floor(m_context_size/2)) ; j <= k + (floor(m_context_size/2)); j++ ){
if(j == k) continue;
std::string con_word="<s>";
if(j >= 0 && j < text.size()) con_word = text[j];
Code *con_word_code = getCode(con_word);
if(con_word_code != NULL){
con->add(con_word_code);
}
}
c->addContext(con);
}
// }
}
}
file1.close();
std::cerr<<"Vocabulary Size : "<<m_vocab.size()<<std::endl;
// finished storing context
return true;
}
Code *cpmStore::getCode(std::string& s){
Code *c = NULL;
if(m_vocab.find(s)!=m_vocab.end()){
c = m_vocab[s];
}
return c;
}
Code* cpmStore::EncodeWord(const std::string& s){
Code *c = NULL;
if(m_vocab.find(s)!=m_vocab.end()){
return m_vocab[s];
}
c = (Code*)calloc(1, sizeof(Code));
c = new Code(m_words, s);
m_vocab[s] = c;
m_revmap[m_words] = c;
m_words++;
return c;
}
Code::Code(unsigned long c, std::string s){
c_code = c;
c_counts=0;
c_string=s;
m_map.clear();
}
Code::~Code(){
c_code=0;
c_counts=0;
c_string="";
m_map.clear();
}
void cpmStore::chop(std::string &str) {
int i = 0;
while (isspace(str[i]) != 0) {
str.replace(i, 1, "");
}
while (isspace(str[str.length() - 1]) != 0) {
str.replace(str.length() - 1, 1, "");
}
return;
}
unsigned short int cpmStore::split_marker_perl(std::string& str, std::string& marker,
std::vector<std::string> &array) {
int found = str.find(marker), prev = 0;
while (found != std::string::npos) // warning!
{
array.push_back(str.substr(prev, found - prev));
prev = found + marker.length();
found = str.find(marker, found + marker.length());
}
array.push_back(str.substr(prev));
return array.size() - 1;
}
Context::Context(){
bow.clear();
}
Context::~Context(){
bow.clear();
}
bool Context::add(Code* indx){
for(std::vector<Code*>::iterator itr=bow.begin(); itr!=bow.end(); itr++){
if(**itr == *indx) return true;
}
bow.push_back(indx);
return true;
}
bool Context::remove(Code* indx){
for(std::vector<Code*>::iterator itr=bow.begin(); itr!=bow.end(); itr++){
if(**itr == *indx){
bow.erase(itr);
return true;
}
}
return false;
}
bool Context::operator==(Context& b){
for(unsigned long i = 0; i < bow.size(); i++){
if(bow[i] != b.bow[i]) return false;
}
return true;
}