#include #include #include #include #include #include #include #include #include #include #include "alignment.h" #include "data_array.h" #include "features/count_source_target.h" #include "features/feature.h" #include "features/is_source_singleton.h" #include "features/is_source_target_singleton.h" #include "features/max_lex_source_given_target.h" #include "features/max_lex_target_given_source.h" #include "features/sample_source_count.h" #include "features/target_given_source_coherent.h" #include "grammar.h" #include "grammar_extractor.h" #include "precomputation.h" #include "rule.h" #include "scorer.h" #include "suffix_array.h" #include "time_util.h" #include "translation_table.h" #include "vocabulary.h" namespace fs = boost::filesystem; namespace po = boost::program_options; using namespace std; using namespace extractor; using namespace features; // Returns the file path in which a given grammar should be written. fs::path GetGrammarFilePath(const fs::path& grammar_path, int file_number) { string file_name = "grammar." + to_string(file_number); return grammar_path / file_name; } int main(int argc, char** argv) { // Sets up the command line arguments map. int max_threads = 1; #pragma omp parallel max_threads = omp_get_num_threads(); string threads_option = "Number of parallel threads for extraction " "(max=" + to_string(max_threads) + ")"; po::options_description desc("Command line options"); desc.add_options() ("help,h", "Show available options") ("source,f", po::value(), "Source language corpus") ("target,e", po::value(), "Target language corpus") ("bitext,b", po::value(), "Parallel text (source ||| target)") ("alignment,a", po::value()->required(), "Bitext word alignment") ("grammars,g", po::value()->required(), "Grammars output path") ("threads,t", po::value()->default_value(1), threads_option.c_str()) ("frequent", po::value()->default_value(100), "Number of precomputed frequent patterns") ("super_frequent", po::value()->default_value(10), "Number of precomputed super frequent patterns") ("max_rule_span", po::value()->default_value(15), "Maximum rule span") ("max_rule_symbols", po::value()->default_value(5), "Maximum number of symbols (terminals + nontermals) in a rule") ("min_gap_size", po::value()->default_value(1), "Minimum gap size") ("max_phrase_len", po::value()->default_value(4), "Maximum frequent phrase length") ("max_nonterminals", po::value()->default_value(2), "Maximum number of nonterminals in a rule") ("min_frequency", po::value()->default_value(1000), "Minimum number of occurrences for a pharse to be considered frequent") ("max_samples", po::value()->default_value(300), "Maximum number of samples") ("tight_phrases", po::value()->default_value(true), "False if phrases may be loose (better, but slower)") ("leave_one_out", po::value()->zero_tokens(), "do leave-one-out estimation of grammars " "(e.g. for extracting grammars for the training set"); po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); // Checks for the help option before calling notify, so the we don't get an // exception for missing required arguments. if (vm.count("help")) { cout << desc << endl; return 0; } po::notify(vm); if (!((vm.count("source") && vm.count("target")) || vm.count("bitext"))) { cerr << "A paralel corpus is required. " << "Use -f (source) with -e (target) or -b (bitext)." << endl; return 1; } int num_threads = vm["threads"].as(); cerr << "Grammar extraction will use " << num_threads << " threads." << endl; // Reads the parallel corpus. Clock::time_point preprocess_start_time = Clock::now(); cerr << "Reading source and target data..." << endl; Clock::time_point start_time = Clock::now(); shared_ptr source_data_array, target_data_array; if (vm.count("bitext")) { source_data_array = make_shared( vm["bitext"].as(), SOURCE); target_data_array = make_shared( vm["bitext"].as(), TARGET); } else { source_data_array = make_shared(vm["source"].as()); target_data_array = make_shared(vm["target"].as()); } Clock::time_point stop_time = Clock::now(); cerr << "Reading data took " << GetDuration(start_time, stop_time) << " seconds" << endl; // Constructs the suffix array for the source data. start_time = Clock::now(); cerr << "Constructing source suffix array..." << endl; shared_ptr source_suffix_array = make_shared(source_data_array); stop_time = Clock::now(); cerr << "Constructing suffix array took " << GetDuration(start_time, stop_time) << " seconds" << endl; // Reads the alignment. start_time = Clock::now(); cerr << "Reading alignment..." << endl; shared_ptr alignment = make_shared(vm["alignment"].as()); stop_time = Clock::now(); cerr << "Reading alignment took " << GetDuration(start_time, stop_time) << " seconds" << endl; shared_ptr vocabulary = make_shared(); // Constructs an index storing the occurrences in the source data for each // frequent collocation. start_time = Clock::now(); cerr << "Precomputing collocations..." << endl; shared_ptr precomputation = make_shared( vocabulary, source_suffix_array, vm["frequent"].as(), vm["super_frequent"].as(), vm["max_rule_span"].as(), vm["max_rule_symbols"].as(), vm["min_gap_size"].as(), vm["max_phrase_len"].as(), vm["min_frequency"].as()); stop_time = Clock::now(); cerr << "Precomputing collocations took " << GetDuration(start_time, stop_time) << " seconds" << endl; // Constructs a table storing p(e | f) and p(f | e) for every pair of source // and target words. start_time = Clock::now(); cerr << "Precomputing conditional probabilities..." << endl; shared_ptr table = make_shared( source_data_array, target_data_array, alignment); stop_time = Clock::now(); cerr << "Precomputing conditional probabilities took " << GetDuration(start_time, stop_time) << " seconds" << endl; Clock::time_point preprocess_stop_time = Clock::now(); cerr << "Overall preprocessing step took " << GetDuration(preprocess_start_time, preprocess_stop_time) << " seconds" << endl; Clock::time_point extraction_start_time = Clock::now(); // Features used to score each grammar rule. vector> features = { make_shared(), make_shared(), make_shared(), make_shared(table), make_shared(table), make_shared(), make_shared() }; shared_ptr scorer = make_shared(features); // Sets up the grammar extractor. GrammarExtractor extractor( source_suffix_array, target_data_array, alignment, precomputation, scorer, vocabulary, vm["min_gap_size"].as(), vm["max_rule_span"].as(), vm["max_nonterminals"].as(), vm["max_rule_symbols"].as(), vm["max_samples"].as(), vm["tight_phrases"].as()); // Creates the grammars directory if it doesn't exist. fs::path grammar_path = vm["grammars"].as(); if (!fs::is_directory(grammar_path)) { fs::create_directory(grammar_path); } // Reads all sentences for which we extract grammar rules (the paralellization // is simplified if we read all sentences upfront). string sentence; vector sentences; while (getline(cin, sentence)) { sentences.push_back(sentence); } // Extracts the grammar for each sentence and saves it to a file. bool leave_one_out = vm.count("leave_one_out"); vector suffixes(sentences.size()); #pragma omp parallel for schedule(dynamic) num_threads(num_threads) for (size_t i = 0; i < sentences.size(); ++i) { string suffix; int position = sentences[i].find("|||"); if (position != sentences[i].npos) { suffix = sentences[i].substr(position); sentences[i] = sentences[i].substr(0, position); } suffixes[i] = suffix; unordered_set blacklisted_sentence_ids; if (leave_one_out) { blacklisted_sentence_ids.insert(i); } Grammar grammar = extractor.GetGrammar( sentences[i], blacklisted_sentence_ids); ofstream output(GetGrammarFilePath(grammar_path, i).c_str()); output << grammar; } for (size_t i = 0; i < sentences.size(); ++i) { cout << " " << sentences[i] << " " << suffixes[i] << endl; } Clock::time_point extraction_stop_time = Clock::now(); cerr << "Overall extraction step took " << GetDuration(extraction_start_time, extraction_stop_time) << " seconds" << endl; return 0; }