source: trunk/autoquest-core-usability-evaluation/src/main/java/de/ugoe/cs/autoquest/usability/util/TextInputUtil.java @ 1152

Last change on this file since 1152 was 1152, checked in by pharms, 11 years ago
  • complete refactoring of task tree model with a separation of task models and task instances
  • Property svn:mime-type set to text/plain
File size: 3.2 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.util;
16
17import java.util.List;
18
19import com.google.common.base.CharMatcher;
20import com.google.common.base.Predicate;
21import com.google.common.base.Splitter;
22import com.google.common.collect.HashMultiset;
23import com.google.common.collect.Iterables;
24import com.google.common.collect.Lists;
25import com.google.common.collect.Multiset;
26
27import de.ugoe.cs.autoquest.eventcore.gui.TextInput;
28import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
29import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
30
31/**
32 * <p>
33 * TODO comment
34 * </p>
35 *
36 * @author Alexander Deicke
37 */
38public class TextInputUtil {
39
40    private TextInputUtil() {
41        // util class
42    }
43
44    public static Multiset<String> aggregateEnteredTextFromTextInputs(List<ITask> tasksWithTextInputEvents)
45    {
46        List<Iterable<String>> allTextInputs = Lists.newArrayList();
47        for (ITask taskWithTextInput : tasksWithTextInputEvents) {
48            TextInput textInput = (TextInput) ((IEventTask) taskWithTextInput).getEventType();
49            allTextInputs.add(splitTextIntoWordsAndSigns(textInput.getEnteredText()));
50        }
51        return HashMultiset.create(Iterables.concat(allTextInputs));
52    }
53
54    public static Iterable<String> splitTextIntoWordsAndSigns(String enteredText) {
55        CharMatcher onlyWords =
56            CharMatcher.WHITESPACE.or(CharMatcher
57                .forPredicate(characterIsJavaIdentifierPartPredicate()));
58        CharMatcher onlySigns =
59            CharMatcher.WHITESPACE.or(CharMatcher
60                .forPredicate(characterIsJavaIdentifierPartPredicate()).negate());
61        Iterable<String> words =
62            Splitter.on(onlyWords).omitEmptyStrings().trimResults().split(enteredText);
63        Iterable<String> signs =
64            Splitter.on(onlySigns).omitEmptyStrings().trimResults().split(enteredText);
65        return Iterables.concat(words, signs);
66    }
67
68    public static Predicate<Character> characterIsJavaIdentifierPartPredicate() {
69        return new Predicate<Character>() {
70
71            @Override
72            public boolean apply(Character character) {
73                return !Character.isJavaIdentifierPart(character);
74            }
75
76        };
77    }
78
79    public static Predicate<Character> characterIsLetterOrDigitPredicate() {
80        return new Predicate<Character>() {
81
82            @Override
83            public boolean apply(Character character) {
84                return !Character.isLetterOrDigit(character);
85            }
86
87        };
88    }
89
90}
Note: See TracBrowser for help on using the repository browser.