【问题标题】:Get,Put key and values from nested hashmap从嵌套哈希图中获取、放置键和值
【发布时间】:2020-08-19 20:20:12
【问题描述】:

我想创建一个嵌套的 HashMap,它返回多个文件中术语的频率。喜欢,

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

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

  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);

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

【问题讨论】:

  • 第二个 hashmap 应该包含什么?
  • @beastlyCoder 第二个哈希图是用它的频率来计算术语。 Map map = new HashMap();//用于频率计数
  • wordToDocumentMap.put(fileName, map);
  • 所以你想要这个词,然后它在文件中出现了多少次
  • @Sanzida 你的问题还不清楚。你有一个 Map>。据推测,“整数”是某种计数,其中一个字符串是文件名,另一个字符串是单词?哪个是哪个?您要将文件名映射到词频映射,还是要将单词映射到“此文件多次包含该词”的映射?

标签: java algorithm hashmap treemap inverted-index


【解决方案1】:

希望我理解正确。

您想要的是能够读取文件列表并将文件名映射到您在上面的代码中创建的映射。所以让我们从你的代码开始,让我们把它变成一个函数:

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;
}

好的,现在你有了一个漂亮的函数,可以从字符串和查询生成地图

现在您将要设置一个系统,用于将文件读入字符串。

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

让我们开始吧(假设 >java 11):

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

path 是你想要的文件的路径。

现在我们可以把它们放在一起了:

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);
}

【讨论】:

  • 这是我的查询,其中包含三个词 edited Wikipedia志愿者 。我单独计算它的频率。如果我的文件名是 a.java,我如何将这三个单词与文件名 a.java 映射?我是初学者。你能告诉我一步一步的过程吗?提前致谢
  • 我不确定你的意思。您可以将变量查询设置为您想要的任何内容。所以代替 String query = "hello";可以是 String query = "edited Wikipedia志愿者";
  • 感谢您的反馈。抱歉最后的评论。我现在可以正确理解您的代码了。
猜你喜欢
  • 2018-02-24
  • 1970-01-01
  • 1970-01-01
  • 2020-11-21
  • 2014-10-17
  • 2015-03-28
  • 1970-01-01
  • 2017-04-16
  • 1970-01-01
相关资源
最近更新 更多