我有一个大约1500到2000字符的文本。这个文本应该被分割成文本块,每个文本大约有400个字符(不需要精确的400个字符)。然而,它不应该只是分裂文本每400个字符,但分裂文本只有在有一个句号的地方。因此,基本上把一个大文本分成几块,而不破坏标点符号。
知道吗?
发布于 2022-05-23 18:03:05
我们可以试着减价
我在这里做了500,因为你的短信是< 400
let str = `I have a text with roughly 1500 - 2000 characters. This text should be split into blocks of text with roughly 400 characters each (does not have to be exactly 400 characters). However it should not just split the text every 400 characters, but split the text only at places where there is a full-stop. So basically divide one big text into several chunks without destroying punctuation. I have a text with roughly 1500 - 2000 characters. This text should be split into blocks of text with roughly 400 characters each (does not have to be exactly 400 characters). However it should not just split the text every 400 characters, but split the text only at places where there is a full-stop. So basically divide one big text into several chunks without destroying punctuation. I have a text with roughly 1500 - 2000 characters. This text should be split into blocks of text with roughly 400 characters each (does not have to be exactly 400 characters). However it should not just split the text every 400 characters, but split the text only at places where there is a full-stop. So basically divide one big text into several chunks without destroying punctuation.`
let nextPunct = str.indexOf(".")
const lines = str.split(/\.\s+/)
  .reduce((acc, line, i) => {
    if (i === 0) acc.push(line)
    else if ((acc[acc.length - 1].length + line.length) > 500) acc.push(line)
    else acc[acc.length - 1] += ". " + line;
    return acc
  }, [])
const res = lines.join(".\n")
console.log(res, lines.map(line => line.length))
https://stackoverflow.com/questions/72352758
复制相似问题