ThreadLocalFeatureStorage

This commit is contained in:
Marcin Junczys-Dowmunt 2015-01-08 15:35:30 +01:00
parent 5da4031724
commit 5b71b2c3a9
4 changed files with 19 additions and 28 deletions

View File

@ -11,10 +11,6 @@
namespace Moses
{
typedef std::vector<std::string> Features;
typedef std::map<std::string, Features> NameFeatureMap;
typedef boost::thread_specific_ptr<NameFeatureMap> TSNameFeatureMap;
class VWFeatureBase : public StatelessFeatureFunction
{
public:

View File

@ -1,9 +0,0 @@
#include "VWFeatureSourceExternalFeatures.h"
namespace Moses
{
TSNameFeatureMap VWFeatureSourceExternalFeatures::m_nameMap;
}

View File

@ -3,17 +3,19 @@
#include <string>
#include <cstdlib>
#include "ThreadLocalFeatureStorage.h"
#include "VWFeatureSource.h"
#include "TabbedSentence.h"
namespace Moses
{
class VWFeatureSourceExternalFeatures : public VWFeatureSource
class VWFeatureSourceExternalFeatures : public VWFeatureSource,
public ThreadLocalFeatureStorage
{
public:
VWFeatureSourceExternalFeatures(const std::string &line)
: VWFeatureSource(line), m_column(0)
: VWFeatureSource(line), ThreadLocalFeatureStorage(this), m_column(0)
{
ReadParameters();
@ -26,7 +28,7 @@ class VWFeatureSourceExternalFeatures : public VWFeatureSource
, const WordsRange &sourceRange
, Discriminative::Classifier *classifier) const
{
Features& features = (*m_nameMap)[GetScoreProducerDescription()];
const Features& features = GetStoredFeatures();
for (size_t i = 0; i < features.size(); i++) {
classifier->AddLabelIndependentFeature(features[i]);
}
@ -40,26 +42,21 @@ class VWFeatureSourceExternalFeatures : public VWFeatureSource
}
virtual void InitializeForInput(InputType const& source) {
UTIL_THROW_IF2(source.GetType() != TabbedSentenceInput, "This feature function requires the TabbedSentence input type");
ThreadLocalFeatureStorage::InitializeForInput(source);
UTIL_THROW_IF2(source.GetType() != TabbedSentenceInput,
"This feature function requires the TabbedSentence input type");
const TabbedSentence& tabbedSentence = static_cast<const TabbedSentence&>(source);
UTIL_THROW_IF2(tabbedSentence.GetColumns().size() <= m_column, "There is no column with index: " << m_column);
if(!m_nameMap.get())
m_nameMap.reset(new NameFeatureMap());
(*m_nameMap)[GetScoreProducerDescription()].clear();
Features& features = (*m_nameMap)[GetScoreProducerDescription()];
const std::string &column = tabbedSentence.GetColumns()[m_column];
const std::string &column = tabbedSentence.GetColumn(m_column);
Features& features = GetStoredFeatures();
Tokenize(features, column, " ");
}
private:
size_t m_column;
static TSNameFeatureMap m_nameMap;
};
}

View File

@ -23,6 +23,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include <vector>
#include <string>
#include <cstdlib>
#include "Sentence.h"
namespace Moses
@ -69,6 +70,12 @@ public:
const TabbedColumns& GetColumns() const {
return m_columns;
}
const std::string& GetColumn(size_t i) const {
UTIL_THROW_IF2(m_columns.size() <= i,
"There is no column with index " << i);
return m_columns[i];
}
private:
TabbedColumns m_columns;