Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

MaxNode.hpp

Go to the documentation of this file.
00001 /*==========================================================================
00002  * Copyright (c) 2004 University of Massachusetts.  All Rights Reserved.
00003  *
00004  * Use of the Lemur Toolkit for Language Modeling and Information Retrieval
00005  * is subject to the terms of the software license set forth in the LICENSE
00006  * file included with this software, and also available at
00007  * http://www.lemurproject.org/license.html
00008  *
00009  *==========================================================================
00010  */
00011 
00012 
00013 //
00014 // MaxNode
00015 //
00016 // 1 April 2004 -- tds
00017 //
00018 
00019 #ifndef INDRI_MAXNODE_HPP
00020 #define INDRI_MAXNODE_HPP
00021 
00022 #include "indri/BeliefNode.hpp"
00023 #include "indri/ScoredExtentResult.hpp"
00024 #include "indri/Annotator.hpp"
00025 #include "indri/ExtentRestrictionNode.hpp"
00026 
00027 namespace indri
00028 {
00029   namespace infnet
00030   {
00031     
00032     class MaxNode : public BeliefNode {
00033     private:
00034       std::vector<BeliefNode*> _children;
00035       indri::utility::greedy_vector<indri::api::ScoredExtentResult> _scores;
00036       indri::utility::greedy_vector<bool> _matches;
00037       std::string _name;
00038 
00039     public:
00040       MaxNode( const std::string& name ) : _name(name) {}
00041       MaxNode( const std::string& name, const std::vector<BeliefNode*>& children ) :
00042         _children( children ),
00043         _name( name )
00044       {
00045         // if we have children - set the sibling flag...
00046         if (_children.size() > 1) {
00047           for (int i=0; i < _children.size(); i++) {
00048             if (_children[i]) {
00049               _children[i]->setSiblingsFlag(1);
00050             }
00051           }
00052         }
00053 
00054 
00055         if(_children.size() == 1) { 
00056           ExtentRestrictionNode* ernode=dynamic_cast<indri::infnet::ExtentRestrictionNode *>(_children[0]);
00057           if(ernode) {
00058             // ExtentRestrictionNode inside a #combine node, need to score unmatched extents!
00059             _children[0]->setSiblingsFlag(1); 
00060           }
00061         }
00062 
00063       }
00064 
00065       lemur::api::DOCID_T nextCandidateDocument() {
00066         lemur::api::DOCID_T candidate = MAX_INT32;
00067 
00068         for( unsigned int i=0; i<_children.size(); i++ ) {
00069           candidate = lemur_compat::min<lemur::api::DOCID_T>( candidate, _children[i]->nextCandidateDocument() );
00070         }
00071 
00072         return candidate;
00073       }
00074 
00075       indri::utility::greedy_vector<indri::api::ScoredExtentResult>& score( lemur::api::DOCID_T documentID, indri::index::Extent &extent, int documentLength ) {
00076         double maxScore = INDRI_TINY_SCORE;
00077         bool scored = false;
00078         
00079         for( unsigned int i=0; i<_children.size(); i++ ) {
00080           const indri::utility::greedy_vector<indri::api::ScoredExtentResult>& childResults = _children[i]->score( documentID, extent, documentLength );
00081 
00082           for( unsigned int j=0; j<childResults.size(); j++ ) {
00083             maxScore = lemur_compat::max<double>( maxScore, childResults[j].score );
00084             scored = true;
00085           }
00086         }
00087 
00088         _scores.clear();
00089         if (scored) 
00090           {
00091             indri::api::ScoredExtentResult result(extent);
00092             result.score = maxScore;
00093             result.document = documentID;
00094             _scores.push_back( result );
00095           }
00096         
00097         return _scores;
00098       }
00099 
00100       void annotate( class Annotator& annotator, lemur::api::DOCID_T documentID, indri::index::Extent &extent ) {
00101         annotator.add( this, documentID, extent);
00102 
00103         // find the maximum score here, then descend only into that one
00104         double maxScore = INDRI_TINY_SCORE;
00105         int maxI = -1;
00106         int maxJ = -1;
00107         int maxBegin = -1;
00108         int maxEnd = -1;
00109 
00110         indri::api::ScoredExtentResult maxResult((double)maxScore, -1);
00111 
00112         for( unsigned int i=0; i<_children.size(); i++ ) {
00113           const indri::utility::greedy_vector<indri::api::ScoredExtentResult>& childResults = _children[i]->score( documentID, extent, extent.end);
00114   
00115           if (childResults.size() > 0) {
00116             for( unsigned int j=0; j<childResults.size(); j++ ) {
00117               if ( maxResult.score < childResults[j].score ) {
00118                 maxResult=childResults[j];
00119                 maxI = i;
00120               }
00121             }
00122           }
00123         }
00124 
00125         indri::index::Extent childExtent;
00126         childExtent.begin=maxResult.begin;
00127         childExtent.end=maxResult.end;
00128         _children[maxI]->annotate( annotator, documentID, childExtent );
00129       }
00130 
00131       double maximumScore() {
00132         double maxScore = INDRI_TINY_SCORE;
00133 
00134         for( unsigned int i=0; i<_children.size(); i++ ) {
00135           maxScore = lemur_compat::max<double>( maxScore, _children[i]->maximumScore() );  
00136         }
00137 
00138         return maxScore;
00139       }
00140 
00141       double maximumBackgroundScore() {
00142         double maxScore = INDRI_TINY_SCORE;
00143 
00144         for( unsigned int i=0; i<_children.size(); i++ ) {
00145           maxScore = lemur_compat::max<double>( maxScore, _children[i]->maximumBackgroundScore() );  
00146         }
00147 
00148         return maxScore;
00149       }
00150 
00151       bool hasMatch( lemur::api::DOCID_T documentID ) {
00152         for( unsigned int i=0; i<_children.size(); i++ ) {
00153           if( _children[i]->hasMatch( documentID ) )
00154             return true;
00155         }
00156 
00157         return false;
00158       }
00159 
00160       const indri::utility::greedy_vector<bool>& hasMatch( lemur::api::DOCID_T documentID, const indri::utility::greedy_vector<indri::index::Extent>& extents ) {
00161         _matches.clear();
00162         _matches.resize( extents.size(), false );
00163 
00164         for( unsigned int i=0; i<_children.size(); i++ ) {
00165           const indri::utility::greedy_vector<bool>& kidMatches = _children[i]->hasMatch( documentID, extents );
00166 
00167           for( unsigned int j=0; j<kidMatches.size(); j++ ) {
00168             if( kidMatches[j] ) {
00169               _matches[j] = true;
00170             }
00171           }
00172         }
00173 
00174         return _matches;
00175       }
00176 
00177       void indexChanged( indri::index::Index& index ) {
00178         // do nothing
00179       }
00180 
00181       const std::string& getName() const {
00182         return _name;
00183       }
00184     };
00185   }
00186 }
00187 
00188 #endif // INDRI_MAXNODE_HPP
00189 

Generated on Tue Jun 15 11:02:54 2010 for Lemur by doxygen 1.3.4