使用流API;过滤掉相关数据后,我想编辑收集的数据。这是到目前为止的代码:
String wordUp = word.substring(0,1).toUpperCase() + word.substring(1);
String wordDown = word.toLowerCase();
ArrayList<String> text = Files.lines(path)
.parallel() // Perform filtering in parallel
.filter(s -> s.contains(wordUp) || s.contains(wordDown) && Arrays.asList(s.split(" ")).contains(word))
.sequential()
.collect(Collectors.toCollection(ArrayList::new));
编辑下面的代码很糟糕,我正在尝试避免使用它。(它也不是完全有效。它是在凌晨4点完成的,请原谅。)
for (int i = 0; i < text.size(); i++) {
String set = "";
List temp = Arrays.asList(text.get(i).split(" "));
int wordPos = temp.indexOf(word);
List<String> com1 = (wordPos >= limit) ? temp.subList(wordPos - limit, wordPos) : new ArrayList<String>();
List<String> com2 = (wordPos + limit < text.get(i).length() -1) ? temp.subList(wordPos + 1, wordPos + limit) : new ArrayList<String>();
for (String s: com1)
set += s + " ";
for (String s: com2)
set += s + " ";
text.set(i, set);
}
它在文本文件中寻找一个特定的单词,一旦行被过滤掉,我只想每次只收集行的一部分。要搜索的关键字两侧的多个单词。
例如:
keyword = "the"
limit = 1
它会发现: "Early in the morning a cow jumped over a fence."
然后应该返回: "in the morning"
* PS任何建议的速度改进都将被投票通过。
您应该考虑两个不同的任务。首先,将文件转换为单词列表:
List<String> words = Files.lines(path)
.flatMap(Pattern.compile(" ")::splitAsStream)
.collect(Collectors.toList());
这使用了您在空格字符处分割的最初想法。这对于简单的任务可能就足够了,但是,您应该研究的文档,BreakIterator
以了解这种简单方法与真实,复杂的单词边界分割之间的区别。
其次,如果您有一个单词列表,那么您的任务是查找您word
的匹配项String
,并使用单个空格字符作为分隔符将单词连接起来,从而将匹配项周围的项目序列转换为单个匹配项:
List<String> matches=IntStream.range(0, words.size())
// find matches
.filter(ix->words.get(ix).matches(word))
// create subLists around the matches
.mapToObj(ix->words.subList(Math.max(0, ix-1), Math.min(ix+2, words.size())))
// reconvert lists into phrases (join with a single space
.map(list->String.join(" ", list))
// collect into a list of matches; here, you can use a different
// terminal operation, like forEach(System.out::println), as well
.collect(Collectors.toList());
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句