资源简介
simrank 算法实现 及 相应文件 社会计算课程实践
代码片段和文件信息
package hty.simrank;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.HashMap;
public class MySimRank {
public HashMap queriesIdMap = new HashMap();
public Integer queryIdGen = 1;
public HashMap adsIdMap = new HashMap();
public Integer adIdGen = 1;
// public HashMap> linksMap = new
// HashMap>();
// public HashMap> queryScore = new
// HashMap>();
// public HashMap> adScore = new
// HashMap>();
public Double[][] queryScoreK = new Double[100][100];
public Double[][] queryScore = new Double[100][100];
public Double[][] adScore = new Double[100][100];
public Integer[][] linksAry = new Integer[100][100];
public void loadGraphFromFile(File file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
int index1 = line.indexOf(“->“);
if (index1 == -1) {
continue;
}
String[] links = line.split(“->“);
String query = links[0];
String adsStr = links[1];
String[] ads = adsStr.split(“;“);
for (int i = 0; i < ads.length; i++) {
if (adsIdMap.get(ads[i]) != null) {
linksAry[queryIdGen][adsIdMap.get(ads[i])] = 1;
} else {
adsIdMap.put(ads[i] adIdGen);
linksAry[queryIdGen][adIdGen] = 1;
adIdGen++;
}
if (linksAry[0][adsIdMap.get(ads[i])] == null) {
linksAry[0][adsIdMap.get(ads[i])] = 0;
}
linksAry[0][adsIdMap.get(ads[i])] += 1;
if (linksAry[queryIdGen][0] == null) {
linksAry[queryIdGen][0] = 0;
}
linksAry[queryIdGen][0] += 1;
}
queriesIdMap.put(queryIdGen query);
queryIdGen++;
}
reader.close();
}
public void initScore() {
// queryScore
for (int i = 0; i < queryIdGen; i++) {
for (int j = 0; j < queryIdGen; j++) {
if (i == j){
queryScore[i][j] = 1.0;
queryScoreK[i][j] = 1.0;
}
else{
queryScore[i][j] = 0.0;
queryScoreK[i][j] = 0.0;
}
}
}
// adScore
for (int i = 0; i < adIdGen; i++) {
for (int j = 0; j < adIdGen; j++) {
if (i == j)
adScore[i][j] = 1.0;
else
adScore[i][j] = 0.0;
}
}
}
public void getSimRankScore(double c) {
// 计算query score
for (int i = 1; i < queryIdGen; i++) {
int numOfQuery1 = linksAry[i][0];
for (int j = i + 1; j < queryIdGen; j++) {
int numOfQuery2 = linksAry[j][0];
double scoreTemp = c / (numOfQuery1 * numOfQuery2);
scoreTemp *= getAdsSimilar(i j);
queryScore[j][i] = scoreTemp;
queryScore[i][j] = scoreTemp;
}
}
//计算ad score
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 5894 2014-06-05 16:48 MySimRank.java
文件 79 2014-06-05 15:37 transMatrix2.txt
- 上一篇:基于Android的网上购物系统
- 下一篇:junit-4.12.jar包完整版
评论
共有 条评论