~bal2277/nlgen2/main

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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 * Copyright 2008 Novamente LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package relex;

import java.io.Serializable;
import java.util.ArrayList;

import relex.feature.Atom;
import relex.feature.FeatureForeach;
import relex.feature.FeatureNode;
import relex.feature.FeatureNodeCallback;
import relex.feature.LinkableView;
import relex.feature.RelationCallback;
import relex.feature.RelationForeach;
import relex.stats.SimpleTruthValue;
import relex.tree.PhraseTree;

/**
 * A ParsedSentence object stores all of the syntactic and semantic
 * information about a sentence parse. The data in the Object is
 * gradually built up by RelationExtractor.
 *
 * ParsedSentence contains:
 * 1. A FeatureNode with metaData about the parse (i.e., the number
 *    of conjunctions)
 * 2. An ArrayList of FeatureNodes (leafConstituents) representing each
 *    word in the sentence. -- the parse data can be found by checking
 *    the links in these words.
 * 3. Strings representing the original sentence, and representations
 *    of its parses
 * 4. Sets of relations, with the semantic data from the sentence.
 * 5. A TruthValue (inherited from Atom) that ranks the relative
 *    likelihood of this parse of being a correct (meaningful) parse
 *    of the sentence.
 */
public class ParsedSentence extends Atom implements Serializable
{
	private static final long serialVersionUID = -5518792541801263127L;

	// Unique ID string identifying this parse.
	private String idString;

	// Back-pointer to collection of other parses for this sentence
	private Sentence sentence;

	// String containing the original sentence
	private String original;

	// String containing the ascii-art tree output by the link grammar parser.
	private String linkString;

	// A string containing the Penn tree-bank style markup,
	// aka "phrase structure" markup, for example
	// (S (NP I) (VP am (NP a big robot)) .)
	private String phraseString;

	private String errorString;

	// Metadata about the sentence; primarily, this consists of diagnostic
	// info returned by the link grammar parser.
	private FeatureNode metaData;

	// An ArrayList of FeatureNodes, each one representing a word in the
	// sentence.  If there are no "link islands", each can be reached by
	// following arcs from the others.
	private ArrayList<FeatureNode> leafConstituents;

	/* -------------------------------------------------------------------- */
	/* Constructors, and setters/getters for private members. */
	// Constructor.
	public ParsedSentence(String originalString)
	{
		original = originalString;
		linkString = null;
		errorString = "";
		phraseString = null;
		leafConstituents = new ArrayList<FeatureNode>();
	}

	public void setMetaData(FeatureNode f) {
		metaData = f;
	}

	public FeatureNode getMetaData() {
		return metaData;
	}

	public String getOriginalSentence() {
		return original;
	}

	public String getIDString() {
		return idString;
	}

	public Sentence getSentence() {
		return sentence;
	}

	public void setSentence(Sentence s) {
		sentence = s;
	}

	public void setIDString(String str) {
		idString = str;
	}

	public String getLinkString() {
		return linkString;
	}

	public void setLinkString(String str) {
		linkString = str;
	}

	public String getPhraseString() {
		return phraseString;
	}

	public void setPhraseString(String str) {
		phraseString = str;
	}

	public void setErrorString(String eString) {
		errorString = eString;
	}

	public String getErrorString() {
		return errorString;
	}

	/* -------------------------------------------------------------------- */
	public int getNumWords()
	{
		return leafConstituents.size();
	}

	/**
	 * Return the i'th word in the sentence, as a feature node
	 */
	public FeatureNode getWordAsNode(int i)
	{
		return leafConstituents.get(i);
	}

	/**
	 * Return the i'th lemmatized word in the sentence, as a string.
	 * This is the "root form" of the word, and not the original word.
	 */
	public String getWord(int i)
	{
		return LinkableView.getWordString(getWordAsNode(i));
	}

	/**
	 * Return the i'th word in the sentence, as a string
	 * This is the original form of the word, and not its lemma.
	 */
	public String getOrigWord(int i)
	{
		return LinkableView.getOrigWordString(getWordAsNode(i));
	}

	/**
	 * Return the part-of-speech of the i'th word in the sentence
	 */
	public String getPOS(int i)
	{
		return LinkableView.getPOS(getWordAsNode(i));
	}

	/**
	 * Return the offset, in the original sentence, to the first
	 * character of the i'th word in the sentence.
	 */
	public int getStartChar(int i)
	{
		return LinkableView.getStartChar(getWordAsNode(i));
	}

	public void addWord(FeatureNode w)
	{
		leafConstituents.add(w);
	}

	/**
	 * Return feature node for the indicated word. Return null
	 * if the word cannot be found in the sentence.  The input
	 * word may be either the word as it appears in the sentence,
	 * or its morphological root.
	 *
	 * If there are multiple occurances of a word in a sentence,
	 * this will return only the left-most such occurance.
	 */
	public FeatureNode findWord(String word)
	{
		class word_cb implements FeatureNodeCallback
		{
			String match_word;
			FeatureNode found;
			word_cb(String mw)
			{
				match_word = mw;
				found = null;
			}

			Boolean test(FeatureNode fn, FeatureNode fstr)
			{
				if (null == fstr) return false;
				String w = fstr.getValue();
				if (match_word.equals(w))
				{
					found = fn;
					return true;
				}
				return false;
			}
			public Boolean FNCallback(FeatureNode fn)
			{
				Boolean rc = test(fn, fn.get("orig_str"));
				if (rc) return rc;
				rc = test(fn, fn.get("str"));
				if (rc) return rc;
				return false;
			}
		}
		word_cb cb = new word_cb(word);
		FeatureForeach.foreachWord(getLeft(), cb);
		return cb.found;
	}

	/* -------------------------------------------------------------------- */
	/* Various different views of the parsed sentence */

	/**
	 * Shows the full feature structure of the parse as it can be found by
	 * tracing links from the left-most word. Islands will be missed.
	 */
	public String fullParseString()
	{
		if (getLeft() != null)
			return getLeft().toString();
		return "";
	}

	/**
	 * Returns a list of the words in the sentence, marked up according to
	 * which "part of speech" they are.  Thus, for example:
	 * "The big red baloon floated away." becomes
	 * LEFT-WALL The.det big.adj red.adj balloon.noun float.verb away.prep .
	 */
	public String printPartsOfSpeech()
	{
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < leafConstituents.size(); i++) {
			sb.append(getWord(i));
			LinkableView w = new LinkableView(getWordAsNode(i));
			String pos = w.getPOS();
			if (pos != null && !pos.equals("WORD"))
				sb.append("." + pos);
			String tense = w.getTenseVal();  // ??? tense is not working ...
			if (tense != null && tense.length() > 0)
				sb.append(tense);
			if (i < leafConstituents.size() - 1)
				sb.append(" ");
			// else
			// sb.append(".");
		}
		return sb.toString();
	}

	public String toString()
	{
		return original;
	}

	/* ---------------------------------------------------------------- */
	/**
	 * Call the callback on each relation in the sentence
	 */
	public Boolean foreach(RelationCallback cb)
	{
		return RelationForeach.foreach(getLeft(), cb);
	}
	
	public Boolean foreach(FeatureNodeCallback cb)
	{
		return RelationForeach.foreach(getLeft(), cb);
	}

	/* ---------------------------------------------------------------- */
	/**
	 * @return the FeatureNode representing the left-most word in the sentence.
	 */
	public FeatureNode getLeft()
	{
		return leafConstituents.get(0);
	}

	/**
	 * @return the phrase tree associated with this parse
	 */
	public PhraseTree getPhraseTree()
	{
		return new PhraseTree(getLeft());
	}

	/* ---------------------------------------------------------------- */
	/* Return unpacked meta information about parse, and ranking too */

	public int getAndCost()
	{
		return getMeta("and_cost");
	}

	public int getDisjunctCost()
	{
		return getMeta("disjunct_cost");
	}

	public int getLinkCost()
	{
		return getMeta("link_cost");
	}

	public int getNumSkippedWords()
	{
		return getMeta("num_skipped_words");
	}

	private int getMeta(String str)
	{
		FeatureNode fn = metaData.get(str);
		if (fn == null) return -1;
		String val = fn.getValue();
		return Integer.parseInt(val);
	}

	/**
	 * Perform a crude parse-ranking based on Link-grammar output.
	 * The ranking will be stored as the "confidence" of the 
	 * TruthValue associated with this parse.
	 *
	 * @returns the score that was assigned.
	 *
	 * A classic example of competing parses for a sentence is:
	 * (S (NP I) (VP saw (NP the man) (PP with (NP the binoculars))) .)
	 * (S (NP I) (VP saw (NP (NP the man) (PP with (NP the binoculars)))) .)
	 * The ranker below gives both about equal scores.
	 *
	 */
	public double simpleRankParse()
	{
		SimpleTruthValue stv = new SimpleTruthValue();
		truth_value = stv;
		stv.setMean(1.0);  // 1.0 == true -- this is a parse.

		// The weights used here are rather ad-hoc; but the
		// basic idea is that we want to penalize skipped words
		// strongly, but disjunct costs not as much. Low link
		// costs are the tiebreaker.
		double weight = 0.4 * getNumSkippedWords();
		weight += 0.2 * getDisjunctCost();
		weight += 0.06 * getAndCost();
		weight += 0.012 * getLinkCost();

		weight = Math.exp(-weight);

		stv.setConfidence(weight);
		return weight;
	}
	
	/**
	 * Take the current parse confidence, and rescale it by the 
	 * indicated amount.  The method simpleRankParse() must have
	 * been previously called to perform the initial ranking.
	 */
	public void rescaleRank(double weight)
	{
		SimpleTruthValue stv = (SimpleTruthValue) truth_value;
		double confidence = stv.getConfidence();
		confidence *= weight;
		stv.setConfidence(confidence);
	}

	public double getRank()
	{
		SimpleTruthValue stv = (SimpleTruthValue) truth_value;
		return stv.getConfidence();
	}

	public int hashCode()
	{
		if (original == null)
			return 0;
		return original.hashCode() | leafConstituents.size();
	}

	public boolean equals(Object x)
	{
		if (! (x instanceof ParsedSentence))
			return false;
		ParsedSentence p = (ParsedSentence)x;
		if (original == null)
			return p.original == null;
		else
			return original.equals(p.original) && this.leafConstituents.equals(p.leafConstituents);
	}
	
} // end ParsedSentence