Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在这个名词查找程序中处理ArrayIndexOutOfBoundException_Java_Parsing_Nlp - Fatal编程技术网

Java 如何在这个名词查找程序中处理ArrayIndexOutOfBoundException

Java 如何在这个名词查找程序中处理ArrayIndexOutOfBoundException,java,parsing,nlp,Java,Parsing,Nlp,我提出了这个问题,我找不到解决这个异常的方法。请帮忙。 我的程序试图从用户给定的句子中找出名词、动词和形容词(这里我只是想找出名词)。如果我在我的程序中犯了错误,那么请指出这些错误,以便我可以更正它。 这是我的密码: enter code here import java.io.FileInputStream; import java.io.InputStream; import java.util.HashSet; import java.util.Set; import opennlp.

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

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.io.FileInputStream;
导入java.io.InputStream;
导入java.util.HashSet;
导入java.util.Set;
导入opennlp.tools.cmdline.parser.ParserTool;
导入opennlp.tools.parser.Parse;
导入opennlp.tools.parser.parser;
导入opennlp.tools.parser.ParserFactory;
导入opennlp.tools.parser.ParserModel;
导入opennlp.tools.util.ObjectStream;
公共类ParserTest{
静态集合=新HashSet();
静态集=新HashSet();
静态集合verbPhrases=newhashset();
静态字符串结果;
gui_demo gd=新的gui_demo();
String line=practice.gui_demo.textField1.getText();
公共ParserTest(字符串行){
//TODO自动生成的构造函数存根
}
公共ParserTest(){
//TODO自动生成的构造函数存根
}
//ObjectStream lineArr=新的明文ByLineStream(新的StringReader(行));
字符串[]lineArr=line.split(“.”);
公共短语(解析p){
如果(p.getType().equals(“NN”)| p.getType().equals(“NNS”)| p.getType().equals(“NNP”)| p.getType().equals(“NNPS”)){
名词短语.add(p.getCoveredText());
System.out.println(p.getCoveredText());
}
如果(p.getType().equals(“JJ”)| p.getType().equals(“JJR”)| p.getType().equals(“JJS”)){
添加(p.getCoveredText());
System.out.println(p.getCoveredText());
}
如果(p.getType().equals(“VB”)| | p.getType().equals(“VBP”)| | p.getType().equals(“VBG”)| | p.getType().equals(“VBN”)){
添加(p.getCoveredText());
System.out.println(p.getCoveredText());
}
for(解析子对象:p.getChildren()){
英语短语(儿童);
}
}
public void parserAction()引发异常{
试一试{
InputStream is=新文件InputStream(“en parser chunking.bin”);
ParserModel模型=新的ParserModel(is);
Parser=ParserFactory.create(model);
对于(int i=1;i您有:

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

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