/
MihalceaEdge.cc
287 lines (256 loc) · 8.85 KB
/
MihalceaEdge.cc
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* MihalceaEdge.cc
*
* Implements the edge creation portion of the Rada Mihalcea
* word-sense disambiguation algorithm. Basically, for every
* word instance pair, there is a list of associatated senses
* for each word instance. This class creates edges between
* each pair of word senses, thus creating a biclique or
* complete bipartite graph between the two lists of word senses.
*
* Some word senses may be unrelated, in which case the edge
* between these to senses is ommited, rather than being assigned
* a score of zero.
*
* Copyright (c) 2008 Linas Vepstas <linas@linas.org>
*/
#include "MihalceaEdge.h"
#include <stdio.h>
#include <sys/time.h>
#include <opencog/atoms/truthvalue/SimpleTruthValue.h>
#include <opencog/nlp/types/atom_types.h>
#include <opencog/nlp/wsd/ForeachWord.h>
#include <opencog/nlp/wsd/MihalceaEdge.h>
#include <opencog/nlp/wsd/SenseCache.h>
#include <opencog/nlp/wsd/SenseSimilarityLCH.h>
#include <opencog/nlp/wsd/SenseSimilaritySQL.h>
#include <opencog/util/platform.h>
#define DEBUG
// #define DETAIL_DEBUG
// #define SENSE_DETAIL_DEBUG
// #define LINK_DEBUG
using namespace opencog;
MihalceaEdge::MihalceaEdge()
{
sen_sim = NULL;
atom_space = NULL;
}
MihalceaEdge::~MihalceaEdge()
{
if (sen_sim) delete sen_sim;
atom_space = NULL;
}
void MihalceaEdge::set_atom_space(AtomSpace *as)
{
atom_space = as;
if (sen_sim) delete sen_sim;
#ifdef HAVE_SQL_STORAGE
sen_sim = new SenseSimilaritySQL(atom_space);
#else
fprintf (stderr,
"Warning/Error: MihalceaEdge: proper operation of word-sense \n"
"disambiguation requires precomputed sense similarities to be\n"
"pulled from SQL stoarage.\n");
sen_sim = new SenseSimilarityLCH();
#endif /* HAVE_SQL_STORAGE */
sense_cache.set_atom_space(as);
}
/** Loop over all parses for this sentence. */
void MihalceaEdge::annotate_sentence(const Handle& h)
{
foreach_parse(h, &MihalceaEdge::annotate_parse_f, this);
}
/**
* For each parse, loop over all word-instance syntactic relationships.
* (i.e. _subj, _obj, _nn, _amod, and so on). For each relationship,
* create an edge between all corresponding (word-instance, word-sense)
* pairs.
*/
void MihalceaEdge::annotate_parse(const Handle& h)
{
words.clear();
foreach_word_instance(h, &EdgeUtils::look_at_word, (EdgeUtils *) this);
// At this point, "words" contains all of the relex-participating
// words in the parse. Loop over word-pairs, and annotate them.
#ifdef DEBUG
word_pair_count = 0;
edge_count = 0;
struct timeval start;
gettimeofday(&start, NULL);
#endif
HandleSet::const_iterator f;
for (f = words.begin(); f != words.end(); ++f)
{
HandleSet::const_iterator s = f;
++s;
for (; s != words.end(); ++s)
{
annotate_word_pair(*f, *s);
}
}
#ifdef DEBUG
struct timeval finish, elapsed;
gettimeofday(&finish, NULL);
timersub(&finish, &start, &elapsed);
double secs = elapsed.tv_sec + 1.0e-6 * elapsed.tv_usec;
double rate = edge_count / secs;
printf("; MihalceaEdge::annotate_parse added %d edges for %d word pairs (rate=%f)\n",
edge_count, word_pair_count, rate);
#endif
}
bool MihalceaEdge::annotate_parse_f(const Handle& h)
{
annotate_parse(h);
return false;
}
/**
* For each pair of parses, create word-sense edge-links between
* the two parses.
*/
void MihalceaEdge::annotate_parse_pair(const Handle& ha, const Handle& hb)
{
words.clear();
foreach_word_instance(ha, &EdgeUtils::look_at_word, (EdgeUtils *) this);
HandleSet pa_words = words;
words.clear();
foreach_word_instance(hb, &EdgeUtils::look_at_word, (EdgeUtils *) this);
#ifdef DETAIL_DEBUG
printf ("; ========================= start sent pair (%zu x %zu) words\n",
pa_words.size(), words.size());
#endif
// At this point, "pa_words" contains all of the relex-participating
// words in parse ha, and "words" contains those of parse hb.
// Loop over word-pairs, and annotate them.
#ifdef DEBUG
word_pair_count = 0;
edge_count = 0;
struct timeval start;
gettimeofday(&start, NULL);
#endif
HandleSet::const_iterator ia;
for (ia = pa_words.begin(); ia != pa_words.end(); ++ia)
{
HandleSet::const_iterator ib;
for (ib = words.begin(); ib != words.end(); ++ib)
{
annotate_word_pair(*ia, *ib);
}
}
#ifdef DEBUG
struct timeval finish, elapsed;
gettimeofday(&finish, NULL);
timersub(&finish, &start, &elapsed);
double secs = elapsed.tv_sec + 1.0e-6 * elapsed.tv_usec;
double rate = edge_count / secs;
printf("; annotate_parse_pair added %d edges for %d word pairs (rate=%f)\n",
edge_count, word_pair_count, rate);
#endif
}
/**
* Create edges between all senses of a pair of words.
*
* This routine implements a doubley-nested foreach loop, iterating
* over all senses of each word, and creating an edge between them.
*
* All of the current word-sense similarity algorithms report zero
* similarity when the two words are different parts of speech.
* Therefore, in order to improve performance, this routine does not
* create any edges between words of differing parts-of-speech.
*/
bool MihalceaEdge::annotate_word_pair(const Handle& first, const Handle& second)
{
#ifdef DETAIL_DEBUG
const std::string &fn = as->get_name(first);
const std::string &sn = as->get_name(second);
printf ("; WordPair %d: (%s, %s)\n", word_pair_count, fn.c_str(), sn.c_str());
#endif
second_word_inst = second;
foreach_word_sense_of_inst(first, &MihalceaEdge::sense_of_first_inst, this);
word_pair_count ++;
return false;
}
/**
* Called for every pair (word-instance,word-sense) of the first
* word-instance of a relex relationship. This, in turn iterates
* over the second word-instance of the relex relationship.
*/
bool MihalceaEdge::sense_of_first_inst(const Handle& first_word_sense_h,
const Handle& first_sense_link_h)
{
first_word_sense = first_word_sense_h;
#ifdef SENSE_DETAIL_DEBUG
const std::string &fn = as->get_name(first_word_sense_h);
printf ("; First word sense: %s\n", fn.c_str());
#endif
// Get the handle of the link itself ...
first_sense_link = first_sense_link_h;
foreach_word_sense_of_inst(second_word_inst,
&MihalceaEdge::sense_of_second_inst, this);
return false;
}
/**
* Use a word-sense similarity/relationship measure to assign an
* initial truth value to the edge. Create an edge only if the
* relationship is greater than zero.
*
* Called for every pair (word-instance,word-sense) of the second
* word-instance of a relex relationship. This routine is the last,
* most deeply nested loop of all of this set of nested loops. This
* routine now has possession of both pairs, and can now create a
* Mihalcea-graph edge between these pairs.
*
* As discussed in the README file, the resulting structure is:
*
* <!-- the word "tree" occured in the sentence -->
* CosenseLink strength=0.49 confidence=0.3
* InheritanceLink strength=0.9 confidence=0.6
* WordInstanceNode "tree_99"
* WordSenseNode "tree_sense_12"
*
* InheritanceLink strength=0.9 confidence=0.1
* WordInstanceNode "bark_144"
* WordSenseNode "bark_sense_23"
*/
bool MihalceaEdge::sense_of_second_inst(const Handle& second_word_sense_h,
const Handle& second_sense_link)
{
#ifdef SENSE_DETAIL_DEBUG
const std::string &fn = as->get_name(second_word_sense_h);
printf ("; Second word sense: %s\n", fn.c_str());
#endif
#ifdef USE_LOCAL_CACHE
// Get the similarity between the two word senses out of the
// cache (if it exists). XXX This appears to be a loosing strategy,
// See the README file for details. The core problem is that the
// cache is using the atomspace in a very inefficient way. XXX
SimpleTruthValue stv(0.5,0.5);
stv = sense_cache.similarity(first_word_sense, second_word_sense_h);
if (stv == TruthValue::DEFAULT_TV())
{
// Similarity was not found in the cache. Go fetch a value
// from the database.
stv = sen_sim->similarity(first_word_sense, second_word_sense_h);
sense_cache.set_similarity(first_word_sense, second_word_sense_h, stv);
}
#else
TruthValuePtr stv = sen_sim->similarity(first_word_sense, second_word_sense_h);
#endif
// Skip making edges between utterly unrelated nodes.
if (stv->get_mean() < 0.01) return false;
// Create a link connecting the first pair to the second pair.
atom_space->add_link(COSENSE_LINK, first_sense_link, second_sense_link)->setTruthValue(stv);
edge_count ++;
#ifdef LINK_DEBUG
Handle fw = get_word_instance_of_sense_link(first_sense_link);
Handle fs = get_word_sense_of_sense_link(first_sense_link);
const char *vfw = as->get_name(fw).c_str();
const char *vfs = as->get_name(fs).c_str();
Handle sw = get_word_instance_of_sense_link(second_sense_link);
Handle ss = get_word_sense_of_sense_link(second_sense_link);
const char *vsw = as->get_name(sw).c_str();
const char *vss = as->get_name(ss).c_str();
printf("slink: %s ## %s <<-->> %s ## %s add\n", vfw, vsw, vfs, vss);
printf("slink: %s ## %s <<-->> %s ## %s add\n", vsw, vfw, vss, vfs);
#endif
return false;
}