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