简体   繁体   English

将文件句子拆分为单词

[英]split file sentences into words

can anyone help me please? 有人可以帮我吗?

I'm trying to split the sentences[] into words[] . 我正在尝试将sentences[]分解为words[] But it shows Syntax error on token "j", delete this token ... 但是它Syntax error on token "j", delete this token显示Syntax error on token "j", delete this token ...

My code is: 我的代码是:

try
{
    int j;
    String paragraph = sample.readFileString(f);
    String[] sentences = paragraph.split("[\\.\\!\\?]");
    for (int i=0;i<sentences.length;i++)
    {  
        System.out.println(i);
        System.out.println(sentences[i]);  
        for( j=0;j<=i;j++)
        {
            String  word[j]=sentences[i].split(" ");
        }
    }
}   

What can I do? 我能做什么?

String  word[j]=sentences[i].split(" ");
          ^^^^^^^^

this is not a valid String or String array declaration. 这不是有效的StringString array声明。

String.split() returns an array. String.split()返回一个数组。 So you have to change 所以你必须改变

String word[j] = sentences[i].split(" ");

to this 对此

String[] word = sentences[i].split(" ");

Instead of for loop with j variable use: 代替使用j变量的for循环,请使用:

String[] words = sentences[i].split(" ");

For multidimensional array: 对于多维数组:

String paragraph = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras non varius nisi. In at erat est, sit amet consectetur est. ";
String[] sentences = paragraph.split("[\\.\\!\\?]");

String[][] words = new String[sentences.length][];

for (int i=0;i<sentences.length;i++) {  
   words[i] = sentences[i].trim().split(" ");
}

System.out.println(words[0][1]); //[sentence][word] - it would be second word of first sentence

split looks like String[]split(String regex) split split看起来像String[]split(String regex) split

so change your String word[j] = sentences[i].split(" "); 因此,请更改您的String word[j] = sentences[i].split(" "); to

String  word[] = sentences[i].split(" ");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 正则表达式拆分包含特定单词的句子 - regex to split sentences containing specific words 反转文件的句子,行和单词 - Reverse sentences, lines and words of a file java - 如何在不多次读取文本文件的情况下,将句子的 ArrayList 拆分为 Java 中的单词的 ArrayList? - How to split an ArrayList of sentences into an ArrayList of words in Java without reading a text file more than once? Java:如何将txt文件中的多个句子拆分为一个数组,然后在数组中搜索单个单词? - Java: How can I split multiple sentences in a txt file to an array and then search for individual words in the array? 将句子的ArrayList拆分为单词,将单词存储到新的ArrayList中 - Split ArrayList of sentences into words, store the words into new ArrayList Java正则表达式将带有值及其度量标准的句子拆分为单个单词的句子 - Java regex to split words in a sentences with value and its metric as single word java-如何在令牌中存储文件中的拆分句子(ArrayIndexBoundException错误为0)java? - How to store split sentences from a file in tokens (ArrayIndexBoundException error at 0) java? 正则表达式将句子拆分为单词 - Regex Splitting Sentences into words 如何将段落分成句子? - How to split paragraphs into sentences? 正则表达式将字符串分成句子 - Regex To Split String Into Sentences
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM