Compare each word of the given text and save the lines in which the text appears

Asked

Viewed 611 times

0

I have the following methods in java, the split method separates each word from a text and stores it in a Treeset<> to maintain the alphabetical order and returns the Treeset<> in question. The compare method should take this Treeset<> and compare it to the text passed by parameter, taking each word, using the method. getLineNumber of the Linenumberreader class and store each line where that word appears 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 receive the same list of Integer. The expected result It would be a list of words and for each word a list composed by the lines where that word appears.

  • 1

    What is wrong with your code? What is the result obtained and what is expected? If possible create a EMCV

  • Edited, thank you for your attention.

1 answer

1

Since you have not posted a compileable example, I will pass more or less what must 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 can’t guarantee that you’re completely correct, but I believe it will give you a good direction of how to proceed.

Observing: I believe that now the variable lineNro should be replaced by something more significant.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.