Java 哈希图到矩阵的转换
Java hashmap to matrix conversion
我启动了一个 hashmap 和一个嵌套的 hashmap 来存储术语、它的出现和频率。
for (i = 1; i < lineTokens.length; i += 2)
{
if (i + 1 >= lineTokens.length) continue;
String fileName = lineTokens[i];
int frequency = Integer.parseInt(lineTokens[i + 1]);
postingList2.put(fileName,frequency);
//System.out.println(postingList2);
}
postingList.put(topic, postingList2);
它给我输出:{cancel={WET4793.txt=16, WET5590.txt=53}, unavailable={WET4291.txt=10}, station info={WET2266.txt=32},宣传计划={WET2776.txt=32},无评级登录={WET5376.txt=76},
我试图用一个矩阵来表示所有的东西。但是我不能将 0 设置为不包含特定术语的文件。
就像:
row-> term
column -> document
mat[row][column]= frequency of occurances of terms in the document.
我在 python 中使用 pandas 数据框轻松完成了。
给定初始 HashMap,转换为 Matrix 需要三个步骤
- 为每个主题创建一个唯一的索引 id (0, 1 ..)
- 为每个文档创建一个唯一的索引 ID (0, 1, ..)
- 使用上述索引填充矩阵
此解决方案将使用地图查找(键为 posting/document)以提高效率。 postings/documents的顺序是可以控制的;这里没有尝试创建特定订单。
步骤 1:为帖子创建唯一 ID 并创建查找映射
Map<String, Integer> topicIndex = new HashMap<>();
List<String> topicList = new ArrayList<>(); // topicList is used to print the matrix
int index = 0;
for (String topic : postingList.keySet()) {
if (!topicIndex.containsKey(topic)) {
topicIndex.put(topic, index++);
topicList.add(topic);
}
}
这张地图的结果是(所有术语现在都有一个唯一的 id):
Topics: {cancel=0, unavailable=1, station info=2, advocacy program=3, no ratingslogin=4}
步骤 2: 为文档创建唯一 ID 并创建查找映射
index = 0;
Map<String, Integer> documentIndex = new HashMap<>();
for (String topic : postingList.keySet()) {
for (String document : postingList.get(topic).keySet()) {
if (!documentIndex.containsKey(document))
documentIndex.put(document, index++);
}
}
这个 Map 的结果是(所有文档现在都有一个唯一的 id):
Documents: {WET4793.txt=0, WET4291.txt=2, WET2266.txt=3, WET2776.txt=4, WET5376.txt=5, WET5590.txt=1}
步骤 3: 创建并填充矩阵
int[][] mat = new int[topicIndex.size()][documentIndex.size()];
for (String topic : postingList.keySet()) {
for (String document : postingList.get(topic).keySet()) {
mat[topicIndex.get(topic)][documentIndex.get(document)] = postingList.get(topic).get(document);
}
}
结果:矩阵现在看起来像这样:
cancel 16 53 0 0 0 0
unavailable 0 0 10 0 0 0
station info 0 0 0 32 0 0
advocacy program 0 0 0 0 32 0
no ratingslogin 0 0 0 0 0 76
编辑:循环打印矩阵
for (int row = 0; row < topicIndex.size(); row++) {
System.out.printf("%-16s", topicList.get(row));
for (int col = 0; col < documentIndex.size(); col++) {
System.out.printf("%2d ", mat[row][col]);
}
System.out.println();
}
我启动了一个 hashmap 和一个嵌套的 hashmap 来存储术语、它的出现和频率。
for (i = 1; i < lineTokens.length; i += 2)
{
if (i + 1 >= lineTokens.length) continue;
String fileName = lineTokens[i];
int frequency = Integer.parseInt(lineTokens[i + 1]);
postingList2.put(fileName,frequency);
//System.out.println(postingList2);
}
postingList.put(topic, postingList2);
它给我输出:{cancel={WET4793.txt=16, WET5590.txt=53}, unavailable={WET4291.txt=10}, station info={WET2266.txt=32},宣传计划={WET2776.txt=32},无评级登录={WET5376.txt=76}, 我试图用一个矩阵来表示所有的东西。但是我不能将 0 设置为不包含特定术语的文件。 就像:
row-> term
column -> document
mat[row][column]= frequency of occurances of terms in the document.
我在 python 中使用 pandas 数据框轻松完成了。
给定初始 HashMap,转换为 Matrix 需要三个步骤
- 为每个主题创建一个唯一的索引 id (0, 1 ..)
- 为每个文档创建一个唯一的索引 ID (0, 1, ..)
- 使用上述索引填充矩阵
此解决方案将使用地图查找(键为 posting/document)以提高效率。 postings/documents的顺序是可以控制的;这里没有尝试创建特定订单。
步骤 1:为帖子创建唯一 ID 并创建查找映射
Map<String, Integer> topicIndex = new HashMap<>();
List<String> topicList = new ArrayList<>(); // topicList is used to print the matrix
int index = 0;
for (String topic : postingList.keySet()) {
if (!topicIndex.containsKey(topic)) {
topicIndex.put(topic, index++);
topicList.add(topic);
}
}
这张地图的结果是(所有术语现在都有一个唯一的 id):
Topics: {cancel=0, unavailable=1, station info=2, advocacy program=3, no ratingslogin=4}
步骤 2: 为文档创建唯一 ID 并创建查找映射
index = 0;
Map<String, Integer> documentIndex = new HashMap<>();
for (String topic : postingList.keySet()) {
for (String document : postingList.get(topic).keySet()) {
if (!documentIndex.containsKey(document))
documentIndex.put(document, index++);
}
}
这个 Map 的结果是(所有文档现在都有一个唯一的 id):
Documents: {WET4793.txt=0, WET4291.txt=2, WET2266.txt=3, WET2776.txt=4, WET5376.txt=5, WET5590.txt=1}
步骤 3: 创建并填充矩阵
int[][] mat = new int[topicIndex.size()][documentIndex.size()];
for (String topic : postingList.keySet()) {
for (String document : postingList.get(topic).keySet()) {
mat[topicIndex.get(topic)][documentIndex.get(document)] = postingList.get(topic).get(document);
}
}
结果:矩阵现在看起来像这样:
cancel 16 53 0 0 0 0
unavailable 0 0 10 0 0 0
station info 0 0 0 32 0 0
advocacy program 0 0 0 0 32 0
no ratingslogin 0 0 0 0 0 76
编辑:循环打印矩阵
for (int row = 0; row < topicIndex.size(); row++) {
System.out.printf("%-16s", topicList.get(row));
for (int col = 0; col < documentIndex.size(); col++) {
System.out.printf("%2d ", mat[row][col]);
}
System.out.println();
}