gpt4 book ai didi

java - 根据长度拆分并添加字符串

转载 作者:太空宇宙 更新时间:2023-11-04 09:27:18 24 4
gpt4 key购买 nike

我有一个段落作为输入字符串。我试图将段落拆分为句子数组,其中每个元素包含的确切句子不超过 250 个字符。

我尝试根据分隔符分割字符串 (as .) 。将所有字符串转换为列表。使用 StringBuilder ,我尝试根据长度(250 个字符)附加字符串。

    List<String> list = new ArrayList<String>();

String text = "Perhaps far exposed age effects. Now distrusts you her delivered applauded affection out sincerity. As tolerably recommend shameless unfeeling he objection consisted. She although cheerful perceive screened throwing met not eat distance. Viewing hastily or written dearest elderly up weather it as. So direction so sweetness or extremity at daughters. Provided put unpacked now but bringing. Unpleasant astonished an diminution up partiality. Noisy an their of meant. Death means up civil do an offer wound of. Called square an in afraid direct. Resolution diminution conviction so mr at unpleasing simplicity no. No it as breakfast up conveying earnestly immediate principle. Him son disposed produced humoured overcame she bachelor improved. Studied however out wishing but inhabit fortune windows. ";

Pattern re = Pattern.compile("[^.!?\\s][^.!?]*(?:[.!?](?!['\"]?\\s|$)[^.!?]*)*[.!?]?['\"]?(?=\\s|$)",
Pattern.MULTILINE | Pattern.COMMENTS);

Matcher reMatcher = re.matcher(text);
while (reMatcher.find()) {
list.add(reMatcher.group());
}
String textDelimted[] = new String[list.size()];
textDelimted = list.toArray(textDelimted);

StringBuilder stringB = new StringBuilder(100);

for (int i = 0; i < textDelimted.length; i++) {
while (stringB.length() + textDelimted[i].length() < 250)
stringB.append(textDelimted[i]);

System.out.println("!#@#$%" +stringB.toString());
}
}

预期结果:

[0]:也许年龄的影响远远暴露。现在她不信任你了,她真诚地表达了热烈的爱意。作为可以容忍的建议,他提出了无耻无情的反对意见。她虽然高兴地察觉到屏幕 throw 遇到了不吃的距离。

[1]:匆忙查看还是写了最亲爱的老人达天气吧。所以对女儿的指导是如此甜蜜或极端。提供现在拆封但带来。不愉快的惊讶减少了偏爱。吵闹和他们的意思。

[2]:死亡意味着民事上的献伤。恐怕直接叫方安了。分辨率减少信念所以先生在令人不快的简单没有。不,它作为早餐传达了认真直接的原则。

[3] 他的儿子性格幽默,克服了她单例汉的进步。学习却出于愿望却居住在幸运之窗。

最佳答案

我认为你需要稍微修改一下你的循环。我的结果匹配。

import java.util.List;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class MyClass {
public static void main(String args[]) {

List<String> list = new ArrayList<String>();

String text = "Perhaps far exposed age effects. Now distrusts you her delivered applauded affection out sincerity. As tolerably recommend shameless unfeeling he objection consisted. She although cheerful perceive screened throwing met not eat distance. Viewing hastily or written dearest elderly up weather it as. So direction so sweetness or extremity at daughters. Provided put unpacked now but bringing. Unpleasant astonished an diminution up partiality. Noisy an their of meant. Death means up civil do an offer wound of. Called square an in afraid direct. Resolution diminution conviction so mr at unpleasing simplicity no. No it as breakfast up conveying earnestly immediate principle. Him son disposed produced humoured overcame she bachelor improved. Studied however out wishing but inhabit fortune windows. ";

Pattern re = Pattern.compile("[^.!?\\s][^.!?]*(?:[.!?](?!['\"]?\\s|$)[^.!?]*)*[.!?]?['\"]?(?=\\s|$)",
Pattern.MULTILINE | Pattern.COMMENTS);

Matcher reMatcher = re.matcher(text);
while (reMatcher.find()) {
list.add(reMatcher.group());
}
String textDelimted[] = new String[list.size()];
textDelimted = list.toArray(textDelimted);

StringBuilder stringB = new StringBuilder(300);

for (int i = 0; i < textDelimted.length; i++) {
if(stringB.length() + textDelimted[i].length() < 250) {
stringB.append(textDelimted[i]);
} else {
System.out.println("!#@#$%" +stringB.toString());
stringB = new StringBuilder(300);
stringB.append(textDelimted[i]);
}

}
System.out.println("!#@#$%" +stringB.toString());
}
}

println 替换为以下代码以获取结果列表:

ArrayList<String> arrlist = new ArrayList<String>(5);
..
arrlist.add(stringB.toString());
..

关于java - 根据长度拆分并添加字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57525131/

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