#include "grammar_extractor.h" #include #include #include #include "grammar.h" #include "rule.h" #include "rule_factory.h" #include "vocabulary.h" using namespace std; namespace extractor { GrammarExtractor::GrammarExtractor( shared_ptr source_suffix_array, shared_ptr target_data_array, shared_ptr alignment, shared_ptr precomputation, shared_ptr scorer, int min_gap_size, int max_rule_span, int max_nonterminals, int max_rule_symbols, int max_samples, bool require_tight_phrases) : vocabulary(make_shared()), rule_factory(make_shared( source_suffix_array, target_data_array, alignment, vocabulary, precomputation, scorer, min_gap_size, max_rule_span, max_nonterminals, max_rule_symbols, max_samples, require_tight_phrases)) {} GrammarExtractor::GrammarExtractor( shared_ptr vocabulary, shared_ptr rule_factory) : vocabulary(vocabulary), rule_factory(rule_factory) {} Grammar GrammarExtractor::GetGrammar(const string& sentence) { vector words = TokenizeSentence(sentence); vector word_ids = AnnotateWords(words); return rule_factory->GetGrammar(word_ids); } vector GrammarExtractor::TokenizeSentence(const string& sentence) { vector result; result.push_back(""); istringstream buffer(sentence); copy(istream_iterator(buffer), istream_iterator(), back_inserter(result)); result.push_back(""); return result; } vector GrammarExtractor::AnnotateWords(const vector& words) { vector result; for (string word: words) { result.push_back(vocabulary->GetTerminalIndex(word)); } return result; } } // namespace extractor