Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added flag to import conditional probability table #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/fast_align.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void ParseLine(const string& line,

string input;
string conditional_probability_filename = "";
string existing_probability_filename = "";
int is_reverse = 0;
int ITERATIONS = 5;
int favor_diagonal = 0;
Expand All @@ -79,13 +80,14 @@ struct option options[] = {
{"alpha", required_argument, 0, 'a'},
{"no_null_word", no_argument, &no_null_word, 1 },
{"conditional_probabilities", required_argument, 0, 'c'},
{"existing_probabilities", required_argument, 0, 'e'},
{0,0,0,0}
};

bool InitCommandLine(int argc, char** argv) {
while (1) {
int oi;
int c = getopt_long(argc, argv, "i:rI:dp:T:ova:Nc:", options, &oi);
int c = getopt_long(argc, argv, "i:rI:dp:T:ova:Nc:e:", options, &oi);
if (c == -1) break;
switch(c) {
case 'i': input = optarg; break;
Expand All @@ -99,6 +101,7 @@ bool InitCommandLine(int argc, char** argv) {
case 'a': alpha = atof(optarg); break;
case 'N': no_null_word = 1; break;
case 'c': conditional_probability_filename = optarg; break;
case 'e': existing_probability_filename = optarg; break;
default: return false;
}
}
Expand All @@ -116,6 +119,7 @@ int main(int argc, char** argv) {
<< " -o: [USE] Optimize how close to the diagonal alignment points should be\n"
<< " -r: Run alignment in reverse (condition on target and predict source)\n"
<< " -c: Output conditional probability table\n"
<< " -e: Start with existing conditional probability table\n"
<< " Advanced options:\n"
<< " -I: number of iterations in EM training (default = 5)\n"
<< " -p: p_null parameter (default = 0.08)\n"
Expand All @@ -132,12 +136,19 @@ int main(int argc, char** argv) {
double prob_align_not_null = 1.0 - prob_align_null;
const unsigned kNULL = d.Convert("<eps>");
TTable s2t, t2s;
if (!existing_probability_filename.empty()) {
bool success = s2t.ImportFromFile(existing_probability_filename.c_str(), '\t', d);
if (!success) {
cerr << "Can't read " << existing_probability_filename << endl;
return 1;
}
}
unordered_map<pair<short, short>, unsigned, PairHash> size_counts;
double tot_len_ratio = 0;
double mean_srclen_multiplier = 0;
vector<double> probs;
for (int iter = 0; iter < ITERATIONS; ++iter) {
const bool final_iteration = (iter == (ITERATIONS - 1));
for (int iter = 0; iter < ITERATIONS || (iter==0 && ITERATIONS==0); ++iter) {
const bool final_iteration = (iter >= (ITERATIONS - 1));
cerr << "ITERATION " << (iter + 1) << (final_iteration ? " (FINAL)" : "") << endl;
ifstream in(input.c_str());
if (!in) {
Expand Down
32 changes: 32 additions & 0 deletions src/ttables.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

#include <cmath>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <tr1/unordered_map>

struct Md {
Expand Down Expand Up @@ -107,6 +110,35 @@ class TTable {
}
file.close();
}
bool ImportFromFile(const char* filename, char delim, Dict& d) {
std::ifstream in(filename);
if (!in) {
return false;
} else {
std::string line;
while(true) {
std::getline(in, line);
if (!in) break;
std::string sourceWord, targetWord, valueString;
std::stringstream stream(line);

bool success = true;
success &= (std::getline(stream, sourceWord, delim) != NULL);
success &= (std::getline(stream, targetWord, delim) != NULL);
success &= (std::getline(stream, valueString, delim) != NULL);

if (success) {
unsigned source = d.Convert(sourceWord);
unsigned target = d.Convert(targetWord);
double value = atof(valueString.c_str());
ttable[source][target] = value;
} else {
return false;
}
}
}
return true;
}
public:
Word2Word2Double ttable;
Word2Word2Double counts;
Expand Down