[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;
|
---|
| 28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
|
---|
[1152] | 29 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
|
---|
[1030] | 30 |
|
---|
[1040] | 31 | /**
|
---|
| 32 | * <p>
|
---|
[1217] | 33 | * Util class to handle text input events/tasks.
|
---|
[1040] | 34 | * </p>
|
---|
| 35 | *
|
---|
| 36 | * @author Alexander Deicke
|
---|
| 37 | */
|
---|
[1030] | 38 | public class TextInputUtil {
|
---|
[1040] | 39 |
|
---|
| 40 | private TextInputUtil() {
|
---|
| 41 | // util class
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[1217] | 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 | */
|
---|
[1152] | 54 | public static Multiset<String> aggregateEnteredTextFromTextInputs(List<ITask> tasksWithTextInputEvents)
|
---|
[1040] | 55 | {
|
---|
[1030] | 56 | List<Iterable<String>> allTextInputs = Lists.newArrayList();
|
---|
[1152] | 57 | for (ITask taskWithTextInput : tasksWithTextInputEvents) {
|
---|
| 58 | TextInput textInput = (TextInput) ((IEventTask) taskWithTextInput).getEventType();
|
---|
[1031] | 59 | allTextInputs.add(splitTextIntoWordsAndSigns(textInput.getEnteredText()));
|
---|
[1030] | 60 | }
|
---|
| 61 | return HashMultiset.create(Iterables.concat(allTextInputs));
|
---|
| 62 | }
|
---|
[1040] | 63 |
|
---|
[1217] | 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 | */
|
---|
[1031] | 74 | public static Iterable<String> splitTextIntoWordsAndSigns(String enteredText) {
|
---|
[1040] | 75 | CharMatcher onlyWords =
|
---|
[1217] | 76 | CharMatcher.WHITESPACE.or(CharMatcher.forPredicate(characterIsNoJavaIdentifierPart()));
|
---|
[1040] | 77 | CharMatcher onlySigns =
|
---|
[1217] | 78 | CharMatcher.WHITESPACE.or(CharMatcher.forPredicate(characterIsNoJavaIdentifierPart())
|
---|
| 79 | .negate());
|
---|
[1040] | 80 | Iterable<String> words =
|
---|
| 81 | Splitter.on(onlyWords).omitEmptyStrings().trimResults().split(enteredText);
|
---|
| 82 | Iterable<String> signs =
|
---|
| 83 | Splitter.on(onlySigns).omitEmptyStrings().trimResults().split(enteredText);
|
---|
[1030] | 84 | return Iterables.concat(words, signs);
|
---|
| 85 | }
|
---|
[1040] | 86 |
|
---|
[1217] | 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() {
|
---|
[1030] | 96 | return new Predicate<Character>() {
|
---|
[1040] | 97 |
|
---|
[1030] | 98 | @Override
|
---|
| 99 | public boolean apply(Character character) {
|
---|
[1040] | 100 | return !Character.isJavaIdentifierPart(character);
|
---|
[1030] | 101 | }
|
---|
[1040] | 102 |
|
---|
[1030] | 103 | };
|
---|
| 104 | }
|
---|
[1040] | 105 |
|
---|
[1217] | 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() {
|
---|
[1030] | 115 | return new Predicate<Character>() {
|
---|
[1040] | 116 |
|
---|
[1030] | 117 | @Override
|
---|
| 118 | public boolean apply(Character character) {
|
---|
| 119 | return !Character.isLetterOrDigit(character);
|
---|
| 120 | }
|
---|
[1040] | 121 |
|
---|
[1030] | 122 | };
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | }
|
---|