src/PredFinder/elf/ElfRelation.cpp
/**
 * Construct a relation from a SnippetFeatureSet found using
 * manual Distillation patterns.
 *
 * @param doc_theory The DocTheory containing the matched
 * SnippetFeatureSet.
 * @param pattern The PatternSet that was used to find
 * the feature_set; only used for metadata.
 * @param feature_set The SnippetFeatureSet that contains
 * SnippetFeatures that we can convert to an ElfRelation.
 *
 * @author nward@bbn.com
 * @date 2010.06.18
 **/
Construct a relation from a SnippetFeatureSet found using
manual Distillation patterns.




Construct a relation from a SnippetFeatureSet found using manual Distillation patterns .
ElfRelation::ElfRelation(const DocTheory* doc_theory, const PatternSet_ptr pattern, const PatternFeatureSet_ptr feature_set) {
	if (doc_theory == NULL)
		throw std::runtime_error("ElfRelation::ElfRelation(DocTheory*, PatternSet_ptr, PatternFeatureSet_ptr): Document theory null");
	_name = std::wstring(pattern->getPatternSetName().to_string());
	_text = L"";
	std::vector<ReturnPatternFeature_ptr> returnFeatures;
	for (size_t i = 0; i < feature_set->getNFeatures(); i++) {
		PatternFeature_ptr feature = feature_set->getFeature(i);
		if (ReturnPatternFeature_ptr rf = dynamic_pointer_cast<ReturnPatternFeature>(feature)) {
			returnFeatures.push_back(rf);
		}
	}
	ReturnPatternFeature_ptr tbdEventFeature;
	std::vector<ReturnPatternFeature_ptr> tbdAgentFeatures;
	std::vector<ReturnPatternFeature_ptr> tbdPatientFeatures;
	BOOST_FOREACH (ReturnPatternFeature_ptr feature, returnFeatures) {
		std::wstring tbd = feature->getReturnValue(L"tbd");
		if (tbd.empty())
			continue;
		if (tbd.compare(L"bad_date") == 0) {
			continue;
		} else if (tbd.compare(L"agent") == 0) {
			tbdAgentFeatures.push_back(feature);
		} else if (tbd.compare(L"patient") == 0) {
			tbdPatientFeatures.push_back(feature);
		} else if (!EITbdAdapter::getEventRole(tbd).empty()) {
			if (tbdEventFeature == 0)
				tbdEventFeature = feature;
			else {
				ostringstream ostr;
				if (feature->getPatternReturn()) {
					feature->getPatternReturn()->dump(ostr, 0);
				}
				std::string main_str;
				main_str = "Feature set has multiple 'tbd' event features; not wise to create an event here.\n";
				main_str += ostr.str();
				throw UnexpectedInputException("ElfRelation::ElfRelation"
					"(DocTheory*, PatternSet_ptr, PatternFeatureSet_ptr)", 
					main_str.c_str());
			}
		} else {
			SessionLogger::warn("LEARNIT") << "Unknown \"tbd\" feature in pattern file: -->" << tbd << "<--\n";
		}
	}
	fix_tbd_arguments(doc_theory, tbdEventFeature, tbdAgentFeatures, tbdPatientFeatures);
	BOOST_FOREACH (ReturnPatternFeature_ptr feature, returnFeatures) {
		std::vector<ElfRelationArg_ptr> args = ElfRelationArgFactory::from_return_feature(doc_theory, feature);
		BOOST_FOREACH(ElfRelationArg_ptr arg, args) {
			insert_argument(arg);
		}
	}
	if (_arg_map.size() < 2)
		throw UnexpectedInputException("ElfRelation::ElfRelation(DocTheory*, PatternSet_ptr, PatternFeatureSet_ptr)", 
		"Feature set did not generate enough relation args");
	std::wstring source = _name;
	boost::algorithm::replace_all(source, L":", L"_");
	std::wstring subpattern_id = L"";
	for (size_t f = 0; f < feature_set->getNFeatures(); f++) {
		PatternFeature_ptr feature = feature_set->getFeature(f);
		Pattern_ptr pattern = feature->getPattern();
		if (pattern.get()) {
			Symbol subpattern_id_sym = pattern->getID();
			if (subpattern_id_sym != Symbol()) {
				subpattern_id = std::wstring(subpattern_id_sym.to_string());
				break;
			}
		}
	}
	_source = L"bbn:manual-" + source;
	if (subpattern_id != L"") {
		_source += L"-" + subpattern_id;
	} else {
		throw std::runtime_error("ElfRelation::ElfRelation(DocTheory*, PatternSet_ptr, PatternFeatureSet_ptr): No id defined for subpattern");
	}
	_confidence = feature_set->getScore();
	_score_group = feature_set->getBestScoreGroup();
	if (abs(_confidence - Pattern::UNSPECIFIED_SCORE) < .01)
		_confidence = DEFAULT_CONFIDENCE;
	SentenceTheory* start_sent_theory = doc_theory->getSentenceTheory(feature_set->getStartSentence());
	if (start_sent_theory == NULL)
		throw std::runtime_error("ElfRelation::ElfRelation(DocTheory*, "
		"PatternSet_ptr, PatternFeatureSet_ptr): Start sentence theory null");
	TokenSequence* start_token_sequence = start_sent_theory->getTokenSequence();
	if (start_token_sequence == NULL)
		throw std::runtime_error("ElfRelation::ElfRelation(DocTheory*, "
		"PatternSet_ptr, PatternFeatureSet_ptr): Start token sequence null");
	_start = start_token_sequence->getToken(feature_set->getStartToken())->getStartEDTOffset();
	SentenceTheory* end_sent_theory = doc_theory->getSentenceTheory(feature_set->getEndSentence());
	if (end_sent_theory == NULL)
		throw std::runtime_error("ElfRelation::ElfRelation(DocTheory*, "
		"PatternSet_ptr, PatternFeatureSet_ptr): End sentence theory null");
	TokenSequence* end_token_sequence = end_sent_theory->getTokenSequence();
	if (end_token_sequence == NULL)
		throw std::runtime_error("ElfRelation::ElfRelation(DocTheory*, "
		"PatternSet_ptr, PatternFeatureSet_ptr): End token sequence null");
	_end = end_token_sequence->getToken(feature_set->getEndToken())->getEndEDTOffset();
	LocatedString* text = MainUtilities::substringFromEdtOffsets(
		doc_theory->getDocument()->getOriginalText(), _start, _end);
	_text = std::wstring(text->toString());
	delete text;
}
ElfRelation :: ElfRelation ( const DocTheory * doc_theory , const PatternSet_ptr pattern , const PatternFeatureSet_ptr feature_set ) { if ( doc_theory == NULL ) throw std :: runtime_error ( " " ) ; _name = std :: wstring ( pattern -> getPatternSetName ( ) . to_string ( ) ) ; _text = L" " ; std :: vector < ReturnPatternFeature_ptr > returnFeatures ; for ( size_t i = 0 ; i < feature_set -> getNFeatures ( ) ; i ++ ) { PatternFeature_ptr feature = feature_set -> getFeature ( i ) ; if ( ReturnPatternFeature_ptr rf = dynamic_pointer_cast < ReturnPatternFeature > ( feature ) ) { returnFeatures . push_back ( rf ) ; } } ReturnPatternFeature_ptr tbdEventFeature ; std :: vector < ReturnPatternFeature_ptr > tbdAgentFeatures ; std :: vector < ReturnPatternFeature_ptr > tbdPatientFeatures ; BOOST_FOREACH ( ReturnPatternFeature_ptr feature , returnFeatures ) { std :: wstring tbd = feature -> getReturnValue ( L" " ) ; if ( tbd . empty ( ) ) continue ; if ( tbd . compare ( L" " ) == 0 ) { continue ; } else if ( tbd . compare ( L" " ) == 0 ) { tbdAgentFeatures . push_back ( feature ) ; } else if ( tbd . compare ( L" " ) == 0 ) { tbdPatientFeatures . push_back ( feature ) ; } else if ( ! EITbdAdapter :: getEventRole ( tbd ) . empty ( ) ) { if ( tbdEventFeature == 0 ) tbdEventFeature = feature ; else { ostringstream ostr ; if ( feature -> getPatternReturn ( ) ) { feature -> getPatternReturn ( ) -> dump ( ostr , 0 ) ; } std :: string main_str ; main_str = " \n " ; main_str += ostr . str ( ) ; throw UnexpectedInputException ( " " " " , main_str . c_str ( ) ) ; } } else { SessionLogger :: warn ( " " ) << " \" \" " << tbd << " \n " ; } } fix_tbd_arguments ( doc_theory , tbdEventFeature , tbdAgentFeatures , tbdPatientFeatures ) ; BOOST_FOREACH ( ReturnPatternFeature_ptr feature , returnFeatures ) { std :: vector < ElfRelationArg_ptr > args = ElfRelationArgFactory :: from_return_feature ( doc_theory , feature ) ; BOOST_FOREACH ( ElfRelationArg_ptr arg , args ) { insert_argument ( arg ) ; } } if ( _arg_map . size ( ) < 2 ) throw UnexpectedInputException ( " " , " " ) ; std :: wstring source = _name ; boost :: algorithm :: replace_all ( source , L" " , L" " ) ; std :: wstring subpattern_id = L" " ; for ( size_t f = 0 ; f < feature_set -> getNFeatures ( ) ; f ++ ) { PatternFeature_ptr feature = feature_set -> getFeature ( f ) ; Pattern_ptr pattern = feature -> getPattern ( ) ; if ( pattern . get ( ) ) { Symbol subpattern_id_sym = pattern -> getID ( ) ; if ( subpattern_id_sym != Symbol ( ) ) { subpattern_id = std :: wstring ( subpattern_id_sym . to_string ( ) ) ; break ; } } } _source = L" " + source ; if ( subpattern_id != L" " ) { _source += L" " + subpattern_id ; } else { throw std :: runtime_error ( " " ) ; } _confidence = feature_set -> getScore ( ) ; _score_group = feature_set -> getBestScoreGroup ( ) ; if ( abs ( _confidence - Pattern :: UNSPECIFIED_SCORE ) < .01 ) _confidence = DEFAULT_CONFIDENCE ; SentenceTheory * start_sent_theory = doc_theory -> getSentenceTheory ( feature_set -> getStartSentence ( ) ) ; if ( start_sent_theory == NULL ) throw std :: runtime_error ( " " " " ) ; TokenSequence * start_token_sequence = start_sent_theory -> getTokenSequence ( ) ; if ( start_token_sequence == NULL ) throw std :: runtime_error ( " " " " ) ; _start = start_token_sequence -> getToken ( feature_set -> getStartToken ( ) ) -> getStartEDTOffset ( ) ; SentenceTheory * end_sent_theory = doc_theory -> getSentenceTheory ( feature_set -> getEndSentence ( ) ) ; if ( end_sent_theory == NULL ) throw std :: runtime_error ( " " " " ) ; TokenSequence * end_token_sequence = end_sent_theory -> getTokenSequence ( ) ; if ( end_token_sequence == NULL ) throw std :: runtime_error ( " " " " ) ; _end = end_token_sequence -> getToken ( feature_set -> getEndToken ( ) ) -> getEndEDTOffset ( ) ; LocatedString * text = MainUtilities :: substringFromEdtOffsets ( doc_theory -> getDocument ( ) -> getOriginalText ( ) , _start , _end ) ; _text = std :: wstring ( text -> toString ( ) ) ; delete text ; }
Construct a relation from a SnippetFeatureSet found using
 manual Distillation patterns.
Construct a relation from a SnippetFeatureSet found using manual Distillation patterns .
// Check for bad relation
 // The pattern set name should always be the output relation name, though it
 //   might get reset if these are TBD patterns.
 // Find all return features for this relation
 // Identify all TBD arguments. TBD arguments are those which have been set
 //   with (tbd some_value) in the pattern file. Their true values will
 //   be determined by other features of the event, e.g. whether the agent
 //   is a group or not. The features are separated into a single 
 //   tbdEventFeature and then vectors of agent and patient features.
 // i.e., tbd is in {"generic_violence","killing","injury","bombing","attack"}
 /*indent=*/ // This might happen with "X was blamed for Y and Z", where Y and Z are separate events
 // Fill in type/role for all arguments that were "TBD"; this will also reset "_name"
 // Now create and insert an argument for each SnippetReturnFeature
 // Make sure we had a valid feature set
 // Generate a source from the pattern set name (namespaces become underscored prefixes).
 // Append the subpattern id. Throw an exception if none is found.
 // We retrieve the feature set's score estimate. If it has
 // not been set (i.e., if it is equal to Pattern::UNSPECIFIED_SCORE, 
 // which is -1.0), set it to our default value. Since _confidence is a double 
 // rather than an int, we play it safe rather than simply test 
 // whether _confidence == Pattern::UNSPECIFIED_SCORE.
 // Get the start offset of the snippet
 // Get the end offset of the snippet
 // Get the text of the snippet
