00001 #pragma once
00002
00003 #include <string>
00004 #include <algorithm>
00005 #include "VWFeatureSource.h"
00006 #include "moses/Util.h"
00007
00008 namespace Moses
00009 {
00010
00011 class VWFeatureSourceWindow : public VWFeatureSource
00012 {
00013 public:
00014 VWFeatureSourceWindow(const std::string &line)
00015 : VWFeatureSource(line), m_size(DEFAULT_WINDOW_SIZE) {
00016 ReadParameters();
00017
00018
00019 VWFeatureBase::UpdateRegister();
00020 }
00021
00022 void operator()(const InputType &input
00023 , const Range &sourceRange
00024 , Discriminative::Classifier &classifier
00025 , Discriminative::FeatureVector &outFeatures) const {
00026 int begin = sourceRange.GetStartPos();
00027 int end = sourceRange.GetEndPos() + 1;
00028 int inputLen = input.GetSize();
00029
00030 for (int i = std::max(0, begin - m_size); i < begin; i++) {
00031 outFeatures.push_back(classifier.AddLabelIndependentFeature("c^" + SPrint(i - begin) + "^" + GetWord(input, i)));
00032 }
00033
00034 for (int i = end; i < std::min(end + m_size, inputLen); i++) {
00035 outFeatures.push_back(classifier.AddLabelIndependentFeature("c^" + SPrint(i - end + 1) + "^" + GetWord(input, i)));
00036 }
00037 }
00038
00039 virtual void SetParameter(const std::string& key, const std::string& value) {
00040 if (key == "size") {
00041 m_size = Scan<size_t>(value);
00042 } else {
00043 VWFeatureSource::SetParameter(key, value);
00044 }
00045 }
00046
00047 private:
00048 static const int DEFAULT_WINDOW_SIZE = 3;
00049
00050 int m_size;
00051 };
00052
00053 }