Compare each word of the given text and save the lines where the text appears

0

I have the following methods in java, the split method separates each word from a text and saves it in a TreeSet < > to keep alphabetical order and returns the TreeSet < > in question. The compare method should take this TreeSet < > and compare with the text passed by parameter, picking up each word, using the .getLineNumber method of the LineNumberReader class and saving each line where that word appears to put in a Map < > each word and the list where it appears.

public Collection<String> split(String book){
    try{
        FileReader path = new FileReader(book);
        LineNumberReader read = new LineNumberReader(path);
        String line;

        while((line = read.readLine()) != null){
            line = line.replaceAll("[^a-zA-Z]", " ").toLowerCase();
            split = line.split(" ");

            for(String s : split){
                if(s.length() >= 1 && !palavras.contains(s)){
                    palavras.add(s);
                }
            }           
        }

        path.close();
        read.close();

    }catch(FileNotFoundException e){
        e.getStackTrace();
        System.out.println("Caminho para o arquivo invalido!");

    }catch(IOException ex){
        ex.getStackTrace();
    }

    return palavras;  
}   

public Map<String, List<Integer>> compare(Collection<String> list, String book){
    FileReader path;
    try {
        path = new FileReader(book);
        LineNumberReader read = new LineNumberReader(path);
        String line;         

        for(String s : list){
            ArrayList<Integer> lineNro = new ArrayList<>();
            while((line = read.readLine()) != null){                    
                line = line.replaceAll("[^a-zA-Z]", " ").toLowerCase();
                split = line.split(" ");                    
                for(String a : split){                        
                    if(s.equals(a)){
                        lineNro.add(read.getLineNumber());
                    }
                    words.put(a, lineNro);                        
                }
            }
        }

        for(Map.Entry<String, List<Integer>> e : words.entrySet()){
            System.out.println(e);
        }

        path.close();
        read.close();

    } catch (FileNotFoundException ex) {
        Logger.getLogger(Spliter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Spliter.class.getName()).log(Level.SEVERE, null, ex);
    }
    return words;      
}

The result is a list of words, but all words are given the same list as Integer. The expected result would be a list of words and for each word a list consisting of the lines where that word appears.

    
asked by anonymous 27.06.2014 / 16:45

1 answer

1

As you have not posted a compilable example, I'll spend more or less what should be done to get the list the way you want:

public Collection<String> split(String book){
    try{
        FileReader path = new FileReader(book);
        LineNumberReader read = new LineNumberReader(path);
        String line;

        while((line = read.readLine()) != null){
            line = line.replaceAll("[â-zA-Z]", " ").toLowerCase();
            //Faz mais sentido ter split como uma variavel local aqui
            String[] split = line.split(" ");

            for(String s : split){
                if(s.length() >= 1 && !palavras.contains(s)){
                    palavras.add(s);
                }
            }           
        }

        path.close();
        read.close();

    }catch(FileNotFoundException e){
        e.getStackTrace();
        System.out.println("Caminho para o arquivo é inválido!");

    }catch(IOException ex){
        ex.getStackTrace();
    }

    return palavras;  
}   
//O map PRECISA de ser do tipo <String, List<String>> em vez de inteiros
public Map<String, List<String>> compare(Collection<String> list, String book){
    FileReader path;

    try {
        path = new FileReader(book);
        LineNumberReader read = new LineNumberReader(path);
        String line;         

        for(String s : list){
            //Não se recomenda usar ArrayList<> foo = new ArrayList<>()
            List<String> lineNro = new ArrayList<>();
            while((line = read.readLine()) != null){                    
                line = line.replaceAll("[â-zA-Z]", " ").toLowerCase();
                String[] split = line.split(" ");                    
                for(String a : split){                        
                    if(s.equals(a)){
                        //Adicione a linha que contém a palavra
                        lineNro.add(line);
                    }
                    // cria o mapa da forma esperada
                    words.put(a, lineNro);                        
                }
            }
        }

        for(Map.Entry<String, List<String>> e : words.entrySet()){
            System.out.println(e);
        }

        path.close();
        read.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    } 
    return words;      
}

I do not guarantee it is completely correct, but I believe it will give you a good direction on how to proceed.

Note: I believe that the lineNro variable should now be replaced by something more meaningful.

    
07.07.2014 / 19:21