Last updates for natural language generation (added comments, new options, cleaned...
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / DefaultQuantitativeDescriptionBuilder.java
1 package eu.etaxonomy.cdm.api.service;
2
3 import java.util.List;
4 import java.util.Map;
5
6 import org.apache.commons.lang.StringUtils;
7
8 import eu.etaxonomy.cdm.model.common.Language;
9 import eu.etaxonomy.cdm.model.description.Feature;
10 import eu.etaxonomy.cdm.model.description.MeasurementUnit;
11 import eu.etaxonomy.cdm.model.description.NaturalLanguageTerm;
12 import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
13 import eu.etaxonomy.cdm.model.description.TextData;
14 import eu.etaxonomy.cdm.model.description.TextFormat;
15
16 public class DefaultQuantitativeDescriptionBuilder extends AbstractQuantitativeDescriptionBuilder {
17
18
19 @Override
20 protected TextData doBuild(Map<StatisticalMeasure,Float> measures, MeasurementUnit mUnit, List<Language> languages){
21 StringBuilder QuantitativeDescription = new StringBuilder(); // this StringBuilder is used to concatenate the different words of the description before saving it in the TextData
22 TextData textData = TextData.NewInstance(); // TextData that will contain the description and the language corresponding
23 // booleans indicating whether a kind of value is present or not and the float that will eventually hold the value
24
25 String unit = "";
26 if ((mUnit!=null)&&(mUnit.getLabel()!=null)){
27 unit = mUnit.getLabel();
28 }
29
30 // the different linking words are taken from NaturalLanguageTerm.class (should this be changed ?)
31 NaturalLanguageTerm nltFrom = NaturalLanguageTerm.FROM();
32 String from = nltFrom.getPreferredRepresentation(languages).getLabel();
33 NaturalLanguageTerm nltTo = NaturalLanguageTerm.TO();
34 String to = nltTo.getPreferredRepresentation(languages).getLabel();
35 NaturalLanguageTerm nltUp_To = NaturalLanguageTerm.UP_TO();
36 String up_To = nltUp_To.getPreferredRepresentation(languages).getLabel();
37 NaturalLanguageTerm nltMost_Frequently = NaturalLanguageTerm.MOST_FREQUENTLY();
38 String most_Frequently = nltMost_Frequently.getPreferredRepresentation(languages).getLabel();
39 NaturalLanguageTerm nltOn_Average = NaturalLanguageTerm.ON_AVERAGE();
40 String on_Average = nltOn_Average.getPreferredRepresentation(languages).getLabel();
41 NaturalLanguageTerm nltMore_Or_Less = NaturalLanguageTerm.MORE_OR_LESS();
42 String more_Or_Less = nltMore_Or_Less.getPreferredRepresentation(languages).getLabel();
43 String space = " "; // should "space" be considered as a linking word and thus be stored in NaturalLanguageTerm.class ?
44
45 // the booleans and floats are updated according to the presence or absence of values
46
47 Boolean max, min, upperb, lowerb, average, sd;
48
49 String averagevalue = getValue(measures,StatisticalMeasure.AVERAGE());
50 if (averagevalue!=null) average=true; else average=false;
51 String sdvalue = getValue(measures,StatisticalMeasure.STANDARD_DEVIATION());
52 if (sdvalue!=null) sd=true; else sd=false;
53 String minvalue = getValue(measures,StatisticalMeasure.MIN());
54 if (minvalue!=null) min=true; else min=false;
55 String maxvalue = getValue(measures,StatisticalMeasure.MAX());
56 if (maxvalue!=null) max=true; else max=false;
57 String lowerbvalue = getValue(measures,StatisticalMeasure.TYPICAL_LOWER_BOUNDARY());
58 if (lowerbvalue!=null) lowerb=true; else lowerb=false;
59 String upperbvalue = getValue(measures,StatisticalMeasure.TYPICAL_UPPER_BOUNDARY());
60 if (upperbvalue!=null) upperb=true; else upperb=false;
61
62
63 // depending on the different associations of values, a sentence is built
64 if (max && min) {
65 QuantitativeDescription.append(space + from + space + minvalue + space + to + space + maxvalue + space + unit);
66 }
67 else if (min) {
68 QuantitativeDescription.append(space + from + space + minvalue + space + unit);
69 }
70 else if (max) {
71 QuantitativeDescription.append(space + up_To + space + maxvalue + space + unit);
72 }
73 if ((max||min)&&(lowerb||upperb)) {
74 QuantitativeDescription.append(","); // merge with below ?
75 }
76 if ((lowerb||upperb)&&(min||max)) {
77 QuantitativeDescription.append(space + most_Frequently);
78 }
79 if (upperb && lowerb) {
80 QuantitativeDescription.append(space + from + space + lowerbvalue + space + to + space + upperbvalue + space + unit);
81 }
82 else if (lowerb) {
83 QuantitativeDescription.append(space + from + space + lowerbvalue + space + unit);
84 }
85 else if (upperb) {
86 QuantitativeDescription.append(space + up_To + space + upperbvalue + space + unit);
87 }
88 if (((max||min)&&(average))||((lowerb||upperb)&&(average))) {
89 QuantitativeDescription.append(",");
90 }
91 if (average) {
92 QuantitativeDescription.append(space + averagevalue + space + unit + space + on_Average);
93 if (sd) {
94 QuantitativeDescription.append("("+ more_Or_Less + space + sdvalue + ")");
95 }
96 }
97 textData.putText(QuantitativeDescription.toString(), languages.get(0)); // which language should be put here ?
98 textData.setFormat(TextFormat.NewInstance(null, "HTML",null )); // the data format is set (not yet real HTML)
99
100 return textData;
101 }
102
103 private String getValue(Map<StatisticalMeasure,Float> measures, Object key) {
104 Float floatValue;
105 Integer intValue;
106 if(measures.containsKey(key)) {
107 floatValue = measures.get(key);
108 intValue=floatValue.intValue();
109 if (floatValue.equals(intValue.floatValue())) return intValue.toString();
110 else return floatValue.toString();
111 }
112 else return null;
113 }
114
115 protected String buildFeature(Feature feature, boolean doItBetter){
116 if (feature==null || feature.getLabel()==null) return "";
117 else {
118 if (doItBetter) { // remove the text between brackets
119 String str= feature.getLabel();
120 StringBuilder strbuilder = new StringBuilder();
121 do {
122 strbuilder.append(StringUtils.substringBefore(str, "<"));
123 }
124 while (!(str=StringUtils.substringAfter(str, ">")).equals(""));
125 return StringUtils.substringBeforeLast(strbuilder.toString()," ");
126 }
127 else{
128 String betterString = StringUtils.replaceChars(feature.getLabel(), "<>",""); // only remove the brackets
129 return betterString;
130 // return StringUtils.substringBeforeLast(betterString," ");
131 }
132 }
133 }
134
135
136 }