-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiword.cpp
151 lines (146 loc) · 3.91 KB
/
iword.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 "iword.h"
/*iword::iword( int p, wordIndex f, int _pofs ) {
pos.push_back(p);
form.push_back(f);
pofs.push_back(_pofs);
}*/
void iword::setPos( int p, int lev){
pos[lev] = p;
}
void iword::addPos( int p){
pos.push_back(p);
}
int iword::getPos(int level) const {
return pos[level];
}
void iword::setForm( wordIndex f, int lev){
form[lev] = f;
}
void iword::addForm( wordIndex f){
form.push_back(f);
}
wordIndex iword::getForm(int level) const {
return form[level];
}
void iword::setStform( string _stform, int lev){
stform[lev] = _stform;
}
void iword::addStform( string _stform){
stform.push_back(_stform);
}
string iword::getStform(int level) const {
return stform[level];
}
void iword::setPofs( int _pofs){
pofs = _pofs;
}
int iword::getPofs() const{
return pofs;
}
void iword::setNullCst(cost _cst) {
nullCst=_cst;
}
cost iword::getNullCst() const {
return nullCst;
}
void iword::setFertCostsVec(const vector<cost> & vec){
vfert=vec;
}
cost iword::getFertCst(int fert) const {
if (fert > vfert.size()-1) return vfert.at(vfert.size()-1);
else return vfert.at(fert);
}
int iword::getFertSize() const{
return vfert.size();
}
string iword::print() const {
ostringstream oss("");
oss<<"pos:";
for (vector<int>::const_iterator it=pos.begin(); it!=pos.end();++it){oss<<*it<<" ";}
oss<<"form:";
for (vector<wordIndex>::const_iterator it=form.begin(); it!=form.end();++it){oss<<*it<<" ";}
oss<<"stform:";
for (vector<string>::const_iterator it=stform.begin(); it!=stform.end();++it){oss<<*it<<" ";}
oss<<"pOfs:"<<pofs;
oss<<" nullCst:"<<nullCst;
oss<<" fert:";
for (vector<cost>::const_iterator it=vfert.begin(); it!=vfert.end();++it){oss<<*it<<" ";}
return oss.str();
}
/* vocTable class
********************/
// void vocTable::load (string fname){
// ifstream ffile(fname.c_str());
// // open input and target (word forms) files:
// if (! ffile){
// cerr << "ERROR while opening file:" << fname << endl;
// exit(EXIT_FAILURE);
// }
// string line;
// if (ffile.eof()){
// cerr << "file "<< fname << " is empty" << endl;
// exit(EXIT_FAILURE);
// }
// while (getline(ffile,line)){
// //line format: wordIndex wordform num_occurencies
// //cout << line << endl;
// istringstream iss(line);
// wordIndex ind;
// string form;
// iss >> ind >> form;
// jash_voc[ind]= form;
// }
// }
void iwordCnColumn::merge(string mode){
if (mode == "best"){
iwordCnColumn::const_iterator bestit;
float best=0;
for (iwordCnColumn::const_iterator cit=this->begin(); cit!=this->end();++cit){
if ( cit->second > best ){
bestit=cit;
best=cit->second;
}
}
nullCst=bestit->first.getNullCst();
for (unsigned int i=0; i<bestit->first.getFertSize();++i){
if (vfert.size()>i){
vfert[i]=bestit->first.getFertCst(i);
}else{
vfert.push_back(bestit->first.getFertCst(i));
}
}
}else{
// Interpolation
iwordCnColumn::const_iterator begit=this->begin();
nullCst=begit->first.getNullCst() * begit->second;
for (int i=0; i<begit->first.getFertSize();++i){
if (vfert.size()>i){
vfert[i] = begit->first.getFertCst(i) * begit->second;
}else{
vfert.push_back(begit->first.getFertCst(i) * begit->second);
}
}
++begit;
for (iwordCnColumn::const_iterator cit=begit; cit!=this->end();++cit){
nullCst += cit->first.getNullCst() * cit->second;
for (int i=0; i<cit->first.getFertSize();++i){
vfert[i] += cit->first.getFertCst(i) * cit->second;
}
}
}
// cout<<"COLUMN FERT:"<<endl;
// for (vector<cost>::const_iterator v=vfert.begin();v!=vfert.end();++v){
// cout<<*v<<" ";
// }
// cout<<endl;
}
cost iwordCnColumn::getNullCst() const {
return nullCst;
}
cost iwordCnColumn::getFertCst(int fert) const {
if (fert > vfert.size()-1) return vfert.at(vfert.size()-1);
else return vfert.at(fert);
}
int iwordCnColumn::getFertSize() const{
return vfert.size();
}