一. 题目如下:
依据实验3建立的索引文件,实现一个基于VSM模型的信息检索程序。
解释下,如何实现一个基于VSM模型的信息检索程序?
// 段落序号,词条, tf-idf值 的二维空间表
private final Table<Integer,String,Double> vectorTable = HashBasedTable.create();
没错,简单无脑的理解,就是给个检索的句子,从这一张二维空间表中找出最佳匹配的段落,这个检索算法模型叫VSM模型
二. 实现步骤
1. 构造数据结构
2. 实现算法
3. 没了,做个测试吧
三. 源代码
1.二维空间表数据结构(准确的讲,它已经实现了检索算法)
package experiment4;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
public class VectorSpace {
// 段落序号, 词条, tf-idf值 的二维空间表
private final Table<Integer,String,Double> vectorTable = HashBasedTable.create();
protected VectorSpace(){
}
//VMS检索
public int VSM(Map<String,Double> words){
int index = 0;
double max = 0;
for(int rkey:vectorTable.rowKeySet()){
Map<String,Double> map = vectorTable.row(rkey);
double cos = cos(map,words);
if(cos>max){
max = cos;
index = rkey;
}
}
return index;
}
/**
* 求两个句子的余弦相似度
* @param map1
*