-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdigramScore.c
More file actions
209 lines (177 loc) · 5.59 KB
/
Copy pathdigramScore.c
File metadata and controls
209 lines (177 loc) · 5.59 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
/*
* digramScore.c --
*
* This file implements the digram scoring methods.
*
* Copyright (c) 2004 Michael Thomas <wart@kobold.org>
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <tcl.h>
#include <score.h>
#include <math.h>
#include <string.h>
static int CreateDigram _ANSI_ARGS_((Tcl_Interp *, ScoreItem *, int, const char **));
static int AddDigram _ANSI_ARGS_((Tcl_Interp *, ScoreItem *, const char *, double));
static void DeleteDigram _ANSI_ARGS_((ClientData));
static int NormalizeDigramLog _ANSI_ARGS_((Tcl_Interp *, ScoreItem *));
static double DigramValue _ANSI_ARGS_((Tcl_Interp *, ScoreItem *, const char *));
static double DigramElementValue _ANSI_ARGS_((Tcl_Interp *, ScoreItem *, const char *));
static int DumpDigram _ANSI_ARGS_((Tcl_Interp *, ScoreItem *, const char *));
typedef struct DigramItem {
ScoreItem header;
double **value;
} DigramItem;
ScoreType DigramLogType = {
"digramlog",
sizeof(DigramItem),
CreateDigram,
AddDigram,
DigramValue,
DigramElementValue,
NormalizeDigramLog,
DeleteDigram,
DumpDigram,
ScoreMethodCmd,
(ScoreType *)NULL
};
ScoreType DigramCountType = {
"digramcount",
sizeof(DigramItem),
CreateDigram,
AddDigram,
DigramValue,
DigramElementValue,
NullScoreNormalizer,
DeleteDigram,
DumpDigram,
ScoreMethodCmd,
(ScoreType *)NULL
};
static int
CreateDigram(Tcl_Interp *interp, ScoreItem *itemPtr, int argc, const char **argv) {
DigramItem *dlPtr = (DigramItem *)itemPtr;
char temp_ptr[TCL_DOUBLE_SPACE];
Tcl_DString dsPtr;
int i, j;
dlPtr->header.elemSize = 2;
dlPtr->value = (double **)ckalloc(sizeof(double *) * 256);
for (i=0; i < 256; i++) {
dlPtr->value[i] = (double *)ckalloc(sizeof(double) * 256);
for (j=0; j < 256; j++) {
dlPtr->value[i][j] = 0;
}
}
sprintf(temp_ptr, "score%d", scoreid);
Tcl_DStringInit(&dsPtr);
Tcl_DStringAppendElement(&dsPtr, temp_ptr);
Tcl_DStringAppendElement(&dsPtr, "configure");
for (i=0; i < argc; i++) {
Tcl_DStringAppendElement(&dsPtr, argv[i]);
}
Tcl_CreateCommand(interp, temp_ptr, ScoreMethodCmd, itemPtr,
itemPtr->typePtr->deleteProc);
if (argc) {
if (Tcl_Eval(interp, Tcl_DStringValue(&dsPtr)) != TCL_OK) {
Tcl_DeleteCommand(interp, temp_ptr);
Tcl_DStringFree(&dsPtr);
return TCL_ERROR;
}
}
Tcl_SetResult(interp, temp_ptr, TCL_VOLATILE);
Tcl_DStringFree(&dsPtr);
return TCL_OK;
}
static void
DeleteDigram(ClientData clientData) {
DigramItem *dlPtr = (DigramItem *)clientData;
int i;
if (dlPtr->value != NULL) {
for(i=0; i < 256; i++) {
if (dlPtr->value[i] != NULL) {
ckfree((char *)dlPtr->value[i]);
}
}
ckfree((char *)dlPtr->value);
}
dlPtr->value = (double **)NULL;
DeleteScore(clientData);
}
static int
AddDigram(Tcl_Interp *interp, ScoreItem *itemPtr, const char *element, double value) {
DigramItem *dlPtr = (DigramItem *)itemPtr;
dlPtr->value[(unsigned char)element[0]][(unsigned char)element[1]] += value;
Tcl_SetObjResult(interp, Tcl_NewStringObj(element, -1));
return TCL_OK;
}
static double
DigramValue(Tcl_Interp *interp, ScoreItem *itemPtr, const char *string) {
DigramItem *dlPtr = (DigramItem *)itemPtr;
return DigramStringValue(string, (double **)dlPtr->value);
}
static double
DigramElementValue(Tcl_Interp *interp, ScoreItem *itemPtr, const char *string) {
DigramItem *dlPtr = (DigramItem *)itemPtr;
return DigramSingleValue((unsigned char)string[0], (unsigned char)string[1], dlPtr->value);
}
static int
NormalizeDigramLog(Tcl_Interp *interp, ScoreItem *itemPtr) {
DigramItem *dlPtr = (DigramItem *)itemPtr;
int i, j;
for(i=0; i < 256; i++) {
for (j=0; j < 256; j++) {
if (dlPtr->value[i][j] > 0.0) {
dlPtr->value[i][j] = log(dlPtr->value[i][j]);
} else {
dlPtr->value[i][j] = 0.0;
}
}
}
Tcl_ResetResult(interp);
return TCL_OK;
}
static int
DumpDigram(Tcl_Interp *interp, ScoreItem *itemPtr, const char *script) {
DigramItem *dlPtr = (DigramItem *)itemPtr;
Tcl_DString dsPtr;
int i, j;
int length;
char element[3];
Tcl_Obj *valueObj = Tcl_NewDoubleObj(0.0);
element[2] = '\0';
Tcl_DStringInit(&dsPtr);
Tcl_DStringAppend(&dsPtr, script, strlen(script));
length = Tcl_DStringLength(&dsPtr);
for (i=1; i < 256; i++) {
for (j=1; j < 256; j++) {
if (DigramSingleValue(i, j, dlPtr->value) > 0.0) {
element[0] = i;
element[1] = j;
Tcl_DStringSetLength(&dsPtr, length);
Tcl_DStringStartSublist(&dsPtr);
Tcl_DStringAppendElement(&dsPtr, (const char *)element);
Tcl_SetDoubleObj(valueObj, DigramSingleValue(i, j, dlPtr->value));
Tcl_DStringAppendElement(&dsPtr, Tcl_GetString(valueObj));
Tcl_DStringEndSublist(&dsPtr);
if (Tcl_EvalEx(interp, Tcl_DStringValue(&dsPtr), Tcl_DStringLength(&dsPtr), 0) != TCL_OK) {
return TCL_ERROR;
}
}
}
}
Tcl_DecrRefCount(valueObj);
return TCL_OK;
}