java - 我如何在这个名词查找程序中处理这个ArrayIndexOutOfBoundException

标签 java parsing nlp

我遇到了这个问题,但我找不到解决此异常的方法。请帮忙。 我的程序试图从用户给定的句子中找出名词、动词和形容词(这里我只是试图找出名词)。如果我在程序中犯了错误,请指出这些错误,以便我纠正它。 这是我的代码:

enter code here

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;

import opennlp.tools.cmdline.parser.ParserTool;
import opennlp.tools.parser.Parse;
import opennlp.tools.parser.Parser;
import opennlp.tools.parser.ParserFactory;
import opennlp.tools.parser.ParserModel;
import opennlp.tools.util.ObjectStream;

public class ParserTest {

static Set<String> nounPhrases = new HashSet<>();
static Set<String> adjectivePhrases = new HashSet<>();
static Set<String> verbPhrases = new HashSet<>();
static String result;
gui_demo gd = new gui_demo();
String line = practice.gui_demo.textField1.getText();
public ParserTest(String line) {
    // TODO Auto-generated constructor stub
}


public ParserTest() {
    // TODO Auto-generated constructor stub
}

//ObjectStream<String> lineArr = new PlainTextByLineStream(new StringReader(line));

String[] lineArr = line.split("."); 
public void getNounPhrases(Parse p) {
    if (p.getType().equals("NN") || p.getType().equals("NNS") || p.getType().equals("NNP") || p.getType().equals("NNPS")) {
         nounPhrases.add(p.getCoveredText());
         System.out.println(p.getCoveredText());
    }

    if (p.getType().equals("JJ") || p.getType().equals("JJR") || p.getType().equals("JJS")) {
        adjectivePhrases.add(p.getCoveredText());
         System.out.println(p.getCoveredText());
    }

    if (p.getType().equals("VB") || p.getType().equals("VBP") || p.getType().equals("VBG")|| p.getType().equals("VBD") || p.getType().equals("VBN")) {
        verbPhrases.add(p.getCoveredText());
         System.out.println(p.getCoveredText());
    }

    for (Parse child : p.getChildren()) {
         getNounPhrases(child);
    }
}


public void parserAction() throws Exception {
    try{
    InputStream is = new FileInputStream("en-parser-chunking.bin");
    ParserModel model = new ParserModel(is);
    Parser parser = ParserFactory.create(model);
    for(int i=1; i <= lineArr.length; i++){
    Parse topParses[] = ParserTool.parseLine(lineArr[i], parser , 1);      //getting Exception

    for (Parse p : topParses){
        //p.show();
        getNounPhrases(p);
    }
    }   
    }catch(Exception e){
        System.out.print(e);
    }
}
public static void main(String[] args) throws Exception {

    new ParserTest().parserAction();
    result = "Nouns :" +nounPhrases.toArray(new String[nounPhrases.size()]);//+ "\n" + "Verbs:" + verbPhrases + "Adjectives:" + adjectivePhrases;
    /*System.out.println("List of Noun Parse : "+nounPhrases);
    System.out.println("List of Adjective Parse : "+adjectivePhrases);
    System.out.println("List of Verb Parse : "+verbPhrases); */
    }

}

程序的输出是:

java.lang.ArrayIndexOutOfBoundsException: 1

最佳答案

你有:

for(int i=1; i <= lineArr.length; i++) {
    Parse topParses[] = ParserTool.parseLine(lineArr[i], parser , 1);
}

数组的最后一个索引总是比它的长度小一个,因为第一个索引是 0而不是1 。因此,lineArr[lineArr.length-1]是最后一个索引。

您需要更改i <= lineArr.lengthi < lineArr.length .

此外,i1 开始而不是0 。如果这样做,您需要确保您的数组至少有两个元素,而您还没有。您也需要更改此设置。

最终结果应该是:

for(int i=0; i < lineArr.length; i++) {
    Parse topParses[] = ParserTool.parseLine(lineArr[i], parser , 1);
}

关于java - 我如何在这个名词查找程序中处理这个ArrayIndexOutOfBoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36779769/

相关文章:

java - 如何 "shift"字符串集合/数组?

c - 如何在 Perl 中有效解析表达式 '^#[0-9A-F]{2}\$[0-9A-F]{4}/$'

neural-network - 怎么可能对 word2vec 使用 softmax 呢?

C++如何读取文件并解析逗号分隔值

python - 为什么pip install bert后不能导入bert中的函数

python - 训练我自己的手套模型时出现编码问题

java - 如何使 spring @retryable 可配置?

java - 当我尝试在 TomCat 上部署并执行这个使用 Spring 的应用程序时,为什么会出现此异常?

java - 如何使用 IntelliJ 制作具有所有依赖项的 jar 文件,即 Fat jar

c++ - 如何在 Spirit X3 中正确指定锚定条件?