1 | package de.ugoe.cs.autoquest.usability.util;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 |
|
---|
5 | import com.google.common.base.CharMatcher;
|
---|
6 | import com.google.common.base.Predicate;
|
---|
7 | import com.google.common.base.Splitter;
|
---|
8 | import com.google.common.collect.HashMultiset;
|
---|
9 | import com.google.common.collect.Iterables;
|
---|
10 | import com.google.common.collect.Lists;
|
---|
11 | import com.google.common.collect.Multiset;
|
---|
12 |
|
---|
13 | import de.ugoe.cs.autoquest.eventcore.gui.TextInput;
|
---|
14 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
|
---|
15 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
|
---|
16 |
|
---|
17 | public class TextInputUtil {
|
---|
18 |
|
---|
19 | public static Multiset<String> aggregateEnteredTextFromTextInputs(List<ITaskTreeNode> nodesWithTextInputEvents) {
|
---|
20 | List<Iterable<String>> allTextInputs = Lists.newArrayList();
|
---|
21 | for(ITaskTreeNode nodeWithTextInput : nodesWithTextInputEvents) {
|
---|
22 | TextInput textInput = (TextInput) ((IEventTask) nodeWithTextInput).getEventType();
|
---|
23 | allTextInputs.add(splitTextIntoWordsAndSigns(textInput.getEnteredText()));
|
---|
24 | }
|
---|
25 | return HashMultiset.create(Iterables.concat(allTextInputs));
|
---|
26 | }
|
---|
27 |
|
---|
28 | public static Iterable<String> splitTextIntoWordsAndSigns(String enteredText) {
|
---|
29 | CharMatcher onlyWords = CharMatcher.WHITESPACE.or(CharMatcher.forPredicate(characterIsJavaIdentifierPartPredicate()));
|
---|
30 | CharMatcher onlySigns = CharMatcher.WHITESPACE.or(CharMatcher.forPredicate(characterIsJavaIdentifierPartPredicate()).negate());
|
---|
31 | Iterable<String> words = Splitter.on(onlyWords).omitEmptyStrings().trimResults().split(enteredText);
|
---|
32 | Iterable<String> signs = Splitter.on(onlySigns).omitEmptyStrings().trimResults().split(enteredText);
|
---|
33 | return Iterables.concat(words, signs);
|
---|
34 | }
|
---|
35 |
|
---|
36 | public static Predicate<Character> characterIsJavaIdentifierPartPredicate() {
|
---|
37 | return new Predicate<Character>() {
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public boolean apply(Character character) {
|
---|
41 | return !Character.isJavaIdentifierPart(character);
|
---|
42 | }
|
---|
43 |
|
---|
44 | };
|
---|
45 | }
|
---|
46 |
|
---|
47 | public static Predicate<Character> characterIsLetterOrDigitPredicate() {
|
---|
48 | return new Predicate<Character>() {
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public boolean apply(Character character) {
|
---|
52 | return !Character.isLetterOrDigit(character);
|
---|
53 | }
|
---|
54 |
|
---|
55 | };
|
---|
56 | }
|
---|
57 |
|
---|
58 | }
|
---|