-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSLM-rescore.cpp
156 lines (126 loc) · 4.43 KB
/
SLM-rescore.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//============================================================================
// Name : SLM.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C, Ansi-style
//============================================================================
#include <stdio.h>
#include <stdlib.h>
#include <tuple>
#include <stdexcept>
#include "Logging.h"
#include "ProgramOptions.h"
#include "BackoffStrategies.h"
#include "NgramBackoffStrategy.h"
#include "LanguageModel.h"
#include "Utils.h"
#include "Timer.h"
#include "NBestList.h"
#include "RescoreModule.h"
#include "TextPreprocessor.h"
int main(int argc, char** argv) {
SLM::ProgramOptions po(argc, argv);
SLM::LanguageModel lm(po);
SLM::BackoffStrategy* bo = SLM::BackoffStrategiesFactory::fromProgramOptions(po, lm)[0];
SLM::RescoreModule rm(bo, po.getTestOutputDirectory());
SLM::ProgressTimer pt;
if(po.isDisableProgress())
{
pt.disablePrinting();
}
SLM::CGNTextPreprocessor cgnTP;
for(std::string inputFile : po.getTestInputFiles())
{
L_V << "SLMr: Reading " << inputFile << "\n";
std::ifstream file(inputFile);
bo->nextFile();
pt.nextFile();
rm.nextFile(inputFile);
int currentRank = 0;
int lineNumber = 0;
std::string retrievedString;
while(std::getline(file, retrievedString))
{
++lineNumber;
bo->nextLine();
pt.nextLine();
std::stringstream linestream(retrievedString);
std::string acousticModelScoreString;
double acousticModelScore;
std::string languageModelScoreString;
double languageModelScore;
std::string numberOfWordsString;
int numberOfWords;
std::string sentenceString;
try
{
std::getline(linestream, acousticModelScoreString, ' ');
acousticModelScore = std::stod(acousticModelScoreString);
std::getline(linestream, languageModelScoreString, ' ');
languageModelScore = std::stod(languageModelScoreString);
std::getline(linestream, numberOfWordsString, ' ');
numberOfWords = std::stoi(numberOfWordsString);
std::getline(linestream, sentenceString);
sentenceString = trim(sentenceString);
} catch (const std::invalid_argument& ia)
{
std::cerr << "Invalid argument: " << ia.what() << '\n';
std::cerr << "In file: " << inputFile << std::endl;
std::cerr << "For line (" << lineNumber << "): " << retrievedString << std::endl;
continue;
}
// rm.addLine(sentenceString, ++currentRank, acousticModelScore, languageModelScore, numberOfWords);
if(!endsWith(sentenceString, "</s>"))
{
std::cerr << "Invalid line (no </s>): " << sentenceString << "(" << inputFile << ":" << lineNumber << ")\n";
continue;
}
// hack
if(po.addSentenceMarkers())
{
sentenceString = "<s> <s> " + sentenceString;
}
std::vector<std::string> tokens = whitespaceTokeniser(sentenceString);
std::vector<std::string> words = cgnTP.removeFillers(tokens);
L_P << "SLMr: Reading " << sentenceString << "\n";
L_P << "SLMr: Contains " << words.size() << " words (incl. sentence markers)\n";
sentenceString = join(words, " ");
L_P << "SLMr: Normalised to " << sentenceString << "\n";
rm.addLine(sentenceString, ++currentRank, acousticModelScore, languageModelScore, numberOfWords);
for(int i = (4-1); i < words.size(); ++i)
{
std::stringstream contextStream;
contextStream << words[i-(4-1)];
for(int ii = 1; ii < 4 - 1; ++ii)
{
contextStream << " " << words[i-(4-1)+ii];
}
try
{
Pattern context = lm.toPattern(contextStream.str());
Pattern focus = lm.toPattern(words[i]);
L_P << "SLMr: [" << lm.toString(context) << "] " << lm.toString(focus) << "\n";
bool isOOV = lm.isOOV(focus);
rm.evaluatePattern(focus, context, isOOV);
} catch (const UnknownTokenError &e)
{
// std::cerr << "Invalid argument: " << e.what() << '\n';
std::cerr << "Unknown token error in file: " << inputFile << std::endl;
std::cerr << "For line (" << lineNumber << "): " << retrievedString << std::endl;
std::cerr << "In pattern: " << contextStream.str() << " " << words[i] << std::endl;
continue;
}
//
pt.nextPattern();
//
pt.toString();
//
}
rm.rescoreLine();
}
rm.rescoreFile();
}
bo->done();
return EXIT_SUCCESS;
}