Spanish SimpleNLG v1.0

parent 701004bf
......@@ -18,146 +18,150 @@
*/
package simplenlg.aggregation;
import java.util.ArrayList;
import java.util.List;
import simplenlg.framework.CoordinatedPhraseElement;
import simplenlg.framework.NLGElement;
import simplenlg.framework.NLGFactory;
import java.util.ArrayList;
import java.util.List;
/**
* This class represents an aggregation rule. All such rules need to implement
* an {@link #apply(NLGElement, NLGElement)} which takes an arbitrary number of
* {@link simplenlg.framework.NLGElement}s and perform some form of aggregation
* on them, returning an <code>SPhraseSpec</code> as a result, or
* <code>null</code> if the operation fails.
*
*
* @author Albert Gatt, University of Malta and University of Aberdeen
*
*/
public abstract class AggregationRule {
protected NLGFactory factory;
/**
* Creates a new instance of AggregationRule
*/
public AggregationRule() {
this.factory = new NLGFactory();
}
/**
* Set the factory that the rule should use to create phrases.
*
* @param factory
* the factory
*/
public void setFactory(NLGFactory factory) {
this.factory = factory;
}
/**
*
* @return the factory being used by this rule to create phrases
*/
public NLGFactory getFactory() {
return this.factory;
}
/**
* Performs aggregation on an arbitrary number of elements in a list. This
* method calls {{@link #apply(NLGElement, NLGElement)} on all pairs of
* elements in the list, recursively aggregating whenever it can.
*
* @param phrases
* the sentences
* @return a list containing the phrases, such that, for any two phrases s1
* and s2, if {@link #apply(NLGElement, NLGElement)} succeeds on s1
* and s2, the list contains the result; otherwise, the list
* contains s1 and s2.
*/
public List<NLGElement> apply(List<NLGElement> phrases) {
List<NLGElement> results = new ArrayList<NLGElement>();;
if (phrases.size() >= 2) {
List<NLGElement> removed = new ArrayList<NLGElement>();
for (int i = 0; i < phrases.size(); i++) {
NLGElement current = phrases.get(i);
if (removed.contains(current)) {
continue;
}
for (int j = i + 1; j < phrases.size(); j++) {
NLGElement next = phrases.get(j);
NLGElement aggregated = apply(current, next);
if (aggregated != null) {
current = aggregated;
removed.add(next);
}
}
results.add(current);
}
} else if(phrases.size() == 1) {
results.add(apply(phrases.get(0)));
}
return results;
}
/**
* Perform aggregation on a single phrase. This method only works on a
* {@link simplenlg.framework.CoordinatedPhraseElement}, in which case it
* calls {@link #apply(List)} on the children of the coordinated phrase,
* returning a coordinated phrase whose children are the result.
*
* @param phrase
* @return aggregated result
*/
public NLGElement apply(NLGElement phrase) {
NLGElement result = null;
if (phrase instanceof CoordinatedPhraseElement) {
List<NLGElement> children = ((CoordinatedPhraseElement) phrase).getChildren();
List<NLGElement> aggregated = apply(children);
if(aggregated.size() == 1) {
result = aggregated.get(0);
} else {
result = this.factory.createCoordinatedPhrase();
for (NLGElement agg : aggregated) {
((CoordinatedPhraseElement) result).addCoordinate(agg);
}
}
}
if(result != null) {
for(String feature: phrase.getAllFeatureNames()) {
result.setFeature(feature, phrase.getFeature(feature));
}
}
return result;
}
/**
* Performs aggregation on a pair of sentences. This is the only method that
* extensions of <code>AggregationRule</code> need to implement.
*
* @param sentence1
* the first sentence
* @param sentence2
* the second sentence
* @return an aggregated sentence, if the method succeeds, <code>null</code>
* otherwise
*/
public abstract NLGElement apply(NLGElement sentence1, NLGElement sentence2);
protected NLGFactory factory;
/**
* Creates a new instance of AggregationRule
*/
public AggregationRule() {
this.factory = new NLGFactory();
}
/**
* @return the factory being used by this rule to create phrases
*/
public NLGFactory getFactory() {
return this.factory;
}
/**
* Set the factory that the rule should use to create phrases.
*
* @param factory the factory
*/
public void setFactory(NLGFactory factory) {
this.factory = factory;
}
/**
* Performs aggregation on an arbitrary number of elements in a list. This
* method calls {{@link #apply(NLGElement, NLGElement)} on all pairs of
* elements in the list, recursively aggregating whenever it can.
*
* @param phrases the sentences
* @return a list containing the phrases, such that, for any two phrases s1
* and s2, if {@link #apply(NLGElement, NLGElement)} succeeds on s1
* and s2, the list contains the result; otherwise, the list
* contains s1 and s2.
*/
public List<NLGElement> apply(List<NLGElement> phrases) {
List<NLGElement> results = new ArrayList<NLGElement>();
if (phrases.size() >= 2) {
List<NLGElement> removed = new ArrayList<NLGElement>();
for (int i = 0; i < phrases.size(); i++) {
NLGElement current = phrases.get(i);
if (removed.contains(current)) {
continue;
}
for (int j = i + 1; j < phrases.size(); j++) {
NLGElement next = phrases.get(j);
NLGElement aggregated = apply(current, next);
if (aggregated != null) {
current = aggregated;
removed.add(next);
}
}
results.add(current);
}
} else if (phrases.size() == 1) {
results.add(apply(phrases.get(0)));
}
return results;
}
/**
* Perform aggregation on a single phrase. This method only works on a
* {@link simplenlg.framework.CoordinatedPhraseElement}, in which case it
* calls {@link #apply(List)} on the children of the coordinated phrase,
* returning a coordinated phrase whose children are the result.
*
* @param phrase
* @return aggregated result
*/
public NLGElement apply(NLGElement phrase) {
NLGElement result = null;
if (phrase instanceof CoordinatedPhraseElement) {
List<NLGElement> children = ((CoordinatedPhraseElement) phrase).getChildren();
List<NLGElement> aggregated = apply(children);
if (aggregated.size() == 1) {
result = aggregated.get(0);
} else {
result = this.factory.createCoordinatedPhrase();
for (NLGElement agg : aggregated) {
((CoordinatedPhraseElement) result).addCoordinate(agg);
}
}
}
if (result != null) {
for (String feature : phrase.getAllFeatureNames()) {
result.setFeature(feature, phrase.getFeature(feature));
}
}
return result;
}
protected NLGFactory getFactory(NLGElement previous, NLGElement next) {
if (previous.getFactory() != null) {
return previous.getFactory();
} else if (next.getFactory() != null) {
return next.getFactory();
} else {
return this.factory;
}
}
/**
* Performs aggregation on a pair of sentences. This is the only method that
* extensions of <code>AggregationRule</code> need to implement.
*
* @param sentence1 the first sentence
* @param sentence2 the second sentence
* @return an aggregated sentence, if the method succeeds, <code>null</code>
* otherwise
*/
public abstract NLGElement apply(NLGElement sentence1, NLGElement sentence2);
}
......@@ -18,11 +18,11 @@
*/
package simplenlg.aggregation;
import java.util.List;
import simplenlg.framework.NLGElement;
import simplenlg.framework.PhraseCategory;
import java.util.List;
/**
* Implementation of the backward conjunction reduction rule. Given two
* sentences <code>s1</code> and <code>s2</code>, this rule elides any
......@@ -96,7 +96,7 @@ public class BackwardConjunctionReductionRule extends AggregationRule {
}
}
return success ? this.factory.createCoordinatedPhrase(previous, next)
return success ? getFactory(previous, next).createCoordinatedPhrase(previous, next)
: null;
}
......
......@@ -18,11 +18,11 @@
*/
package simplenlg.aggregation;
import java.util.List;
import simplenlg.framework.NLGElement;
import simplenlg.framework.PhraseCategory;
import java.util.List;
/**
* <P>
* Implementation of the forward conjunction rule. Given two sentences
......@@ -109,8 +109,8 @@ public class ForwardConjunctionReductionRule extends AggregationRule {
}
}
return success ? this.factory.createCoordinatedPhrase(previous, next)
: null;
}
return success ? getFactory(previous, next).createCoordinatedPhrase(previous, next)
: null;
}
}
......@@ -81,5 +81,12 @@ public enum Form {
* verb being used as a noun. The present participle remains as a verb. For
* example, <em>Jim was <b>sleeping</b></em>.
*/
PRESENT_PARTICIPLE;
PRESENT_PARTICIPLE,
/**
* Subjunctive forms of verbs are typically used to express various states
* of unreality such as wish, emotion, possibility, judgment, opinion,
* obligation, or action that has not yet occurred.
*/
SUBJUNCTIVE
}
......@@ -26,31 +26,35 @@ package simplenlg.features;
* recorded in the {@code Feature.PERSON} feature and applies to clauses,
* coordinated phrases, noun phrases and verb phrases.
* </p>
*
*
* @author A. Gatt, D. Westwater, University of Aberdeen.
* @version 4.0
*
*/
public enum Person {
/**
* The enumeration to show that the narration is written in the first
* person. First person narrative uses the personal pronouns of <em>I</em>
* and <em>we</em>.
*/
FIRST,
/**
* The enumeration to show that the narration is written in the first
* person. First person narrative uses the personal pronouns of <em>I</em>
* and <em>we</em>.
*/
FIRST,
/**
* The enumeration to show that the narration is written in the second
* person. Second person narrative uses the personal pronoun of <em>you</em>.
*/
SECOND,
/**
* The enumeration to show that the narration is written in the second
* person. Second person narrative uses the personal pronoun of <em>you</em>.
*/
SECOND,
/**
* The enumeration to show that the narration is written in the third
* person. Third person narrative uses the personal pronouns of <em>he</em>,
* <em>her</em> and <em>they</em>.
*/
THIRD,
/**
* The enumeration to show that the narration is written in the third
* person. Third person narrative uses the personal pronouns of <em>he</em>,
* <em>her</em> and <em>they</em>.
*/
THIRD;
/**
* The enumeration to show that the narration is impersonal.
*/
NONE
}
......@@ -25,30 +25,40 @@ package simplenlg.features;
* applied to a verb. The tense is recorded in the {@code Feature.TENSE} feature
* and applies to verbs and their associated phrases.
* </p>
*
*
*
* @author A. Gatt and D. Westwater, University of Aberdeen.
* @version 4.0
*
*/
public enum Tense {
/**
* The action described by the verb will happen in the future. For example,
* <em>John will kiss Mary</em>, <em>the dog will eat a bone</em>.
*/
FUTURE,
/**
* The action described by the verb happened in the past. For example,
* <em>John kissed Mary</em>, <em>the dog ate a bone</em>.
*/
PAST,
/**
* The action described by the verb is happening in the present time. For
* example, <em>John kisses Mary</em>, <em>the dog eats a bone</em>.
*/
PRESENT;
/**
* The action described by the verb will happen in the future. For example,
* <em>John will kiss Mary</em>, <em>the dog will eat a bone</em>.
*/
FUTURE,
/**
* The action described by the verb happened in the past. For example,
* <em>John kissed Mary</em>, <em>the dog ate a bone</em>.
*/
PAST,
/**
* The action described by the verb is happening in the present time. For
* example, <em>John kisses Mary</em>, <em>the dog eats a bone</em>.
*/
PRESENT,
/**
* The action described by the verb expresses a hypothetical state of affairs,
* or an uncertain event, that is contingent on another set of circumstances.
*/
CONDITIONAL,
/**
* The action described by the verb combines past tense (reference to a past time)
* and imperfective aspect (reference to a continuing or repeated event or state)
*/
IMPERFECT
}
This diff is collapsed.
......@@ -18,11 +18,11 @@
*/
package simplenlg.framework;
import java.util.List;
import simplenlg.features.InternalFeature;
import simplenlg.features.LexicalFeature;
import java.util.List;
/**
* <p>
* This class defines the <code>NLGElement</code> that is used to represent an
......@@ -31,98 +31,96 @@ import simplenlg.features.LexicalFeature;
* <em>eat</em>) and for setting and retrieving the base word. The base word is
* a <code>WordElement</code> constructed by the lexicon.
* </p>
*
*
* @author D. Westwater, University of Aberdeen.
* @version 4.0
*
*/
public class InflectedWordElement extends NLGElement {
/**
* Constructs a new inflected word using the giving word as the base form.
* Constructing the word also requires a lexical category (such as noun,
* verb).
*
* @param word
* the base form for this inflected word.
* @param category
* the lexical category for the word.
*/
public InflectedWordElement(String word, LexicalCategory category) {
super();
setFeature(LexicalFeature.BASE_FORM, word);
setCategory(category);
}
/**
* Constructs a new inflected word using the giving word as the base form.
* Constructing the word also requires a lexical category (such as noun,
* verb).
*
* @param word the base form for this inflected word.
* @param category the lexical category for the word.
*/
public InflectedWordElement(String word, LexicalCategory category) {
super();
setFeature(LexicalFeature.BASE_FORM, word);
setCategory(category);
}
/**
* Constructs a new inflected word from a WordElement
*
* @param word
* underlying wordelement
*/
public InflectedWordElement(WordElement word) {
super();
setFeature(InternalFeature.BASE_WORD, word);
// AG: changed to use the default spelling variant
// setFeature(LexicalFeature.BASE_FORM, word.getBaseForm());
String defaultSpelling = word.getDefaultSpellingVariant();
setFeature(LexicalFeature.BASE_FORM, defaultSpelling);
setCategory(word.getCategory());
}
/**
* Constructs a new inflected word from a WordElement
*
* @param word underlying wordelement
*/
public InflectedWordElement(WordElement word) {
super();
setFeature(InternalFeature.BASE_WORD, word);
// AG: changed to use the default spelling variant
// setFeature(LexicalFeature.BASE_FORM, word.getBaseForm());
String defaultSpelling = word.getDefaultSpellingVariant();
setFeature(LexicalFeature.BASE_FORM, defaultSpelling);
setCategory(word.getCategory());
if (word.getParent() != null) {
setParent(word.getParent());
}
}
/**
* This method returns null as the inflected word has no child components.
*/
@Override
public List<NLGElement> getChildren() {
return null;
}
/**
* This method returns null as the inflected word has no child components.
*/
@Override
public List<NLGElement> getChildren() {
return null;
}
@Override
public String toString() {
return "InflectedWordElement[" + getBaseForm() + ':' //$NON-NLS-1$
+ getCategory().toString() + ']';
}
@Override
public String toString() {
return "InflectedWordElement[" + getBaseForm() + ':' //$NON-NLS-1$
+ getCategory().toString() + ']';
}
@Override
public String printTree(String indent) {
StringBuffer print = new StringBuffer();
print.append("InflectedWordElement: base=").append(getBaseForm()) //$NON-NLS-1$
.append(", category=").append(getCategory().toString()).append( //$NON-NLS-1$
", ").append(super.toString()).append('\n'); //$NON-NLS-1$
return print.toString();
}
@Override
public String printTree(String indent) {
StringBuffer print = new StringBuffer();
print.append("InflectedWordElement: base=").append(getBaseForm()) //$NON-NLS-1$
.append(", category=").append(getCategory().toString()).append( //$NON-NLS-1$
", ").append(super.toString()).append('\n'); //$NON-NLS-1$
return print.toString();
}
/**
* Retrieves the base form for this element. The base form is the originally
* supplied word.
*
* @return a <code>String</code> forming the base form of the element.
*/
public String getBaseForm() {
return getFeatureAsString(LexicalFeature.BASE_FORM);
}
/**
* Retrieves the base form for this element. The base form is the originally
* supplied word.
*
* @return a <code>String</code> forming the base form of the element.
*/
public String getBaseForm() {
return getFeatureAsString(LexicalFeature.BASE_FORM);
}
/**
* Sets the base word for this element.
*
* @param word
* the <code>WordElement</code> representing the base word as
* read from the lexicon.
*/
public void setBaseWord(WordElement word) {
setFeature(InternalFeature.BASE_WORD, word);
}
/**
* Retrieves the base word for this element.
*
* @return the <code>WordElement</code> representing the base word as read
* from the lexicon.
*/
public WordElement getBaseWord() {
NLGElement baseWord = this