forked from simplenlg/simplenlg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExternalTest.cs
475 lines (380 loc) · 20 KB
/
ExternalTest.cs
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
* Ported to C# by Gert-Jan de Vries
*/
using System.Collections.Generic;
using NUnit.Framework;
using SimpleNLG.Main;
using SimpleNLG.Main.aggregation;
using SimpleNLG.Main.features;
using SimpleNLG.Main.framework;
using SimpleNLG.Main.lexicon;
using SimpleNLG.Main.phrasespec;
using SimpleNLG.Main.realiser.english;
namespace SimpleNLG.Test.external
{
[TestFixture]
public class ExternalTest
{
private Lexicon lexicon = null;
private NLGFactory phraseFactory = null;
private Realiser realiser = null;
[SetUp]
public virtual void setUp()
{
lexicon = Lexicon.DefaultLexicon;
phraseFactory = new NLGFactory(lexicon);
realiser = new Realiser(lexicon);
}
[Test]
public virtual void forcherTest()
{
phraseFactory.Lexicon = lexicon;
PhraseElement s1 = phraseFactory.createClause(null, "associate", "Marie");
s1.setFeature(Feature.PASSIVE, true);
PhraseElement pp1 = phraseFactory.createPrepositionPhrase("with"); //$NON-NLS-1$
pp1.addComplement("Peter"); //$NON-NLS-1$
pp1.addComplement("Paul"); //$NON-NLS-1$
s1.addPostModifier(pp1);
Assert.AreEqual("Marie is associated with Peter and Paul", realiser.realise(s1).Realisation); //$NON-NLS-1$
SPhraseSpec s2 = phraseFactory.createClause();
s2.setSubject(phraseFactory.createNounPhrase("Peter")); //$NON-NLS-1$
s2.setVerb("have"); //$NON-NLS-1$
s2.setObject("something to do"); //$NON-NLS-1$
s2.addPostModifier(phraseFactory.createPrepositionPhrase("with", "Paul")); //$NON-NLS-1$ //$NON-NLS-2$
Assert.AreEqual("Peter has something to do with Paul", realiser.realise(s2).Realisation); //$NON-NLS-1$
}
[Test]
public virtual void luTest()
{
phraseFactory.Lexicon = lexicon;
PhraseElement
s1 = phraseFactory.createClause("we", "consider", "John"); //$NON-NLS-1$ - $NON-NLS-1$ - $NON-NLS-1$
s1.addPostModifier("a friend"); //$NON-NLS-1$
Assert.AreEqual("we consider John a friend", realiser.realise(s1).Realisation); //$NON-NLS-1$
}
[Test]
public virtual void dwightTest()
{
phraseFactory.Lexicon = lexicon;
NPPhraseSpec noun4 = phraseFactory.createNounPhrase("FGFR3 gene in every cell"); //$NON-NLS-1$
noun4.setSpecifier("the");
PhraseElement prep1 = phraseFactory.createPrepositionPhrase("of", noun4); //$NON-NLS-1$
PhraseElement noun1 = phraseFactory.createNounPhrase("the", "patient's mother"); //$NON-NLS-1$ //$NON-NLS-2$
PhraseElement noun2 = phraseFactory.createNounPhrase("the", "patient's father"); //$NON-NLS-1$ //$NON-NLS-2$
PhraseElement noun3 = phraseFactory.createNounPhrase("changed copy"); //$NON-NLS-1$
noun3.addPreModifier("one"); //$NON-NLS-1$
noun3.addComplement(prep1);
CoordinatedPhraseElement coordNoun1 = new CoordinatedPhraseElement(noun1, noun2);
coordNoun1.Conjunction = "or"; //$NON-NLS-1$
PhraseElement verbPhrase1 = phraseFactory.createVerbPhrase("have"); //$NON-NLS-1$
verbPhrase1.setFeature(Feature.TENSE, Tense.PRESENT);
PhraseElement sentence1 = phraseFactory.createClause(coordNoun1, verbPhrase1, noun3);
realiser.DebugMode = true;
Assert.AreEqual(
"the patient's mother or the patient's father has one changed copy of the FGFR3 gene in every cell",
realiser.realise(sentence1).Realisation); //$NON-NLS-1$
noun3 = phraseFactory.createNounPhrase("a", "gene test"); //$NON-NLS-1$ //$NON-NLS-2$
noun2 = phraseFactory.createNounPhrase("an", "LDL test"); //$NON-NLS-1$ //$NON-NLS-2$
noun1 = phraseFactory.createNounPhrase("the", "clinic"); //$NON-NLS-1$ //$NON-NLS-2$
verbPhrase1 = phraseFactory.createVerbPhrase("perform"); //$NON-NLS-1$
CoordinatedPhraseElement coord1 = new CoordinatedPhraseElement(noun2, noun3);
sentence1 = phraseFactory.createClause(noun1, verbPhrase1, coord1);
sentence1.setFeature(Feature.TENSE, Tense.PAST);
Assert.AreEqual("the clinic performed an LDL test and a gene test",
realiser.realise(sentence1).Realisation); //$NON-NLS-1$
}
[Test]
public virtual void novelliTest()
{
PhraseElement
p = phraseFactory.createClause("Mary", "chase", "George"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
PhraseElement pp = phraseFactory.createPrepositionPhrase("in", "the park"); //$NON-NLS-1$ //$NON-NLS-2$
p.addPostModifier(pp);
Assert.AreEqual("Mary chases George in the park", realiser.realise(p).Realisation); //$NON-NLS-1$
SPhraseSpec
run = phraseFactory.createClause("you", "go", "running"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
run.setFeature(Feature.MODAL, "should"); //$NON-NLS-1$
run.addPreModifier("really"); //$NON-NLS-1$
SPhraseSpec think = phraseFactory.createClause("I", "think"); //$NON-NLS-1$ //$NON-NLS-2$
think.setObject(run);
run.setFeature(Feature.SUPRESSED_COMPLEMENTISER, true);
string text = realiser.realise(think).Realisation;
Assert.AreEqual("I think you should really go running", text); //$NON-NLS-1$
}
[Test]
public virtual void piotrekTest()
{
phraseFactory.Lexicon = lexicon;
PhraseElement
sent = phraseFactory.createClause("I", "shoot", "the duck"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
sent.setFeature(Feature.TENSE, Tense.PAST);
PhraseElement
loc = phraseFactory.createPrepositionPhrase("at", "the Shooting Range"); //$NON-NLS-1$ //$NON-NLS-2$
sent.addPostModifier(loc);
sent.setFeature(Feature.CUE_PHRASE, "then"); //$NON-NLS-1$
Assert.AreEqual("then I shot the duck at the Shooting Range",
realiser.realise(sent).Realisation); //$NON-NLS-1$
}
[Test]
public virtual void prescottTest()
{
phraseFactory.Lexicon = lexicon;
PhraseElement
embedded = phraseFactory.createClause("Jill", "prod", "Spot"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
PhraseElement sent = phraseFactory.createClause("Jack", "see", embedded); //$NON-NLS-1$ //$NON-NLS-2$
embedded.setFeature(Feature.SUPRESSED_COMPLEMENTISER, true);
embedded.setFeature(Feature.FORM, Form.BARE_INFINITIVE);
Assert.AreEqual("Jack sees Jill prod Spot", realiser.realise(sent).Realisation); //$NON-NLS-1$
}
[Test]
public virtual void wissnerTest()
{
PhraseElement p = phraseFactory.createClause("a wolf", "eat"); //$NON-NLS-1$ //$NON-NLS-2$
p.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.WHAT_OBJECT);
Assert.AreEqual("what does a wolf eat", realiser.realise(p).Realisation); //$NON-NLS-1$
}
[Test]
public virtual void phanTest()
{
PhraseElement subjectElement = phraseFactory.createNounPhrase("I");
PhraseElement verbElement = phraseFactory.createVerbPhrase("run");
PhraseElement prepPhrase = phraseFactory.createPrepositionPhrase("from");
prepPhrase.addComplement("home");
verbElement.addComplement(prepPhrase);
SPhraseSpec newSentence = phraseFactory.createClause();
newSentence.setSubject(subjectElement);
newSentence.VerbPhrase = verbElement;
Assert.AreEqual("I run from home", realiser.realise(newSentence).Realisation); //$NON-NLS-1$
}
[Test]
public virtual void kerberTest()
{
SPhraseSpec sp = phraseFactory.createClause("he", "need");
SPhraseSpec secondSp = phraseFactory.createClause();
secondSp.setVerb("build");
secondSp.setObject("a house");
secondSp.setFeature(Feature.FORM, Form.INFINITIVE);
sp.setObject("stone");
sp.addComplement(secondSp);
Assert.AreEqual("he needs stone to build a house", realiser.realise(sp).Realisation);
SPhraseSpec sp2 = phraseFactory.createClause("he", "give");
sp2.setIndirectObject("I");
sp2.setObject("the book");
Assert.AreEqual("he gives me the book", realiser.realise(sp2).Realisation);
}
[Test]
public virtual void stephensonTest()
{
SPhraseSpec qs2 = phraseFactory.createClause();
qs2 = phraseFactory.createClause();
qs2.setSubject("moles of Gold");
qs2.setVerb("are");
qs2.setFeature(Feature.NUMBER, NumberAgreement.PLURAL);
qs2.setFeature(Feature.PASSIVE, false);
qs2.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.HOW_MANY);
qs2.setObject("in a 2.50 g sample of pure Gold");
DocumentElement sentence = phraseFactory.createSentence(qs2);
Assert.AreEqual("How many moles of Gold are in a 2.50 g sample of pure Gold?",
realiser.realise(sentence).Realisation);
}
[Test]
public virtual void pierreTest()
{
SPhraseSpec p = phraseFactory.createClause("Mary", "chase", "George");
p.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.WHAT_OBJECT);
Assert.AreEqual("What does Mary chase?", realiser.realiseSentence(p));
p = phraseFactory.createClause("Mary", "chase", "George");
p.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.YES_NO);
Assert.AreEqual("Does Mary chase George?", realiser.realiseSentence(p));
p = phraseFactory.createClause("Mary", "chase", "George");
p.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.WHERE);
Assert.AreEqual("Where does Mary chase George?", realiser.realiseSentence(p));
p = phraseFactory.createClause("Mary", "chase", "George");
p.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.WHY);
Assert.AreEqual("Why does Mary chase George?", realiser.realiseSentence(p));
p = phraseFactory.createClause("Mary", "chase", "George");
p.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.HOW);
Assert.AreEqual("How does Mary chase George?", realiser.realiseSentence(p));
}
[Test]
public virtual void data2TextTest()
{
SPhraseSpec p = phraseFactory.createClause("the dog", "weigh", "12");
Assert.AreEqual("The dog weighes 12.", realiser.realiseSentence(p));
NLGElement dataDropout2 = phraseFactory.createNLGElement("data dropouts");
dataDropout2.Plural = true;
SPhraseSpec sentence2 = phraseFactory.createClause();
sentence2.setSubject(phraseFactory.createStringElement("there"));
sentence2.setVerb("be");
sentence2.setObject(dataDropout2);
Assert.AreEqual("There are data dropouts.", realiser.realiseSentence(sentence2));
SPhraseSpec weather1 = phraseFactory.createClause("SE 10-15", "veer", "S 15-20");
weather1.setFeature(Feature.FORM, Form.GERUND);
Assert.AreEqual("SE 10-15 veering S 15-20.", realiser.realiseSentence(weather1));
SPhraseSpec weather2 = phraseFactory.createClause("cloudy and misty", "be", "XXX");
weather2.VerbPhrase.setFeature(Feature.ELIDED, true);
Assert.AreEqual("Cloudy and misty.", realiser.realiseSentence(weather2));
SPhraseSpec weather3 = phraseFactory.createClause("S 15-20", "increase", "20-25");
weather3.setFeature(Feature.FORM, Form.GERUND);
weather3.getSubject().setFeature(Feature.ELIDED, true);
Assert.AreEqual("Increasing 20-25.", realiser.realiseSentence(weather3));
SPhraseSpec weather4 = phraseFactory.createClause("S 20-25", "back", "SSE");
weather4.setFeature(Feature.FORM, Form.GERUND);
weather4.getSubject().setFeature(Feature.ELIDED, true);
CoordinatedPhraseElement coord = new CoordinatedPhraseElement();
coord.addCoordinate(weather1);
coord.addCoordinate(weather3);
coord.addCoordinate(weather4);
coord.Conjunction = "then";
Assert.AreEqual("SE 10-15 veering S 15-20, increasing 20-25 then backing SSE.",
realiser.realiseSentence(coord));
SPhraseSpec weather5 = phraseFactory.createClause("rain", null, "likely");
Assert.AreEqual("Rain likely.", realiser.realiseSentence(weather5));
}
[Test]
public virtual void rafaelTest()
{
IList<NLGElement> ss = new List<NLGElement>();
ClauseCoordinationRule coord = new ClauseCoordinationRule();
coord.Factory = phraseFactory;
ss.Add(agreePhrase("John Lennon")); // john lennon agreed with it
ss.Add(disagreePhrase("Geri Halliwell")); // Geri Halliwell disagreed with it
ss.Add(commentPhrase("Melanie B")); // Mealnie B commented on it
ss.Add(agreePhrase("you")); // you agreed with it
ss.Add(commentPhrase("Emma Bunton")); //Emma Bunton commented on it
IList<NLGElement> results = coord.apply(ss);
IList<string> ret = realizeAll(results);
Assert.AreEqual(
"[John Lennon and you agreed with it, Geri Halliwell disagreed with it, Melanie B and Emma Bunton commented on it]",
ret.ToStringNLG());
}
private NLGElement commentPhrase(string name)
{
// used by testRafael
SPhraseSpec s = phraseFactory.createClause();
s.setSubject(phraseFactory.createNounPhrase(name));
s.VerbPhrase = phraseFactory.createVerbPhrase("comment on");
s.setObject("it");
s.setFeature(Feature.TENSE, Tense.PAST);
return s;
}
private NLGElement agreePhrase(string name)
{
// used by testRafael
SPhraseSpec s = phraseFactory.createClause();
s.setSubject(phraseFactory.createNounPhrase(name));
s.VerbPhrase = phraseFactory.createVerbPhrase("agree with");
s.setObject("it");
s.setFeature(Feature.TENSE, Tense.PAST);
return s;
}
private NLGElement disagreePhrase(string name)
{
// used by testRafael
SPhraseSpec s = phraseFactory.createClause();
s.setSubject(phraseFactory.createNounPhrase(name));
s.VerbPhrase = phraseFactory.createVerbPhrase("disagree with");
s.setObject("it");
s.setFeature(Feature.TENSE, Tense.PAST);
return s;
}
private List<string> realizeAll(IList<NLGElement> results)
{
// used by testRafael
List<string> ret = new List<string>();
foreach (NLGElement e in results)
{
string r = realiser.realise(e).Realisation;
ret.Add(r);
}
return ret;
}
[Test]
public virtual void wikipediaTest()
{
NPPhraseSpec subject = phraseFactory.createNounPhrase("the", "woman");
subject.Plural = true;
SPhraseSpec sentence = phraseFactory.createClause(subject, "smoke");
sentence.setFeature(Feature.NEGATED, true);
Assert.AreEqual("The women do not smoke.", realiser.realiseSentence(sentence));
SPhraseSpec s1 = phraseFactory.createClause("the man", "be", "hungry");
SPhraseSpec s2 = phraseFactory.createClause("the man", "buy", "an apple");
NLGElement result = (new ClauseCoordinationRule()).apply(s1, s2);
Assert.AreEqual("The man is hungry and buys an apple.", realiser.realiseSentence(result));
}
[Test]
public virtual void leanTest()
{
SPhraseSpec sentence = phraseFactory.createClause();
sentence.setVerb("be");
sentence.setObject("a ball");
sentence.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.WHAT_SUBJECT);
Assert.AreEqual("What is a ball?", realiser.realiseSentence(sentence));
sentence = phraseFactory.createClause();
sentence.setVerb("be");
NPPhraseSpec @object = phraseFactory.createNounPhrase("example");
@object.Plural = true;
@object.addModifier("of jobs");
sentence.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.WHAT_SUBJECT);
sentence.setObject(@object);
Assert.AreEqual("What are examples of jobs?", realiser.realiseSentence(sentence));
SPhraseSpec p = phraseFactory.createClause();
NPPhraseSpec sub1 = phraseFactory.createNounPhrase("Mary");
sub1.setFeature(LexicalFeature.GENDER, Gender.FEMININE);
sub1.setFeature(Feature.PRONOMINAL, true);
sub1.setFeature(Feature.PERSON, Person.FIRST);
p.setSubject(sub1);
p.setVerb("chase");
p.setObject("the monkey");
string output2 = realiser.realiseSentence(p); // Realiser created earlier.
Assert.AreEqual("I chase the monkey.", output2);
SPhraseSpec test = phraseFactory.createClause();
NPPhraseSpec subject = phraseFactory.createNounPhrase("Mary");
subject.setFeature(Feature.PRONOMINAL, true);
subject.setFeature(Feature.PERSON, Person.SECOND);
test.setSubject(subject);
test.setVerb("cry");
test.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.WHY);
test.setFeature(Feature.TENSE, Tense.PRESENT);
Assert.AreEqual("Why do you cry?", realiser.realiseSentence(test));
test = phraseFactory.createClause();
subject = phraseFactory.createNounPhrase("Mary");
subject.setFeature(Feature.PRONOMINAL, true);
subject.setFeature(Feature.PERSON, Person.SECOND);
test.setSubject(subject);
test.setVerb("be");
test.setObject("crying");
test.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.WHY);
test.setFeature(Feature.TENSE, Tense.PRESENT);
Assert.AreEqual("Why are you crying?", realiser.realiseSentence(test));
}
[Test]
public virtual void kalijurandTest()
{
string lemma = "walk";
WordElement word = lexicon.lookupWord(lemma, new LexicalCategory(LexicalCategory.LexicalCategoryEnum.VERB));
InflectedWordElement inflectedWord = new InflectedWordElement(word);
inflectedWord.setFeature(Feature.FORM, Form.PAST_PARTICIPLE);
string form = realiser.realise(inflectedWord).Realisation;
Assert.AreEqual("walked", form);
inflectedWord = new InflectedWordElement(word);
inflectedWord.setFeature(Feature.PERSON, Person.THIRD);
form = realiser.realise(inflectedWord).Realisation;
Assert.AreEqual("walks", form);
}
[Test]
public virtual void layTest()
{
string lemma = "slap";
WordElement word = lexicon.lookupWord(lemma, new LexicalCategory(LexicalCategory.LexicalCategoryEnum.VERB));
InflectedWordElement inflectedWord = new InflectedWordElement(word);
inflectedWord.setFeature(Feature.FORM, Form.PRESENT_PARTICIPLE);
string form = realiser.realise(inflectedWord).Realisation;
Assert.AreEqual("slapping", form);
VPPhraseSpec v = phraseFactory.createVerbPhrase("slap");
v.setFeature(Feature.PROGRESSIVE, true);
string progressive = realiser.realise(v).Realisation;
Assert.AreEqual("is slapping", progressive);
}
}
}