Skip to content

Instantly share code, notes, and snippets.

@prskr
Last active January 17, 2018 08:21
Show Gist options
  • Select an option

  • Save prskr/8ea0f916e0a1510c323270b248a6a419 to your computer and use it in GitHub Desktop.

Select an option

Save prskr/8ea0f916e0a1510c323270b248a6a419 to your computer and use it in GitHub Desktop.
Utility to transform Stream of tweets to a Stream of single words
package de.fhro.inf.prg3.a13.utils;
import de.fhro.inf.prg3.a13.model.Tweet;
import java.util.regex.Pattern;
import java.util.stream.Stream;
/**
* @author Peter Kurfer
*/
public abstract class SplitUtils {
/**
* Pattern to filter for words that are only alpha numerical
*/
private static final Pattern ALPHA_NUMERICAL = Pattern.compile("[a-z0-9@]+");
/**
* Pattern to split for words
*/
private static final Pattern WORD_SPLIT = Pattern.compile("\\W");
private SplitUtils() {
}
public static Stream<String> splitTweetText(final Stream<Tweet> tweetStream) {
return tweetStream
.map(Tweet::getText)
.flatMap(WORD_SPLIT::splitAsStream)
.map(String::toLowerCase)
.filter(word -> ALPHA_NUMERICAL.matcher(word).matches());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment