Commit db573df1 authored by Saad Mahamood's avatar Saad Mahamood

Test code reorganisation and cleanup

+ Cleaned up some of the test code in SNLG (lots more to do!).
+ Incremented version number to 4.4.8-SNAPSHOT. This will be the
release version number as well.
parent a9995a69
......@@ -5,7 +5,7 @@
<groupId>uk.ac.abdn</groupId>
<artifactId>SimpleNLG</artifactId>
<version>4.4.7-SNAPSHOT</version>
<version>4.4.8-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SimpleNLG</name>
......
/*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (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.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is "Simplenlg".
*
* The Initial Developer of the Original Code is Ehud Reiter, Albert Gatt and Dave Westwater.
* Portions created by Ehud Reiter, Albert Gatt and Dave Westwater are Copyright (C) 2010-11 The University of Aberdeen. All Rights Reserved.
*
* Contributor(s): Ehud Reiter, Albert Gatt, Dave Wewstwater, Roman Kutlak, Margaret Mitchell.
*/
package simplenlg.external;
import junit.framework.Assert;
import org.junit.Test;
import simplenlg.features.Feature;
import simplenlg.features.InterrogativeType;
import simplenlg.framework.NLGFactory;
import simplenlg.lexicon.Lexicon;
import simplenlg.phrasespec.PPPhraseSpec;
import simplenlg.phrasespec.SPhraseSpec;
import simplenlg.realiser.english.Realiser;
/**
* Further tests from third parties. This test class exists mainly to host code
* to address questions posted by users at the SimpleNLG forum.
*
* @author Rodrigo de Oliveira, University of Aberdeen
*
*/
public class External3Test {
/**
* Case 1 checks that "What do you think about John?" can be generated.
*
* Case 2 checks that the same clause is generated, even when an object is
* declared.
*/
@Test
public void interrogativeTest() {
Lexicon lexicon = Lexicon.getDefaultLexicon();
NLGFactory nlg = new NLGFactory(lexicon);
Realiser realiser = new Realiser(lexicon);
// Case 1, no object is explicitly given:
SPhraseSpec clause = nlg.createClause("you", "think");
PPPhraseSpec aboutJohn = nlg.createPrepositionPhrase("about", "John");
clause.addPostModifier(aboutJohn);
clause.setFeature(Feature.INTERROGATIVE_TYPE,
InterrogativeType.WHAT_OBJECT);
String realisation = realiser.realiseSentence(clause);
System.out.println(realisation);
Assert.assertEquals("What do you think about John?", realisation);
// Case 2:
// Add "bad things" as the object so the object doesn't remain null:
clause.setObject("bad things");
realisation = realiser.realiseSentence(clause);
System.out.println(realiser.realiseSentence(clause));
Assert.assertEquals("What do you think about John?", realisation);
}
}
......@@ -55,7 +55,7 @@ import simplenlg.realiser.english.Realiser;
* @author ereiter
*
*/
public class External1Test {
public class ExternalTest {
private Lexicon lexicon = null;
......
/*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (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.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is "Simplenlg".
*
* The Initial Developer of the Original Code is Ehud Reiter, Albert Gatt and Dave Westwater.
* Portions created by Ehud Reiter, Albert Gatt and Dave Westwater are Copyright (C) 2010-11 The University of Aberdeen. All Rights Reserved.
*
* Contributor(s): Ehud Reiter, Albert Gatt, Dave Wewstwater, Roman Kutlak, Margaret Mitchell, Saad Mahamood
*/
package simplenlg.syntax.english;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import simplenlg.aggregation.NewAggregator;
import simplenlg.features.Feature;
import simplenlg.features.Tense;
import simplenlg.framework.NLGElement;
@Ignore
public class ClauseAggregationTest2 extends SimpleNLG4Test {
private NewAggregator aggregator;
private NLGElement s1, s2, s3, s4;
/**
* Instantiates a new clause aggregation test.
*
* @param name
* the name
*/
public ClauseAggregationTest2(String name) {
super(name);
aggregator = new NewAggregator();
aggregator.initialise();
}
@Before
public void setUp() {
super.setUp();
this.s1 = this.phraseFactory.createClause("John", "write",
this.phraseFactory.createNounPhrase("an", "article"));
this.s2 = this.phraseFactory.createClause("Mary", "edit",
this.phraseFactory.createNounPhrase("an", "article"));
this.s1.setFeature(Feature.PROGRESSIVE, true);
this.s2.setFeature(Feature.PROGRESSIVE, true);
this.s3 = this.phraseFactory.createClause("John", "read",
this.phraseFactory.createNounPhrase("a", "book"));
this.s4 = this.phraseFactory.createClause("Mary", "read",
this.phraseFactory.createNounPhrase("an", "article"));
this.s3.setFeature(Feature.TENSE, Tense.PAST);
this.s4.setFeature(Feature.TENSE, Tense.PAST);
this.s3.setFeature(Feature.PROGRESSIVE, true);
this.s4.setFeature(Feature.PROGRESSIVE, true);
}
@Override
@After
public void tearDown() {
super.tearDown();
this.aggregator = null;
this.s1 = null; this.s2 = null; this.s3 = null; this.s4 = null;
}
//Functionality disabled for the time being
// @Test
// public void testConjReduction1() {
// NLGElement element = this.aggregator.realise(this.s1, this.s2);
// Assert.assertEquals("John is writing and Mary is editing an article",
// this.realiser.realise(element).getRealisation());
// }
//
// @Test
// public void testConjReduction2() {
// this.s1.setFeature(Feature.PASSIVE, true);
// this.s2.setFeature(Feature.PASSIVE, true);
// NLGElement element = this.aggregator.realise(this.s1, this.s2);
// Assert.assertEquals("John is writing and Mary is editing an article",
// this.realiser.realise(element).getRealisation());
// }
//
// @Test
// public void testConjReduction3() {
// NLGElement element = this.aggregator.realise(this.s3, this.s4);
// Assert.assertEquals("John was reading a book and Mary an article",
// this.realiser.realise(element).getRealisation());
// }
}
......@@ -849,5 +849,78 @@ public class ClauseTest extends SimpleNLG4Test {
"however, surfers are carried to the shore by waves", this.realiser //$NON-NLS-1$
.realise(_s1).getRealisation());
}
/**
* Check that setComplement replaces earlier complements
*/
@Test
public void setComplementTest() {
SPhraseSpec s = this.phraseFactory.createClause();
s.setSubject("I");
s.setVerb("see");
s.setObject("a dog");
Assert.assertEquals("I see a dog", this.realiser.realise(s)
.getRealisation());
s.setObject("a cat");
Assert.assertEquals("I see a cat", this.realiser.realise(s)
.getRealisation());
s.setObject("a wolf");
Assert.assertEquals("I see a wolf", this.realiser.realise(s)
.getRealisation());
}
/**
* Test for subclauses involving WH-complements Based on a query by Owen
* Bennett
*/
@Test
public void subclausesTest() {
// Once upon a time, there was an Accountant, called Jeff, who lived in
// a forest.
// main sentence
NPPhraseSpec acct = this.phraseFactory.createNounPhrase("a",
"accountant");
// first postmodifier of "an accountant"
VPPhraseSpec sub1 = this.phraseFactory.createVerbPhrase("call");
sub1.addComplement("Jeff");
sub1.setFeature(Feature.FORM, Form.PAST_PARTICIPLE);
// this is an appositive modifier, which makes simplenlg put it between
// commas
sub1.setFeature(Feature.APPOSITIVE, true);
acct.addPostModifier(sub1);
// second postmodifier of "an accountant" is "who lived in a forest"
SPhraseSpec sub2 = this.phraseFactory.createClause();
VPPhraseSpec subvp = this.phraseFactory.createVerbPhrase("live");
subvp.setFeature(Feature.TENSE, Tense.PAST);
subvp.setComplement(this.phraseFactory.createPrepositionPhrase("in",
this.phraseFactory.createNounPhrase("a", "forest")));
sub2.setVerbPhrase(subvp);
// simplenlg can't yet handle wh-clauses in NPs, so we need to hack it
// by setting the subject to "who"
sub2.setSubject("who");
acct.addPostModifier(sub2);
// main sentence
SPhraseSpec s = this.phraseFactory.createClause("there", "be", acct);
s.setFeature(Feature.TENSE, Tense.PAST);
// add front modifier "once upon a time"
s.addFrontModifier("once upon a time");
Assert.assertEquals(
"once upon a time there was an accountant, called Jeff, who lived in a forest",
this.realiser.realise(s).getRealisation());
}
}
......@@ -50,6 +50,23 @@ public class CoordinationTest extends SimpleNLG4Test {
public void tearDown() {
super.tearDown();
}
/**
* Check that empty coordinate phrases are not realised as "null"
*/
@Test
public void emptyCoordinationTest() {
// first a simple phrase with no coordinates
CoordinatedPhraseElement coord = this.phraseFactory
.createCoordinatedPhrase();
Assert.assertEquals("", this.realiser.realise(coord).getRealisation());
// now one with a premodifier and nothing else
coord.addPreModifier(this.phraseFactory.createAdjectivePhrase("nice"));
Assert.assertEquals("nice", this.realiser.realise(coord)
.getRealisation());
}
/**
* Test pre and post-modification of coordinate VPs inside a sentence.
......
......@@ -31,7 +31,10 @@ import simplenlg.framework.LexicalCategory;
import simplenlg.framework.NLGElement;
import simplenlg.framework.NLGFactory;
import simplenlg.framework.PhraseElement;
import simplenlg.phrasespec.AdvPhraseSpec;
import simplenlg.phrasespec.NPPhraseSpec;
import simplenlg.phrasespec.SPhraseSpec;
import simplenlg.phrasespec.VPPhraseSpec;
/**
* Tests that check that realization of different Features against NLGElements.
......@@ -328,5 +331,38 @@ public class FeatureTest extends SimpleNLG4Test {
}
/**
* Test for repetition of the future auxiliary "will", courtesy of Luxor
* Vlonjati
*/
@Test
public void testFutureTense() {
SPhraseSpec test = this.phraseFactory.createClause();
NPPhraseSpec subj = this.phraseFactory.createNounPhrase("I");
VPPhraseSpec verb = this.phraseFactory.createVerbPhrase("go");
AdvPhraseSpec adverb = this.phraseFactory
.createAdverbPhrase("tomorrow");
test.setSubject(subj);
test.setVerbPhrase(verb);
test.setFeature(Feature.TENSE, Tense.FUTURE);
test.addPostModifier(adverb);
String sentence = realiser.realiseSentence(test);
Assert.assertEquals("I will go tomorrow.", sentence);
SPhraseSpec test2 = this.phraseFactory.createClause();
NLGElement vb = this.phraseFactory.createWord("go", LexicalCategory.VERB);
test2.setSubject(subj);
test2.setVerb(vb);
test2.setFeature(Feature.TENSE, Tense.FUTURE);
test2.addPostModifier(adverb);
String sentence2 = realiser.realiseSentence(test);
Assert.assertEquals("I will go tomorrow.", sentence2);
}
}
......@@ -34,8 +34,11 @@ import simplenlg.framework.LexicalCategory;
import simplenlg.framework.NLGElement;
import simplenlg.framework.NLGFactory;
import simplenlg.framework.PhraseElement;
import simplenlg.lexicon.Lexicon;
import simplenlg.phrasespec.NPPhraseSpec;
import simplenlg.phrasespec.PPPhraseSpec;
import simplenlg.phrasespec.SPhraseSpec;
import simplenlg.realiser.english.Realiser;
/**
* JUnit test case for interrogatives.
......@@ -67,16 +70,7 @@ public class InterrogativeTest extends SimpleNLG4Test {
@Before
protected void setUp() {
super.setUp();
// the woman kissed the man behind the curtain
// // there is the dog on the rock
// this.s2 = new SPhraseSpec();
// this.s2.setSubject("there");
// this.s2.setHead("be");
// this.s2.setComplement(this.dog);
// this.s2.addModifier(SModifierPosition.POST_VERB, this.onTheRock);
//
// // the man gives the woman John's flower
PhraseElement john = this.phraseFactory.createNounPhrase("John"); //$NON-NLS-1$
john.setFeature(Feature.POSSESSIVE, true);
......@@ -788,7 +782,7 @@ public class InterrogativeTest extends SimpleNLG4Test {
}
/*
/**
* Test a simple "how" question, based on query from Albi Oxa
*/
@Test
......@@ -809,4 +803,34 @@ public class InterrogativeTest extends SimpleNLG4Test {
Assert.assertEquals("How are you?", result);
}
/**
* Case 1 checks that "What do you think about John?" can be generated.
*
* Case 2 checks that the same clause is generated, even when an object is
* declared.
*/
@Test
public void testWhatObjectInterrogative() {
Lexicon lexicon = Lexicon.getDefaultLexicon();
NLGFactory nlg = new NLGFactory(lexicon);
Realiser realiser = new Realiser(lexicon);
// Case 1, no object is explicitly given:
SPhraseSpec clause = nlg.createClause("you", "think");
PPPhraseSpec aboutJohn = nlg.createPrepositionPhrase("about", "John");
clause.addPostModifier(aboutJohn);
clause.setFeature(Feature.INTERROGATIVE_TYPE,
InterrogativeType.WHAT_OBJECT);
String realisation = realiser.realiseSentence(clause);
System.out.println(realisation);
Assert.assertEquals("What do you think about John?", realisation);
// Case 2:
// Add "bad things" as the object so the object doesn't remain null:
clause.setObject("bad things");
realisation = realiser.realiseSentence(clause);
System.out.println(realiser.realiseSentence(clause));
Assert.assertEquals("What do you think about John?", realisation);
}
}
......@@ -51,6 +51,17 @@ public class PhraseSpecTest extends SimpleNLG4Test {
super.tearDown();
}
/**
* Check that empty phrases are not realised as "null"
*/
@Test
public void emptyPhraseRealisationTest() {
SPhraseSpec emptyClause = this.phraseFactory.createClause();
Assert.assertEquals("", this.realiser.realise(emptyClause)
.getRealisation());
}
/**
* Test SPhraseSpec
*/
......
......@@ -14,9 +14,10 @@
* The Initial Developer of the Original Code is Ehud Reiter, Albert Gatt and Dave Westwater.
* Portions created by Ehud Reiter, Albert Gatt and Dave Westwater are Copyright (C) 2010-11 The University of Aberdeen. All Rights Reserved.
*
* Contributor(s): Ehud Reiter, Albert Gatt, Dave Wewstwater, Roman Kutlak, Margaret Mitchell.
* Contributor(s): Ehud Reiter, Albert Gatt, Dave Westwater, Roman Kutlak, Margaret Mitchell, and Saad Mahamood.
*/
package simplenlg.external;
package simplenlg.syntax.english;
import junit.framework.Assert;
......@@ -24,11 +25,7 @@ import org.junit.Before;
import org.junit.Test;
import simplenlg.features.Feature;
import simplenlg.features.Form;
import simplenlg.features.Tense;
import simplenlg.framework.CoordinatedPhraseElement;
import simplenlg.framework.LexicalCategory;
import simplenlg.framework.NLGElement;
import simplenlg.framework.NLGFactory;
import simplenlg.lexicon.Lexicon;
import simplenlg.phrasespec.AdvPhraseSpec;
......@@ -36,14 +33,14 @@ import simplenlg.phrasespec.NPPhraseSpec;
import simplenlg.phrasespec.SPhraseSpec;
import simplenlg.phrasespec.VPPhraseSpec;
import simplenlg.realiser.english.Realiser;
/**
* Further tests from third parties
*
* @author Albert Gatt, University of Malta and University of Aberdeen
* {@link PremodifierTest} contains a series of JUnit test cases for Premodifiers.
*
* @author Saad Mahamood
*/
public class External2Test {
public class PremodifierTest {
private Lexicon lexicon = null;
private NLGFactory phraseFactory = null;
private Realiser realiser = null;
......@@ -55,31 +52,7 @@ public class External2Test {
realiser = new Realiser(lexicon);
}
/**
* Check that empty phrases are not realised as "null"
*/
@Test
public void emptyPhraseRealisationTest() {
SPhraseSpec emptyClause = this.phraseFactory.createClause();
Assert.assertEquals("", this.realiser.realise(emptyClause)
.getRealisation());
}
/**
* Check that empty coordinate phrases are not realised as "null"
*/
@Test
public void emptyCoordinationTest() {
// first a simple phrase with no coordinates
CoordinatedPhraseElement coord = this.phraseFactory
.createCoordinatedPhrase();
Assert.assertEquals("", this.realiser.realise(coord).getRealisation());
// now one with a premodifier and nothing else
coord.addPreModifier(this.phraseFactory.createAdjectivePhrase("nice"));
Assert.assertEquals("nice", this.realiser.realise(coord)
.getRealisation());
}
/**
* Test change from "a" to "an" in the presence of a premodifier with a
......@@ -138,187 +111,4 @@ public class External2Test {
.realise(vp2).getRealisation());
}
// public void participleModifierTest() {
//
// String verb = "associate";
// VPPhraseSpec adjP = this.phraseFactory.createVerbPhrase(verb);
// adjP.setFeature(Feature.TENSE, Tense.PAST);
//
// NPPhraseSpec np = this.phraseFactory.createNounPhrase("a", "thrombus");
// np.addPreModifier(adjP);
// String realised = this.realiser.realise(np).getRealisation();
// System.out.println(realised);
// // cch TESTING The following line doesn't work when the lexeme is a
// // verb.
// // morphP.preMod.Add(new AdjPhraseSpec((Lexeme)modifier));
//
// // It doesn't work for verb "associate" as adjective past participle.
// // Instead of realizing as "associated" it realizes as "ed".
// // Need to use verb phrase.
//
// // cch TODO : handle general case making phrase type corresponding to
// // lexeme category and usage.
// }
/**
* Check that setComplement replaces earlier complements
*/
@Test
public void setComplementTest() {
SPhraseSpec s = this.phraseFactory.createClause();
s.setSubject("I");
s.setVerb("see");
s.setObject("a dog");
Assert.assertEquals("I see a dog", this.realiser.realise(s)
.getRealisation());
s.setObject("a cat");
Assert.assertEquals("I see a cat", this.realiser.realise(s)
.getRealisation());
s.setObject("a wolf");
Assert.assertEquals("I see a wolf", this.realiser.realise(s)
.getRealisation());
}
/**
* Test for subclauses involving WH-complements Based on a query by Owen
* Bennett
*/
@Test
public void subclausesTest() {
// Once upon a time, there was an Accountant, called Jeff, who lived in
// a forest.
// main sentence
NPPhraseSpec acct = this.phraseFactory.createNounPhrase("a",
"accountant");
// first postmodifier of "an accountant"
VPPhraseSpec sub1 = this.phraseFactory.createVerbPhrase("call");
sub1.addComplement("Jeff");
sub1.setFeature(Feature.FORM, Form.PAST_PARTICIPLE);
// this is an appositive modifier, which makes simplenlg put it between
// commas
sub1.setFeature(Feature.APPOSITIVE, true);
acct.addPostModifier(sub1);
// second postmodifier of "an accountant" is "who lived in a forest"
SPhraseSpec sub2 = this.phraseFactory.createClause();
VPPhraseSpec subvp = this.phraseFactory.createVerbPhrase("live");
subvp.setFeature(Feature.TENSE, Tense.PAST);
subvp.setComplement(this.phraseFactory.createPrepositionPhrase("in",
this.phraseFactory.createNounPhrase("a", "forest")));
sub2.setVerbPhrase(subvp);
// simplenlg can't yet handle wh-clauses in NPs, so we need to hack it
// by setting the subject to "who"
sub2.setSubject("who");
acct.addPostModifier(sub2);
// main sentence
SPhraseSpec s = this.phraseFactory.createClause("there", "be", acct);
s.setFeature(Feature.TENSE, Tense.PAST);
// add front modifier "once upon a time"
s.addFrontModifier("once upon a time");
Assert.assertEquals(
"once upon a time there was an accountant, called Jeff, who lived in a forest",
this.realiser.realise(s).getRealisation());
}
/**
* Test for repetition of the future auxiliary "will", courtesy of Luxor
* Vlonjati
*/
@Test
public void futureTenseTest() {
SPhraseSpec test = this.phraseFactory.createClause();
NPPhraseSpec subj = this.phraseFactory.createNounPhrase("I");
VPPhraseSpec verb = this.phraseFactory.createVerbPhrase("go");
AdvPhraseSpec adverb = this.phraseFactory
.createAdverbPhrase("tomorrow");
test.setSubject(subj);
test.setVerbPhrase(verb);
test.setFeature(Feature.TENSE, Tense.FUTURE);
test.addPostModifier(adverb);
String sentence = realiser.realiseSentence(test);
Assert.assertEquals("I will go tomorrow.", sentence);
SPhraseSpec test2 = this.phraseFactory.createClause();
NLGElement vb = this.phraseFactory.createWord("go", LexicalCategory.VERB);
test2.setSubject(subj);
test2.setVerb(vb);
test2.setFeature(Feature.TENSE, Tense.FUTURE);
test2.addPostModifier(adverb);
String sentence2 = realiser.realiseSentence(test);
Assert.assertEquals("I will go tomorrow.", sentence2);
}
/**
* Tests that no empty space is added when a StringElement is instantiated with an empty string
* or null object.
*/
@Test
public void nullAndEmptyStringElementTest() {
NLGElement nullStringElement = this.phraseFactory.createStringElement(null);
NLGElement emptyStringElement = this.phraseFactory.createStringElement("");
NLGElement beautiful = this.phraseFactory.createStringElement("beautiful");
NLGElement horseLike = this.phraseFactory.createStringElement("horse-like");
NLGElement creature = this.phraseFactory.createStringElement("creature");