【问题标题】:Can't figure out how to solve this here: ArrayIndexOutOfBoundsException: 0无法弄清楚如何在这里解决这个问题:ArrayIndexOutOfBoundsException:0
【发布时间】:2015-03-26 15:47:55
【问题描述】:

我的问题如下:

我有一个 15k 行的 .train 文件用于 Apache NLP,这是整个文本的示例:

La joven Estefania Lopez Montesinos de la comision <START:falla> Embarcadero  Historiador Beti <END> y la niña Maria Donderis Sanchis de la Falla <START:falla> Blasco Ibáñez  Plaza Maestro Ripoll <END>

如您所见,标签被包裹在&lt;START:category&gt;tag&lt;END&gt; 之间,并且标签可能不止一个词。

我需要做的是将这个巨大的文本文件转换成如下所示的文件: (也就是说,我需要用标记分隔每一行,然后在一个制表符分隔的列中,如果标记不是标签,我必须在我的文本中放置一个“O”,如果它是一个标签,则在我的文本中放置)

La  O
joven   O
Estefanía   O
Lopez   O
Montesinos  O
de  O
la  O
comisión    O
Embarcadero FALLA
Historiador FALLA
Beti    FALLA
y   O
la  O
niña    O
María   O
Donderis    O
Sanchez O
de  O
la  O
falla   O
Blasco  FALLA
Ibañez  FALLA
Plaza   FALLA
Maestro FALLA
Ripoll  FALLA

为了创建后一个文本形式的文件,我创建了一个类,它从第一个文本逐行接收并像这样操作:

import java.util.ArrayList;
import java.util.List;


public class Tokenizer {
    public static ArrayList<Token> inspect (String input){
        ArrayList<Token> tokens = new ArrayList<Token>();


        if(input.contains("<START:falla>")){
            String[] chunks = input.split("<START:falla>");
            for(String piece : chunks){
                if(piece.contains("<END>")){
                    String[] polaridad = piece.trim().split("<END>");
                    String falla = polaridad[0]; // Here is where I get the error.
                    String[] falles = falla.trim().split( " " );
                    for(String s : falles){
                        if ( !s.trim().isEmpty() ){
                            Token word = new Token(s);
                            word.setType(true);
                            tokens.add(word);
                        }
                    }
                    if(polaridad.length>1){
                        String weird = polaridad[1];
                        if(!weird.isEmpty()){
                            String[] nofalles = weird.trim().split( " " );
                            for(String s : nofalles){
                                if ( !s.trim().isEmpty() ){
                                    Token word = new Token(s);
                                    word.setType(false);
                                    tokens.add(word);
                                }
                            }
                        }
                    }       

                }else{  // if(!piece.contains("<END>"))
                    String[] pieces = piece.trim().split(" ");
                    for(String s : pieces){
                        if ( !s.trim().isEmpty() ){
                            Token word = new Token(s);
                            word.setType(false);
                            tokens.add(word);
                        }
                    }
                }
            }
        }else{
            // splits using spaces
            String[] firstPass = input.trim().split( " " );

            for ( String s : firstPass ) {
                // the current part cannot be empty
                if ( !s.trim().isEmpty() ){
                    Token word = new Token(s);
                    word.setType(false);
                    tokens.add(word);
                }
            }
        }   
        return tokens;      
    }
}

如您所见,在这个类中,我为每个单词设置了属性“setType”,以便之后可以以指定的格式写入文件。

我不知道如何解决这个问题,因为同一个课程可以完美地处理另一个文本。

非常感谢您。

【问题讨论】:

  • 您添加了indexoutofboundsexception 标签但没有阅读?
  • 是的,我做到了,如果您阅读了我的问题,您会发现它以前可以与其他文本一起使用。因此,我来​​这里问问。
  • 你得到了异常,因为polaridad 是空的。
  • 如果您对 indexoutofboundsexception 有更多了解,您就会知道它是根据输入发生的。意思是新文本导致它。

标签: java string indexoutofboundsexception tokenize


【解决方案1】:

我怀疑piece 等于&lt;END&gt;String.split(String) 的文档说:

“尾随的空字符串 [因此] 不包含在结果数组中。”

因此,以下产生一个长度为零的数组:

String piece = "<END>"
piece.trim().split("<END>") => []

如果您想保留空的尾随标记,请将第二个负数参数传递给split,您会得到一个长度为 2 的数组:

piece.trim().split("<END>", -1) => [, ]

【讨论】:

  • 非常感谢。我知道解决方案很简单。
猜你喜欢
  • 2022-07-24
  • 1970-01-01
  • 1970-01-01
  • 2020-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多