Java 将字符串划分为所有可能的4个字母的后续短语
我想做的基本上是:Java 将字符串划分为所有可能的4个字母的后续短语,java,arrays,string,Java,Arrays,String,我想做的基本上是: 读文件 删除所有标点符号并将所有字母转换为小写 将单词转换为4个字母的短语(如果单词短于4个字符,则将其作为一个整体) 例如: 输入:你好,我的身份是杜德先生 输出:hell、ello、my、iden、dent、enti、ntif、tifi、ific、fica、icat、cati、atio、tion、is、mist、iste、ster、dude 如果我能将每个4字短语作为一个数组中的单独值来获取,那就太好了 现在,我已经设法完成的事情: public String[] Ope
public String[] OpenFile() throws IOException {
FileReader fr = new FileReader(path);
BufferedReader textReader = new BufferedReader(fr);
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
int i;
for (i = 0; i < numberOfLines; i++) {
textData[i] = textReader.readLine();
textData[i] = textData[i].replaceAll("[^A-Za-ząčęėįšųūž]+", " ").toLowerCase();
}
textReader.close();
return textData;
}
public String[]OpenFile()引发IOException{
FileReader fr=新的FileReader(路径);
BufferedReader textReader=新的BufferedReader(fr);
int numberOfLines=readLines();
字符串[]文本数据=新字符串[numberOfLines];
int i;
对于(i=0;i
textData[i]
是我需要分割的每一行文本。
我尝试过几种方法,例如
.toCharArray
和2D数组,但我似乎无法管理字母排列部分。如何完成任务3?基本上,对于每个单词,您需要迭代可能的位置,从以下位置开始四个字母的序列:
public static List<String> sequences (String line) {
List result = new LinkedList<>();
String[] words = line.split(" ");
for (String word : words) {
if (word.length() <= 4) {
result.add(word);
} else {
for (int i = 0; i <= word.length() - 4; ++i) {
result.add(word.substring(i, i + 4));
}
}
}
return result;
}
公共静态列表序列(字符串行){
列表结果=新建LinkedList();
String[]words=line.split(“”);
for(字符串字:字){
if(word.length()启动示例:
List<String> result = new ArrayList<String>();
for (int i = 0; i < textData.length; i++) {
String[] currLine = textData[i].split("\\s+");
for (String word : currLine) {
if (word.length() > 4) {
for (int j = 0; j < currLine.length - 4; j++) {
result.add(word.substring(j, j + 4));
}
} else {
result.add(word);
}
}
}
List result=new ArrayList();
对于(int i=0;i4){
对于(int j=0;j
我没有测试它,所以请检查并让我知道它是否有效。测试时间:
publicstaticvoidmain(字符串[]args){
String text=“你好,我的身份是杜德先生。”;
String[]words=text.replaceAll(“[^(\\w)]+”,“”)。toLowerCase().split(“”);
for(字符串字:字){
如果(word.length()首先需要按空格和标点符号拆分方法。请注意第三行中的拆分,即按空格和标点符号的任意组合拆分
在我的例子中,我有
String text = "Hello, my identification is Mister Dude.";
String[] textArray = text.split("\\W+");
List<String> result = new ArrayList<>();
for (String word : textArray) {
result.addAll(Arrays.asList(split(word.toLowerCase(), 4)));
}
您是否考虑过使用包含4个元素的环形缓冲区?为什么输出中包含my
和is
(2个字母单词)?@MaxZoom(如果单词短于4个字符,则将其作为一个整体)小补丁:i@AdamStelmaszczyk对,这两个问题都是正确的。编辑我的答案来解决这两个问题。谢谢注意!这有一个问题,它包括标点符号,这在问题中是不需要的。第二个问题是:如果它有双空格,它会生成不需要的输出。它没有,但非常接近:)有趣的是,四分之三的回答者(包括我)一开始都犯了同样的错误。谢谢你注意到小写字母的要求。
String text = "Hello, my identification is Mister Dude.";
String[] textArray = text.split("\\W+");
List<String> result = new ArrayList<>();
for (String word : textArray) {
result.addAll(Arrays.asList(split(word.toLowerCase(), 4)));
}
private static String[] split(String word, int letters) {
if (word == null || word.length() == 0) {
return new String[] {};
} else if (word.length() <= letters) {
return new String[] { word };
} else {
int quantity = (word.length() - letters) + 1;
String[] val = new String[quantity];
int a = 0;
while (a + letters <= word.length()) {
val[a] = word.substring(a, a + letters);
a++;
}
return val;
}
}
[hell, ello, my, iden, dent, enti, ntif, tifi, ific, fica, icat, cati, atio, tion, is, mist, iste, ster, dude]