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