【发布时间】:2014-06-15 12:25:47
【问题描述】:
假设我有这个单词列表:
String[] stopWords = new String[]{"i","a","and","about","an","are","as","at","be","by","com","for","from","how","in","is","it","not","of","on","or","that","the","this","to","was","what","when","where","who","will","with","the","www"};
比我有文字
String text = "I would like to do a nice novel about nature AND people"
是否有方法可以匹配 stopWords 并在忽略大小写的情况下删除它们;像这样的地方吗?:
String noStopWordsText = remove(text, stopWords);
结果:
" would like do nice novel nature people"
如果您知道 regex 会很好用,但我真的更喜欢像 commons 解决方案这样更注重性能的解决方案。
顺便说一句,我现在正在使用这种缺乏适当的不敏感大小写处理的公共方法:
private static final String[] stopWords = new String[]{"i", "a", "and", "about", "an", "are", "as", "at", "be", "by", "com", "for", "from", "how", "in", "is", "it", "not", "of", "on", "or", "that", "the", "this", "to", "was", "what", "when", "where", "who", "will", "with", "the", "www", "I", "A", "AND", "ABOUT", "AN", "ARE", "AS", "AT", "BE", "BY", "COM", "FOR", "FROM", "HOW", "IN", "IS", "IT", "NOT", "OF", "ON", "OR", "THAT", "THE", "THIS", "TO", "WAS", "WHAT", "WHEN", "WHERE", "WHO", "WILL", "WITH", "THE", "WWW"};
private static final String[] blanksForStopWords = new String[]{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""};
noStopWordsText = StringUtils.replaceEach(text, stopWords, blanksForStopWords);
【问题讨论】:
-
你的字符串中有标点符号吗?
-
您是否有一些硬数字表明正则表达式解决方案性能不足,或者这只是过早的优化?我的意思是,这绝对不是 性能最高的解决方案,但除非这就是你所做的一切并且你需要每秒执行 10K 次,否则我敢打赌这不是问题。