gpt4 book ai didi

ruby - 将字符串拆分为最大字符数的 block 而不打断单词

转载 作者:数据小太阳 更新时间:2023-10-29 08:02:30 30 4
gpt4 key购买 nike

我想将一个字符串拆分成 block ,每个 block 都在最大字符数内,比如 2000,并且不拆分单词。

我尝试过如下操作:

text.chars.each_slice(2000).map(&:join)

但有时,单词会被拆分。我尝试了一些正则表达式:

text.scan(/.{1,2000}\b|.{1,2000}/).map(&:strip)

来自 this question , 但我不太明白它是如何工作的,它给了我一些不稳定的行为,有时会给出只包含句点的 block 。

任何指点将不胜感激。

最佳答案

代码

def max_groups(str, n)
arr = []
pos = 0
loop do
break arr if pos == str.size
m = str.match(/.{1,#{n}}(?=[ ]|\z)|.{,#{n-1}}[ ]/, pos)
return nil if m.nil?
arr << m[0]
pos += m[0].size
end
end

示例

str = "Now is the time for all good people to party"
# 12345678901234567890123456789012345678901234
# 0 1 2 3 4

max_groups(str, 5)
#=> nil
max_groups(str, 6)
#=> ["Now is", " the ", "time ", "for ", "all ", "good ", "people", " to
max_groups(str, 10)
#=> ["Now is the", " time for ", "all good ", "people to ", "party"]
max_groups(str, 14)
#=> ["Now is the ", "time for all ", "good people to", " party"]
max_groups(str, 15)
#=> ["Now is the time", " for all good ", "people to party"]
max_groups(str, 29)
#=> ["Now is the time for all good ", "people to party"]
max_groups(str, 43)
#=> ["Now is the time for all good people to ", "party"]
max_groups(str, 44)
#=> ["Now is the time for all good people to party"]

str = "How you do?"
# 123456789012345678
# 0 1

max_groups(str, 4)
#=> ["How ", " ", " ", "you ", "do?"]

关于ruby - 将字符串拆分为最大字符数的 block 而不打断单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49087131/

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