ARPAPredictor.h
Go to the documentation of this file.
/******************************************************
* Presage, an extensible predictive text entry system
* ---------------------------------------------------
*
* Copyright (C) 2008 Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
**********(*)*/
#ifndef PRESAGE_ARPAPREDICTOR
#define PRESAGE_ARPAPREDICTOR
#include "predictor.h"
#include "../core/logger.h"
#include "../core/progress.h"
#include "../core/dispatcher.h"
#include <assert.h>
#include <fstream>
#include <iomanip>
class cmp {
public:
bool operator() (const float& f1, const float& f2 ) const {
return f2 < f1;
}
};
class ARPAData
{
public:
ARPAData() {};
ARPAData(float lp,float la) : logProb(lp), logAlfa(la) {};
float logProb;
float logAlfa;
};
class TrigramKey
{
public:
TrigramKey(int wd1, int wd2, int wd3) : key1(wd1), key2(wd2), key3(wd3) {};
bool operator<(const TrigramKey &right) const
{
if(key1 < right.key1)
return true;
if(key1 == right.key1)
if(key2 < right.key2 )
return true;
if(key1 == right.key1 && key2 == right.key2)
if(key3 < right.key3)
return true;
return false;
}
bool operator==(const TrigramKey &right) const
{
return (key1 == right.key1 && key2 == right.key2 && key3 == right.key3 );
}
int key1;
int key2;
int key3;
};
class BigramKey
{
public:
BigramKey(int wd1, int wd2) : key1(wd1), key2(wd2) {};
bool operator<(const BigramKey &right) const
{
if(key1 < right.key1)
return true;
if(key1 == right.key1)
if(key2 < right.key2 )
return true;
return false;
}
bool operator==(const TrigramKey &right) const
{
return (key1 == right.key1 && key2 == right.key2);
}
int key1;
int key2;
};
class ARPAPredictor : public Predictor, public Observer {
public:
ARPAPredictor(Configuration*, ContextTracker*, const char*);
~ARPAPredictor();
virtual Prediction predict(const size_t size, const char** filter) const;
virtual void learn(const std::vector<std::string>& change);
virtual void update (const Observable* variable);
void set_vocab_filename (const std::string& value);
void set_arpa_filename (const std::string& value);
void set_timeout (const std::string& value);
private:
std::string LOGGER;
std::string ARPAFILENAME;
std::string VOCABFILENAME;
std::string TIMEOUT;
std::string arpaFilename;
std::string vocabFilename;
int timeout;
std::map<std::string,int> vocabCode;
std::map<int,std::string> vocabDecode;
std::map<int,ARPAData> unigramMap;
std::map<BigramKey,ARPAData>bigramMap;
std::map<TrigramKey,float>trigramMap;
void loadVocabulary();
void createARPATable();
bool matchesPrefixAndFilter(std::string , std::string , const char** ) const;
void addUnigram(std::string);
void addBigram(std::string);
void addTrigram(std::string);
inline float computeTrigramBackoff(int,int,int) const;
inline float computeBigramBackoff(int,int) const;
int unigramCount;
int bigramCount;
int trigramCount;
int unigramTot;
int bigramTot;
int trigramTot;
ProgressBar<char>* unigramProg;
ProgressBar<char>* bigramProg;
ProgressBar<char>* trigramProg;
Dispatcher<ARPAPredictor> dispatcher;
};
#endif // PRESAGE_ARPAPREDICTOR
ARPAPredictor::computeTrigramBackoff
float computeTrigramBackoff(int, int, int) const
Definition: ARPAPredictor.cpp:409
ARPAPredictor::learn
virtual void learn(const std::vector< std::string > &change)
Definition: ARPAPredictor.cpp:455
ARPAPredictor::trigramProg
ProgressBar< char > * trigramProg
Definition: ARPAPredictor.h:165
ARPAPredictor::ARPAFILENAME
std::string ARPAFILENAME
Definition: ARPAPredictor.h:129
ARPAPredictor::trigramCount
int trigramCount
Definition: ARPAPredictor.h:157
ARPAPredictor::TIMEOUT
std::string TIMEOUT
Definition: ARPAPredictor.h:131
predictor.h
Observer
Definition: observer.h:33
BigramKey::operator==
bool operator==(const TrigramKey &right) const
Definition: ARPAPredictor.h:100
TrigramKey::operator<
bool operator<(const TrigramKey &right) const
Definition: ARPAPredictor.h:58
ARPAPredictor::unigramTot
int unigramTot
Definition: ARPAPredictor.h:159
ARPAPredictor::timeout
int timeout
Definition: ARPAPredictor.h:135
ARPAPredictor::set_vocab_filename
void set_vocab_filename(const std::string &value)
Definition: ARPAPredictor.cpp:61
ARPAPredictor
Definition: ARPAPredictor.h:111
ARPAPredictor::VOCABFILENAME
std::string VOCABFILENAME
Definition: ARPAPredictor.h:130
ARPAPredictor::addUnigram
void addUnigram(std::string)
Definition: ARPAPredictor.cpp:214
TrigramKey::key2
int key2
Definition: ARPAPredictor.h:79
ARPAData
Definition: ARPAPredictor.h:44
ARPAPredictor::bigramCount
int bigramCount
Definition: ARPAPredictor.h:156
TrigramKey::key3
int key3
Definition: ARPAPredictor.h:80
ARPAPredictor::~ARPAPredictor
~ARPAPredictor()
Definition: ARPAPredictor.cpp:298
ARPAData::logAlfa
float logAlfa
Definition: ARPAPredictor.h:50
Prediction
Definition: prediction.h:46
ARPAPredictor::unigramCount
int unigramCount
Definition: ARPAPredictor.h:155
cmp::operator()
bool operator()(const float &f1, const float &f2) const
Definition: ARPAPredictor.h:58
Predictor
Definition: predictor.h:45
cmp
Definition: ARPAPredictor.h:37
BigramKey::BigramKey
BigramKey(int wd1, int wd2)
Definition: ARPAPredictor.h:86
ARPAPredictor::vocabCode
std::map< std::string, int > vocabCode
Definition: ARPAPredictor.h:137
ARPAPredictor::dispatcher
Dispatcher< ARPAPredictor > dispatcher
Definition: ARPAPredictor.h:167
ContextTracker
Tracks user interaction and context.
Definition: contextTracker.h:154
BigramKey::operator<
bool operator<(const BigramKey &right) const
Definition: ARPAPredictor.h:88
ARPAPredictor::update
virtual void update(const Observable *variable)
Definition: ARPAPredictor.cpp:461
ARPAPredictor::matchesPrefixAndFilter
bool matchesPrefixAndFilter(std::string, std::string, const char **) const
Definition: ARPAPredictor.cpp:305
ARPAPredictor::trigramMap
std::map< TrigramKey, float > trigramMap
Definition: ARPAPredictor.h:142
ARPAPredictor::predict
virtual Prediction predict(const size_t size, const char **filter) const
Generate prediction.
Definition: ARPAPredictor.cpp:320
ARPAPredictor::computeBigramBackoff
float computeBigramBackoff(int, int) const
Definition: ARPAPredictor.cpp:443
ARPAPredictor::unigramMap
std::map< int, ARPAData > unigramMap
Definition: ARPAPredictor.h:140
ARPAPredictor::vocabFilename
std::string vocabFilename
Definition: ARPAPredictor.h:134
ARPAPredictor::addBigram
void addBigram(std::string)
Definition: ARPAPredictor.cpp:241
ARPAPredictor::addTrigram
void addTrigram(std::string)
Definition: ARPAPredictor.cpp:268
ARPAPredictor::bigramMap
std::map< BigramKey, ARPAData > bigramMap
Definition: ARPAPredictor.h:141
TrigramKey
Definition: ARPAPredictor.h:53
ARPAPredictor::createARPATable
void createARPATable()
Definition: ARPAPredictor.cpp:106
ARPAData::logProb
float logProb
Definition: ARPAPredictor.h:48
ProgressBar< char >
ARPAData::ARPAData
ARPAData()
Definition: ARPAPredictor.h:47
ARPAPredictor::arpaFilename
std::string arpaFilename
Definition: ARPAPredictor.h:133
ARPAPredictor::loadVocabulary
void loadVocabulary()
Definition: ARPAPredictor.cpp:79
Configuration
Definition: configuration.h:35
ARPAPredictor::set_timeout
void set_timeout(const std::string &value)
Definition: ARPAPredictor.cpp:73
ARPAPredictor::LOGGER
std::string LOGGER
Definition: ARPAPredictor.h:128
TrigramKey::operator==
bool operator==(const TrigramKey &right) const
Definition: ARPAPredictor.h:74
BigramKey::key2
int key2
Definition: ARPAPredictor.h:105
BigramKey
Definition: ARPAPredictor.h:83
ARPAPredictor::unigramProg
ProgressBar< char > * unigramProg
Definition: ARPAPredictor.h:163
ARPAPredictor::ARPAPredictor
ARPAPredictor(Configuration *, ContextTracker *, const char *)
Definition: ARPAPredictor.cpp:37
Observable
Definition: observable.h:36
ARPAPredictor::vocabDecode
std::map< int, std::string > vocabDecode
Definition: ARPAPredictor.h:138
BigramKey::key1
int key1
Definition: ARPAPredictor.h:104
Dispatcher< ARPAPredictor >
ARPAPredictor::bigramTot
int bigramTot
Definition: ARPAPredictor.h:160
ARPAPredictor::bigramProg
ProgressBar< char > * bigramProg
Definition: ARPAPredictor.h:164
ARPAPredictor::trigramTot
int trigramTot
Definition: ARPAPredictor.h:161
TrigramKey::key1
int key1
Definition: ARPAPredictor.h:78
ARPAPredictor::set_arpa_filename
void set_arpa_filename(const std::string &value)
Definition: ARPAPredictor.cpp:67
TrigramKey::TrigramKey
TrigramKey(int wd1, int wd2, int wd3)
Definition: ARPAPredictor.h:56