#ifndef _RULE_EXTRACTOR_H_ #define _RULE_EXTRACTOR_H_ #include #include #include #include "phrase.h" using namespace std; namespace extractor { typedef vector> PhraseAlignment; class Alignment; class DataArray; class PhraseBuilder; class PhraseLocation; class Rule; class RuleExtractorHelper; class Scorer; class TargetPhraseExtractor; /** * Structure containing data about the occurrences of a source-target phrase pair * in the parallel corpus. */ struct Extract { Extract(const Phrase& source_phrase, const Phrase& target_phrase, double pairs_count, const PhraseAlignment& alignment) : source_phrase(source_phrase), target_phrase(target_phrase), pairs_count(pairs_count), alignment(alignment) {} Phrase source_phrase; Phrase target_phrase; double pairs_count; PhraseAlignment alignment; }; /** * Component for extracting SCFG rules. */ class RuleExtractor { public: RuleExtractor(shared_ptr source_data_array, shared_ptr target_data_array, shared_ptr alingment, shared_ptr phrase_builder, shared_ptr scorer, shared_ptr vocabulary, int min_gap_size, int max_rule_span, int max_nonterminals, int max_rule_symbols, bool require_aligned_terminal, bool require_aligned_chunks, bool require_tight_phrases); // For testing only. RuleExtractor(shared_ptr source_data_array, shared_ptr phrase_builder, shared_ptr scorer, shared_ptr target_phrase_extractor, shared_ptr helper, int max_rule_span, int min_gap_size, int max_nonterminals, int max_rule_symbols, bool require_tight_phrases); virtual ~RuleExtractor(); // Extracts SCFG rules given a source phrase and a set of its occurrences // in the source data. virtual vector ExtractRules(const Phrase& phrase, const PhraseLocation& location) const; protected: RuleExtractor(); private: // Finds all target phrases that can be aligned with the source phrase for a // particular occurrence in the data. vector ExtractAlignments(const Phrase& phrase, const vector& matching) const; // Extracts all target phrases for a given occurrence of the source phrase in // the data. Constructs a vector of Extracts using these target phrases. void AddExtracts( vector& extracts, const Phrase& source_phrase, const unordered_map& source_indexes, const vector>& target_gaps, const vector& target_low, int target_phrase_low, int target_phrase_high, int sentence_id) const; // Adds a leading and/or trailing nonterminal to the source phrase and // extracts target phrases that can be aligned with the extended source // phrase. void AddNonterminalExtremities( vector& extracts, const vector& matching, const vector& chunklen, const Phrase& source_phrase, int source_back_low, int source_back_high, const vector& source_low, const vector& source_high, const vector& target_low, const vector& target_high, vector> target_gaps, int sentence_id, int source_sent_start, int starts_with_x, int ends_with_x, int extend_left, int extend_right) const; private: shared_ptr target_data_array; shared_ptr source_data_array; shared_ptr phrase_builder; shared_ptr scorer; shared_ptr target_phrase_extractor; shared_ptr helper; int max_rule_span; int min_gap_size; int max_nonterminals; int max_rule_symbols; bool require_tight_phrases; }; } // namespace extractor #endif