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
ARPAData::ARPAData
ARPAData(float lp, float la)
Definition ARPAPredictor.h:49
ARPAData::ARPAData
ARPAData()
Definition ARPAPredictor.h:48
ARPAData::logAlfa
float logAlfa
Definition ARPAPredictor.h:51
ARPAData::logProb
float logProb
Definition ARPAPredictor.h:50
ARPAPredictor::predict
virtual Prediction predict(const size_t size, const char **filter) const
Generate prediction.
Definition ARPAPredictor.cpp:320
ARPAPredictor::addBigram
void addBigram(std::string)
Definition ARPAPredictor.cpp:241
ARPAPredictor::bigramProg
ProgressBar< char > * bigramProg
Definition ARPAPredictor.h:165
ARPAPredictor::addUnigram
void addUnigram(std::string)
Definition ARPAPredictor.cpp:214
ARPAPredictor::vocabFilename
std::string vocabFilename
Definition ARPAPredictor.h:135
ARPAPredictor::vocabCode
std::map< std::string, int > vocabCode
Definition ARPAPredictor.h:138
ARPAPredictor::unigramCount
int unigramCount
Definition ARPAPredictor.h:156
ARPAPredictor::createARPATable
void createARPATable()
Definition ARPAPredictor.cpp:106
ARPAPredictor::trigramMap
std::map< TrigramKey, float > trigramMap
Definition ARPAPredictor.h:143
ARPAPredictor::set_arpa_filename
void set_arpa_filename(const std::string &value)
Definition ARPAPredictor.cpp:67
ARPAPredictor::update
virtual void update(const Observable *variable)
Definition ARPAPredictor.cpp:461
ARPAPredictor::computeBigramBackoff
float computeBigramBackoff(int, int) const
Definition ARPAPredictor.cpp:443
ARPAPredictor::trigramTot
int trigramTot
Definition ARPAPredictor.h:162
ARPAPredictor::VOCABFILENAME
std::string VOCABFILENAME
Definition ARPAPredictor.h:131
ARPAPredictor::dispatcher
Dispatcher< ARPAPredictor > dispatcher
Definition ARPAPredictor.h:168
ARPAPredictor::vocabDecode
std::map< int, std::string > vocabDecode
Definition ARPAPredictor.h:139
ARPAPredictor::unigramProg
ProgressBar< char > * unigramProg
Definition ARPAPredictor.h:164
ARPAPredictor::matchesPrefixAndFilter
bool matchesPrefixAndFilter(std::string, std::string, const char **) const
Definition ARPAPredictor.cpp:305
ARPAPredictor::computeTrigramBackoff
float computeTrigramBackoff(int, int, int) const
Definition ARPAPredictor.cpp:409
ARPAPredictor::trigramProg
ProgressBar< char > * trigramProg
Definition ARPAPredictor.h:166
ARPAPredictor::learn
virtual void learn(const std::vector< std::string > &change)
Definition ARPAPredictor.cpp:455
ARPAPredictor::arpaFilename
std::string arpaFilename
Definition ARPAPredictor.h:134
ARPAPredictor::ARPAFILENAME
std::string ARPAFILENAME
Definition ARPAPredictor.h:130
ARPAPredictor::set_vocab_filename
void set_vocab_filename(const std::string &value)
Definition ARPAPredictor.cpp:61
ARPAPredictor::~ARPAPredictor
~ARPAPredictor()
Definition ARPAPredictor.cpp:298
ARPAPredictor::ARPAPredictor
ARPAPredictor(Configuration *, ContextTracker *, const char *)
Definition ARPAPredictor.cpp:37
ARPAPredictor::addTrigram
void addTrigram(std::string)
Definition ARPAPredictor.cpp:268
ARPAPredictor::set_timeout
void set_timeout(const std::string &value)
Definition ARPAPredictor.cpp:73
ARPAPredictor::unigramMap
std::map< int, ARPAData > unigramMap
Definition ARPAPredictor.h:141
ARPAPredictor::timeout
int timeout
Definition ARPAPredictor.h:136
ARPAPredictor::bigramTot
int bigramTot
Definition ARPAPredictor.h:161
ARPAPredictor::unigramTot
int unigramTot
Definition ARPAPredictor.h:160
ARPAPredictor::bigramMap
std::map< BigramKey, ARPAData > bigramMap
Definition ARPAPredictor.h:142
ARPAPredictor::TIMEOUT
std::string TIMEOUT
Definition ARPAPredictor.h:132
ARPAPredictor::LOGGER
std::string LOGGER
Definition ARPAPredictor.h:129
ARPAPredictor::trigramCount
int trigramCount
Definition ARPAPredictor.h:158
ARPAPredictor::loadVocabulary
void loadVocabulary()
Definition ARPAPredictor.cpp:79
ARPAPredictor::bigramCount
int bigramCount
Definition ARPAPredictor.h:157
BigramKey::operator<
bool operator<(const BigramKey &right) const
Definition ARPAPredictor.h:89
BigramKey::key1
int key1
Definition ARPAPredictor.h:105
BigramKey::key2
int key2
Definition ARPAPredictor.h:106
BigramKey::operator==
bool operator==(const TrigramKey &right) const
Definition ARPAPredictor.h:101
BigramKey::BigramKey
BigramKey(int wd1, int wd2)
Definition ARPAPredictor.h:87
Configuration
Definition configuration.h:36
ContextTracker
Tracks user interaction and context.
Definition contextTracker.h:155
Dispatcher
Definition dispatcher.h:42
Observable
Definition observable.h:37
Observer::Observer
Observer()
Definition observer.h:40
Prediction
Definition prediction.h:47
Predictor::Predictor
Predictor(Configuration *configuration, ContextTracker *contextTracker, const char *predictorName="Predictor", const char *shortDescription="", const char *longDescription="")
Definition predictor.cpp:39
ProgressBar
Definition progress.h:31
TrigramKey
Definition ARPAPredictor.h:55
TrigramKey::TrigramKey
TrigramKey(int wd1, int wd2, int wd3)
Definition ARPAPredictor.h:57
TrigramKey::operator<
bool operator<(const TrigramKey &right) const
Definition ARPAPredictor.h:59
TrigramKey::key2
int key2
Definition ARPAPredictor.h:80
TrigramKey::key3
int key3
Definition ARPAPredictor.h:81
TrigramKey::operator==
bool operator==(const TrigramKey &right) const
Definition ARPAPredictor.h:75
TrigramKey::key1
int key1
Definition ARPAPredictor.h:79
cmp
Definition ARPAPredictor.h:38
cmp::operator()
bool operator()(const float &f1, const float &f2) const
Definition ARPAPredictor.h:40
dispatcher.h
logger.h
predictor.h
progress.h