commit 4f3807b66763ce27ab73fefbc41f641978b25045 Author: vaydheesh Date: Thu Jul 4 18:05:28 2019 +0530 Wrapped: interchunk, postchunk, transfer diff --git a/.gitignore b/.gitignore index 6855cf4..c056295 100644 --- a/.gitignore +++ b/.gitignore @@ -109,3 +109,12 @@ test-driver /tests/data/nno-nob.t1x.bin /tests/test-suite.log /compile_commands.json + +/python/Makefile.in +/python/Makefile +/python/apertium_core.py +/python/apertium_core_wrap.cpp +/python/setup.py +/python/build* +*.egg-info/ +*.egg diff --git a/Makefile.am b/Makefile.am index bae0ea7..5c6ff78 100644 --- a/Makefile.am +++ b/Makefile.am @@ -11,6 +11,10 @@ pkgconfig_DATA = apertium.pc aclocaldir = $(datadir)/aclocal aclocal_DATA = apertium.m4 +if HAVE_PYTHON_BINDINGS +SUBDIRS += python +endif + EXTRA_DIST=autogen.sh README-MODES apertium.m4 utf8 tests install-data-local: diff --git a/configure.ac b/configure.ac index c326415..46afc87 100644 --- a/configure.ac +++ b/configure.ac @@ -191,4 +191,18 @@ AX_CHECK_COMPILE_FLAG([-std=c++20], [CXXFLAGS="$CXXFLAGS -std=c++20"], [ ]) ]) -AC_OUTPUT([Makefile apertium.pc apertium/Makefile tests/Makefile tests/tagger/Makefile]) +AC_CONFIG_FILES([python/setup.py]) + +AC_SUBST(APERTIUM_CFLAGS) +AC_SUBST(LIBS) + +AM_PATH_PYTHON([3.4], [], [AC_MSG_WARN([Can't generate SWIG wrapper or run tests without Python])]) + +AC_ARG_ENABLE([python-bindings], + AS_HELP_STRING([--enable-python-bindings], + [build python bindings (default=disabled)]), + [enable_python_bindings=$enableval], + [enable_python_bindings=no]) +AM_CONDITIONAL([HAVE_PYTHON_BINDINGS], [test x$enable_python_bindings = xyes]) + +AC_OUTPUT([Makefile apertium.pc apertium/Makefile tests/Makefile tests/tagger/Makefile python/Makefile]) diff --git a/python/Makefile.am b/python/Makefile.am new file mode 100644 index 0000000..8aa782b --- /dev/null +++ b/python/Makefile.am @@ -0,0 +1,9 @@ +SWIG_INTERFACE = apertium_core.i + +BUILT_SOURCES = %_wrap.cpp + +%_wrap.cpp: $(SWIG_INTERFACE) setup.py + $(PYTHON) setup.py build + +install-exec-local: + $(PYTHON) setup.py install --prefix=$(DESTDIR)$(prefix) diff --git a/python/apertium_core.i b/python/apertium_core.i new file mode 100644 index 0000000..1a702ea --- /dev/null +++ b/python/apertium_core.i @@ -0,0 +1,77 @@ +%module apertium_core + +%{ +#define SWIG_FILE_WITH_INIT +#include +#include +#include + +class apertium: public Transfer, public Interchunk, public Postchunk +{ +public: + /** + * Imitates functionality of apertium-core binaries using file path + */ + void interchunk_text(char arg, char *transferfile, char *datafile, char *input_path, char *output_path); + void postchunk_text(char arg, char *transferfile, char *datafile, char *input_path, char *output_path); + void transfer_text(char arg, char *transferfile, char *datafile, char *input_path, char *output_path); +}; + +void +apertium::transfer_text(char arg, char *transferfile, char *datafile, char *input_path, char *output_path) +{ + FILE *input = fopen(input_path, "r"), *output = fopen(output_path, "w"); + + switch(arg) + { + case 'b': + setPreBilingual(true); + setUseBilingual(false); + break; + + case 'n': + setUseBilingual(false); + break; + } + Transfer::read(transferfile, datafile); + transfer(input, output); + fclose(input); + fclose(output); +} + +void +apertium::interchunk_text(char arg, char *transferfile, char *datafile, char *input_path, char *output_path) +{ + FILE *input = fopen(input_path, "r"), *output = fopen(output_path, "w"); + Interchunk::read(transferfile, datafile); + interchunk(input, output); + fclose(input); + fclose(output); +} + +void +apertium::postchunk_text(char arg, char *transferfile, char *datafile, char *input_path, char *output_path) +{ + FILE *input = fopen(input_path, "r"), *output = fopen(output_path, "w"); + Postchunk::read(transferfile, datafile); + postchunk(input, output); + fclose(input); + fclose(output); +} + +%} + +%include +%include +%include + +class apertium: public Transfer, public Interchunk, public Postchunk +{ +public: + /** + * Imitates functionality of apertium-core binaries using file path + */ + void interchunk_text(char arg, char *transferfile, char *datafile, char *input_path, char *output_path); + void postchunk_text(char arg, char *transferfile, char *datafile, char *input_path, char *output_path); + void transfer_text(char arg, char *transferfile, char *datafile, char *input_path, char *output_path); +}; diff --git a/python/setup.py.in b/python/setup.py.in new file mode 100644 index 0000000..000973d --- /dev/null +++ b/python/setup.py.in @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 + +''' +Setup for SWIG Python bindings for apertium +''' +from os import path +from distutils.core import Extension, setup +from distutils.command.build import build + + +class CustomBuild(build): + sub_commands = [ + ('build_ext', build.has_ext_modules), + ('build_py', build.has_pure_modules), + ('build_clib', build.has_c_libraries), + ('build_scripts', build.has_scripts), + ] + + +def get_sources(): + sources = ['apertium_core.i'] + cc_sources = ['postchunk.cc', + 'interchunk.cc', 'interchunk_word.cc', + 'apertium_re.cc', 'string_utils.cc', 'trx_reader.cc', 'utf_converter.cc', + 'transfer.cc', 'transfer_mult.cc', 'transfer_word.cc', 'transfer_data.cc', + 'transfer_word_list.cc', 'transfer_instr.cc', 'transfer_token.cc', 'xml_reader.cc'] + rel_path = '@top_srcdir@/apertium' + sources.extend(path.join(rel_path, f) for f in cc_sources) + return sources + +def get_include_dirs(): + # Remove '-I' from Flags, as python add '-I' on its own + dirs = '@APERTIUM_CFLAGS@'.replace('-I', '').split() + return dirs + ['..'] + + +apertium_core_module = Extension( + name='_apertium_core', + sources=get_sources(), + swig_opts=['-c++', '-I..', '-I@top_srcdir@/apertium', '-Wall']+'@APERTIUM_CFLAGS@'.split(), + include_dirs=get_include_dirs(), + library_dirs=['/usr/include/libxml2', '/usr/local/lib'], + extra_compile_args='@CPPFLAGS@'.split(), + extra_link_args='@LIBS@'.split(), +) + +setup( + name='@PACKAGE@', + version='@PACKAGE_VERSION@', + description='SWIG interface to @PACKAGE_NAME@', + long_description='SWIG interface to @PACKAGE_NAME@ for use in apertium-python', + # TODO: author, maintainer, url + author_email='@PACKAGE_BUGREPORT@', + license='GPL-3.0+', + maintainer_email='@PACKAGE_BUGREPORT@', + cmdclass={'build': CustomBuild}, + ext_modules=[apertium_core_module], + py_modules=['apertium_core'], +)