1 | package tudelft.mentalhealth.motivatepersisting;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.util.Collections;
|
---|
5 | import java.util.LinkedList;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Locale;
|
---|
8 | import java.util.stream.Collectors;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * a motivational answer for some situation. This answer contans random aspects
|
---|
12 | * and is generated only once. Create a new object if you need a new answer.
|
---|
13 | */
|
---|
14 | public class MotivationalAnswer {
|
---|
15 | private final Situation situation;
|
---|
16 | private final String text;
|
---|
17 | private final ProbOfCategoriesPerSituation probabilities = ProbOfCategoriesPerSituation
|
---|
18 | .instance();
|
---|
19 | private final List<StatementSelection> selectedStatements;
|
---|
20 | private final Statements allStatements;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | *
|
---|
24 | * @param situation the situation of the patient.
|
---|
25 | * @param locale the language to use towards the patient. Currently
|
---|
26 | * supported are Locale("nl","NL") and Locale("en","").
|
---|
27 | */
|
---|
28 | public MotivationalAnswer(Situation situation, Locale locale) {
|
---|
29 | this.situation = situation;
|
---|
30 | this.allStatements = new Statements(locale);
|
---|
31 | this.selectedStatements = createStatements();
|
---|
32 | this.text = createText(selectedStatements);
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | *
|
---|
37 | * @return the ext for this motivational answer. This text will remain
|
---|
38 | * constant once the MotivationalAnswer has been created. Create a
|
---|
39 | * new MotivationalAnswer to get a new text.
|
---|
40 | */
|
---|
41 | public String getText() {
|
---|
42 | return text;
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public String toString() {
|
---|
47 | return " MotivationalAnswer(" + text + ")";
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | *
|
---|
52 | * @return the statements selected for this motivational answer.
|
---|
53 | */
|
---|
54 | public List<StatementSelection> getStatements() {
|
---|
55 | return Collections.unmodifiableList(selectedStatements);
|
---|
56 | }
|
---|
57 |
|
---|
58 | /***************** internals ***************/
|
---|
59 | /**
|
---|
60 | * @param stats the statements to be used for the text.
|
---|
61 | * @return text generated.
|
---|
62 | * @throws IOException
|
---|
63 | */
|
---|
64 | private String createText(List<StatementSelection> stats) {
|
---|
65 |
|
---|
66 | String text = "";
|
---|
67 | for (StatementSelection statement : stats) {
|
---|
68 | text = text + allStatements.getText(statement.getCategory(),
|
---|
69 | statement.getSubcategory()) + " ";
|
---|
70 | }
|
---|
71 |
|
---|
72 | return text;
|
---|
73 | }
|
---|
74 |
|
---|
75 | private List<StatementSelection> createStatements() {
|
---|
76 |
|
---|
77 | long nstatements = Math
|
---|
78 | .round(MeanNrStatements.instance().getMean(situation));
|
---|
79 | List<StatementSelection> selection = selectBasic();
|
---|
80 |
|
---|
81 | while (selection.size() < nstatements) {
|
---|
82 | List<Category> excludes = selection.stream()
|
---|
83 | .map(sel -> sel.getCategory()).collect(Collectors.toList());
|
---|
84 | selection.add(randomSelection(excludes));
|
---|
85 | }
|
---|
86 |
|
---|
87 | return selection;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Pick a random StatementSelection in a {@link Category} that is not yet
|
---|
92 | * selected,
|
---|
93 | *
|
---|
94 | * @param excludes the {@link Categories} excluded of selection
|
---|
95 | * @return random {@link StatementSelection}, excluding the excludes
|
---|
96 | */
|
---|
97 | protected StatementSelection randomSelection(List<Category> excludes) {
|
---|
98 | Category category = probabilities.getWeightedRandomCategory(excludes,
|
---|
99 | situation);
|
---|
100 | return new StatementSelection(category,
|
---|
101 | probabilities.selectSubcategory(category, situation));
|
---|
102 |
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * @return one Selection from each category that has p(ALL)>0.5
|
---|
107 | */
|
---|
108 | protected List<StatementSelection> selectBasic() {
|
---|
109 | List<StatementSelection> selection = new LinkedList<>();
|
---|
110 |
|
---|
111 | for (Category cat : Category.values()) {
|
---|
112 | if (probabilities.get(cat, Subcategory.ALL, situation) > 0.5) {
|
---|
113 | selection.add(new StatementSelection(cat,
|
---|
114 | probabilities.selectSubcategory(cat, situation)));
|
---|
115 | }
|
---|
116 | }
|
---|
117 | return selection;
|
---|
118 | }
|
---|
119 |
|
---|
120 | }
|
---|