source: trunk/autoquest-core-usability-evaluation/src/main/java/de/ugoe/cs/autoquest/usability/rules/metrics/TextInputEntryRepetitionsMetric.java @ 1217

Last change on this file since 1217 was 1217, checked in by adeicke, 11 years ago
  • Added proper formating and JavaDoc?.
  • Several renaming refactorings.
  • Property svn:mime-type set to text/plain
File size: 5.9 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.usability.rules.metrics;
16
17import static de.ugoe.cs.autoquest.usability.taskmodel.filter.types.EventTypeFilter.TEXT_INPUT;
18import static de.ugoe.cs.autoquest.usability.util.TextInputUtil.aggregateEnteredTextFromTextInputs;
19
20import java.util.List;
21
22import com.google.common.base.Optional;
23import com.google.common.base.Predicate;
24import com.google.common.collect.Multiset;
25import com.google.common.collect.Multisets;
26
27import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
28import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskModel;
29import de.ugoe.cs.autoquest.usability.EvaluationMethodCaller;
30import de.ugoe.cs.autoquest.usability.result.UsabilityProblemDescriptionResolver;
31import de.ugoe.cs.autoquest.usability.result.UsabilityProblemDescription;
32import de.ugoe.cs.autoquest.usability.rules.UsabilityMetric;
33import de.ugoe.cs.autoquest.usability.rules.UsabilityRule;
34import de.ugoe.cs.autoquest.usability.taskmodel.filter.FilterResult;
35import de.ugoe.cs.autoquest.usability.taskmodel.filter.IterativeDFSFilterStrategy;
36import de.ugoe.cs.autoquest.usability.taskmodel.filter.types.TaskModelFilter;
37
38/**
39 * <p>
40 * Metric, which measures either the repetition of entered words or the maximum repetition of a
41 * single word.
42 * </p>
43 *
44 * @author Alexander Deicke
45 */
46public class TextInputEntryRepetitionsMetric extends UsabilityRule implements UsabilityMetric {
47
48    /**
49     * <p>
50     * Constructor. Creates a new {@link TextInputEntryRepetitionsMetric} for a given task model.
51     * </p>
52     *
53     * @param taskModel
54     */
55    public TextInputEntryRepetitionsMetric(ITaskModel taskModel) {
56        super(taskModel);
57        this.name = "TextInputEntryRepetitions";
58        this.defect =
59            new UsabilityProblemDescriptionResolver().descriptionFor(this.getClass()
60                .getSimpleName());
61    }
62
63    /*
64     * (non-Javadoc)
65     *
66     * @see de.ugoe.cs.autoquest.usability.rules.UsabilityRule#check()
67     */
68    @Override
69    public Optional<UsabilityProblemDescription> calculate() {
70        FilterResult textInputEvents = extractNodesFromTaskTree();
71        float evaluationMetric = calculateEvaluationMetric(textInputEvents.tasksMatchedFilter());
72        return this.defect.isPresent(evaluationMetric);
73    }
74
75    /**
76     *
77     * <p>
78     * Filters all text input events from task model.
79     * </p>
80     *
81     * @return {@code FilterResult}
82     */
83    private FilterResult extractNodesFromTaskTree() {
84        return new TaskModelFilter(new IterativeDFSFilterStrategy()).filterByEventType(TEXT_INPUT)
85            .from(this.taskModel);
86    }
87
88    /**
89     *
90     * <p>
91     * Calculates the metric.
92     * </p>
93     *
94     * @param textInputEvents
95     *            all text input events
96     * @return either number of repeated words or the number of repetitions of the most entered word
97     */
98    private float calculateEvaluationMetric(List<ITask> textInputEvents) {
99        Multiset<String> enteredTextFragments = aggregateEnteredTextFromTextInputs(textInputEvents);
100        Multiset<String> orderedTextFragmentsWithMultipleOccurences =
101            onlyTextFragmentsWithMultipleOccurences(enteredTextFragments);
102        if (orderedTextFragmentsWithMultipleOccurences.isEmpty())
103            return 0;
104        String wordWithHighestRepetitionInTextFragments =
105            orderedTextFragmentsWithMultipleOccurences.iterator().next();
106        int numberOfRepeatedWords = orderedTextFragmentsWithMultipleOccurences.entrySet().size();
107        int maxRepetitions =
108            orderedTextFragmentsWithMultipleOccurences
109                .count(wordWithHighestRepetitionInTextFragments);
110        return Math.max(numberOfRepeatedWords, maxRepetitions - 1);
111    }
112
113    /**
114     *
115     * <p>
116     * Return only words, which at least entered twice.
117     * </p>
118     *
119     * @param allTextInputs
120     *            all text input events
121     * @return all word, which used min. twice
122     */
123    private Multiset<String> onlyTextFragmentsWithMultipleOccurences(final Multiset<String> allTextInputs)
124    {
125        return Multisets.copyHighestCountFirst(Multisets.filter(allTextInputs,
126                                                                new Predicate<String>() {
127
128                                                                    @Override
129                                                                    public boolean apply(String word)
130                                                                    {
131                                                                        return allTextInputs
132                                                                            .count(word) > 1;
133                                                                    }
134
135                                                                }));
136    }
137
138    /*
139     * (non-Javadoc)
140     *
141     * @see
142     * de.ugoe.cs.autoquest.usability.rules.UsabilityRule#callEvaluationMetho(de.ugoe.cs.autoquest
143     * .usability.EvaluationMethodCaller)
144     */
145    @Override
146    public Optional<UsabilityProblemDescription> callEvaluationMethod(EvaluationMethodCaller evaluationMethodCaller)
147    {
148        return evaluationMethodCaller.check(this);
149    }
150
151}
Note: See TracBrowser for help on using the repository browser.