File size: 2,594 Bytes
6ae4a85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include "Moses2Wrapper.h"
#include <iostream>
#include <fstream>
#include <cassert>
#include <string.h>


// Generic helper definitions for shared library support
#if defined _WIN32
#define IMPORT __declspec(dllimport)
#define EXPORT __declspec(dllexport)
#else    // !(defined _WIN32 || defined __CYGWIN__) -- i.e., not Windows
#define __stdcall
#if __GNUC__ >= 4
#define IMPORT __attribute__ ((visibility ("default")))
#define EXPORT __attribute__ ((visibility ("default")))
#else   // __GNUC__ < 4, which does not support the __attribute__ tag
#define IMPORT
#define EXPORT
#endif  // __GNUC__ >= 4
#endif 


using namespace std;
using namespace Moses2;

extern "C" EXPORT MosesApiErrorCode __stdcall GetMosesSystem(const char* filePath, Moses2::Moses2Wrapper * *pObject) {
	if (*pObject == NULL) {
		*pObject = new Moses2::Moses2Wrapper(filePath);
		return MS_API_OK;
	}
	else {
		return MS_API_E_FAILURE;
	}
}

extern "C" EXPORT MosesApiErrorCode __stdcall Translate(Moses2::Moses2Wrapper * pObject, long id, bool nbest, const char* input, char** output) {
	if (pObject != NULL)
	{
		std::string tr = pObject->Translate(input, id, nbest);
		*output = Moses2Wrapper::CopyString(tr.c_str());
		return MS_API_OK;
	}
	else {
		return MS_API_E_FAILURE;
	}
}

extern "C" EXPORT MosesApiErrorCode __stdcall FreeMemory(char* output) {
	if (output != nullptr) {
		Moses2Wrapper::Free(output);
		return MS_API_OK;
	}
	else {
		return MS_API_E_FAILURE;
	}
}

extern "C" EXPORT MosesApiErrorCode __stdcall ReleaseSystem(Moses2::Moses2Wrapper **pObject) {
	if (*pObject != NULL)
	{
		delete* pObject;
		*pObject = NULL;
		return MS_API_OK;
	}
	else {
		return MS_API_E_FAILURE;
	}
}

extern "C" EXPORT MosesApiErrorCode __stdcall EngineVersion() {
	//std::cout << "windows build on v1142/ msvc 14.27.29110"<< std::endl;
	std::cout << "0.0.1" << std::endl;
	return MS_API_OK;
}

int main(int argc, char** argv)
{
	assert(argc >= 2);
	cerr << "Starting" << endl;
	string filePath(argv[1]); // = ".\\enu.rus.generalnn_contextual_translit.mosesconfig.ini";
	Moses2::Moses2Wrapper *pObject = nullptr;
	MosesApiErrorCode ret = GetMosesSystem(filePath.c_str(), &pObject);
	assert(ret == MS_API_OK);

	ifstream inFile;
	inFile.open(argv[2]);

	long id = 44;
	string input;
	while (std::getline(inFile, input))
	{
		char* output;
		ret = Translate(pObject, id, true, input.c_str(), &output);
		assert(ret == MS_API_OK);
		cerr << output << flush;

		ret = FreeMemory(output);
		assert(ret == MS_API_OK);

		++id;
	}

	ret = ReleaseSystem(&pObject);
	assert(ret == MS_API_OK);

	cerr << "Finished" << endl;
}