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

Last change on this file since 1292 was 1292, checked in by adeicke, 11 years ago

Refactored filter mechanism.

  • Property svn:mime-type set to text/plain
File size: 5.7 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.UsabilityProblemDescription;
31import de.ugoe.cs.autoquest.usability.result.UsabilityProblemDescriptionResolver;
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.TaskModelFilter;
36
37/**
38 * <p>
39 * Metric, which measures either the repetition of entered words or the maximum repetition of a
40 * single word.
41 * </p>
42 *
43 * @author Alexander Deicke
44 */
45public class TextInputEntryRepetitionsMetric extends UsabilityRule implements UsabilityMetric {
46
47    /**
48     * <p>
49     * Constructor. Creates a new {@link TextInputEntryRepetitionsMetric} for a given task model.
50     * </p>
51     *
52     * @param taskModel
53     */
54    public TextInputEntryRepetitionsMetric(ITaskModel taskModel) {
55        super(taskModel);
56        this.name = "TextInputEntryRepetitions";
57        this.defect =
58            new UsabilityProblemDescriptionResolver().descriptionFor(this.getClass()
59                .getSimpleName());
60    }
61
62    /*
63     * (non-Javadoc)
64     *
65     * @see de.ugoe.cs.autoquest.usability.rules.UsabilityRule#check()
66     */
67    @Override
68    public Optional<UsabilityProblemDescription> calculate() {
69        FilterResult textInputEvents = extractNodesFromTaskTree();
70        float evaluationMetric = calculateEvaluationMetric(textInputEvents.tasksMatchedFilter());
71        return this.defect.isPresent(evaluationMetric);
72    }
73
74    /**
75     *
76     * <p>
77     * Filters all text input events from task model.
78     * </p>
79     *
80     * @return {@code FilterResult}
81     */
82    private FilterResult extractNodesFromTaskTree() {
83        return new TaskModelFilter().filter(taskModel, TEXT_INPUT);
84    }
85
86    /**
87     *
88     * <p>
89     * Calculates the metric.
90     * </p>
91     *
92     * @param textInputEvents
93     *            all text input events
94     * @return either number of repeated words or the number of repetitions of the most entered word
95     */
96    private float calculateEvaluationMetric(List<ITask> textInputEvents) {
97        Multiset<String> enteredTextFragments = aggregateEnteredTextFromTextInputs(textInputEvents);
98        Multiset<String> orderedTextFragmentsWithMultipleOccurences =
99            onlyTextFragmentsWithMultipleOccurences(enteredTextFragments);
100        if (orderedTextFragmentsWithMultipleOccurences.isEmpty())
101            return 0;
102        String wordWithHighestRepetitionInTextFragments =
103            orderedTextFragmentsWithMultipleOccurences.iterator().next();
104        int numberOfRepeatedWords = orderedTextFragmentsWithMultipleOccurences.entrySet().size();
105        int maxRepetitions =
106            orderedTextFragmentsWithMultipleOccurences
107                .count(wordWithHighestRepetitionInTextFragments);
108        return Math.max(numberOfRepeatedWords, maxRepetitions - 1);
109    }
110
111    /**
112     *
113     * <p>
114     * Return only words, which at least entered twice.
115     * </p>
116     *
117     * @param allTextInputs
118     *            all text input events
119     * @return all word, which used min. twice
120     */
121    private Multiset<String> onlyTextFragmentsWithMultipleOccurences(final Multiset<String> allTextInputs)
122    {
123        return Multisets.copyHighestCountFirst(Multisets.filter(allTextInputs,
124                                                                new Predicate<String>() {
125
126                                                                    @Override
127                                                                    public boolean apply(String word)
128                                                                    {
129                                                                        return allTextInputs
130                                                                            .count(word) > 1;
131                                                                    }
132
133                                                                }));
134    }
135
136    /*
137     * (non-Javadoc)
138     *
139     * @see
140     * de.ugoe.cs.autoquest.usability.rules.UsabilityRule#callEvaluationMetho(de.ugoe.cs.autoquest
141     * .usability.EvaluationMethodCaller)
142     */
143    @Override
144    public Optional<UsabilityProblemDescription> callEvaluationMethod(EvaluationMethodCaller evaluationMethodCaller)
145    {
146        return evaluationMethodCaller.check(this);
147    }
148
149}
Note: See TracBrowser for help on using the repository browser.