Java.lang.ArrayIndexOutOfBoundsException

Signaler
Messages postés
10
Date d'inscription
lundi 6 décembre 2010
Statut
Membre
Dernière intervention
11 février 2013
-
Messages postés
6414
Date d'inscription
mardi 8 mars 2005
Statut
Modérateur
Dernière intervention
29 juillet 2020
-
Bonjour à tous,
quand j'ai executé mon programme java j'ai obtenu l'erreur suivante :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at nlg.ReadAndGenerate.main(ReadAndGenerate.java:143)
Java Result: 1

j'ai essayé à résoudre le problème mais j'ai pas réussi, donc s'il vous plais si quelqu'un peut me données meme une idée je serais très reconnaissante.

Le code java est le suivant:

package realise;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import simplenlg.features.Feature;
import simplenlg.features.Form;
import simplenlg.features.Gender;
import simplenlg.features.LexicalFeature;
import simplenlg.features.NumberAgreement;
import simplenlg.features.Tense;
import simplenlg.framework.DocumentElement;
import simplenlg.framework.NLGElement;
import simplenlg.framework.NLGFactory;
import simplenlg.framework.PhraseElement;
import simplenlg.lexicon.Lexicon;
import simplenlg.lexicon.XMLLexicon;
import simplenlg.phrasespec.NPPhraseSpec;
import simplenlg.phrasespec.SPhraseSpec;
import simplenlg.phrasespec.VPPhraseSpec;
import simplenlg.realiser.english.Realiser;





public class ReadAndGenerate {
/** The realiser. */
Realiser realiser;

NLGFactory phraseFactory;

Lexicon lexicon;
NLGFactory docFactory = new NLGFactory(this.lexicon);


static Map<String,Object> nounFeatures = new HashMap<>();
static {
nounFeatures.put("number", Feature.NUMBER);
nounFeatures.put("gender", LexicalFeature.GENDER);
nounFeatures.put("masculine", Gender.MASCULINE);
nounFeatures.put("feminine", Gender.FEMININE);
nounFeatures.put("neuter", Gender.NEUTER);
nounFeatures.put("both", NumberAgreement.BOTH);
nounFeatures.put("plural", NumberAgreement.PLURAL);
nounFeatures.put("singular", NumberAgreement.SINGULAR);

}

static Map<String,Object> verbFeatures = new HashMap<String,Object>();
static {
verbFeatures.put("tense", Feature.TENSE);
verbFeatures.put("form", Feature.FORM);
verbFeatures.put("past", Tense.PAST);
verbFeatures.put("present",Tense.PRESENT);
verbFeatures.put("future", Tense.FUTURE);
verbFeatures.put("negated", Feature.NEGATED);
verbFeatures.put("progressive", Feature.PROGRESSIVE);
verbFeatures.put("passive", Feature.PASSIVE);
verbFeatures.put("perfect", Feature.PERFECT);
verbFeatures.put("bare_infinitive", Form.BARE_INFINITIVE);
verbFeatures.put("gerund", Form.GERUND);
verbFeatures.put("imperative", Form.IMPERATIVE);
verbFeatures.put("infinitive", Form.INFINITIVE);
verbFeatures.put("normal", Form.NORMAL);
verbFeatures.put("past_participle", Form.PAST_PARTICIPLE);
verbFeatures.put("present_participle", Form.PRESENT_PARTICIPLE);

}



public ReadAndGenerate() {

File lex = new File("./res/default-lexicon.xml");
lexicon = new XMLLexicon(lex.getAbsolutePath());
this.phraseFactory = new NLGFactory(this.lexicon);
this.realiser = new Realiser(this.lexicon);
this.realiser.setFormatter(null);
}

NPPhraseSpec treatSubject(String input) throws Exception{
Map<String,String> features =getAttributes(input);
NPPhraseSpec element = this.phraseFactory.createNounPhrase(features.get("lex"));
applyFeatures(element,features,nounFeatures);
return element;
}

VPPhraseSpec treatVerb(String input)throws Exception{
Map<String,String> features =getAttributes(input);
VPPhraseSpec element = this.phraseFactory.createVerbPhrase(features.get("lex"));
applyFeatures(element,features,verbFeatures);
return element;
}


private void applyFeatures(NLGElement element,
Map<String, String> features, Map<String,Object> simpleNLGFeatures) {

for (String key : features.keySet()){
if (key.compareTo("lex")!=0){
if (key.compareTo("set")==0){
element.setFeature((String) simpleNLGFeatures.get(features.get(key)), true);
}
else{
element.setFeature((String) simpleNLGFeatures.get(key),(Object) simpleNLGFeatures.get(features.get(key)));
}

}
}
}

NPPhraseSpec treatObject(String input)throws Exception{
Map<String,String> features =getAttributes(input);
NPPhraseSpec element = this.phraseFactory.createNounPhrase(features.get("lex"));
applyFeatures(element,features,nounFeatures);
return element;
}


private Map<String,String> getAttributes(String chaine) throws Exception{
Map<String,String> features = new HashMap<>();

for (String pairs: chaine.split(",")){
String[] atrVal= pairs.split("=");
if (atrVal.length!=2){
throw new Exception("badly formatted attribute=value pair in " + chaine);
}
features.put(atrVal[0].trim(), atrVal[1].trim());
}

return features;
}


static public void main(String args[]) throws Exception{


ReadAndGenerate engine = new ReadAndGenerate();
Scanner scan = new Scanner(new File(args[0])).useDelimiter("\\)\\),");
List text = new ArrayList<>();
engine.phraseFactory.setLexicon(engine.lexicon);

engine.realiser.setLexicon(engine.lexicon);



while(scan.hasNext()){
String line = scan.next();
String[] els= line.split("\\)");
int i = 0;
NPPhraseSpec subject = engine.treatSubject(els[i].substring(els[i].lastIndexOf("(")+1));
i++;
SPhraseSpec clause;
VPPhraseSpec verb = engine.treatVerb(els[i].substring(els[i].lastIndexOf("(")+1));
i++;
NPPhraseSpec object = engine.treatObject(els[i].substring(els[i].lastIndexOf("(")+1));



clause = engine.phraseFactory.createClause(subject,verb,object);
DocumentElement sentence1 = engine.docFactory.createSentence(clause);
NLGElement realised = engine.realiser.realise(sentence1);

System.out.println(realised.getRealisation());
}



}
}

1 réponse

Messages postés
6414
Date d'inscription
mardi 8 mars 2005
Statut
Modérateur
Dernière intervention
29 juillet 2020
360
Salut,

String line = scan.next(); 
System.out.println("line:"+line);
String[] els= line.split("\\)"); 


Je pense que tu verras que lors de ton test, au moins une fois, tu as une ligne vide d'où l'accès impossible au premier élément de ton tableau.