从嵌套的哈希映射中获取、放置键和值。

huangapple go评论79阅读模式
英文:

Get,Put key and values from nested hashmap

问题

我想创建一个嵌套的HashMap,用于返回多个文件中各项的频率。就像这样:

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

我已经能够返回一个术语在文件中出现的次数。

Map<String, Integer> map = new HashMap<>(); // 用于频率计数
String str = "Wikipedia is a free online encyclopedia, created and edited by volunteers around the world."; // 假设str是文件a.java的内容

// 查询字符串
String query = "edited Wikipedia volunteers";

// 在空格上分割给定字符串和查询字符串
String[] strArr = str.split("\\s+");
String[] queryArr = query.split("\\s+");

// 用于保存字符串中每个查询词的频率的映射
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);
        }
    }
}

// 显示映射
System.out.println(map);

在我的代码中,它逐个计算给定查询的频率。但我想将查询术语及其频率与其文件名进行映射。我在网络上搜索了解决方案,但发现很难找到适用于我的解决方案。任何帮助将不胜感激!

英文:

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

Map&lt;String, Map&lt;String, Integer&gt;&gt; wordToDocumentMap=new HashMap&lt;&gt;();

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

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

    // The query string
    String query = &quot;edited Wikipedia volunteers&quot;;

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

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

    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!

答案1

得分: 1

public Map<String, Integer> createFreqMap(String str, String query) {

    Map<String, Integer> map = new HashMap<>(); // 用于频率计数

    // 查询字符串
    String query = "edited Wikipedia volunteers";

    // 在空格上分割给定字符串和查询字符串
    String[] strArr = str.split("\\s+");
    String[] queryArr = query.split("\\s+");

    // 用于保存字符串中每个查询词的频率的映射
    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);
            }
        }
    }

    // 显示映射
    System.out.println(map);
    return map;
}

// 现在您有一个巧妙的函数,它可以从字符串和查询构建映射

// 现在您要设置一个系统,将文件读入字符串中

// 有许多方法可以做到这一点。您可以在这里查看一些适用于不同Java版本的方法: https://stackoverflow.com/a/326440/9789673

// 让我们选择这种方式(假设>Java 11):
String content = Files.readString(path, StandardCharsets.US_ASCII);

// 其中路径是您想要的文件的路径。

// 现在我们可以把所有这些放在一起:

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));
}
英文:

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&lt;String, Integer&gt; createFreqMap(String str, String query) {
Map&lt;String, Integer&gt; map = new HashMap&lt;&gt;();//for frequecy count       
// The query string
String query = &quot;edited Wikipedia volunteers&quot;;
// Split the given string and the query string on space
String[] strArr = str.split(&quot;\\s+&quot;);
String[] queryArr = query.split(&quot;\\s+&quot;);
// Map to hold the frequency of each word of query in the string
Map&lt;String, Integer&gt; map = new HashMap&lt;&gt;();
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 = [&quot;this.txt&quot;, &quot;that.txt&quot;]
Map&lt;String, Map&lt;String, Integer&gt;&gt; output = new HashMap&lt;&gt;();
String query = &quot;edited Wikipedia volunteers&quot;; //String query = &quot;hello&quot;;
for (int i = 0; i &lt; paths.length; i++) {
String content = Files.readString(paths[i], StandardCharsets.US_ASCII);
output.put(paths[i], createFreqMap(content, query);
}

huangapple
  • 本文由 发表于 2020年8月20日 04:20:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63494435.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定