From 8bce53d2a076ef76f3fea34b2bddb0a713ec4ea8 Mon Sep 17 00:00:00 2001 From: Chris Dyer Date: Fri, 17 Jan 2014 04:07:16 -0500 Subject: new de split --- decoder/ff_csplit.cc | 36 ++++++++++++++++++++++++++++++++++-- decoder/ff_csplit.h | 1 + 2 files changed, 35 insertions(+), 2 deletions(-) (limited to 'decoder') diff --git a/decoder/ff_csplit.cc b/decoder/ff_csplit.cc index e6f78f84..33b6cea8 100644 --- a/decoder/ff_csplit.cc +++ b/decoder/ff_csplit.cc @@ -14,12 +14,19 @@ #include "stringlib.h" #include "tdict.h" +#ifndef HAVE_OLD_CPP +# include +#else +# include +namespace std { using std::tr1::unordered_set; } +#endif using namespace std; struct BasicCSplitFeaturesImpl { BasicCSplitFeaturesImpl(const string& param) : word_count_(FD::Convert("WordCount")), letters_sq_(FD::Convert("LettersSq")), + letters_log_(FD::Convert("LettersLog")), letters_sqrt_(FD::Convert("LettersSqrt")), in_dict_(FD::Convert("InDict")), in_dict_sub_word_(FD::Convert("InDictSubWord")), @@ -31,13 +38,14 @@ struct BasicCSplitFeaturesImpl { high_freq_(FD::Convert("HighFreq")), med_freq_(FD::Convert("MedFreq")), freq_(FD::Convert("Freq")), + in_dict_full_word_(FD::Convert("InDictFullWord")), fl1_(FD::Convert("FreqLen1")), fl2_(FD::Convert("FreqLen2")), bad_(FD::Convert("Bad")) { vector argv; int argc = SplitOnWhitespace(param, &argv); - if (argc != 1 && argc != 2) { - cerr << "Expected: freqdict.txt [badwords.txt]\n"; + if (argc != 1 && argc != 2 && argc != 3) { + cerr << "Expected: freqdict.txt [badwords.txt] [sensitvewords.txt]\n"; abort(); } freq_dict_.Load(argv[0]); @@ -51,6 +59,14 @@ struct BasicCSplitFeaturesImpl { bad_words_.insert(TD::Convert(badword)); } } + if (argc == 3) { + ReadFile rf(argv[2]); + istream& in = *rf.stream(); + string line; + while(getline(in, line)) { + special_feats_[TD::Convert(line)] = FD::Convert("CS:"+line); + } + } } void TraversalFeaturesImpl(const Hypergraph::Edge& edge, @@ -59,6 +75,7 @@ struct BasicCSplitFeaturesImpl { const int word_count_; const int letters_sq_; + const int letters_log_; const int letters_sqrt_; const int in_dict_; const int in_dict_sub_word_; @@ -70,11 +87,13 @@ struct BasicCSplitFeaturesImpl { const int high_freq_; const int med_freq_; const int freq_; + const int in_dict_full_word_; const int fl1_; const int fl2_; const int bad_; FreqDict freq_dict_; set bad_words_; + unordered_map special_feats_; }; BasicCSplitFeatures::BasicCSplitFeatures(const string& param) : @@ -85,8 +104,15 @@ void BasicCSplitFeaturesImpl::TraversalFeaturesImpl( const int src_word_length, SparseVector* features) const { const bool subword = (edge.i_ > 0) || (edge.j_ < src_word_length); + string len_bias = "LenBias_0"; + int swlen = log(src_word_length) / log(1.69); + if (swlen > 9) swlen = 9; + len_bias[8] += swlen; + int fid_len_bias_ = FD::Convert(len_bias); + features->set_value(fid_len_bias_, 1.0); features->set_value(word_count_, 1.0); features->set_value(letters_sq_, (edge.j_ - edge.i_) * (edge.j_ - edge.i_)); + features->set_value(letters_log_, log(edge.j_ - edge.i_)); features->set_value(letters_sqrt_, sqrt(edge.j_ - edge.i_)); const WordID word = edge.rule_->e_[1]; const char* sword = TD::Convert(word).c_str(); @@ -117,10 +143,14 @@ void BasicCSplitFeaturesImpl::TraversalFeaturesImpl( features->set_value(in_dict_, 1.0); if (subword) features->set_value(in_dict_sub_word_, 1.0); } else { + if (!subword) features->set_value(in_dict_full_word_, 1.0); features->set_value(oov_, 1.0); if (subword) features->set_value(oov_sub_word_, 1.0); freq = 99.0f; } + const unordered_map::const_iterator it = special_feats_.find(word); + if (it != special_feats_.end()) + features->set_value(it->second, 1.0); if (bad_words_.count(word) != 0) features->set_value(bad_, 1.0); if (chars < 5) @@ -139,6 +169,8 @@ void BasicCSplitFeaturesImpl::TraversalFeaturesImpl( features->set_value(fl2_, freq / chars); } +void BasicCSplitFeatures::PrepareForInput(const SentenceMetadata& smeta) {} + void BasicCSplitFeatures::TraversalFeaturesImpl( const SentenceMetadata& smeta, const Hypergraph::Edge& edge, diff --git a/decoder/ff_csplit.h b/decoder/ff_csplit.h index 64d42526..79bf2886 100644 --- a/decoder/ff_csplit.h +++ b/decoder/ff_csplit.h @@ -10,6 +10,7 @@ class BasicCSplitFeaturesImpl; class BasicCSplitFeatures : public FeatureFunction { public: BasicCSplitFeatures(const std::string& param); + virtual void PrepareForInput(const SentenceMetadata& smeta); protected: virtual void TraversalFeaturesImpl(const SentenceMetadata& smeta, const HG::Edge& edge, -- cgit v1.2.3