package cn.edu.bjut.textmining.chapter2; import java.text.BreakIterator; import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SentenceSplitDemo { private static final String DOT_PLACEHOLDER = ""; private static final Pattern COMMON_TITLE = Pattern.compile( "\\b(Mr|Mrs|Ms|Dr|Prof|Sr|Jr)\\.", Pattern.CASE_INSENSITIVE); public static void main(String[] args) { String text = "Mr. Smith likes Java. Text mining is useful!"; System.out.println(splitSentences(text)); } public static List splitSentences(String text) { List sentences = new ArrayList(); if (text == null || text.trim().length() == 0) { return sentences; } String protectedText = protectCommonTitles(text); BreakIterator iterator = BreakIterator.getSentenceInstance(Locale.US); iterator.setText(protectedText); int start = iterator.first(); int end = iterator.next(); while (end != BreakIterator.DONE) { String sentence = protectedText.substring(start, end) .replace(DOT_PLACEHOLDER, ".") .trim(); if (sentence.length() > 0) { sentences.add(sentence); } start = end; end = iterator.next(); } return sentences; } private static String protectCommonTitles(String text) { Matcher matcher = COMMON_TITLE.matcher(text); StringBuffer buffer = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement( buffer, Matcher.quoteReplacement(matcher.group(1) + DOT_PLACEHOLDER)); } matcher.appendTail(buffer); return buffer.toString(); } }