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
|
%module constraint_grammar
%include <std_map.i>
%include <std_string.i>
%include <std_vector.i>
%inline%{
namespace CG3 {}
using namespace CG3;
%}
%include <Grammar.hpp>
%typemap(in) (int argc, char **argv) {
if (PyTuple_Check($input)) {
int i = 0;
$1 = PyTuple_Size($input);
$2 = (char **) malloc(($1 + 1)*sizeof(char *));
for (i = 0; i < $1; i++) {
PyObject *py_obj = PyTuple_GetItem($input, i);
if (PyUnicode_Check(py_obj)) {
$2[i] = strdup(PyUnicode_AsUTF8(py_obj));
}
else {
PyErr_SetString(PyExc_TypeError, "tuple must contain strings");
free($2);
return NULL;
}
}
$2[i] = 0;
} else {
PyErr_SetString(PyExc_TypeError, "not a tuple");
return NULL;
}
}
%typemap(freearg) (int argc, char **argv) {
free((char *) $2);
}
%inline%{
#define SWIG_FILE_WITH_INIT
#include "stdafx.hpp"
#include "Grammar.hpp"
#include "TextualParser.hpp"
#include "BinaryGrammar.hpp"
#include "ApertiumApplicator.hpp"
#include "MatxinApplicator.hpp"
#include "GrammarApplicator.hpp"
#include <getopt.h>
class CGProc: public CG3::Grammar
{
private:
CG3::Grammar grammar;
public:
CGProc(char *dictionary_path);
void cg_proc(int argc, char **argv, char *input_path, char *output_path);
};
CGProc::CGProc(char *dictionary_path)
{
std::unique_ptr<CG3::IGrammarParser> parser;
FILE* dictionary = fopen(dictionary_path, "rb");
fread(&CG3::cbuffers[0][0], 1, 4, dictionary);
fclose(dictionary);
if (CG3::cbuffers[0][0] == 'C' && CG3::cbuffers[0][1] == 'G' && CG3::cbuffers[0][2] == '3' && CG3::cbuffers[0][3] == 'B') {
parser.reset(new CG3::BinaryGrammar(grammar, std::cerr));
}
else {
parser.reset(new CG3::TextualParser(grammar, std::cerr));
}
parser->parse_grammar(dictionary_path);
}
void CGProc::cg_proc(int argc, char **argv, char *input_path, char *output_path)
{
bool trace = false;
bool wordform_case = false;
bool print_word_forms = true;
bool only_first = false;
int sections = 0;
std::string single_rule;
std::ifstream input;
input.open(input_path, std::ios::binary);
std::istream& ux_stdin = input;
std::ofstream output;
output.open(output_path, std::ios::binary);
std::ostream& ux_stdout = output;
int c = 0;
optind = 1;
while (true) {
c = getopt(argc, argv, "s:f:tn1wz");
if (c == -1) {
break;
}
switch (c) {
case 'f':
break;
case 't':
trace = true;
break;
case 's':
sections = atoi(optarg);
break;
case 'n':
print_word_forms = false;
break;
case '1':
only_first = true;
break;
case 'w':
wordform_case = true;
break;
case 'z':
break;
}
}
grammar.reindex();
std::unique_ptr<CG3::GrammarApplicator> applicator;
CG3::ApertiumApplicator* apertiumApplicator = new CG3::ApertiumApplicator(std::cerr);
apertiumApplicator->wordform_case = wordform_case;
apertiumApplicator->print_word_forms = print_word_forms;
apertiumApplicator->print_only_first = only_first;
applicator.reset(apertiumApplicator);
applicator->setGrammar(&grammar);
for (int32_t i = 1; i <= sections; i++) {
applicator->sections.push_back(i);
}
applicator->trace = trace;
applicator->unicode_tags = true;
applicator->unique_tags = false;
applicator->runGrammarOnText(ux_stdin, ux_stdout);
u_cleanup();
}
%}
|