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
|
@SuppressWarnings("unused")
|
43
|
private static final Logger logger = Logger.getLogger(NaturalLanguageGeneratorTest.class);
|
44
|
|
45
|
@SpringBeanByType
|
46
|
private INaturalLanguageGenerator generator;
|
47
|
|
48
|
private FeatureTree featureTree;
|
49
|
private TaxonDescription description;
|
50
|
Set<Feature> featureSet = new HashSet<Feature>();
|
51
|
|
52
|
@Before
|
53
|
public void setUp() throws Exception {
|
54
|
// set up your test objects here
|
55
|
|
56
|
NonViralName tnb = NonViralName.NewInstance(null);
|
57
|
Taxon taxon = Taxon.NewInstance(tnb, null);
|
58
|
description = TaxonDescription.NewInstance(taxon);
|
59
|
|
60
|
featureTree= FeatureTree.NewInstance();
|
61
|
FeatureNode root = FeatureNode.NewInstance();
|
62
|
featureTree.setRoot(root);
|
63
|
String[][][] tableStrings = { { {"a","b"} } , { { "a1" , "a2" } , { "b1" } } };
|
64
|
buildBranches(root,tableStrings,0,2,0);
|
65
|
for (Iterator<Feature> f = featureSet.iterator() ; f.hasNext() ;){
|
66
|
Feature feature = f.next();
|
67
|
CategoricalData cg = CategoricalData.NewInstance();
|
68
|
cg.setFeature(feature);
|
69
|
State state = State.NewInstance(null, feature.getLabel()+"state", null);
|
70
|
StateData stateData = StateData.NewInstance();
|
71
|
stateData.setState(state);
|
72
|
cg.addStateData(stateData);
|
73
|
description.addElement(cg);
|
74
|
}
|
75
|
Feature qFeature = Feature.NewInstance(null, "c", null);
|
76
|
QuantitativeData qd = QuantitativeData.NewInstance();
|
77
|
MeasurementUnit munit = MeasurementUnit.NewInstance(null, "mm", null);
|
78
|
StatisticalMeasurementValue smv = StatisticalMeasurementValue.NewInstance();
|
79
|
smv.setType(StatisticalMeasure.AVERAGE());
|
80
|
smv.setValue(12);
|
81
|
qd.addStatisticalValue(smv);
|
82
|
qd.setUnit(munit);
|
83
|
qd.setFeature(qFeature);
|
84
|
description.addElement(qd);
|
85
|
FeatureNode qNode = FeatureNode.NewInstance(qFeature);
|
86
|
root.addChild(qNode);
|
87
|
}
|
88
|
|
89
|
@Test
|
90
|
public void testGenerateNaturalLanguageDescription() {
|
91
|
assertNotNull("FeatureTree should exist", featureTree);
|
92
|
assertNotNull("TaxonDescription should exist", description);
|
93
|
StringBuilder stringBuilder = new StringBuilder();
|
94
|
List<TextData> result = generator.generateNaturalLanguageDescription(featureTree,description, Language.DEFAULT());
|
95
|
for (Iterator<TextData> td = result.iterator() ; td.hasNext();) {
|
96
|
TextData textD = td.next();
|
97
|
stringBuilder.append(textD.getText(Language.DEFAULT()));
|
98
|
}
|
99
|
assertTrue("Empty text",!stringBuilder.equals(""));
|
100
|
System.out.println(stringBuilder.toString());
|
101
|
}
|
102
|
|
103
|
public void buildBranches(FeatureNode parent, String[][][] children, int level, int depth, int nodeNumber) {
|
104
|
int i = nodeNumber;
|
105
|
int j;
|
106
|
for (j=0; j<children[level][i].length ; j++) {
|
107
|
Feature feature = Feature.NewInstance(null, children[level][i][j], null);
|
108
|
featureSet.add(feature);
|
109
|
FeatureNode child = FeatureNode.NewInstance(feature);
|
110
|
parent.addChild(child);
|
111
|
if (level<depth-1) {
|
112
|
buildBranches(child, children,level+1,depth, j);
|
113
|
}
|
114
|
}
|
115
|
|
116
|
}
|
117
|
|
118
|
}
|