Exception in thread "main" java.lang.ArrayIndexOutOfBoundsExcept

-
Bonjour,
comment résoudre cet erreur svp :(

package ontologie;

import edu.cmu.lti.lexical_db.ILexicalDatabase;
import edu.cmu.lti.lexical_db.NictWordNet;
import edu.cmu.lti.ws4j.RelatednessCalculator;
import edu.cmu.lti.ws4j.impl.HirstStOnge;
import edu.cmu.lti.ws4j.impl.JiangConrath;
import edu.cmu.lti.ws4j.impl.LeacockChodorow;
import edu.cmu.lti.ws4j.impl.Lesk;
import edu.cmu.lti.ws4j.impl.Lin;
import edu.cmu.lti.ws4j.impl.Path;
import edu.cmu.lti.ws4j.impl.Resnik;
import edu.cmu.lti.ws4j.impl.WuPalmer;
import edu.cmu.lti.ws4j.util.WS4JConfiguration;
import edu.smu.tspell.wordnet.WordNetDatabase;
import edu.smu.tspell.wordnet.impl.file.PropertyNames;
import edu.smu.tspell.wordnet.Synset;
import edu.smu.tspell.wordnet.WordNetDatabase;
import edu.smu.tspell.wordnet.WordSense;
import edu.smu.tspell.wordnet.impl.file.PropertyNames;
import java.lang.reflect.Method;
import java.util.ArrayList;

import java.util.Scanner;
import java.util.Vector;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import rita.RiWordNet;

import info.debatty.java.stringsimilarity.*;
         
  
  
public class Test {
private static Iterable<String> part;

   
     public Test() {
  database = WordNetDatabase.getFileInstance();
 }
     
      private WordNetDatabase database;
 private final static String[] SYNSET_TYPES = {"", "noun", "verb"};
        
      private static ILexicalDatabase db = new NictWordNet();
      
     public List<Synset> getSynsets(String word) {
  return Arrays.asList(database.getSynsets(word));
 }

        public static double jaro(String s, String t) {
        int s_len = s.length();
        int t_len = t.length();
 
        if (s_len == 0 && t_len == 0) return 1;
 
      //  int match_distance = Integer.max(s_len, t_len) / 2 - 1;
           int match_distance = Math.max(s_len, t_len)/2 - 1;
        
        
        boolean[] s_matches = new boolean[s_len];
        boolean[] t_matches = new boolean[t_len];
 
        int matches = 0;
        int transpositions = 0;
 
        for (int i = 0; i < s_len; i++) {
            int start = Math.max(0, i-match_distance);
            int end = Math.max(i+match_distance+1, t_len);
 
            for (int j = start; j < end; j++) {
                if (t_matches[j]) continue;
                if (s.charAt(i) != t.charAt(j)) continue;
                s_matches[i] = true;
                t_matches[j] = true;
                matches++;
                break;
            }
        }
 
        if (matches == 0) return 0;
 
        int k = 0;
        for (int i = 0; i < s_len; i++) {
            if (!s_matches[i]) continue;
            while (!t_matches[k]) k++;
            if (s.charAt(i) != t.charAt(k)) transpositions++;
            k++;
        }
 
        return (((double)matches / s_len) +
                ((double)matches / t_len) +
                (((double)matches - transpositions/2.0) / matches)) / 3.0;
    }
 
       public static void main(String[] args) {
    
  System.setProperty(PropertyNames.DATABASE_DIRECTORY, "C:\\Program Files (x86)\\WordNet\\2.1\\dict");
  System.out.println(System.getProperty(PropertyNames.DATABASE_DIRECTORY, "."));
  Similarity tester = new Similarity();
                String[] word = {"identify"};
              
                  Vector v1 = new Vector();
                 for (int m = 0; m < word.length; m++) {
                 
  List<Synset> synset_list = tester.getSynsets(word [m] );
               
  for (Synset synset : synset_list) {
                            for (String syn: synset.getWordForms()) {
                                v1.add(syn.toString());
                            }}}
                 String[] word1 = {"name"};
              
                  Vector v2 = new Vector();
                 for (int z = 0; z < word1.length; z++) {
                 
  List<Synset> synset_list1 = tester.getSynsets(word1 [z] );
               
  for (Synset synset1 : synset_list1) {
                            for (String syn1: synset1.getWordForms()) {
                                v2.add(syn1.toString());
                            }}}
                 
                
                           System.out.println(v1);
                           System.out.println(v2);
                            
                            
                            
                          
      for (int i = 0; i < v1.size()-1; i++) {
           for (int j = 0; j < v2.size()-1; j++) {
      
          
             // System.out.println("----------------------------------------------------------------------------- ");
              System.out.println(""+ jaro(v1.get(i).toString(),v2.get(j).toString()));
                            
          
       }
      }
       
       }

}




voici l'erreur
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at ontologie.Test.jaro(Test.java:74)
at ontologie.Test.main(Test.java:138)
Java Result: 1
Afficher la suite 

1 réponse

Messages postés
16039
Date d'inscription
samedi 31 mai 2008
Statut
Modérateur
Dernière intervention
17 octobre 2019
88
0
Merci
Bonjour,

Vérifies tes calculs de start et end, tu ne peux pas faire s_matches[i], s.charAt(i), t_matches[j] ou t.charAt(j) lorsque i ou j sont supérieurs ou égaux à s_matches.length, s.length(), t_matches.length ou t.length()

        for (int i = 0; i < s_len; i++) {
            int start = Math.max(0, i-match_distance);
            int end = Math.max(i+match_distance+1, t_len);
 
            for (int j = start; j < end; j++) {
                if (t_matches[j]) continue;
                if (s.charAt(i) != t.charAt(j)) continue;
                s_matches[i] = true;
                t_matches[j] = true; 

La confiance n'exclut pas le contrôle
Commenter la réponse de KX