Example of CalculateBeta.java
This commit is contained in:
parent
eb4aeef8d1
commit
aca200fcc7
2 changed files with 77 additions and 29 deletions
|
@ -2,24 +2,83 @@
|
|||
|
||||
package edu.cornell.mannlib.vitro.webapp.search.solr;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.solr.common.SolrInputDocument;
|
||||
import org.apache.solr.common.SolrInputField;
|
||||
|
||||
import com.hp.hpl.jena.ontology.OntModel;
|
||||
import com.hp.hpl.jena.rdf.model.Model;
|
||||
import com.hp.hpl.jena.rdf.model.Property;
|
||||
import com.hp.hpl.jena.rdf.model.RDFNode;
|
||||
import com.hp.hpl.jena.rdf.model.Resource;
|
||||
import com.hp.hpl.jena.rdf.model.StmtIterator;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
|
||||
import edu.cornell.mannlib.vitro.webapp.search.VitroTermNames;
|
||||
|
||||
public class CalculateBeta implements DocumentModifier{
|
||||
|
||||
private static final String[] fieldsToAddBetaTo = {
|
||||
VitroTermNames.NAME_RAW,
|
||||
VitroTermNames.NAME_LOWERCASE,
|
||||
VitroTermNames.NAME_UNSTEMMED,
|
||||
VitroTermNames.NAME_STEMMED
|
||||
};
|
||||
|
||||
private static final String[] fieldsToMultiplyBetaBy = {
|
||||
VitroTermNames.ALLTEXT,
|
||||
VitroTermNames.ALLTEXTUNSTEMMED,
|
||||
};
|
||||
|
||||
Model fullModel;
|
||||
int totalInd;
|
||||
public static Map<String,Float> betas = new Hashtable<String,Float>();
|
||||
|
||||
public CalculateBeta(OntModel fullModel){
|
||||
this.fullModel=fullModel;
|
||||
this.totalInd = fullModel.listIndividuals().toList().size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void modifyDocument(Individual individual, SolrInputDocument doc) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
// get beta value
|
||||
float beta = 0;
|
||||
if(betas.containsKey(individual.getURI())){
|
||||
beta = betas.get(individual.getURI());
|
||||
}else{
|
||||
beta = calculateBeta(individual.getURI()); // or calculate & put in map
|
||||
betas.put(individual.getURI(), beta);
|
||||
}
|
||||
//doc.addField(term.BETA,beta);
|
||||
|
||||
/*
|
||||
may want to do something like:
|
||||
|
||||
Field f = doc.getField(termALLTEXTUSTEMMED);
|
||||
f.setBoost( beta * f.getBoost() )
|
||||
|
||||
|
||||
*/
|
||||
for(String term: fieldsToAddBetaTo){
|
||||
SolrInputField f = doc.getField( term );
|
||||
f.setBoost( beta + f.getBoost() );
|
||||
}
|
||||
|
||||
for(String term: fieldsToMultiplyBetaBy){
|
||||
SolrInputField f = doc.getField( term );
|
||||
f.setBoost( beta * f.getBoost() );
|
||||
}
|
||||
|
||||
doc.setDocumentBoost( beta * doc.getDocumentBoost() );
|
||||
}
|
||||
|
||||
public float calculateBeta(String uri){
|
||||
float beta=0;
|
||||
RDFNode node = (Resource) fullModel.getResource(uri);
|
||||
StmtIterator stmtItr = fullModel.listStatements((Resource)null, (Property)null,node);
|
||||
int Conn = stmtItr.toList().size();
|
||||
beta = (float)Conn/totalInd;
|
||||
beta *= 100;
|
||||
beta += 1;
|
||||
return beta;
|
||||
}
|
||||
|
||||
public Float getBeta(String uri){
|
||||
return betas.get(uri);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -55,7 +55,7 @@ public class IndividualToSolrDocument implements Obj2DocIface {
|
|||
|
||||
private List<DocumentModifier> documentModifiers = new ArrayList<DocumentModifier>();
|
||||
|
||||
public static Map<String,Float> betas = new Hashtable<String,Float>();
|
||||
|
||||
|
||||
private static List<String> contextNodeClassNames = new ArrayList<String>();
|
||||
|
||||
|
@ -209,18 +209,7 @@ public class IndividualToSolrDocument implements Obj2DocIface {
|
|||
log.debug("could not index name of related object: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// adding beta value
|
||||
|
||||
float beta = 0;
|
||||
if(betas.containsKey(uri)){
|
||||
beta = betas.get(uri);
|
||||
}else{
|
||||
beta = searchQueryHandler.calculateBeta(uri); // or calculate & put in map
|
||||
betas.put(uri, beta);
|
||||
}
|
||||
//doc.addField(term.BETA,beta);
|
||||
}
|
||||
|
||||
// adding PHI value
|
||||
boolean isPerson = (superClassNames.contains("Person")) ? true : false ;
|
||||
|
@ -234,10 +223,10 @@ public class IndividualToSolrDocument implements Obj2DocIface {
|
|||
|
||||
//doc.addField(term.PHI, phi); // adding phi value
|
||||
|
||||
doc.addField(term.NAME_RAW, value, NAME_BOOST+beta+phi);
|
||||
doc.addField(term.NAME_LOWERCASE, value.toLowerCase(),NAME_BOOST+beta+phi);
|
||||
doc.addField(term.NAME_UNSTEMMED, value,NAME_BOOST+beta+phi);
|
||||
doc.addField(term.NAME_STEMMED, value, NAME_BOOST+beta+phi);
|
||||
doc.addField(term.NAME_RAW, value, NAME_BOOST+phi);
|
||||
doc.addField(term.NAME_LOWERCASE, value.toLowerCase(),NAME_BOOST+phi);
|
||||
doc.addField(term.NAME_UNSTEMMED, value,NAME_BOOST+phi);
|
||||
doc.addField(term.NAME_STEMMED, value, NAME_BOOST+phi);
|
||||
doc.addField(term.NAME_PHONETIC, value, PHONETIC_BOOST);
|
||||
|
||||
long tContextNodes = System.currentTimeMillis();
|
||||
|
@ -323,10 +312,10 @@ public class IndividualToSolrDocument implements Obj2DocIface {
|
|||
log.debug("time to include data property statements, object property statements in the index: " + Long.toString(System.currentTimeMillis() - tPropertyStatements));
|
||||
|
||||
String alltext = allTextValue.toString();
|
||||
doc.addField(term.ALLTEXT, alltext, 2.5F*beta*phi);
|
||||
doc.addField(term.ALLTEXTUNSTEMMED, alltext, 2.5F*beta*phi);
|
||||
doc.addField(term.ALLTEXT, alltext, 2.5F*phi);
|
||||
doc.addField(term.ALLTEXTUNSTEMMED, alltext, 2.5F*phi);
|
||||
doc.addField(term.ALLTEXT_PHONETIC, alltext, PHONETIC_BOOST);
|
||||
doc.setDocumentBoost(2.5F*beta*phi);
|
||||
doc.setDocumentBoost(2.5F*phi);
|
||||
|
||||
//run the document modifiers
|
||||
if( documentModifiers != null ){
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue