New classes for Natural language generation
[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.Iterator;
4 import java.util.Map;
5 import java.util.Set;
6
7 import eu.etaxonomy.cdm.model.common.Language;
8 import eu.etaxonomy.cdm.model.description.Feature;
9 import eu.etaxonomy.cdm.model.description.MeasurementUnit;
10 import eu.etaxonomy.cdm.model.description.NaturalLanguageTerm;
11 import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
12 import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
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 @Override
19 protected TextData doBuild(Map<StatisticalMeasure,Float> measures, MeasurementUnit mUnit){
20 StringBuilder QuantitativeDescription = new StringBuilder();
21 TextData textData = TextData.NewInstance();
22 Language language = Language.DEFAULT();
23
24 boolean average = false;
25 float averagevalue = new Float(0);
26 boolean sd = false;
27 float sdvalue = new Float(0);
28 boolean min = false;
29 float minvalue = new Float(0);
30 boolean max = false;
31 float maxvalue = new Float(0);
32 boolean lowerb = false;
33 float lowerbvalue = new Float(0);
34 boolean upperb = false;
35 float upperbvalue = new Float(0);
36
37 String unit = mUnit.getLabel();
38
39 NaturalLanguageTerm nltFrom = NaturalLanguageTerm.FROM();
40 String from = nltFrom.getPreferredRepresentation(language).getLabel();
41 NaturalLanguageTerm nltTo = NaturalLanguageTerm.TO();
42 String to = nltTo.getPreferredRepresentation(language).getLabel();
43 NaturalLanguageTerm nltUp_To = NaturalLanguageTerm.UP_TO();
44 String up_To = nltUp_To.getPreferredRepresentation(language).getLabel();
45 NaturalLanguageTerm nltMost_Frequently = NaturalLanguageTerm.MOST_FREQUENTLY();
46 String most_Frequently = nltMost_Frequently.getPreferredRepresentation(language).getLabel();
47 NaturalLanguageTerm nltOn_Average = NaturalLanguageTerm.ON_AVERAGE();
48 String on_Average = nltOn_Average.getPreferredRepresentation(language).getLabel();
49 NaturalLanguageTerm nltMore_Or_Less = NaturalLanguageTerm.MORE_OR_LESS();
50 String more_Or_Less = nltMore_Or_Less.getPreferredRepresentation(language).getLabel();
51 String space = " ";
52
53 if (measures.containsKey(StatisticalMeasure.AVERAGE())) {
54 average = true;
55 averagevalue = measures.get(StatisticalMeasure.AVERAGE());
56 } else if(measures.containsKey(StatisticalMeasure.STANDARD_DEVIATION())) {
57 sd = true;
58 sdvalue = measures.get(StatisticalMeasure.STANDARD_DEVIATION());
59 } else if (measures.containsKey(StatisticalMeasure.MIN())) {
60 min = true;
61 minvalue = measures.get(StatisticalMeasure.MIN());
62 } else if (measures.containsKey(StatisticalMeasure.MAX())) {
63 max = true;
64 maxvalue = measures.get(StatisticalMeasure.MAX());
65 } else if (measures.containsKey(StatisticalMeasure.TYPICAL_LOWER_BOUNDARY())) {
66 lowerb = true;
67 lowerbvalue = measures.get(StatisticalMeasure.TYPICAL_LOWER_BOUNDARY());
68 } else if (measures.containsKey(StatisticalMeasure.TYPICAL_UPPER_BOUNDARY())) {
69 upperb = true;
70 upperbvalue = measures.get(StatisticalMeasure.TYPICAL_UPPER_BOUNDARY());
71 }
72
73 if (max && min) {
74 QuantitativeDescription.append(space + from + space + minvalue + space + to + space + maxvalue + space + unit);
75 }
76 else if (min) {
77 QuantitativeDescription.append(space + from + space + minvalue + space + unit);
78 }
79 else if (max) {
80 QuantitativeDescription.append(space + up_To + space + maxvalue + space + unit);
81 }
82 if ((max||min)&&(lowerb||upperb)) {
83 QuantitativeDescription.append(","); // fusion avec dessous ?
84 }
85 if ((lowerb||upperb)&&(min||max)) {
86 QuantitativeDescription.append(space + most_Frequently + space);
87 }
88 if (upperb && lowerb) {
89 QuantitativeDescription.append(space + from + space + lowerbvalue + space + to + space + upperbvalue + space + unit);
90 }
91 else if (lowerb) {
92 QuantitativeDescription.append(space + from + lowerbvalue + space + unit);
93 }
94 else if (upperb) {
95 QuantitativeDescription.append(space + up_To + space + upperbvalue + space + unit);
96 }
97 if (((max||min)&&(average))||((lowerb||upperb)&&(average))) {
98 QuantitativeDescription.append(",");
99 }
100 if (average) {
101 QuantitativeDescription.append(space + averagevalue + space + unit + space + on_Average);
102 if (sd) {
103 QuantitativeDescription.append("("+ more_Or_Less + space + sdvalue + ")");
104 }
105 }
106 textData.putText(QuantitativeDescription.toString(), language);
107 textData.setFormat(TextFormat.NewInstance(null, "HTML",null ));
108
109 return textData;
110 }
111 }