Go through arraylistString and compare substrings if they are the same or similar

1

My question is How can I compare the strings that I have within a arraylist<String> so that it shows me the ones that are repeated? BUT these strings are irregular, for example: I have the following files in a folder.

I have a method that lifts everything in the folder and loads it into an arraylist.

ArrayList<String> arrayArchivos = new ArrayList<>();
ArrayList<String> Array1 = new ArrayList<>();

public void buscarArchivo(File ruta) {
//        Creo el vector que contendra todos los archivos de una ruta especificada.
        File[] archivo = ruta.listFiles();
//        Evaluo si la carpeta especificada contiene archivos.
        if (archivo != null) {
            arrayArchivos.clear();
//            Recorro el vector el cual tiene almacenado la ruta del archivo a buscar.
            for (int i = 0; i < archivo.length; i++) {
//                Evaluo si el archivo o la ruta es una carpeta.
                if (archivo[i].isDirectory()) {
//                    Le paso la nueva ruta de la carpeta si se cambia la ruta e busca nuevamente.
                    buscarArchivo(archivo[i]);
                } else {
                    String file = archivo[i].getName();
//                    Quitamos la extencion
                    arrayArchivos.add(file.substring(0, file.length() - 4));
                }
            }
//            Llamamos al metodo evaluarNombre.
            evaluarNombre();
        }
    }

In my method evaluateName () I have:

public void evaluarNombre() {

        if (arrayArchivos != null) {
            for (String arrayArchivo : arrayArchivos) {
//              Quitamos los espacios al principio y al final con trim(), luego mostramos solo letras.
                String b = arrayArchivo.replaceAll("[^a-zA-Z ]", "").trim();
//              Quitamos los espacios innecesarios para dejar solo un espacio entre letras.
                String d = b.replaceAll(" +", " ");
//              Añadimos a un nuevo array todos los String filtrados.
                Array1.add(d);

            }

        } else {
            System.out.println("No se encontro ningun archivo.");
        }
        contar(Array1);
    }

In the count (Array1)

method
private void contar(ArrayList<String> arraStrings) {
//        Declaramos un contador.
        int c = 0;
        if (arraStrings != null) {
//           Nos posicionamos en un elemento y recorremos el resto comparando.
            for (int i = 0; i < arraStrings.size(); i++) {
                for (int j = i + 1; j < arraStrings.size(); j++) {
//                Si los nombres de las cadenas son iguales, el contador se ingrementa.
                    if (arraStrings.get(i).substring(0, arraStrings.get(i).lastIndexOf(" ")).equals(arraStrings.get(j).substring(0, arraStrings.get(j).lastIndexOf(" ")))) {
                        c++;
                    }
                }

//            Evaluo que el contador sea mayor a 1.
                if (c > 1) {
                    System.out.println("El archivo '" + arraStrings.get(i) + "' Se repite " + c);
                }
            }
        } else {
            System.out.println("Array vacio");
        }
    }

The output I get is:

The file 'Maluma Corazon Dexter Remix Global Music' Repeats 2

The file 'Maluma Corazon Dexter Remix Global Music' Repeats 2

The file 'Agapornis Moves the Waist Dexter Remix Global Music' Repeats 2

The file 'Lali Una Mas Dexter Remix Global Music' Repeats 3

The file 'Lali Una Mas Dexter Remix Global Music' Repeats 3

The file 'Charlie Puth Attention David Guetta Vs Dexter Remix Global Music' Repeats 3

As you can see in the image, it marks me files that are not repeated, such as:

Agapornis Moves the Dexter Remix Global Music Waist

Charlie Puth Attention David Guetta Vs Dexter Remix Global Music

However, he tells me. Like the file

Ozuna Ft Zion & Lennox Egoista Dexter Remix (Global Music11) copies

YES is repeated but with the difference that it has the word copy , I would be very helpful your collaboration to be able to identify what I am doing wrong, of course thank you.

    
asked by Gerardo Ferreyra 24.01.2018 в 18:54
source

2 answers

1

First you have to define what criteria you will use to determine which 2 strings are "repeated".

  • They are really the same: cadena1.equals(cadena2) returns true
  • One contains the other: cadena1.contains(cadena2) || cadena2.contains(cadena1) returns true
  • Some criterion of distance of edition or similarity: like those implemented in this repository link

After you define that you should pay attention that each text string must have its own repetition counter:

private void contar(ArrayList<String> arraStrings) {
    if (arraStrings != null) {
// Nos posicionamos en un elemento y recorremos el resto comparando.
        for (int i = 0; i < arraStrings.size(); i++) {
// Declaramos un contador.
            int c = 0;
            for (int j = i + 1; j < arraStrings.size(); j++) {
// Si los nombres de las cadenas son iguales, el contador se ingrementa.
                if (cadenasIguales(arraStrings.get(i), arraStrings.get(j))) {
                    c++;
                }
            }

// Evaluo que el contador sea mayor a 1.
            if (c > 1) {
                System.out.println("El archivo '" + arraStrings.get(i) + "' Se repite " + c);
            }
        }
    } else {
        System.out.println("Array vacio");
    }
}

private boolean cadenasIguales(String cadena1, String cadena2) {
//logica original
//    return cadena1.substring(0, cadena1.lastIndexOf(" ")).equals(cadena2.substring(0, cadena2.lastIndexOf(" ")));
    return cadena1.contains(cadena2) || cadena2.contains(cadena1);
}

Finally, the previous code will report different repetitions for each repeated string:

  • abc
  • abc copy
  • abc copia2

"abc" repeats 2 times while "abc copia" repeats 1 time.

    
answered by 25.01.2018 / 15:20
source
-1

You can use a string that stores the string of the position of the array you are in, and within that position you go through another for the arraylist again and compare to see if it looks like another.

    
answered by 24.01.2018 в 20:23