-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathngramScore.c
More file actions
197 lines (164 loc) · 5.34 KB
/
Copy pathngramScore.c
File metadata and controls
197 lines (164 loc) · 5.34 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
/*
* ngramScore.c --
*
* This file implements the generic n-gram 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>
#include <wordtree.h>
static int CreateNgram _ANSI_ARGS_((Tcl_Interp *, ScoreItem *, int, const char **));
static int AddNgram _ANSI_ARGS_((Tcl_Interp *, ScoreItem *, const char *, double));
void DeleteNgramScore _ANSI_ARGS_((ClientData));
static int NormalizeNgramLog _ANSI_ARGS_((Tcl_Interp *, ScoreItem *));
static double NgramValue _ANSI_ARGS_((Tcl_Interp *, ScoreItem *, const char *));
static double NgramElementValue _ANSI_ARGS_((Tcl_Interp *, ScoreItem *, const char *));
double NgramStringValue _ANSI_ARGS_((const char *, double **));
double NgramSingleValue _ANSI_ARGS_((unsigned char, unsigned char, double **));
static void NormalizeTreeNodeLog _ANSI_ARGS_((TreeNode *));
static int DumpNgramScore _ANSI_ARGS_((Tcl_Interp *, ScoreItem *, const char *));
typedef struct NgramItem {
ScoreItem header;
TreeNode *rootNode;
} NgramItem;
ScoreType NgramLogType = {
"ngramlog",
sizeof(NgramItem),
CreateNgram,
AddNgram,
NgramValue,
NgramElementValue,
NormalizeNgramLog,
DeleteNgramScore,
DumpNgramScore,
ScoreMethodCmd,
(ScoreType *)NULL
};
ScoreType NgramCountType = {
"ngramcount",
sizeof(NgramItem),
CreateNgram,
AddNgram,
NgramValue,
NgramElementValue,
NullScoreNormalizer,
DeleteNgramScore,
DumpNgramScore,
ScoreMethodCmd,
(ScoreType *)NULL
};
static int
CreateNgram(Tcl_Interp *interp, ScoreItem *itemPtr, int argc, const char **argv) {
NgramItem *ngPtr = (NgramItem *)itemPtr;
char temp_ptr[TCL_DOUBLE_SPACE];
Tcl_DString dsPtr;
int i;
ngPtr->header.elemSize = -1;
ngPtr->rootNode = createWordTreeRoot();
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;
}
void
DeleteNgramScore(ClientData clientData) {
NgramItem *ngPtr = (NgramItem *)clientData;
deleteWordTree(ngPtr->rootNode);
// ckfree((char *)(ngPtr->rootNode));
DeleteScore(clientData);
}
static int
AddNgram(Tcl_Interp *interp, ScoreItem *itemPtr, const char *element, double value) {
NgramItem *ngPtr = (NgramItem *)itemPtr;
addWordToTree(ngPtr->rootNode, element, (unsigned short int) value);
Tcl_SetObjResult(interp, Tcl_NewStringObj(element, -1));
return TCL_OK;
}
static double
NgramValue(Tcl_Interp *interp, ScoreItem *itemPtr, const char *string) {
NgramItem *ngPtr = (NgramItem *)itemPtr;
int wordLen;
unsigned short int value = 0;
double totalVal = 0;
int i;
wordLen = strlen(string);
for (i=0; i <= wordLen-itemPtr->elemSize; i++) {
if (treeMatchString(ngPtr->rootNode, string+i, &value)) {
totalVal += (double) value;
}
}
return totalVal;
}
static double
NgramElementValue(Tcl_Interp *interp, ScoreItem *itemPtr, const char *string) {
NgramItem *ngPtr = (NgramItem *)itemPtr;
unsigned short int value=0;
if (! treeContainsWord(ngPtr->rootNode, string, &value, strlen(string))) {
return 0.0;
}
return (double) value;
}
static int
NormalizeNgramLog(Tcl_Interp *interp, ScoreItem *itemPtr) {
NgramItem *ngPtr = (NgramItem *)itemPtr;
NormalizeTreeNodeLog(ngPtr->rootNode);
Tcl_ResetResult(interp);
return TCL_OK;
}
static void
NormalizeTreeNodeLog(TreeNode *rootNode) {
int count = 0;
if (rootNode == NULL) {
return;
}
if (rootNode->measure > 0) {
rootNode->measure = (unsigned short int) (log((double) (rootNode->measure)) * 1000.0);
}
for (count=0; rootNode->next && rootNode->next[count]; count++) {
NormalizeTreeNodeLog(rootNode->next[count]);
}
}
static int
DumpNgramScore(Tcl_Interp *interp, ScoreItem *itemPtr, const char *script) {
NgramItem *ngPtr = (NgramItem *)itemPtr;
Tcl_DString element;
Tcl_DString command;
Tcl_DStringInit(&element);
Tcl_DStringInit(&command);
Tcl_DStringAppend(&command, script, strlen(script));
Tcl_ResetResult(interp);
return DumpTreeNode(interp, ngPtr->rootNode, &command, &element, 0);
}