【发布时间】: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