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 string defaultL2File = "cos.L2";
00029 };
00030
00032 class CosSimQueryRep : public ArrayQueryRep {
00033 public:
00035 CosSimQueryRep(const TermQuery &qry, const Index &dbIndex,
00036 double *idfValue);
00038 CosSimQueryRep(DOCID_T docId, const Index &dbIndex, double *idfValue);
00039 virtual ~CosSimQueryRep() {}
00040 protected:
00041 double *idf;
00042 const Index &ind;
00043 };
00044
00046 class CosSimDocRep : public DocumentRep {
00047 public:
00048 CosSimDocRep(DOCID_T docID, double *idfValue, double norm, int dl) :
00049 DocumentRep(docID, dl), idf(idfValue), dNorm(norm) {
00050 }
00051 virtual ~CosSimDocRep() { }
00053 virtual double termWeight(TERMID_T termID, const DocInfo *info) const {
00054 return (idf[termID]*info->termCount());
00055 }
00057 virtual double scoreConstant() const { return dNorm;}
00058
00059 private:
00061 double *idf;
00063 double dNorm;
00064 };
00065
00067 class CosSimScoreFunc : public ScoreFunction {
00068 public:
00069 CosSimScoreFunc(const Index &dbIndex): ind(dbIndex) {}
00071 virtual double adjustedScore(double origScore, const TextQueryRep *qRep,
00072 const DocumentRep *dRep) const {
00073 return origScore/dRep->scoreConstant();
00074 }
00075 protected:
00076 const Index &ind;
00077 };
00078
00080 class CosSimRetMethod : public TextQueryRetMethod {
00081 public:
00083 CosSimRetMethod(const Index &dbIndex, ScoreAccumulator &accumulator);
00088 CosSimRetMethod(const Index &dbIndex, const string &L2file,
00089 ScoreAccumulator &accumulator);
00090
00091 virtual ~CosSimRetMethod();
00092
00093 virtual TextQueryRep *computeTextQueryRep(const TermQuery &qry) {
00094 return (new CosSimQueryRep(qry, ind, idfV));
00095 }
00096
00097 virtual TextQueryRep *computeTextQueryRep(DOCID_T docid) {
00098 return (new CosSimQueryRep(docid, ind, idfV));
00099 }
00100
00101 virtual DocumentRep *computeDocRep(DOCID_T docID) {
00102 return (new CosSimDocRep(docID, idfV, docNorm(docID),
00103 ind.docLength(docID)));
00104 }
00105 virtual ScoreFunction *scoreFunc() {
00106 return (scFunc);
00107 }
00108 virtual void updateTextQuery(TextQueryRep &qryRep,
00109 const DocIDSet &relDocs);
00110 void setFeedbackParam(CosSimParameter::FeedbackParam &feedbackParam);
00111 protected:
00112
00113 double *idfV;
00114 ScoreFunction *scFunc;
00115 double * docNorms;
00116 int numDocs, numTerms;
00117
00119
00120 CosSimParameter::FeedbackParam fbParam;
00122 const string &L2FileName;
00124
00126 double docNorm(DOCID_T docID);
00128 void loadDocNorms();
00129 };
00130
00131 inline void CosSimRetMethod::setFeedbackParam(CosSimParameter::FeedbackParam
00132 &feedbackParam) {
00133 fbParam = feedbackParam;
00134 }
00135 #endif