[1040] | 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 |
|
---|
[1030] | 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;
|
---|
[1319] | 28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance;
|
---|
[1152] | 29 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
|
---|
[1319] | 30 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
|
---|
[1030] | 31 |
|
---|
[1040] | 32 | /**
|
---|
| 33 | * <p>
|
---|
[1217] | 34 | * Util class to handle text input events/tasks.
|
---|
[1040] | 35 | * </p>
|
---|
| 36 | *
|
---|
| 37 | * @author Alexander Deicke
|
---|
| 38 | */
|
---|
[1030] | 39 | public class TextInputUtil {
|
---|
[1040] | 40 |
|
---|
| 41 | private TextInputUtil() {
|
---|
| 42 | // util class
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[1217] | 45 | /**
|
---|
| 46 | *
|
---|
| 47 | * <p>
|
---|
[1319] | 48 | * Returns all entered words and signs of all instances of text input events.
|
---|
[1217] | 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 | */
|
---|
[1152] | 55 | public static Multiset<String> aggregateEnteredTextFromTextInputs(List<ITask> tasksWithTextInputEvents)
|
---|
[1040] | 56 | {
|
---|
[1030] | 57 | List<Iterable<String>> allTextInputs = Lists.newArrayList();
|
---|
[1152] | 58 | for (ITask taskWithTextInput : tasksWithTextInputEvents) {
|
---|
[1319] | 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("");
|
---|
[1030] | 67 | }
|
---|
[1319] | 68 | System.out.println(allTextInputs);
|
---|
[1030] | 69 | return HashMultiset.create(Iterables.concat(allTextInputs));
|
---|
| 70 | }
|
---|
[1040] | 71 |
|
---|
[1217] | 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 | */
|
---|
[1031] | 82 | public static Iterable<String> splitTextIntoWordsAndSigns(String enteredText) {
|
---|
[1040] | 83 | CharMatcher onlyWords =
|
---|
[1217] | 84 | CharMatcher.WHITESPACE.or(CharMatcher.forPredicate(characterIsNoJavaIdentifierPart()));
|
---|
[1040] | 85 | CharMatcher onlySigns =
|
---|
[1217] | 86 | CharMatcher.WHITESPACE.or(CharMatcher.forPredicate(characterIsNoJavaIdentifierPart())
|
---|
| 87 | .negate());
|
---|
[1040] | 88 | Iterable<String> words =
|
---|
| 89 | Splitter.on(onlyWords).omitEmptyStrings().trimResults().split(enteredText);
|
---|
| 90 | Iterable<String> signs =
|
---|
| 91 | Splitter.on(onlySigns).omitEmptyStrings().trimResults().split(enteredText);
|
---|
[1030] | 92 | return Iterables.concat(words, signs);
|
---|
| 93 | }
|
---|
[1040] | 94 |
|
---|
[1217] | 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() {
|
---|
[1030] | 104 | return new Predicate<Character>() {
|
---|
[1040] | 105 |
|
---|
[1030] | 106 | @Override
|
---|
| 107 | public boolean apply(Character character) {
|
---|
[1040] | 108 | return !Character.isJavaIdentifierPart(character);
|
---|
[1030] | 109 | }
|
---|
[1040] | 110 |
|
---|
[1030] | 111 | };
|
---|
| 112 | }
|
---|
[1040] | 113 |
|
---|
[1217] | 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() {
|
---|
[1030] | 123 | return new Predicate<Character>() {
|
---|
[1040] | 124 |
|
---|
[1030] | 125 | @Override
|
---|
| 126 | public boolean apply(Character character) {
|
---|
| 127 | return !Character.isLetterOrDigit(character);
|
---|
| 128 | }
|
---|
[1040] | 129 |
|
---|
[1030] | 130 | };
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | }
|
---|