00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef _COSSIMRETMETHOD_HPP
00016 #define _COSSIMRETMETHOD_HPP
00017
00018 #include "TextQueryRetMethod.hpp"
00019
00021 namespace CosSimParameter {
00022 struct FeedbackParam {
00023 int howManyTerms;
00024 double posCoeff;
00025 };
00026 static int defaultHowManyTerms = 50;
00027 static double defaultPosCoeff = 0.5;
00028 static const char *defaultL2File = "cos.L2";
00029 };
00030
00032 class CosSimQueryRep : public ArrayQueryRep {
00033 public:
00035 CosSimQueryRep(TextQuery &qry, Index &dbIndex, double *idfValue);
00037 CosSimQueryRep(int docId, Index &dbIndex, double *idfValue);
00038 virtual ~CosSimQueryRep() {}
00039 protected:
00040 double *idf;
00041 Index &ind;
00042 };
00043
00045 class CosSimDocRep : public DocumentRep {
00046 public:
00047 CosSimDocRep(int docID, Index &dbIndex, double *idfValue, double norm) :
00048 DocumentRep(docID), ind(dbIndex), idf(idfValue), dNorm(norm) {
00049 }
00050 virtual ~CosSimDocRep() { }
00052 virtual double termWeight(int termID, DocInfo *info) {
00053 return (idf[termID]*info->termCount());
00054 }
00056 virtual double scoreConstant() { return dNorm;}
00057
00058 private:
00059 Index & ind;
00060 double *idf;
00062 double dNorm;
00063 };
00064
00066 class CosSimScoreFunc : public ScoreFunction {
00067 public:
00068 CosSimScoreFunc(Index &dbIndex): ind(dbIndex) {}
00070 virtual double adjustedScore(double origScore, TextQueryRep *qRep,
00071 DocumentRep *dRep) {
00072 return origScore/dRep->scoreConstant();
00073 }
00074 protected:
00075 Index &ind;
00076 };
00077
00079 class CosSimRetMethod : public TextQueryRetMethod {
00080 public:
00082 CosSimRetMethod(Index &dbIndex, ScoreAccumulator &accumulator);
00087 CosSimRetMethod(Index &dbIndex, const char *L2file,
00088 ScoreAccumulator &accumulator);
00089
00090 virtual ~CosSimRetMethod();
00091
00092 virtual TextQueryRep *computeTextQueryRep(TextQuery &qry) {
00093 return (new CosSimQueryRep(qry, ind, idfV));
00094 }
00095
00096 virtual TextQueryRep *computeTextQueryRep(int docid) {
00097 return (new CosSimQueryRep(docid, ind, idfV));
00098 }
00099
00100 virtual DocumentRep *computeDocRep(int docID) {
00101 return (new CosSimDocRep(docID, ind, idfV, docNorm(docID)));
00102 }
00103 virtual ScoreFunction *scoreFunc() {
00104 return (scFunc);
00105 }
00106 virtual void updateTextQuery(TextQueryRep &qryRep, DocIDSet &relDocs);
00107 void setFeedbackParam(CosSimParameter::FeedbackParam &feedbackParam);
00108 protected:
00109
00110 double *idfV;
00111 ScoreFunction *scFunc;
00112 double * docNorms;
00113 int numDocs, numTerms;
00114
00116
00117 CosSimParameter::FeedbackParam fbParam;
00119 const char *L2FileName;
00121
00123 double docNorm(int docID);
00125 void loadDocNorms();
00126 };
00127
00128 inline void CosSimRetMethod::setFeedbackParam(CosSimParameter::FeedbackParam
00129 &feedbackParam) {
00130 fbParam = feedbackParam;
00131 }
00132 #endif