src/Generic/patterns/PatternMatcher.cpp
/** Initialize the private member variables _sentenceSlotMatchScore and 
  * _sentenceBestMatchScore for the given slot.  
  *
  * @param sentencesNearSlotNames The set of sentence indices such that
  *        all of the names appropriate for the slot are contained within
  *        a 5-sentence window around that index.
  */
Initialize the private member variables _sentenceSlotMatchScore and
_sentenceBestMatchScore for the given slot.

@param sentencesNearSlotNames The set of sentence indices such that
all of the names appropriate for the slot are contained within
a 5-sentence window around that index.
Initialize the private member variables _sentenceSlotMatchScore and _sentenceBestMatchScore for the given slot . @param sentencesNearSlotNames The set of sentence indices such that all of the names appropriate for the slot are contained within a 5 - sentence window around that index .
void PatternMatcher::fillSlotScores(const AbstractSlot* slot) {
	DocPropForest::ptr docPropForest = getDocTheory()->getPropForest();
	fillSentencesNearSlotNames(docPropForest, slot);
	Symbol slotLabel = Symbol(L"NONE");
	if (const SimpleSlot* simpleSlot = dynamic_cast<const SimpleSlot*>(slot)) {
		slotLabel = simpleSlot->getLabel();
	}
	boost::wregex exact_match_regex(slot->getBackoffRegexText());
	if (SessionLogger::dbg_or_msg_enabled("PatternMatcher")) {
		std::wstringstream wss;
		wss << L"\nNode matcher:\n";
		slot->getMatcher(AbstractSlot::NODE)->print(wss);
		wss << L"\nEdge matcher:\n";
		slot->getMatcher(AbstractSlot::EDGE)->print(wss);
		wss << L"\nFull matcher:\n";
		slot->getMatcher(AbstractSlot::FULL)->print(wss);
		SessionLogger::dbg("PatternMatcher") << wss.str() << L"\n";
	}
	BOOST_FOREACH(AbstractSlot::MatchType matchType, AbstractSlot::ALL_MATCH_TYPES) {
		_slotSentenceMatchScores[slot][matchType].clear();
		_slotSentenceMatchFeatures[slot][matchType].clear();
	}
	int n_sentences = getDocTheory()->getNSentences();
	for (int cur_sent = 0; cur_sent != n_sentences; cur_sent++) {
		std::wstring sentString = getDocTheory()->getSentenceTheory(cur_sent)->getPrimaryParse()->getRoot()->toTextString();
		bool is_exact_match = false;
		if (boost::regex_search(sentString, exact_match_regex)) {
			is_exact_match = true;		
		} 		
		if (!is_exact_match && (_slotsWithDisabledNameFilter.find(slot) == _slotsWithDisabledNameFilter.end()) &&
		            (_sentencesNearSlotNames[slot].find(cur_sent) == _sentencesNearSlotNames[slot].end()) ) 
		{
			BOOST_FOREACH(AbstractSlot::MatchType matchType, AbstractSlot::ALL_MATCH_TYPES) {
				_slotSentenceMatchScores[slot][matchType].push_back(0.0f);
				_slotSentenceMatchFeatures[slot][matchType].push_back(PatternFeatureSet_ptr());
			}
			SessionLogger::dbg("PatternMatcher") << "NO_NAMES\n";
		} else {
			PropNode::PropNodes_ptr sentence_root_nodes = (*docPropForest)[cur_sent];
			PropNode::PropNodes sentence_nodes;
			PropNode::enumAllNodes(*sentence_root_nodes, sentence_nodes);
			boost::shared_ptr<PropNodeMatch> nodeMatcher = boost::dynamic_pointer_cast<PropNodeMatch>(slot->getMatcher(AbstractSlot::NODE));
			PatternFeatureSet_ptr nodePFS = boost::make_shared<PatternFeatureSet>();
			if (SessionLogger::dbg_or_msg_enabled("PatternMatcher")){
				BOOST_FOREACH(PropNode::PropNode_ptr node, sentence_nodes) {
					std::wostringstream stream;
					node->compactPrint(stream, true, false, false, 0);
					SessionLogger::dbg("PatternMatcher") << cur_sent << " " << stream.str() << "\n";
				}
			}
			BOOST_FOREACH(AbstractSlot::MatchType matchType, AbstractSlot::ALL_MATCH_TYPES) {				
				float score = slot->getMatcher(matchType)->compareToTarget(sentence_nodes, false);
				if (matchType == AbstractSlot::NODE) {
					if (score > 0.0f) {
						boost::shared_ptr<PropMatch> propMatch = slot->getMatcher(matchType);
						for (size_t i = 0; i < propMatch->getMatchCovered().size(); i++) {						
							if (propMatch->getMatchCovered().at(i)) {
								Symbol predicate = nodeMatcher->getNthPredicateSymbol(i);
								nodePFS->addFeature(boost::make_shared<CoveredPropNodePFeature>(matchType, propMatch->getMatchCovering().at(i), 
									propMatch->getMatchCovered().at(i), slotLabel, predicate, int(i), int(propMatch->getMatchCovered().size()),
									getActiveLanguageVariant()));
							}
						}
					} else if (is_exact_match) {
						boost::shared_ptr<PropMatch> propMatch = slot->getMatcher(matchType);
						CoveredPropNodePFeature::CPN_struct cpn_struct;
						cpn_struct.pnode_sent_no = cur_sent;
						cpn_struct.pnode_start_token = 0;
						cpn_struct.pnode_end_token = getDocTheory()->getSentenceTheory(cur_sent)->getTokenSequence()->getNTokens() - 1;
						cpn_struct.query_predicate = Symbol(L"exact_match");
						cpn_struct.document_predicate = Symbol(L"exact_match");
						cpn_struct.query_slot = slotLabel;
						cpn_struct.matcher_total = int(propMatch->getMatchCovered().size());
						for (size_t i = 0; i < propMatch->getMatchCovered().size(); i++) {	
							cpn_struct.matcher_index = static_cast<int>(i);
							nodePFS->addFeature(boost::make_shared<CoveredPropNodePFeature>(matchType, 1.05f, cpn_struct, getActiveLanguageVariant()));
						}
					}
				}
				if (is_exact_match)
					score = 1.05f;
				_slotSentenceMatchScores[slot][matchType].push_back(score);
			}
			BOOST_FOREACH(AbstractSlot::MatchType matchType, AbstractSlot::ALL_MATCH_TYPES) {				
				_slotSentenceMatchFeatures[slot][matchType].push_back(nodePFS);
			}
		}
	}
	BOOST_FOREACH(AbstractSlot::MatchType matchType, AbstractSlot::ALL_MATCH_TYPES) {
		const std::vector<float> &scores = _slotSentenceMatchScores[slot][matchType];
		_slotBestMatchScores[slot][matchType] = *(std::max_element(scores.begin(), scores.end()));
	}
}
void PatternMatcher :: fillSlotScores ( const AbstractSlot * slot ) { DocPropForest :: ptr docPropForest = getDocTheory ( ) -> getPropForest ( ) ; fillSentencesNearSlotNames ( docPropForest , slot ) ; Symbol slotLabel = Symbol ( L" " ) ; if ( const SimpleSlot * simpleSlot = dynamic_cast < const SimpleSlot * > ( slot ) ) { slotLabel = simpleSlot -> getLabel ( ) ; } boost :: wregex exact_match_regex ( slot -> getBackoffRegexText ( ) ) ; if ( SessionLogger :: dbg_or_msg_enabled ( " " ) ) { std :: wstringstream wss ; wss << L" \n \n " ; slot -> getMatcher ( AbstractSlot :: NODE ) -> print ( wss ) ; wss << L" \n \n " ; slot -> getMatcher ( AbstractSlot :: EDGE ) -> print ( wss ) ; wss << L" \n \n " ; slot -> getMatcher ( AbstractSlot :: FULL ) -> print ( wss ) ; SessionLogger :: dbg ( " " ) << wss . str ( ) << L" \n " ; } BOOST_FOREACH ( AbstractSlot :: MatchType matchType , AbstractSlot :: ALL_MATCH_TYPES ) { _slotSentenceMatchScores [ slot ] [ matchType ] . clear ( ) ; _slotSentenceMatchFeatures [ slot ] [ matchType ] . clear ( ) ; } int n_sentences = getDocTheory ( ) -> getNSentences ( ) ; for ( int cur_sent = 0 ; cur_sent != n_sentences ; cur_sent ++ ) { std :: wstring sentString = getDocTheory ( ) -> getSentenceTheory ( cur_sent ) -> getPrimaryParse ( ) -> getRoot ( ) -> toTextString ( ) ; bool is_exact_match = false ; if ( boost :: regex_search ( sentString , exact_match_regex ) ) { is_exact_match = true ; } if ( ! is_exact_match && ( _slotsWithDisabledNameFilter . find ( slot ) == _slotsWithDisabledNameFilter . end ( ) ) && ( _sentencesNearSlotNames [ slot ] . find ( cur_sent ) == _sentencesNearSlotNames [ slot ] . end ( ) ) ) { BOOST_FOREACH ( AbstractSlot :: MatchType matchType , AbstractSlot :: ALL_MATCH_TYPES ) { _slotSentenceMatchScores [ slot ] [ matchType ] . push_back ( 0.0f ) ; _slotSentenceMatchFeatures [ slot ] [ matchType ] . push_back ( PatternFeatureSet_ptr ( ) ) ; } SessionLogger :: dbg ( " " ) << " \n " ; } else { PropNode :: PropNodes_ptr sentence_root_nodes = ( * docPropForest ) [ cur_sent ] ; PropNode :: PropNodes sentence_nodes ; PropNode :: enumAllNodes ( * sentence_root_nodes , sentence_nodes ) ; boost :: shared_ptr < PropNodeMatch > nodeMatcher = boost :: dynamic_pointer_cast < PropNodeMatch > ( slot -> getMatcher ( AbstractSlot :: NODE ) ) ; PatternFeatureSet_ptr nodePFS = boost :: make_shared < PatternFeatureSet > ( ) ; if ( SessionLogger :: dbg_or_msg_enabled ( " " ) ) { BOOST_FOREACH ( PropNode :: PropNode_ptr node , sentence_nodes ) { std :: wostringstream stream ; node -> compactPrint ( stream , true , false , false , 0 ) ; SessionLogger :: dbg ( " " ) << cur_sent << " " << stream . str ( ) << " \n " ; } } BOOST_FOREACH ( AbstractSlot :: MatchType matchType , AbstractSlot :: ALL_MATCH_TYPES ) { float score = slot -> getMatcher ( matchType ) -> compareToTarget ( sentence_nodes , false ) ; if ( matchType == AbstractSlot :: NODE ) { if ( score > 0.0f ) { boost :: shared_ptr < PropMatch > propMatch = slot -> getMatcher ( matchType ) ; for ( size_t i = 0 ; i < propMatch -> getMatchCovered ( ) . size ( ) ; i ++ ) { if ( propMatch -> getMatchCovered ( ) . at ( i ) ) { Symbol predicate = nodeMatcher -> getNthPredicateSymbol ( i ) ; nodePFS -> addFeature ( boost :: make_shared < CoveredPropNodePFeature > ( matchType , propMatch -> getMatchCovering ( ) . at ( i ) , propMatch -> getMatchCovered ( ) . at ( i ) , slotLabel , predicate , int ( i ) , int ( propMatch -> getMatchCovered ( ) . size ( ) ) , getActiveLanguageVariant ( ) ) ) ; } } } else if ( is_exact_match ) { boost :: shared_ptr < PropMatch > propMatch = slot -> getMatcher ( matchType ) ; CoveredPropNodePFeature :: CPN_struct cpn_struct ; cpn_struct . pnode_sent_no = cur_sent ; cpn_struct . pnode_start_token = 0 ; cpn_struct . pnode_end_token = getDocTheory ( ) -> getSentenceTheory ( cur_sent ) -> getTokenSequence ( ) -> getNTokens ( ) - 1 ; cpn_struct . query_predicate = Symbol ( L" " ) ; cpn_struct . document_predicate = Symbol ( L" " ) ; cpn_struct . query_slot = slotLabel ; cpn_struct . matcher_total = int ( propMatch -> getMatchCovered ( ) . size ( ) ) ; for ( size_t i = 0 ; i < propMatch -> getMatchCovered ( ) . size ( ) ; i ++ ) { cpn_struct . matcher_index = static_cast < int > ( i ) ; nodePFS -> addFeature ( boost :: make_shared < CoveredPropNodePFeature > ( matchType , 1.05f , cpn_struct , getActiveLanguageVariant ( ) ) ) ; } } } if ( is_exact_match ) score = 1.05f ; _slotSentenceMatchScores [ slot ] [ matchType ] . push_back ( score ) ; } BOOST_FOREACH ( AbstractSlot :: MatchType matchType , AbstractSlot :: ALL_MATCH_TYPES ) { _slotSentenceMatchFeatures [ slot ] [ matchType ] . push_back ( nodePFS ) ; } } } BOOST_FOREACH ( AbstractSlot :: MatchType matchType , AbstractSlot :: ALL_MATCH_TYPES ) { const std :: vector < float > & scores = _slotSentenceMatchScores [ slot ] [ matchType ] ; _slotBestMatchScores [ slot ] [ matchType ] = * ( std :: max_element ( scores . begin ( ) , scores . end ( ) ) ) ; } }
Initialize the private member variables _sentenceSlotMatchScore and
 _sentenceBestMatchScore for the given slot.
Initialize the private member variables _sentenceSlotMatchScore and _sentenceBestMatchScore for the given slot .
// Get the PropTree forest for the document.
 // Find which sentences are near names from the slot
 // Get a regex that can be used to check if we have an exact match for a slot.
 //std::wcerr << wss.str() << L"\n";
 // Clear our slot sentence matches
 // Match each sentence against the slot, and record the scores.
 // We still want to run the prop-matching below, so we can get the node coverage as best as possible
 // The slot's names were not all found near this sentence: set
 // all scores to zero.
 // All of the slot's names were found near this sentence: use
 // each of the slot's PropMatchers to compare the slot's PropTree 
 // with the sentence's PropTree.
 // Use node coverage for all matchers, since other matchers don't store it right
 //SessionLogger::dbg("PatternMatcher") << "SCORE\n";
 // Determine the maximum match score for each match type.
