src/ProfileGenerator/ProfileSlot.cpp
/*! \brief  Sorts the hypotheses and decides which to output.
*/
\brief  Sorts the hypotheses and decides which to output.
\ brief Sorts the hypotheses and decides which to output .
void ProfileSlot::generateOutputHypotheses(Profile_ptr existingProfile, bool print_to_screen) {
	if (_output_up_to_date && !print_to_screen)
		return;
	_outputHypotheses.clear();
	_namesAlreadyDisplayed.clear();
	_titlesAlreadyDisplayed.clear();
	_nationalitiesAlreadyDisplayed.clear();
	if (_unique_id == DESCRIPTION_UNIQUE_ID) {
		DescriptionHypothesis::gatherExistingProfileInformation(existingProfile, _namesAlreadyDisplayed,
			_titlesAlreadyDisplayed, _nationalitiesAlreadyDisplayed);
	}
	if (_rawHypotheses.empty())
		return;
	if (print_to_screen) {
		SessionLogger::info("PG", "wcout") << "<h2>SLOT: " << _displayName << "</h2>\n";
	}
	std::list<GenericHypothesis_ptr> unusedHypotheses;
	int n_english_hypotheses = 0;
	BOOST_FOREACH (GenericHypothesis_ptr hypoth, _rawHypotheses) {
		unusedHypotheses.push_back(hypoth);
		if (hypoth->nEnglishFacts() > 0)
			n_english_hypotheses++;
	}
	int n_foreign_hypotheses_uploaded = 0;
	while (true) {
		GenericHypothesis_ptr bestHypothesis = getBestHypothesis(unusedHypotheses);
		if (bestHypothesis == GenericHypothesis_ptr())
			break;
		if (reachesCutoff(bestHypothesis)) 
			break;
		removeHypothesis(bestHypothesis, unusedHypotheses);
		if (!isValidHypothesis(bestHypothesis, existingProfile, n_foreign_hypotheses_uploaded, false, print_to_screen))
			continue;
		if (print_to_screen) {
			printHypothesisToScreen(existingProfile, "UPLOADED", bestHypothesis);
		}
		_outputHypotheses.push_back(bestHypothesis);		
		if (bestHypothesis->nEnglishFacts() == 0)
			n_foreign_hypotheses_uploaded++;
	}
	if (_unique_id == EMPLOYER_UNIQUE_ID && _outputHypotheses.size() > 0) {
		bool has_current = false;
		bool has_past = false;
		BOOST_FOREACH (GenericHypothesis_ptr hypoth, _outputHypotheses) {
			EmploymentHypothesis_ptr empHypoth = boost::dynamic_pointer_cast<EmploymentHypothesis>(hypoth);
			if (empHypoth == EmploymentHypothesis_ptr()) continue; 
			if (empHypoth->getTense() == EmploymentHypothesis::CURRENT) has_current = true;
			if (empHypoth->getTense() == EmploymentHypothesis::PAST) has_past = true;
		}
		EmploymentHypothesis::status_tense_t tense_to_find = EmploymentHypothesis::UNSET;
		if (has_current && !has_past) tense_to_find = EmploymentHypothesis::PAST;
		GenericHypothesis_ptr bestHypothesis = GenericHypothesis_ptr();
		if (tense_to_find != EmploymentHypothesis::UNSET)
			bestHypothesis = getBestEmploymentHypothesis(unusedHypotheses, tense_to_find);
		if (bestHypothesis != GenericHypothesis_ptr() && 
			bestHypothesis->nSupportingFacts() > 1 &&
			_outputHypotheses.front()->nDoublyReliableEnglishTextFacts() > 0 &&
			(float)bestHypothesis->nDoublyReliableEnglishTextFacts() / _outputHypotheses.front()->nDoublyReliableEnglishTextFacts() > 0.1 &&
			isValidHypothesis(bestHypothesis, existingProfile, n_foreign_hypotheses_uploaded, true, false))
		{
			if (print_to_screen)
				printHypothesisToScreen(existingProfile, "UPLOADED", bestHypothesis);
			_outputHypotheses.push_back(bestHypothesis);
			removeHypothesis(bestHypothesis, unusedHypotheses);
		}
		while (true) {
			GenericHypothesis_ptr bestHypothesis = getBestHypothesis(unusedHypotheses);
			if (bestHypothesis == GenericHypothesis_ptr())
				break;
			if (_outputHypotheses.size() >= 4) 
				break;
			removeHypothesis(bestHypothesis, unusedHypotheses);
			if (!isValidHypothesis(bestHypothesis, existingProfile, n_foreign_hypotheses_uploaded, true, print_to_screen))
				continue;
			EmploymentHypothesis_ptr empHypoth = boost::dynamic_pointer_cast<EmploymentHypothesis>(bestHypothesis);
			if (empHypoth == EmploymentHypothesis_ptr()) continue; 
			if (bestHypothesis->nDoublyReliableEnglishTextFacts() > 0 &&
				(float)bestHypothesis->nDoublyReliableEnglishTextFacts() / _outputHypotheses.front()->nDoublyReliableEnglishTextFacts() > 0.1 &&
				(empHypoth->hasSecondaryTitleWord() || empHypoth->employerIsPoliticalParty()))
			{
				if (print_to_screen)
					printHypothesisToScreen(existingProfile, "UPLOADED", bestHypothesis);
				_outputHypotheses.push_back(bestHypothesis);
			} else {
				if (print_to_screen)
					printHypothesisToScreen(existingProfile, "CUTOFF", bestHypothesis);
			}
		}
	}
	if (print_to_screen) {
		GenericHypothesis_ptr bestHypothesis = getBestHypothesis(unusedHypotheses);
		if (bestHypothesis != GenericHypothesis_ptr()) {
			printHypothesisToScreen(existingProfile, "CUTOFF", bestHypothesis);
			removeHypothesis(bestHypothesis, unusedHypotheses);
		}
		if (_print_all_hypotheses_to_html) {
			for (int i = 0; i < 50; i++) { 
				GenericHypothesis_ptr bestHypothesis = getBestHypothesis(unusedHypotheses);
				if (bestHypothesis == GenericHypothesis_ptr()) {
					break;
				} else {
					printHypothesisToScreen(existingProfile, "CUTOFF", bestHypothesis);
					removeHypothesis(bestHypothesis, unusedHypotheses);
				}
			}
		}
		int leftovers = static_cast<int>(unusedHypotheses.size());
		bool singular = leftovers == 1;
		const char * toBe = singular ? "is" : "are";
		const char * ending = singular ? "is" : "es";
		SessionLogger::info("PG", "cout") << "<i>There " << toBe << " " << leftovers << " additional hypothes" << ending << "</i>\n";
		SessionLogger::info("PG", "cout") << "<p>\n";
	}
	_output_up_to_date = true;
}
void ProfileSlot :: generateOutputHypotheses ( Profile_ptr existingProfile , bool print_to_screen ) { if ( _output_up_to_date && ! print_to_screen ) return ; _outputHypotheses . clear ( ) ; _namesAlreadyDisplayed . clear ( ) ; _titlesAlreadyDisplayed . clear ( ) ; _nationalitiesAlreadyDisplayed . clear ( ) ; if ( _unique_id == DESCRIPTION_UNIQUE_ID ) { DescriptionHypothesis :: gatherExistingProfileInformation ( existingProfile , _namesAlreadyDisplayed , _titlesAlreadyDisplayed , _nationalitiesAlreadyDisplayed ) ; } if ( _rawHypotheses . empty ( ) ) return ; if ( print_to_screen ) { SessionLogger :: info ( " " , " " ) << " " << _displayName << " \n " ; } std :: list < GenericHypothesis_ptr > unusedHypotheses ; int n_english_hypotheses = 0 ; BOOST_FOREACH ( GenericHypothesis_ptr hypoth , _rawHypotheses ) { unusedHypotheses . push_back ( hypoth ) ; if ( hypoth -> nEnglishFacts ( ) > 0 ) n_english_hypotheses ++ ; } int n_foreign_hypotheses_uploaded = 0 ; while ( true ) { GenericHypothesis_ptr bestHypothesis = getBestHypothesis ( unusedHypotheses ) ; if ( bestHypothesis == GenericHypothesis_ptr ( ) ) break ; if ( reachesCutoff ( bestHypothesis ) ) break ; removeHypothesis ( bestHypothesis , unusedHypotheses ) ; if ( ! isValidHypothesis ( bestHypothesis , existingProfile , n_foreign_hypotheses_uploaded , false , print_to_screen ) ) continue ; if ( print_to_screen ) { printHypothesisToScreen ( existingProfile , " " , bestHypothesis ) ; } _outputHypotheses . push_back ( bestHypothesis ) ; if ( bestHypothesis -> nEnglishFacts ( ) == 0 ) n_foreign_hypotheses_uploaded ++ ; } if ( _unique_id == EMPLOYER_UNIQUE_ID && _outputHypotheses . size ( ) > 0 ) { bool has_current = false ; bool has_past = false ; BOOST_FOREACH ( GenericHypothesis_ptr hypoth , _outputHypotheses ) { EmploymentHypothesis_ptr empHypoth = boost :: dynamic_pointer_cast < EmploymentHypothesis > ( hypoth ) ; if ( empHypoth == EmploymentHypothesis_ptr ( ) ) continue ; if ( empHypoth -> getTense ( ) == EmploymentHypothesis :: CURRENT ) has_current = true ; if ( empHypoth -> getTense ( ) == EmploymentHypothesis :: PAST ) has_past = true ; } EmploymentHypothesis :: status_tense_t tense_to_find = EmploymentHypothesis :: UNSET ; if ( has_current && ! has_past ) tense_to_find = EmploymentHypothesis :: PAST ; GenericHypothesis_ptr bestHypothesis = GenericHypothesis_ptr ( ) ; if ( tense_to_find != EmploymentHypothesis :: UNSET ) bestHypothesis = getBestEmploymentHypothesis ( unusedHypotheses , tense_to_find ) ; if ( bestHypothesis != GenericHypothesis_ptr ( ) && bestHypothesis -> nSupportingFacts ( ) > 1 && _outputHypotheses . front ( ) -> nDoublyReliableEnglishTextFacts ( ) > 0 && ( float ) bestHypothesis -> nDoublyReliableEnglishTextFacts ( ) / _outputHypotheses . front ( ) -> nDoublyReliableEnglishTextFacts ( ) > 0.1 && isValidHypothesis ( bestHypothesis , existingProfile , n_foreign_hypotheses_uploaded , true , false ) ) { if ( print_to_screen ) printHypothesisToScreen ( existingProfile , " " , bestHypothesis ) ; _outputHypotheses . push_back ( bestHypothesis ) ; removeHypothesis ( bestHypothesis , unusedHypotheses ) ; } while ( true ) { GenericHypothesis_ptr bestHypothesis = getBestHypothesis ( unusedHypotheses ) ; if ( bestHypothesis == GenericHypothesis_ptr ( ) ) break ; if ( _outputHypotheses . size ( ) >= 4 ) break ; removeHypothesis ( bestHypothesis , unusedHypotheses ) ; if ( ! isValidHypothesis ( bestHypothesis , existingProfile , n_foreign_hypotheses_uploaded , true , print_to_screen ) ) continue ; EmploymentHypothesis_ptr empHypoth = boost :: dynamic_pointer_cast < EmploymentHypothesis > ( bestHypothesis ) ; if ( empHypoth == EmploymentHypothesis_ptr ( ) ) continue ; if ( bestHypothesis -> nDoublyReliableEnglishTextFacts ( ) > 0 && ( float ) bestHypothesis -> nDoublyReliableEnglishTextFacts ( ) / _outputHypotheses . front ( ) -> nDoublyReliableEnglishTextFacts ( ) > 0.1 && ( empHypoth -> hasSecondaryTitleWord ( ) || empHypoth -> employerIsPoliticalParty ( ) ) ) { if ( print_to_screen ) printHypothesisToScreen ( existingProfile , " " , bestHypothesis ) ; _outputHypotheses . push_back ( bestHypothesis ) ; } else { if ( print_to_screen ) printHypothesisToScreen ( existingProfile , " " , bestHypothesis ) ; } } } if ( print_to_screen ) { GenericHypothesis_ptr bestHypothesis = getBestHypothesis ( unusedHypotheses ) ; if ( bestHypothesis != GenericHypothesis_ptr ( ) ) { printHypothesisToScreen ( existingProfile , " " , bestHypothesis ) ; removeHypothesis ( bestHypothesis , unusedHypotheses ) ; } if ( _print_all_hypotheses_to_html ) { for ( int i = 0 ; i < 50 ; i ++ ) { GenericHypothesis_ptr bestHypothesis = getBestHypothesis ( unusedHypotheses ) ; if ( bestHypothesis == GenericHypothesis_ptr ( ) ) { break ; } else { printHypothesisToScreen ( existingProfile , " " , bestHypothesis ) ; removeHypothesis ( bestHypothesis , unusedHypotheses ) ; } } } int leftovers = static_cast < int > ( unusedHypotheses . size ( ) ) ; bool singular = leftovers == 1 ; const char * toBe = singular ? " " : " " ; const char * ending = singular ? " " : " " ; SessionLogger :: info ( " " , " " ) << " " << toBe << " " << leftovers << " " << ending << " \n " ; SessionLogger :: info ( " " , " " ) << " \n " ; } _output_up_to_date = true ; }
\brief  Sorts the hypotheses and decides which to output.
\ brief Sorts the hypotheses and decides which to output .
// If we are printing to screen, we'd like to regenerate this in this context, even if it's redundant...
 // This is a special case that should only occur for ST_DESCRIPTION,
 //   which we want to make non-redundant with the other slots
 // Get the next best hypothesis
 // Decide whether to finish output
 // Second round of hypothesis finding. Cannot be incorporated in the above 
 // while loop since the ordering isn't entirely dependent upon hypothesis
 // ranking.
 // If missing PAST employer, look to see if best PAST employer is valid
 // should never happen?
 //std::cerr << "Outputting PAST employment hypothesis for: " << UnicodeUtil::toUTF8StdString(existingProfile->getName()) << " (" << bestHypothesis->nSupportingFacts() << ")\n";;
 // If title is something like "member", or organization is a political party, 
 // output it if we have enough confidence in it. Removes any hypothesis it
 // looks at from unusedHypotheses
 // Get the next best hypothesis
 // should never happen?
 //std::cerr << "Outputting secondary employment hypothesis for: " << UnicodeUtil::toUTF8StdString(existingProfile->getName()) << " (" << bestHypothesis->nSupportingFacts() << ")\n";
 // don't print more than 50 to screen
