gpt4 book ai didi

java - 如何使用 RxJava2 将多个单词组合成一个句子?

转载 作者:行者123 更新时间:2023-11-30 10:12:35 25 4
gpt4 key购买 nike

基本上,我有一个词流Observable<String>

我想要的是累积它们,直到我有一个“.”。当我有一个“。”我想发出整个句子。依此类推,直到流完成。

1) The -> nothing, accumulated
2) quick -> nothing, accumulated
3) brown -> nothing, accumulated
4) fox -> nothing, accumulated
5) jumps -> nothing, accumulated
6) over -> nothing, accumulated
7) the -> nothing, accumulated
8) lazy -> nothing, accumulated
9) dog. -> emit the whole sentence, clear accumulator

scan, reduce - 看起来很像,但不完全一样,不确定

最佳答案

有分机运算符bufferUntil您可以使用 collect 直到字符串包含标点符号,然后将字符串列表连接成一个字符串:

Flowable.fromArray("The", "quick", "brown", "fox", "jumps",
"over", "the", "lazy", "dog.",
"This", "sentence", "is", "false.")
.compose(FlowableTransformers.bufferUntil(v -> v.endsWith(".")))
.map(list -> Strings.join(" ", list))
.test()
.assertResult(
"The quick brown fox jumps over the lazy dog.",
"This sentence is false."
);

关于java - 如何使用 RxJava2 将多个单词组合成一个句子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51825843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com