Cleanup & reindent .cpp files

This commit is contained in:
Philippe Vaucher
2014-03-19 11:26:31 +01:00
parent e85651ef84
commit 460205e943
26 changed files with 2349 additions and 2805 deletions

View File

@@ -19,7 +19,6 @@
#include "postprocess.h"
PostProcess::PostProcess(Config* config)
{
this->config = config;
@@ -29,12 +28,11 @@ PostProcess::PostProcess(Config* config)
std::ifstream infile(filename.str().c_str());
string region, pattern;
while (infile >> region >> pattern)
{
RegexRule* rule = new RegexRule(region, pattern);
//cout << "REGION: " << region << " PATTERN: " << pattern << endl;
//cout << "REGION: " << region << " PATTERN: " << pattern << endl;
if (rules.find(region) == rules.end())
{
@@ -61,16 +59,16 @@ PostProcess::~PostProcess()
// TODO: Delete all entries in rules vector
map<string, vector<RegexRule*> >::iterator iter;
for (iter = rules.begin(); iter != rules.end(); ++iter) {
for (iter = rules.begin(); iter != rules.end(); ++iter)
{
for (int i = 0; i < iter->second.size(); i++)
{
delete iter->second[i];
delete iter->second[i];
}
}
}
void PostProcess::addLetter(char letter, int charposition, float score)
{
if (score < config->postProcessMinConfidence)
@@ -80,8 +78,8 @@ void PostProcess::addLetter(char letter, int charposition, float score)
if (score < config->postProcessConfidenceSkipLevel)
{
float adjustedScore = abs(config->postProcessConfidenceSkipLevel - score) + config->postProcessMinConfidence;
insertLetter(SKIP_CHAR, charposition, adjustedScore );
float adjustedScore = abs(config->postProcessConfidenceSkipLevel - score) + config->postProcessMinConfidence;
insertLetter(SKIP_CHAR, charposition, adjustedScore );
}
//if (letter == '0')
@@ -96,25 +94,24 @@ void PostProcess::insertLetter(char letter, int charposition, float score)
score = score - config->postProcessMinConfidence;
int existingIndex = -1;
if (letters.size() < charposition + 1)
{
for (int i = letters.size(); i < charposition + 1; i++)
{
vector<Letter> tmp;
letters.push_back(tmp);
}
for (int i = letters.size(); i < charposition + 1; i++)
{
vector<Letter> tmp;
letters.push_back(tmp);
}
}
for (int i = 0; i < letters[charposition].size(); i++)
{
if (letters[charposition][i].letter == letter &&
letters[charposition][i].charposition == charposition)
{
existingIndex = i;
break;
}
if (letters[charposition][i].letter == letter &&
letters[charposition][i].charposition == charposition)
{
existingIndex = i;
break;
}
}
if (existingIndex == -1)
@@ -128,18 +125,17 @@ void PostProcess::insertLetter(char letter, int charposition, float score)
}
else
{
letters[charposition][existingIndex].occurences = letters[charposition][existingIndex].occurences + 1;
letters[charposition][existingIndex].totalscore = letters[charposition][existingIndex].totalscore + score;
letters[charposition][existingIndex].occurences = letters[charposition][existingIndex].occurences + 1;
letters[charposition][existingIndex].totalscore = letters[charposition][existingIndex].totalscore + score;
}
}
void PostProcess::clear()
{
for (int i = 0; i < letters.size(); i++)
{
letters[i].clear();
letters[i].clear();
}
letters.resize(0);
@@ -157,22 +153,18 @@ void PostProcess::analyze(string templateregion, int topn)
timespec startTime;
getTime(&startTime);
// Get a list of missing positions
for (int i = letters.size() -1; i >= 0; i--)
{
if (letters[i].size() == 0)
{
unknownCharPositions.push_back(i);
unknownCharPositions.push_back(i);
}
}
if (letters.size() == 0)
return;
// Sort the letters as they are
for (int i = 0; i < letters.size(); i++)
{
@@ -180,8 +172,6 @@ void PostProcess::analyze(string templateregion, int topn)
sort(letters[i].begin(), letters[i].end(), letterCompare);
}
if (this->config->debugPostProcess)
{
@@ -189,7 +179,7 @@ void PostProcess::analyze(string templateregion, int topn)
for (int i = 0; i < letters.size(); i++)
{
for (int j = 0; j < letters[i].size(); j++)
cout << "PostProcess Letter: " << letters[i][j].charposition << " " << letters[i][j].letter << " -- score: " << letters[i][j].totalscore << " -- occurences: " << letters[i][j].occurences << endl;
cout << "PostProcess Letter: " << letters[i][j].charposition << " " << letters[i][j].letter << " -- score: " << letters[i][j].totalscore << " -- occurences: " << letters[i][j].occurences << endl;
}
}
@@ -211,7 +201,6 @@ void PostProcess::analyze(string templateregion, int topn)
vector<Letter> tmp;
findAllPermutations(tmp, 0, config->postProcessMaxSubstitutions);
timespec sortStartTime;
getTime(&sortStartTime);
@@ -228,11 +217,8 @@ void PostProcess::analyze(string templateregion, int topn)
cout << " -- PostProcess Sort Time: " << diffclock(sortStartTime, sortEndTime) << "ms." << endl;
}
matchesTemplate = false;
if (templateregion != "")
{
vector<RegexRule*> regionRules = rules[templateregion];
@@ -241,22 +227,20 @@ void PostProcess::analyze(string templateregion, int topn)
{
for (int j = 0; j < regionRules.size(); j++)
{
allPossibilities[i].matchesTemplate = regionRules[j]->match(allPossibilities[i].letters);
if (allPossibilities[i].matchesTemplate)
{
allPossibilities[i].letters = regionRules[j]->filterSkips(allPossibilities[i].letters);
//bestChars = regionRules[j]->filterSkips(allPossibilities[i].letters);
matchesTemplate = true;
break;
}
allPossibilities[i].matchesTemplate = regionRules[j]->match(allPossibilities[i].letters);
if (allPossibilities[i].matchesTemplate)
{
allPossibilities[i].letters = regionRules[j]->filterSkips(allPossibilities[i].letters);
//bestChars = regionRules[j]->filterSkips(allPossibilities[i].letters);
matchesTemplate = true;
break;
}
}
if (i >= topn - 1)
break;
break;
//if (matchesTemplate || i >= TOP_N - 1)
//break;
//break;
}
}
@@ -264,11 +248,11 @@ void PostProcess::analyze(string templateregion, int topn)
{
for (int z = 0; z < allPossibilities.size(); z++)
{
if (allPossibilities[z].matchesTemplate)
{
bestChars = allPossibilities[z].letters;
break;
}
if (allPossibilities[z].matchesTemplate)
{
bestChars = allPossibilities[z].letters;
break;
}
}
}
else
@@ -276,7 +260,7 @@ void PostProcess::analyze(string templateregion, int topn)
bestChars = allPossibilities[0].letters;
}
// Now adjust the confidence scores to a percentage value
// Now adjust the confidence scores to a percentage value
if (allPossibilities.size() > 0)
{
float maxPercentScore = calculateMaxConfidenceScore();
@@ -289,28 +273,23 @@ void PostProcess::analyze(string templateregion, int topn)
}
if (this->config->debugPostProcess)
{
// Print top words
for (int i = 0; i < allPossibilities.size(); i++)
{
cout << "Top " << topn << " Possibilities: " << allPossibilities[i].letters << " :\t" << allPossibilities[i].totalscore;
if (allPossibilities[i].letters == bestChars)
cout << " <--- ";
cout << " <--- ";
cout << endl;
if (i >= topn - 1)
break;
break;
}
cout << allPossibilities.size() << " total permutations" << endl;
}
if (config->debugTiming)
{
timespec endTime;
@@ -333,8 +312,8 @@ float PostProcess::calculateMaxConfidenceScore()
{
if (letters[i].size() > 0)
{
totalScore += (letters[i][0].totalscore / letters[i][0].occurences) + config->postProcessMinConfidence;
numScores++;
totalScore += (letters[i][0].totalscore / letters[i][0].occurences) + config->postProcessMinConfidence;
numScores++;
}
}
@@ -372,19 +351,18 @@ vector<int> PostProcess::getMaxDepth(int topn)
nextLeastDropCharPos = getNextLeastDrop(depth);
}
return depth;
}
int PostProcess::getPermutationCount(vector<int> depth)
{
int permutationCount = 1;
for (int i = 0; i < depth.size(); i++)
{
permutationCount *= (depth[i] + 1);
}
for (int i = 0; i < depth.size(); i++)
{
permutationCount *= (depth[i] + 1);
}
return permutationCount;
return permutationCount;
}
int PostProcess::getNextLeastDrop(vector<int> depth)
@@ -392,21 +370,21 @@ int PostProcess::getNextLeastDrop(vector<int> depth)
int nextLeastDropCharPos = -1;
float leastNextDrop = 99999999999;
for (int i = 0; i < letters.size(); i++)
for (int i = 0; i < letters.size(); i++)
{
if (depth[i] + 1 >= letters[i].size())
continue;
float drop = letters[i][depth[i]].totalscore - letters[i][depth[i]+1].totalscore;
if (drop < leastNextDrop)
{
if (depth[i] + 1 >= letters[i].size())
continue;
float drop = letters[i][depth[i]].totalscore - letters[i][depth[i]+1].totalscore;
if (drop < leastNextDrop)
{
nextLeastDropCharPos = i;
leastNextDrop = drop;
}
nextLeastDropCharPos = i;
leastNextDrop = drop;
}
}
return nextLeastDropCharPos;
return nextLeastDropCharPos;
}
const vector<PPResult> PostProcess::getResults()
@@ -426,23 +404,23 @@ void PostProcess::findAllPermutations(vector<Letter> prevletters, int charPos, i
if (charPos == letters.size() - 1)
{
// Last letter, add the word
PPResult possibility;
possibility.letters = "";
possibility.totalscore = 0;
possibility.matchesTemplate = false;
for (int z = 0; z < prevletters.size(); z++)
{
if (prevletters[z].letter != SKIP_CHAR)
possibility.letters = possibility.letters + prevletters[z].letter;
possibility.totalscore = possibility.totalscore + prevletters[z].totalscore;
}
// Last letter, add the word
PPResult possibility;
possibility.letters = "";
possibility.totalscore = 0;
possibility.matchesTemplate = false;
for (int z = 0; z < prevletters.size(); z++)
{
if (prevletters[z].letter != SKIP_CHAR)
possibility.letters = possibility.letters + prevletters[z].letter;
possibility.totalscore = possibility.totalscore + prevletters[z].totalscore;
}
if (letters[charPos][i].letter != SKIP_CHAR)
possibility.letters = possibility.letters + letters[charPos][i].letter;
possibility.totalscore = possibility.totalscore +letters[charPos][i].totalscore;
if (letters[charPos][i].letter != SKIP_CHAR)
possibility.letters = possibility.letters + letters[charPos][i].letter;
possibility.totalscore = possibility.totalscore +letters[charPos][i].totalscore;
allPossibilities.push_back(possibility);
allPossibilities.push_back(possibility);
}
else
{
@@ -450,9 +428,9 @@ void PostProcess::findAllPermutations(vector<Letter> prevletters, int charPos, i
float scorePercentDiff = abs( letters[charPos][0].totalscore - letters[charPos][i].totalscore ) / letters[charPos][0].totalscore;
if (i != 0 && letters[charPos][i].letter != SKIP_CHAR && scorePercentDiff > 0.10f )
findAllPermutations(prevletters, charPos + 1, substitutionsLeft - 1);
findAllPermutations(prevletters, charPos + 1, substitutionsLeft - 1);
else
findAllPermutations(prevletters, charPos + 1, substitutionsLeft);
findAllPermutations(prevletters, charPos + 1, substitutionsLeft);
prevletters.pop_back();
}
@@ -460,19 +438,15 @@ void PostProcess::findAllPermutations(vector<Letter> prevletters, int charPos, i
if (letters[charPos].size() == 0)
{
// No letters for this char position...
// Just pass it along
findAllPermutations(prevletters, charPos + 1, substitutionsLeft);
// No letters for this char position...
// Just pass it along
findAllPermutations(prevletters, charPos + 1, substitutionsLeft);
}
}
bool wordCompare( const PPResult &left, const PPResult &right ){
bool wordCompare( const PPResult &left, const PPResult &right )
{
if (left.totalscore < right.totalscore)
return false;
return true;
@@ -486,7 +460,6 @@ bool letterCompare( const Letter &left, const Letter &right )
return true;
}
RegexRule::RegexRule(string region, string pattern)
{
this->original = pattern;
@@ -495,31 +468,31 @@ RegexRule::RegexRule(string region, string pattern)
numchars = 0;
for (int i = 0; i < pattern.size(); i++)
{
if (pattern.at(i) == '[')
if (pattern.at(i) == '[')
{
while (pattern.at(i) != ']' )
{
while (pattern.at(i) != ']' )
{
this->regex = this->regex + pattern.at(i);
i++;
}
this->regex = this->regex + ']';
this->regex = this->regex + pattern.at(i);
i++;
}
this->regex = this->regex + ']';
}
else if (pattern.at(i) == '?')
{
this->regex = this->regex + '.';
this->skipPositions.push_back(numchars);
}
else if (pattern.at(i) == '@')
{
this->regex = this->regex + "\\a";
}
else if (pattern.at(i) == '#')
{
this->regex = this->regex + "\\d";
}
}
else if (pattern.at(i) == '?')
{
this->regex = this->regex + '.';
this->skipPositions.push_back(numchars);
}
else if (pattern.at(i) == '@')
{
this->regex = this->regex + "\\a";
}
else if (pattern.at(i) == '#')
{
this->regex = this->regex + "\\d";
}
numchars++;
numchars++;
}
trexp.Compile(this->regex.c_str());
@@ -529,7 +502,6 @@ RegexRule::RegexRule(string region, string pattern)
// cout << "AA Skip position: " << skipPositions[z] << endl;
}
bool RegexRule::match(string text)
{
if (text.length() != numchars)
@@ -544,17 +516,17 @@ string RegexRule::filterSkips(string text)
for (int i = 0; i < text.size(); i++)
{
bool skip = false;
for (int j = 0; j < skipPositions.size(); j++)
for (int j = 0; j < skipPositions.size(); j++)
{
if (skipPositions[j] == i)
{
if (skipPositions[j] == i)
{
skip = true;
break;
}
skip = true;
break;
}
}
if (skip == false)
response = response + text[i];
if (skip == false)
response = response + text[i];
}
return response;