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 |
+
#include <unistd.h>
|
| 7 |
+
#include <csignal>
|
| 8 |
+
|
| 9 |
+
#if defined(__GLIBCXX__) || defined(__GLIBCPP__)
|
| 10 |
+
|
| 11 |
+
#include "Fdstream.h"
|
| 12 |
+
|
| 13 |
+
using namespace std;
|
| 14 |
+
|
| 15 |
+
#define CHILD_STDIN_READ pipefds_input[0]
|
| 16 |
+
#define CHILD_STDIN_WRITE pipefds_input[1]
|
| 17 |
+
#define CHILD_STDOUT_READ pipefds_output[0]
|
| 18 |
+
#define CHILD_STDOUT_WRITE pipefds_output[1]
|
| 19 |
+
#define CHILD_STDERR_READ pipefds_error[0]
|
| 20 |
+
#define CHILD_STDERR_WRITE pipefds_error[1]
|
| 21 |
+
|
| 22 |
+
namespace MosesTuning
|
| 23 |
+
{
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
// Child exec error signal
|
| 27 |
+
void exec_failed (int sig)
|
| 28 |
+
{
|
| 29 |
+
cerr << "Exec failed. Child process couldn't be launched." << endl;
|
| 30 |
+
exit (EXIT_FAILURE);
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
PreProcessFilter::PreProcessFilter(const string& filterCommand)
|
| 34 |
+
: m_toFilter(NULL),
|
| 35 |
+
m_fromFilter(NULL)
|
| 36 |
+
{
|
| 37 |
+
#if defined __MINGW32__
|
| 38 |
+
//TODO(jie): replace this function with boost implementation
|
| 39 |
+
#else
|
| 40 |
+
// Child error signal install
|
| 41 |
+
// sigaction is the replacement for the traditional signal() method
|
| 42 |
+
struct sigaction action;
|
| 43 |
+
action.sa_handler = exec_failed;
|
| 44 |
+
sigemptyset(&action.sa_mask);
|
| 45 |
+
action.sa_flags = 0;
|
| 46 |
+
if (sigaction(SIGUSR1, &action, NULL) < 0) {
|
| 47 |
+
perror("SIGUSR1 install error");
|
| 48 |
+
exit(EXIT_FAILURE);
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
int pipe_status;
|
| 52 |
+
int pipefds_input[2];
|
| 53 |
+
int pipefds_output[2];
|
| 54 |
+
// int pipefds_error[2];
|
| 55 |
+
|
| 56 |
+
// Create the pipes
|
| 57 |
+
// We do this before the fork so both processes will know about
|
| 58 |
+
// the same pipe and they can communicate.
|
| 59 |
+
|
| 60 |
+
pipe_status = pipe(pipefds_input);
|
| 61 |
+
if (pipe_status == -1) {
|
| 62 |
+
perror("Error creating the pipe");
|
| 63 |
+
exit(EXIT_FAILURE);
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
pipe_status = pipe(pipefds_output);
|
| 67 |
+
if (pipe_status == -1) {
|
| 68 |
+
perror("Error creating the pipe");
|
| 69 |
+
exit(EXIT_FAILURE);
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
/*
|
| 73 |
+
pipe_status = pipe(pipefds_error);
|
| 74 |
+
if (pipe_status == -1)
|
| 75 |
+
{
|
| 76 |
+
perror("Error creating the pipe");
|
| 77 |
+
exit(EXIT_FAILURE);
|
| 78 |
+
}
|
| 79 |
+
*/
|
| 80 |
+
|
| 81 |
+
pid_t pid;
|
| 82 |
+
// Create child process; both processes continue from here
|
| 83 |
+
pid = fork();
|
| 84 |
+
|
| 85 |
+
if (pid == pid_t(0)) {
|
| 86 |
+
// Child process
|
| 87 |
+
|
| 88 |
+
// When the child process finishes sends a SIGCHLD signal
|
| 89 |
+
// to the parent
|
| 90 |
+
|
| 91 |
+
// Tie the standard input, output and error streams to the
|
| 92 |
+
// appropiate pipe ends
|
| 93 |
+
// The file descriptor 0 is the standard input
|
| 94 |
+
// We tie it to the read end of the pipe as we will use
|
| 95 |
+
// this end of the pipe to read from it
|
| 96 |
+
dup2 (CHILD_STDIN_READ,0);
|
| 97 |
+
dup2 (CHILD_STDOUT_WRITE,1);
|
| 98 |
+
// dup2 (CHILD_STDERR_WRITE,2);
|
| 99 |
+
// Close in the child the unused ends of the pipes
|
| 100 |
+
close(CHILD_STDIN_WRITE);
|
| 101 |
+
close(CHILD_STDOUT_READ);
|
| 102 |
+
//close(CHILD_STDERR_READ);
|
| 103 |
+
|
| 104 |
+
// Execute the program
|
| 105 |
+
execl("/bin/bash", "bash", "-c", filterCommand.c_str() , (char*)NULL);
|
| 106 |
+
|
| 107 |
+
// We should never reach this point
|
| 108 |
+
// Tell the parent the exec failed
|
| 109 |
+
kill(getppid(), SIGUSR1);
|
| 110 |
+
exit(EXIT_FAILURE);
|
| 111 |
+
} else if (pid > pid_t(0)) {
|
| 112 |
+
// Parent
|
| 113 |
+
|
| 114 |
+
// Close in the parent the unused ends of the pipes
|
| 115 |
+
close(CHILD_STDIN_READ);
|
| 116 |
+
close(CHILD_STDOUT_WRITE);
|
| 117 |
+
// close(CHILD_STDERR_WRITE);
|
| 118 |
+
|
| 119 |
+
m_toFilter = new ofdstream(CHILD_STDIN_WRITE);
|
| 120 |
+
m_fromFilter = new ifdstream(CHILD_STDOUT_READ);
|
| 121 |
+
} else {
|
| 122 |
+
perror("Error: fork failed");
|
| 123 |
+
exit(EXIT_FAILURE);
|
| 124 |
+
}
|
| 125 |
+
#endif // defined
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
string PreProcessFilter::ProcessSentence(const string& sentence)
|
| 129 |
+
{
|
| 130 |
+
*m_toFilter << sentence << "\n";
|
| 131 |
+
string processedSentence;
|
| 132 |
+
m_fromFilter->getline(processedSentence);
|
| 133 |
+
return processedSentence;
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
PreProcessFilter::~PreProcessFilter()
|
| 137 |
+
{
|
| 138 |
+
delete m_toFilter;
|
| 139 |
+
delete m_fromFilter;
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
#endif
|
mosesdecoder/mert/UtilTest.cpp
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include "Util.h"
|
| 2 |
+
|
| 3 |
+
#define BOOST_TEST_MODULE UtilTest
|
| 4 |
+
#include <boost/test/unit_test.hpp>
|
| 5 |
+
|
| 6 |
+
using namespace MosesTuning;
|
| 7 |
+
|
| 8 |
+
BOOST_AUTO_TEST_CASE(util_get_next_pound_test)
|
| 9 |
+
{
|
| 10 |
+
{
|
| 11 |
+
std::string str("9 9 7 ");
|
| 12 |
+
std::string substr;
|
| 13 |
+
std::vector<std::string> res;
|
| 14 |
+
|
| 15 |
+
while (!str.empty()) {
|
| 16 |
+
getNextPound(str, substr);
|
| 17 |
+
res.push_back(substr);
|
| 18 |
+
}
|
| 19 |
+
BOOST_REQUIRE(res.size() == 3);
|
| 20 |
+
BOOST_CHECK_EQUAL("9", res[0]);
|
| 21 |
+
BOOST_CHECK_EQUAL("9", res[1]);
|
| 22 |
+
BOOST_CHECK_EQUAL("7", res[2]);
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
{
|
| 26 |
+
std::string str("ref.0,ref.1,ref.2");
|
| 27 |
+
std::string substr;
|
| 28 |
+
std::vector<std::string> res;
|
| 29 |
+
const std::string delim(",");
|
| 30 |
+
|
| 31 |
+
while (!str.empty()) {
|
| 32 |
+
getNextPound(str, substr, delim);
|
| 33 |
+
res.push_back(substr);
|
| 34 |
+
}
|
| 35 |
+
BOOST_REQUIRE(res.size() == 3);
|
| 36 |
+
BOOST_CHECK_EQUAL("ref.0", res[0]);
|
| 37 |
+
BOOST_CHECK_EQUAL("ref.1", res[1]);
|
| 38 |
+
BOOST_CHECK_EQUAL("ref.2", res[2]);
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
BOOST_AUTO_TEST_CASE(util_tokenize_test)
|
| 43 |
+
{
|
| 44 |
+
{
|
| 45 |
+
std::vector<std::string> res;
|
| 46 |
+
Tokenize("9 9 7", ' ', &res);
|
| 47 |
+
BOOST_REQUIRE(res.size() == 3);
|
| 48 |
+
BOOST_CHECK_EQUAL("9", res[0]);
|
| 49 |
+
BOOST_CHECK_EQUAL("9", res[1]);
|
| 50 |
+
BOOST_CHECK_EQUAL("7", res[2]);
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
{
|
| 54 |
+
std::vector<std::string> res;
|
| 55 |
+
Tokenize("9 8 7 ", ' ', &res);
|
| 56 |
+
BOOST_REQUIRE(res.size() == 3);
|
| 57 |
+
BOOST_CHECK_EQUAL("9", res[0]);
|
| 58 |
+
BOOST_CHECK_EQUAL("8", res[1]);
|
| 59 |
+
BOOST_CHECK_EQUAL("7", res[2]);
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
{
|
| 63 |
+
std::vector<std::string> res;
|
| 64 |
+
Tokenize("ref.0,ref.1,", ',', &res);
|
| 65 |
+
BOOST_REQUIRE(res.size() == 2);
|
| 66 |
+
BOOST_CHECK_EQUAL("ref.0", res[0]);
|
| 67 |
+
BOOST_CHECK_EQUAL("ref.1", res[1]);
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
BOOST_AUTO_TEST_CASE(util_ends_with_test)
|
| 72 |
+
{
|
| 73 |
+
BOOST_CHECK(EndsWith("abc:", ":"));
|
| 74 |
+
BOOST_CHECK(EndsWith("a b c:", ":"));
|
| 75 |
+
BOOST_CHECK(!EndsWith("a", ":"));
|
| 76 |
+
BOOST_CHECK(!EndsWith("a:b", ":"));
|
| 77 |
+
|
| 78 |
+
BOOST_CHECK(EndsWith("ab ", " "));
|
| 79 |
+
BOOST_CHECK(!EndsWith("ab", " "));
|
| 80 |
+
BOOST_CHECK(!EndsWith("a b", " "));
|
| 81 |
+
}
|
mosesdecoder/mert/VocabularyTest.cpp
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include "Vocabulary.h"
|
| 2 |
+
#include "Singleton.h"
|
| 3 |
+
|
| 4 |
+
#define BOOST_TEST_MODULE MertVocabulary
|
| 5 |
+
#include <boost/test/unit_test.hpp>
|
| 6 |
+
|
| 7 |
+
using namespace MosesTuning;
|
| 8 |
+
|
| 9 |
+
namespace mert
|
| 10 |
+
{
|
| 11 |
+
namespace
|
| 12 |
+
{
|
| 13 |
+
|
| 14 |
+
void TearDown()
|
| 15 |
+
{
|
| 16 |
+
Singleton<Vocabulary>::Delete();
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
} // namespace
|
| 20 |
+
|
| 21 |
+
BOOST_AUTO_TEST_CASE(vocab_basic)
|
| 22 |
+
{
|
| 23 |
+
Vocabulary vocab;
|
| 24 |
+
BOOST_REQUIRE(vocab.empty());
|
| 25 |
+
vocab.clear();
|
| 26 |
+
|
| 27 |
+
BOOST_CHECK_EQUAL(0, vocab.Encode("hello"));
|
| 28 |
+
BOOST_CHECK_EQUAL(0, vocab.Encode("hello"));
|
| 29 |
+
BOOST_CHECK_EQUAL(1, vocab.Encode("world"));
|
| 30 |
+
|
| 31 |
+
BOOST_CHECK_EQUAL(2, vocab.size());
|
| 32 |
+
|
| 33 |
+
int v;
|
| 34 |
+
BOOST_CHECK(vocab.Lookup("hello", &v));
|
| 35 |
+
BOOST_CHECK_EQUAL(0, v);
|
| 36 |
+
BOOST_CHECK(vocab.Lookup("world", &v));
|
| 37 |
+
BOOST_CHECK_EQUAL(1, v);
|
| 38 |
+
|
| 39 |
+
BOOST_CHECK(!vocab.Lookup("java", &v));
|
| 40 |
+
|
| 41 |
+
vocab.clear();
|
| 42 |
+
BOOST_CHECK(!vocab.Lookup("hello", &v));
|
| 43 |
+
BOOST_CHECK(!vocab.Lookup("world", &v));
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
BOOST_AUTO_TEST_CASE(vocab_factory_test)
|
| 47 |
+
{
|
| 48 |
+
Vocabulary* vocab1 = VocabularyFactory::GetVocabulary();
|
| 49 |
+
Vocabulary* vocab2 = VocabularyFactory::GetVocabulary();
|
| 50 |
+
Vocabulary* vocab3 = VocabularyFactory::GetVocabulary();
|
| 51 |
+
|
| 52 |
+
BOOST_REQUIRE(vocab1 != NULL);
|
| 53 |
+
BOOST_CHECK(vocab1 == vocab2);
|
| 54 |
+
BOOST_CHECK(vocab2 == vocab3);
|
| 55 |
+
|
| 56 |
+
TearDown();
|
| 57 |
+
}
|
| 58 |
+
} // namespace mert
|