#ifndef _PRECOMPUTATION_H_ #define _PRECOMPUTATION_H_ #include #include #include #include #include #include #include #include #include using namespace std; namespace extractor { typedef boost::hash> VectorHash; typedef unordered_map, vector, VectorHash> Index; class DataArray; class SuffixArray; class Vocabulary; /** * Data structure wrapping an index with all the occurrences of the most * frequent discontiguous collocations in the source data. * * Let a, b, c be contiguous collocations. The index will contain an entry for * every collocation of the form: * - aXb, where a and b are frequent * - aXbXc, where a and b are super-frequent and c is frequent or * b and c are super-frequent and a is frequent. */ class Precomputation { public: // Constructs the index using the suffix array. Precomputation( shared_ptr vocabulary, shared_ptr suffix_array, int num_frequent_patterns, int num_super_frequent_patterns, int max_rule_span, int max_rule_symbols, int min_gap_size, int max_frequent_phrase_len, int min_frequency); // Creates empty precomputation data structure. Precomputation(); virtual ~Precomputation(); // Returns whether a pattern is contained in the index of collocations. virtual bool Contains(const vector& pattern) const; // Returns the list of collocations for a given pattern. virtual vector GetCollocations(const vector& pattern) const; bool operator==(const Precomputation& other) const; static int NONTERMINAL; private: // Finds the most frequent contiguous collocations. vector> FindMostFrequentPatterns( shared_ptr suffix_array, int num_frequent_patterns, int max_frequent_phrase_len, int min_frequency); // Given the locations of the frequent contiguous collocations in a sentence, // it adds new entries to the index for each discontiguous collocation // matching the criteria specified in the class description. void UpdateIndex( shared_ptr data_array, shared_ptr vocabulary, const vector>& matchings, int max_rule_span, int min_gap_size, int max_rule_symbols); void AppendSubpattern( vector& pattern, shared_ptr data_array, shared_ptr vocabulary, int start, int size); // Adds an occurrence of a collocation. void AppendCollocation(vector& collocations, const vector& collocation); friend class boost::serialization::access; template void save(Archive& ar, unsigned int) const { int num_entries = index.size(); ar << num_entries; for (pair, vector> entry: index) { ar << entry; } } template void load(Archive& ar, unsigned int) { int num_entries; ar >> num_entries; for (size_t i = 0; i < num_entries; ++i) { pair, vector> entry; ar >> entry; index.insert(entry); } } BOOST_SERIALIZATION_SPLIT_MEMBER(); Index index; }; } // namespace extractor #endif