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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
/******************************************************************************
IrstLM: IRST Language Model Toolkit
Copyright (C) 2006 Marcello Federico, ITC-irst Trento, Italy
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include <iomanip>
#include <assert.h>
#include "mempool.h"
#include "htable.h"
#include "dictionary.h"
#include "ngram.h"
#include "index.h"
using namespace std;
ngram::ngram(dictionary* d,int sz){
dict=d;
size=sz;
succ=0;
freq=0;
info=0;
pinfo=0;
link=NULL;
isym=-1;
memset(word,0,sizeof(int)*MAX_NGRAM);
memset(midx,0,sizeof(int)*MAX_NGRAM);
}
ngram::ngram(ngram& ng){
size=ng.size;
freq=ng.freq;
succ=0;
info=0;
pinfo=0;
link=NULL;
isym=-1;
dict=ng.dict;
memcpy(word,ng.word,sizeof(int)*MAX_NGRAM);
memcpy(midx,ng.word,sizeof(int)*MAX_NGRAM);
}
void ngram::trans (const ngram& ng){
size=ng.size;
freq=ng.freq;
if (dict == ng.dict){
info=ng.info;
isym=ng.isym;
memcpy(word,ng.word,sizeof(int)*MAX_NGRAM);
memcpy(midx,ng.midx,sizeof(int)*MAX_NGRAM);
}
else{
info=0;
memset(midx,0,sizeof(int)*MAX_NGRAM);
isym=-1;
for (int i=1;i<=size;i++)
word[MAX_NGRAM-i]=dict->encode(ng.dict->decode(*ng.wordp(i)));
}
}
ifstream& operator>> ( ifstream& fi , ngram& ng){
char w[MAX_WORD];
memset(w,0,MAX_WORD);
w[0]='\0';
if (!(fi >> setw(MAX_WORD) >> w))
return fi;
if (strlen(w)==(MAX_WORD-1))
cerr << "ngram: a too long word was read ("
<< w << ")\n";
if (ng.dict->intsymb() &&
(strlen(w)==1) && (index(ng.dict->intsymb(),w[0])!=NULL)){
ng.isym=(long)index(ng.dict->intsymb(),w[0]) -
(long)ng.dict->intsymb();
ng.size=0;
return fi;
}
int c=ng.dict->encode(w);
if (c == -1 ){
cerr << "ngram: " << w << " is OOV \n";
exit(1);
}
memcpy(ng.word,ng.word+1,(MAX_NGRAM-1)*sizeof(int));
ng.word[MAX_NGRAM-1]=(int)c;
ng.freq=1;
if (ng.size<MAX_NGRAM) ng.size++;
return fi;
}
int ngram::pushw(char* w){
assert(dict!=NULL);
int c=dict->encode(w);
if (c == -1 ){
cerr << "ngram: " << w << " is OOV \n";
exit(1);
}
pushc(c);
return 1;
}
int ngram::pushc(int c){
int buff[MAX_NGRAM-1];
memcpy(buff,word+1,(MAX_NGRAM-1)*sizeof(int));
memcpy(word,buff,(MAX_NGRAM-1)*sizeof(int));
word[MAX_NGRAM-1]=(int)c;
if (size<MAX_NGRAM) size++;
return 1;
}
istream& operator>> ( istream& fi , ngram& ng){
char w[MAX_WORD];
memset(w,0,MAX_WORD);
w[0]='\0';
assert(ng.dict != NULL);
if (!(fi >> setw(MAX_WORD) >> w))
return fi;
if (strlen(w)==(MAX_WORD-1))
cerr << "ngram: a too long word was read ("
<< w << ")\n";
if (ng.dict->intsymb() &&
(strlen(w)==1) && (index(ng.dict->intsymb(),w[0])!=NULL)){
ng.isym=(long)index(ng.dict->intsymb(),w[0])-(long)ng.dict->intsymb();
ng.size=0;
return fi;
}
ng.pushw(w);
ng.freq=1;
return fi;
}
ofstream& operator<< (ofstream& fo,ngram& ng){
assert(ng.dict != NULL);
for (int i=ng.size;i>0;i--)
fo << ng.dict->decode(ng.word[MAX_NGRAM-i]) << " ";
//fo << "[size " << ng.size << " freq " << ng.freq << "]";
fo << ng.freq;
return fo;
}
ostream& operator<< (ostream& fo,ngram& ng){
assert(ng.dict != NULL);
for (int i=ng.size;i>0;i--)
fo << ng.dict->decode(ng.word[MAX_NGRAM-i]) << " ";
//fo << "[size " << ng.size << " freq " << ng.freq << "]";
fo << ng.freq;
return fo;
}
/*
main(int argc, char** argv){
dictionary d(argv[1]);
ifstream txt(argv[1]);
ngram ng(&d);
while (txt >> ng){
cout << ng << "\n";
}
ngram ng2=ng;
cerr << "copia l'ultimo =" << ng << "\n";
}
*/
|