javaalgorithmhashmaptreemapinverted-index

Get,Put key and values from nested hashmap


I want to create a nested HashMap which returns the frequency of terms among multiple files. Like,

Map<String, Map<String, Integer>> wordToDocumentMap=new HashMap<>();

I have been able to return the number of times a term appears in a file.

  Map<String, Integer> map = new HashMap<>();//for frequecy count       
   String str = "Wikipedia is a free online encyclopedia, created and edited by 
     volunteers around the world."; //String str suppose a file a.java

    // The query string
    String query = "edited Wikipedia volunteers";

    // Split the given string and the query string on space
    String[] strArr = str.split("\\s+");
    String[] queryArr = query.split("\\s+");

    // Map to hold the frequency of each word of query in the string
    Map<String, Integer> map = new HashMap<>();

    for (String q : queryArr) {
        for (String s : strArr) {
            if (q.equals(s)) {
                map.put(q, map.getOrDefault(q, 0) + 1);
            }
        }
    }

    // Display the map
    System.out.println(map);

In my code its count the frequency of the given query Individually. But I want to Map the query term and its frequency with its filenames. I have searched around the web for a solution but am finding it tough to find a solution that applies to me. Any help would be appreciated!


Solution

  • I hope I'm understanding you correctly.

    What you want is to be able to read in a list of files and map the file name to the map you create in the code above. So let's start with your code and let's turn it into a function:

    public Map<String, Integer> createFreqMap(String str, String query) {
    
        Map<String, Integer> map = new HashMap<>();//for frequecy count       
    
        // The query string
        String query = "edited Wikipedia volunteers";
    
        // Split the given string and the query string on space
        String[] strArr = str.split("\\s+");
        String[] queryArr = query.split("\\s+");
    
        // Map to hold the frequency of each word of query in the string
        Map<String, Integer> map = new HashMap<>();
    
        for (String q : queryArr) {
            for (String s : strArr) {
                if (q.equals(s)) {
                    map.put(q, map.getOrDefault(q, 0) + 1);
                }
            }
        }
    
        // Display the map
        System.out.println(map);
        return map;
    }
    

    OK so now you have a nifty function that makes a map from a string and a query

    Now you're going to want to set up a system for reading in a file to a string.

    There are a bunch of ways to do this. You can look here for some ways that work for different java versions: https://stackoverflow.com/a/326440/9789673

    lets go with this (assuming >java 11):

    String content = Files.readString(path, StandardCharsets.US_ASCII);
    

    Where path is the path to the file you want.

    Now we can put it all together:

    String[] paths = ["this.txt", "that.txt"]
    Map<String, Map<String, Integer>> output = new HashMap<>();
    String query = "edited Wikipedia volunteers"; //String query = "hello";
    for (int i = 0; i < paths.length; i++) {
        String content = Files.readString(paths[i], StandardCharsets.US_ASCII);
        output.put(paths[i], createFreqMap(content, query);
    }