commit 6f70c46d63177429b167f46aeb5c6c819e6e59ec Author: Lokendra Singh Date: Mon Aug 26 12:01:32 2019 +0530 added: getopts and more flags (#56) * added: getopts and more flags * typemap: switch list -> tuple list being mutable can't be used as dictionary keys diff --git a/python/apertium_core.i b/python/apertium_core.i index 2c6624f..3f87b36 100644 --- a/python/apertium_core.i +++ b/python/apertium_core.i @@ -9,31 +9,31 @@ // Wrapper on char ** for char **argv // Modified for python 3 from http://www.swig.org/Doc1.3/Python.html#Python_nn59 -%typemap(in) char ** { - if (PyList_Check($input)) { - int size = PyList_Size($input); +%typemap(in) (int argc, char **argv) { + if (PyTuple_Check($input)) { int i = 0; - $1 = (char **) malloc((size+1)*sizeof(char *)); - for (i = 0; i < size; i++) { - PyObject *py_obj = PyList_GetItem($input, i); + $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)) { - $1[i] = strdup(PyUnicode_AsUTF8(py_obj)); + $2[i] = strdup(PyUnicode_AsUTF8(py_obj)); } else { - PyErr_SetString(PyExc_TypeError, "list must contain strings"); - free($1); + PyErr_SetString(PyExc_TypeError, "tuple must contain strings"); + free($2); return NULL; } } - $1[i] = 0; + $2[i] = 0; } else { - PyErr_SetString(PyExc_TypeError, "not a list"); + PyErr_SetString(PyExc_TypeError, "not a tuple"); return NULL; } } -%typemap(freearg) char ** { - free((char *) $1); +%typemap(freearg) (int argc, char **argv) { + free((char *) $2); } %inline%{ @@ -44,16 +44,47 @@ #include #include +#include + /** * Imitates functionality of apertium-core binaries using file path */ -void pretransfer(char arg, char *input_path, char *output_path) +void pretransfer(int argc, char **argv, char *input_path, char *output_path) { - bool useMaxEnt = false; - FILE *input = fopen(input_path, "r"), *output = fopen(output_path, "w"); - processStream(input, output, false, false, false); + FILE* input = fopen(input_path, "r"); + FILE* output = fopen(output_path, "w"); + bool compound_sep = false; + bool null_flush = false; + bool surface_forms = false; + + optind = 1; + while (true) + { + int c = getopt(argc, argv, "enz"); + if(c == -1) + { + break; + } + switch (c) + { + case 'z': + null_flush = true; + break; + + case 'e': + compound_sep = true; + break; + + case 'n': + surface_forms = true; + break; + default: + break; + } + } + processStream(input, output, null_flush, surface_forms, compound_sep); fclose(input); fclose(output); } @@ -67,11 +98,21 @@ class ApertiumTransfer: public Transfer read(transferfile, datafile); } - void transfer_text(char arg, char *input_path, char *output_path) + void transfer_text(int argc, char **argv, char *input_path, char *output_path) { - FILE *input = fopen(input_path, "r"), *output = fopen(output_path, "w"); - switch(arg) + FILE* input = fopen(input_path, "r"); + FILE* output = fopen(output_path, "w"); + optind = 1; + while (true) { + int c = getopt(argc, argv, "nbx:cztT"); + if(c == -1) + { + break; + } + + switch(c) + { case 'b': setPreBilingual(true); setUseBilingual(false); @@ -80,6 +121,30 @@ class ApertiumTransfer: public Transfer case 'n': setUseBilingual(false); break; + + case 'x': + setExtendedDictionary(optarg); + break; + + case 'c': + setCaseSensitiveness(true); + break; + + case 't': + setTrace(true); + break; + + case 'T': + setTrace(true); + setTraceATT(true); + break; + + case 'z': + setNullFlush(true); + break; + default: + break; + } } transfer(input, output); fclose(input); @@ -106,9 +171,31 @@ class ApertiumInterchunk: public Interchunk read(transferfile, datafile); } - void interchunk_text(char arg, char *input_path, char *output_path) + void interchunk_text(int argc, char **argv, char *input_path, char *output_path) { - FILE *input = fopen(input_path, "r"), *output = fopen(output_path, "w"); + FILE* input = fopen(input_path, "r"); + FILE* output = fopen(output_path, "w"); + optind = 1; + while (true) + { + int c = getopt(argc, argv, "zt"); + if(c == -1) + { + break; + } + switch (c) + { + case 'z': + setNullFlush(true); + break; + + case 't': + setTrace(true); + break; + default: + break; + } + } interchunk(input, output); fclose(input); fclose(output); @@ -124,9 +211,31 @@ class ApertiumPostchunk: public Postchunk read(transferfile, datafile); } - void postchunk_text(char arg, char *input_path, char *output_path) + void postchunk_text(int argc, char **argv, char *input_path, char *output_path) { - FILE *input = fopen(input_path, "r"), *output = fopen(output_path, "w"); + FILE* input = fopen(input_path, "r"); + FILE* output = fopen(output_path, "w"); + optind = 1; + while (true) + { + int c = getopt(argc, argv, "zt"); + if(c == -1) + { + break; + } + switch (c) + { + case 'z': + setNullFlush(true); + break; + + case 't': + setTrace(true); + break; + default: + break; + } + } postchunk(input, output); fclose(input); fclose(output);