📄 associationmartix.java
字号:
package searchingEngine.queryExpansion;
import java.util.*;
import searchingEngine.dataPreprocessing.invertedFile.DocNode;
import searchingEngine.dataPreprocessing.invertedFile.TermNode;
import searchingEngine.dataPreprocessing.wordPosition.DocNodeWpos;
import searchingEngine.utilites.dataConverter.RawConverter;
import searchingEngine.utilites.sorting.GetSortedList;
public class AssociationMartix {
/**
* @param args
*/
public static final boolean IS_METRIC = true;
public static final boolean IS_NOT_METRIC = false;
public final int termAmt;
public final int termAmt2;
private HeadNode[] assoMat;
public AssociationMartix(int termAmt){
this.termAmt = termAmt;
termAmt2 = -1;
assoMat = new HeadNode[termAmt];
}
public AssociationMartix(int termAmt,int termAmt2){
this.termAmt = termAmt;
this.termAmt2 = termAmt2;
assoMat = new HeadNode[termAmt];
}
public HeadNode[] getAssoMat(){
return assoMat;
}
private int getFrequence(DocNodeWpos docNodeWpos) {
return docNodeWpos.wpos_list.size();
}
private int getFrequence(TermNode termNode) {
int result=0;
for (int i =0;i<termNode.doc_list.size();i++)
result += getFrequence((DocNodeWpos)termNode.doc_list.get(i));
return result;
}
private int Cij(TermNode term0,TermNode term1) {
int term0Index = 0,term1Index = 0;
LinkedList<DocNode> term0doc_list = term0.doc_list;
LinkedList<DocNode> term1doc_list = term1.doc_list;
int term0DocLength = term0doc_list.size();
int term1DocLength = term1doc_list.size();
int correlation=0;
DocNodeWpos info0;
DocNodeWpos info1;
while (term0Index<term0DocLength && term1Index<term1DocLength) {
info0 = (DocNodeWpos)term0doc_list.get(term0Index);
info1 = (DocNodeWpos)term1doc_list.get(term1Index);
if (info0.compareTo(info1)==0) {
correlation += getFrequence(info0) * getFrequence(info1);
term0Index++;
term1Index++;
} else if (info0.compareTo(info1)<0) {
term0Index++;
} else term1Index++;
}
return correlation;
}
private int r(int wpos0, int wpos1) {
return Math.abs(wpos0-wpos1);
}
private float inverseR (int wpos0, int wpos1) {
return (float)1.0/(float)r(wpos0,wpos1);
}
private float Mij(TermNode term0,TermNode term1) {
int term0Index = 0,term1Index = 0;
LinkedList<DocNode> term0DocNode = term0.doc_list;
LinkedList<DocNode> term1DocNode = term1.doc_list;
int term0DocLength = term0DocNode.size();
int term1DocLength = term1DocNode.size();
float correlation=0;
float subCor = 0;
DocNodeWpos info0;
DocNodeWpos info1;
while (term0Index<term0DocLength && term1Index<term1DocLength) {
info0 = (DocNodeWpos)term0DocNode.get(term0Index);
info1 = (DocNodeWpos)term1DocNode.get(term1Index);
if (info0.compareTo(info1)==0) {
for (int i=0; i<info0.wpos_list.size(); i++) {
for (int j=0; j<info1.wpos_list.size(); j++) {
subCor+= inverseR(info0.wpos_list.get(i),info1.wpos_list.get(j));
}
}
correlation += subCor;
subCor =0;
term0Index++;
term1Index++;
} else if (info0.compareTo(info1)<0) {
term0Index++;
} else term1Index++;
}
return correlation;
}
private float Sij(float Cii, float Cjj, float Cij){
return Cij/(Cii+Cjj-Cij);
}
private float Sij( TermNode Vi, TermNode Vj, float Mij){
return Mij/(getFrequence(Vi)*getFrequence(Vj));
}
private void normalize() {
for (int i=0;i<termAmt;i++)
for (int j=0;j<i;j++) {
setValueAt(Sij(getValueAt(i,i),getValueAt(j,j),getValueAt(i,j)),i,j);
}
for (int i=0;i<termAmt;i++)
setValueAt(Sij(getValueAt(i,i),getValueAt(i,i),getValueAt(i,i)),i,i);
}
private void normalize(List<TermNode> termList) {
for (int i=0;i<termAmt;i++) {
for (int j=0;j<=i;j++) {
setValueAt(Sij(termList.get(i),termList.get(j),getValueAt(i,j)),i,j);
}
}
}
public void generateAssoMat(List<TermNode> list, boolean isMetric){
float correlations[];
for (int i=0;i<termAmt;i++) {
correlations = new float[i+1];
for (int j=0;j<=i;j++) {
if (isMetric) correlations[j] = Mij(list.get(i),list.get(j));
else correlations[j] = Cij(list.get(i),list.get(j));
}
assoMat[i] = new HeadNode(list.get(i).term,correlations);
}
if (isMetric) normalize(list);
else normalize();
}
public void generateAssoMat(List<TermNode> list,List<TermNode> list2, boolean isMetric){
float correlations[];
for (int i=0;i<termAmt;i++) {
correlations = new float[termAmt2];
for (int j=0;j<=termAmt2;j++) {
if (isMetric) correlations[j] = Mij(list.get(i),list2.get(j));
else correlations[j] = Cij(list.get(i),list2.get(j));
}
assoMat[i] = new HeadNode(list.get(i).term,correlations);
}
if (isMetric) normalize(list);
else normalize();
}
public void setValueAt(float value, int i, int j) {
assoMat[i].correlations[j] = value;
}
public float getValueAt(int i, int j) {
return assoMat[i].correlations[j];
}
public String toString(){
if (assoMat == null) return "empty";
String result = "";
for(int i =0 ; i<assoMat.length ; i++) {
result += assoMat[i] + "\n";
}
return result;
}
public static void main(String[] args) throws Exception{
//LinkedList<LinkedList<TermNode>> hash_table = (LinkedList<LinkedList<TermNode>>)RawConverter.loadObject("hashtable");
LinkedList<TermNode> termList = (LinkedList<TermNode>)RawConverter.loadObject("G:/ir/wpos/X/combined0.dat");
AssociationMartix am = new AssociationMartix(termList.size());
am.generateAssoMat(termList,IS_METRIC);
System.out.println("AssociationMartix:");
RawConverter.saveObject(am,"G:/ir/wpos/X/am0.dat");
}
public class HeadNode implements Comparable<HeadNode>{
public final String term;
private float correlations[];
public HeadNode(String term,float[] correlations){
this.term = term;
this.correlations = correlations;
}
public int compareTo(String arg0) {
return term.compareToIgnoreCase(arg0);
}
public int compareTo(HeadNode arg0) {
return term.compareToIgnoreCase(arg0.term);
}
public String toString(){
String result = term + " " + " [ ";
for (int i=0; i<correlations.length; i++){
result += correlations[i] + " ";
}
result += "]";
return result;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -