StopwordUtils { public static Pattern buildStopwordPattern( Collection<String> stopwords, Boolean caseSensitive, String... additionalTerms) { StringJoiner sj = new StringJoiner("|"); for (String s : stopwords) { sj.add(Pattern.quote(s)); } if (additionalTerms != null) { for (String s : additionalTerms) { sj.add(s); } } if (caseSensitive) { return Pattern.compile("\\b(" + sj.toString() + ")\\b"); } else { return Pattern.compile("\\b(" + sj.toString() + ")\\b", Pattern.CASE_INSENSITIVE); } } private  StopwordUtils(); static Pattern buildStopwordPattern(
      Collection<String> stopwords, Boolean caseSensitive, String... additionalTerms); static boolean isStopWord(
      String word, Collection<String> stopwords, Boolean caseSensitive);  }
@Test public void testBuildStopwordPattern() { List<String> stopwords = Arrays.asList("stop", "word"); assertEquals( "\\b(\\Qstop\\E|\\Qword\\E)\\b", StopwordUtils.buildStopwordPattern(stopwords, false).pattern()); assertEquals( "\\b(\\Qstop\\E|\\Qword\\E)\\b", StopwordUtils.buildStopwordPattern(stopwords, true).pattern()); assertEquals( "\\b(\\Qstop\\E|\\Qword\\E|hello|world)\\b", StopwordUtils.buildStopwordPattern(stopwords, true, "hello", "world").pattern()); assertEquals( Pattern.CASE_INSENSITIVE, (StopwordUtils.buildStopwordPattern(stopwords, false).flags() & Pattern.CASE_INSENSITIVE)); assertEquals( 0, (StopwordUtils.buildStopwordPattern(stopwords, true).flags() & Pattern.CASE_INSENSITIVE)); }