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 |
|
---|
15 | package de.ugoe.cs.autoquest.usability.util;
|
---|
16 |
|
---|
17 | import java.util.List;
|
---|
18 |
|
---|
19 | import com.google.common.base.CharMatcher;
|
---|
20 | import com.google.common.base.Predicate;
|
---|
21 | import com.google.common.base.Splitter;
|
---|
22 | import com.google.common.collect.HashMultiset;
|
---|
23 | import com.google.common.collect.Iterables;
|
---|
24 | import com.google.common.collect.Lists;
|
---|
25 | import com.google.common.collect.Multiset;
|
---|
26 |
|
---|
27 | import de.ugoe.cs.autoquest.eventcore.gui.TextInput;
|
---|
28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
|
---|
29 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * <p>
|
---|
33 | * Util class to handle text input events/tasks.
|
---|
34 | * </p>
|
---|
35 | *
|
---|
36 | * @author Alexander Deicke
|
---|
37 | */
|
---|
38 | public class TextInputUtil {
|
---|
39 |
|
---|
40 | private TextInputUtil() {
|
---|
41 | // util class
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | *
|
---|
46 | * <p>
|
---|
47 | * Returns all entered words and signs of text input events.
|
---|
48 | * </p>
|
---|
49 | *
|
---|
50 | * @param tasksWithTextInputEvents
|
---|
51 | * tasks with event type {@link TextInput}
|
---|
52 | * @return set of all entered word and signs with unique entries
|
---|
53 | */
|
---|
54 | public static Multiset<String> aggregateEnteredTextFromTextInputs(List<ITask> tasksWithTextInputEvents)
|
---|
55 | {
|
---|
56 | List<Iterable<String>> allTextInputs = Lists.newArrayList();
|
---|
57 | for (ITask taskWithTextInput : tasksWithTextInputEvents) {
|
---|
58 | TextInput textInput = (TextInput) ((IEventTask) taskWithTextInput).getEventType();
|
---|
59 | allTextInputs.add(splitTextIntoWordsAndSigns(textInput.getEnteredText()));
|
---|
60 | }
|
---|
61 | return HashMultiset.create(Iterables.concat(allTextInputs));
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | *
|
---|
66 | * <p>
|
---|
67 | * Splits entered text into words and signs.
|
---|
68 | * </p>
|
---|
69 | *
|
---|
70 | * @param enteredText
|
---|
71 | * entered text (e.g. from text input event)
|
---|
72 | * @return collection of words and signs
|
---|
73 | */
|
---|
74 | public static Iterable<String> splitTextIntoWordsAndSigns(String enteredText) {
|
---|
75 | CharMatcher onlyWords =
|
---|
76 | CharMatcher.WHITESPACE.or(CharMatcher.forPredicate(characterIsNoJavaIdentifierPart()));
|
---|
77 | CharMatcher onlySigns =
|
---|
78 | CharMatcher.WHITESPACE.or(CharMatcher.forPredicate(characterIsNoJavaIdentifierPart())
|
---|
79 | .negate());
|
---|
80 | Iterable<String> words =
|
---|
81 | Splitter.on(onlyWords).omitEmptyStrings().trimResults().split(enteredText);
|
---|
82 | Iterable<String> signs =
|
---|
83 | Splitter.on(onlySigns).omitEmptyStrings().trimResults().split(enteredText);
|
---|
84 | return Iterables.concat(words, signs);
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | *
|
---|
89 | * <p>
|
---|
90 | * Determines, if a character is not part of a Java identifier.
|
---|
91 | * </p>
|
---|
92 | *
|
---|
93 | * @return true, iff no part of Java identifier
|
---|
94 | */
|
---|
95 | public static Predicate<Character> characterIsNoJavaIdentifierPart() {
|
---|
96 | return new Predicate<Character>() {
|
---|
97 |
|
---|
98 | @Override
|
---|
99 | public boolean apply(Character character) {
|
---|
100 | return !Character.isJavaIdentifierPart(character);
|
---|
101 | }
|
---|
102 |
|
---|
103 | };
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | *
|
---|
108 | * <p>
|
---|
109 | * Determines if the specified character is not a letter or digit.
|
---|
110 | * </p>
|
---|
111 | *
|
---|
112 | * @return
|
---|
113 | */
|
---|
114 | public static Predicate<Character> characterIsNoLetterOrDigitPredicate() {
|
---|
115 | return new Predicate<Character>() {
|
---|
116 |
|
---|
117 | @Override
|
---|
118 | public boolean apply(Character character) {
|
---|
119 | return !Character.isLetterOrDigit(character);
|
---|
120 | }
|
---|
121 |
|
---|
122 | };
|
---|
123 | }
|
---|
124 |
|
---|
125 | }
|
---|