1
|
package eu.etaxonomy.cdm.api.service;
|
2
|
|
3
|
import static org.junit.Assert.assertNotNull;
|
4
|
import static org.junit.Assert.assertEquals;
|
5
|
import static org.junit.Assert.assertTrue;
|
6
|
|
7
|
import java.util.HashSet;
|
8
|
import java.util.Iterator;
|
9
|
import java.util.List;
|
10
|
import java.util.Set;
|
11
|
|
12
|
import org.apache.log4j.Logger;
|
13
|
import org.junit.Before;
|
14
|
import org.junit.Ignore;
|
15
|
import org.junit.Test;
|
16
|
import org.unitils.spring.annotation.SpringBeanByType;
|
17
|
|
18
|
import eu.etaxonomy.cdm.model.common.Language;
|
19
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
20
|
import eu.etaxonomy.cdm.model.description.CategoricalData;
|
21
|
import eu.etaxonomy.cdm.model.description.Feature;
|
22
|
import eu.etaxonomy.cdm.model.description.FeatureTree;
|
23
|
import eu.etaxonomy.cdm.model.description.FeatureNode;
|
24
|
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
|
25
|
import eu.etaxonomy.cdm.model.description.QuantitativeData;
|
26
|
import eu.etaxonomy.cdm.model.description.State;
|
27
|
import eu.etaxonomy.cdm.model.description.StateData;
|
28
|
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
|
29
|
import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
|
30
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
31
|
import eu.etaxonomy.cdm.model.description.TextData;
|
32
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
33
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
34
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
35
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
36
|
import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest;
|
37
|
import eu.etaxonomy.cdm.api.service.NaturalLanguageGenerator;
|
38
|
|
39
|
@Ignore //FIXME Remove @Ignore once maximes code is completely comitted
|
40
|
public class NaturalLanguageGeneratorTest extends CdmIntegrationTest {
|
41
|
|
42
|
private static final Logger logger = Logger.getLogger(NaturalLanguageGeneratorTest.class);
|
43
|
|
44
|
@SpringBeanByType
|
45
|
private INaturalLanguageGenerator generator;
|
46
|
|
47
|
private FeatureTree featureTree;
|
48
|
private TaxonDescription description;
|
49
|
Set<Feature> featureSet = new HashSet<Feature>();
|
50
|
|
51
|
@Before
|
52
|
public void setUp() throws Exception {
|
53
|
// set up your test objects here
|
54
|
|
55
|
NonViralName tnb = NonViralName.NewInstance(null);
|
56
|
Taxon taxon = Taxon.NewInstance(tnb, null);
|
57
|
description = TaxonDescription.NewInstance(taxon);
|
58
|
|
59
|
featureTree= FeatureTree.NewInstance();
|
60
|
FeatureNode root = FeatureNode.NewInstance();
|
61
|
featureTree.setRoot(root);
|
62
|
String[][][] tableStrings = { { {"a","b"} } , { { "a1" , "a2" } , { "b1" } } };
|
63
|
buildBranches(root,tableStrings,0,2,0);
|
64
|
for (Iterator<Feature> f = featureSet.iterator() ; f.hasNext() ;){
|
65
|
Feature feature = f.next();
|
66
|
CategoricalData cg = CategoricalData.NewInstance();
|
67
|
cg.setFeature(feature);
|
68
|
State state = State.NewInstance(null, feature.getLabel()+"state", null);
|
69
|
StateData stateData = StateData.NewInstance();
|
70
|
stateData.setState(state);
|
71
|
cg.addState(stateData);
|
72
|
description.addElement(cg);
|
73
|
}
|
74
|
Feature qFeature = Feature.NewInstance(null, "c", null);
|
75
|
QuantitativeData qd = QuantitativeData.NewInstance();
|
76
|
MeasurementUnit munit = MeasurementUnit.NewInstance(null, "mm", null);
|
77
|
StatisticalMeasurementValue smv = StatisticalMeasurementValue.NewInstance();
|
78
|
smv.setType(StatisticalMeasure.AVERAGE());
|
79
|
smv.setValue(12);
|
80
|
qd.addStatisticalValue(smv);
|
81
|
qd.setUnit(munit);
|
82
|
qd.setFeature(qFeature);
|
83
|
description.addElement(qd);
|
84
|
FeatureNode qNode = FeatureNode.NewInstance(qFeature);
|
85
|
root.addChild(qNode);
|
86
|
}
|
87
|
|
88
|
@Test
|
89
|
public void testGenerateNaturalLanguageDescription() {
|
90
|
assertNotNull("FeatureTree should exist", featureTree);
|
91
|
assertNotNull("TaxonDescription should exist", description);
|
92
|
StringBuilder stringBuilder = new StringBuilder();
|
93
|
List<TextData> result = generator.generateNaturalLanguageDescription(featureTree,description, Language.DEFAULT());
|
94
|
for (Iterator<TextData> td = result.iterator() ; td.hasNext();) {
|
95
|
TextData textD = td.next();
|
96
|
stringBuilder.append(textD.getText(Language.DEFAULT()));
|
97
|
}
|
98
|
assertTrue("Empty text",!stringBuilder.equals(""));
|
99
|
System.out.println(stringBuilder.toString());
|
100
|
}
|
101
|
|
102
|
public void buildBranches(FeatureNode parent, String[][][] children, int level, int depth, int nodeNumber) {
|
103
|
int i = nodeNumber;
|
104
|
int j;
|
105
|
for (j=0; j<children[level][i].length ; j++) {
|
106
|
Feature feature = Feature.NewInstance(null, children[level][i][j], null);
|
107
|
featureSet.add(feature);
|
108
|
FeatureNode child = FeatureNode.NewInstance(feature);
|
109
|
parent.addChild(child);
|
110
|
if (level<depth-1) {
|
111
|
buildBranches(child, children,level+1,depth, j);
|
112
|
}
|
113
|
}
|
114
|
|
115
|
}
|
116
|
|
117
|
}
|