forked from servalproject/smac
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlowercasealpha.c
More file actions
217 lines (195 loc) · 6.06 KB
/
lowercasealpha.c
File metadata and controls
217 lines (195 loc) · 6.06 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
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
215
216
217
/*
Copyright (C) 2012 Paul Gardner-Stephen
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <strings.h>
#include <math.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include "arithmetic.h"
#include "message_stats.h"
#include "charset.h"
#undef DEBUG
double entropy3(int c1,int c2, char *string);
int decodeLCAlphaSpace(range_coder *c,unsigned char *s,int length)
{
int c1=charIdx(' ');
int c2=charIdx(' ');
int c3;
int o,i;
for(o=0;o<length;o++) {
#ifdef DEBUG
printf("so far: '%s', c1=%d(%c), c2=%d(%c)\n",s,c1,chars[c1],c2,chars[c2]);
#endif
int substituted=0;
if (!charInWord(chars[c2])) {
/* We are at a word break, so see if we can do word substitution.
Either way, we must flag whether we performed word substitution */
substituted=1-range_decode_symbol(c,wordSubstitutionFlag,2);
#ifdef DEBUG
printf("substitution flag = %d @ offset %d\n",substituted,o);
#endif
}
if (substituted)
{
int w=range_decode_symbol(c,wordFrequencies,wordCount);
int wordLength=strlen(wordList[w]);
/* place word into output stream */
bcopy(wordList[w],&s[o],wordLength);
/* skip rest of word, but make sure we stay on track for 3rd order model
state. */
o+=wordLength-1;
if (s[o]) {
c1=charIdx(s[o-1]);
if (c1<0) { exit(-1); }
c2=charIdx(s[o]);
if (c2<0) { exit(-1); }
}
continue;
} else {
c3=range_decode_symbol(c,char_freqs3[c1][c2],69);
s[o]=chars[c3];
#ifdef DEBUG
printf(" decode alpha char %d = %c\n",c3,s[o]);
#endif
}
c1=c2; c2=c3;
}
return 0;
}
int encodeLCAlphaSpace(range_coder *c,unsigned char *s)
{
int c1=charIdx(' ');
int c2=charIdx(' ');
int o;
for(o=0;s[o];o++) {
int c3=charIdx(s[o]);
#ifdef DEBUG
printf(" encoding @ %d, c1=%d(%c) c2=%d(%c), c3=%d(%c)\n",
o,c1,chars[c1],c2,chars[c2],c3,chars[c3]);
#endif
if (!charInWord(chars[c2])) {
/* We are at a word break, so see if we can do word substitution.
Either way, we must flag whether we performed word substitution */
int w;
int longestWord=-1;
int longestLength=0;
if (charInWord(chars[c3])) {
/* Find the first word that matches */
w=0;
int bit;
for(bit=30;bit>=0;bit--) {
if ((w|(1<<bit))>=wordCount) continue;
int t=w|(1<<bit);
/* stop if we have found first match */
int d2=strncmp(wordList[t],(char *)&s[o],strlen(wordList[t]));
/* if wordList[w-1] is lexographically earlier than the text,
and wordList[w] is not lexographically earlier than the next, then
we have found the point we are looking for, and can stop. */
if (d2>=0) {
int d1=t ? strncmp(wordList[t-1],(char *)&s[o],strlen(wordList[t-1])) : -1;
if (d1<0)
{
// printf("word '%s' comes before '%s'\n",wordList[t-1],&s[o]);
// printf("but '%s' equals or comes after '%s'\n",wordList[t],&s[o]);
w=t;
break;
}
} else if (d2<0) {
/* if both are before where we are looking for, then set this bit in w. */
// printf("word '%s' comes before '%s'\n",wordList[t],&s[o]);
w=t;
} else
/* we have gone too far in the list, so don't set this bit.
Continue iterating through lower-order bits. */
continue;
}
// printf("starting to look from '%s' for '%s'\n",wordList[w],&s[o]);
for(;w<wordCount;w++) {
int d;
d=strncmp(wordList[w],(char *)&s[o],strlen(wordList[w]));
if (d<0) {
/* skip words prior to the one we are looking for */
continue;
} else if (d==0) {
#ifdef DEBUG
printf(" word match: strlen=%d, longestLength=%d\n",
(int)strlen(wordList[w]),(int)longestLength
);
#endif
double entropy=entropy3(c1,c2,wordList[w]);
range_coder *t=range_new_coder(1024);
range_encode_symbol(t,wordSubstitutionFlag,2,0);
range_encode_symbol(t,wordFrequencies,wordCount,w);
double substEntropy=t->entropy;
range_coder_free(t);
double savings=entropy-substEntropy;
if (strlen(wordList[w])>longestLength) {
longestLength=strlen(wordList[w]);
longestWord=w;
#ifdef DEBUG
printf("spotted substitutable instance of '%s' -- save %f bits (%f vs %f)\n",
wordList[w],savings,substEntropy,entropy);
#endif
}
} else
/* ran out of matching words, so stop search */
break;
}
}
if (longestWord>-1) {
/* Encode "we are substituting a word here */
double entropy=c->entropy;
range_encode_symbol(c,wordSubstitutionFlag,2,0);
/* Encode the word */
range_encode_symbol(c,wordFrequencies,wordCount,longestWord);
#ifdef DEBUG
printf("substituted %s at a cost of %f bits.\n",
wordList[longestWord],c->entropy-entropy);
#endif
/* skip rest of word, but make sure we stay on track for 3rd order model
state. */
o+=longestLength-1;
if (s[o]) {
c1=charIdx(s[o-1]);
if (c1<0) { exit(-1); }
c2=charIdx(s[o]);
if (c2<0) { exit(-1); }
}
#ifdef DEBUG
printf(" post substitution @ %d, c1=%d(%c), c2=%d(%c)\n",
o,c1,chars[c1],c2,chars[c2]);
#endif
continue;
} else {
/* Encode "not substituting a word here" symbol */
double entropy=c->entropy;
range_encode_symbol(c,wordSubstitutionFlag,2,1);
#ifdef DEBUG
printf("incurring non-substitution penalty = %f bits\n",
c->entropy-entropy);
#endif
}
} else {
#ifdef DEBUG
printf(" not a wordbreak @ %d, c1=%d(%c), c2=%d(%c)\n",
o,c2,chars[c2],c3,chars[c3]);
#endif
}
range_encode_symbol(c,char_freqs3[c1][c2],69,c3);
c1=c2; c2=c3;
}
return 0;
}