commit 3987d7956eae237f2f813b4ee17e4df2786566b3 Author: vaydheesh Date: Sun Jun 2 15:26:13 2019 +0000 0. Init diff --git a/python/Makefile.am b/python/Makefile.am new file mode 100644 index 0000000..db4026b --- /dev/null +++ b/python/Makefile.am @@ -0,0 +1,29 @@ +cc_sources = alphabet.cc att_compiler.cc compiler.cc compression.cc entry_token.cc \ + expander.cc fst_processor.cc lt_locale.cc match_exe.cc \ + match_node.cc match_state.cc node.cc pattern_list.cc \ + regexp_compiler.cc sorted_vector.cc state.cc transducer.cc \ + trans_exe.cc xml_parse_util.cc tmx_compiler.cc + +# SWIG_INTERFACE = analysis.i + +EXTRA_DIST = analysis.i + +pkgdata_DATA = analysis.py + +lib_LTLIBRARIES = libAnalysisPython.la +libAnalysisPython_la_SOURCES = analysis_wrap_python.cpp +libAnalysisPython_la_SOURCES = analysis_wrap_python.cpp +foo_wrap_python.cpp: analysis.i + $(SWIG) -python $(SWIG_ARGS) -o $@ $< + + +BUILT_SOURCES = analysis_wrap.cpp analysis.py +CLEANFILES = $(BUILT_SOURCES) + +library_includedir = $(includedir)/$(GENERIC_LIBRARY_NAME)-$(GENERIC_API_VERSION)/$(GENERIC_LIBRARY_NAME) +library_include_HEADERS = $(h_sources) + +%_wrap.cpp %.py: %.i ../src/libdivvun.la + $(SWIG) -c++ -python -I$(top_srcdir)/src/lib -o $*_wrap.cpp -outdir . $(srcdir)/$*.i + CPPFLAGS="$(CPPFLAGS)" CXXFLAGS="$(CXXFLAGS)" LDFLAGS="-Wl,-rpath,${prefix}/lib $(LDFLAGS)" + diff --git a/python/analysis.i b/python/analysis.i new file mode 100644 index 0000000..d295346 --- /dev/null +++ b/python/analysis.i @@ -0,0 +1,58 @@ +%module analysis + +%{ +#define SWIG_FILE_WITH_INIT +#include +#include +#include +#include + +class FST: private FSTProcessor +{ +public: + /** + * Reads from input_path and stores result at output_path + */ + void init_analysis(char *automorf_path, char *input_path, char *output_path); + bool validity() const; + +}; + +bool +FST::validity() const +{ + return valid(); +} + +void +FST::init_analysis(char *automorf_path, char *input_path, char *output_path) +{ + setDictionaryCaseMode(true); + LtLocale::tryToSetLocale(); + FILE *in = fopen(automorf_path, "rb"); + load(in); + initAnalysis(); + FILE *input = fopen(input_path, "r"), *output = fopen(output_path, "w"); + analysis(input, output); + fclose(in); + fclose(input); + fclose(output); +} + +%} + + +#include +#include +#include +#include + +class FST: private FSTProcessor +{ +public: + /** + * Reads from input_path and stores result at output_path + */ + void init_analysis(char *automorf_path, char *input_path, char *output_path); + bool validity() const; +}; diff --git a/python/notes.md b/python/notes.md new file mode 100644 index 0000000..f4c6cbb --- /dev/null +++ b/python/notes.md @@ -0,0 +1,2 @@ +- add python & swig checkers in config.ac from libdivun +- diff --git a/python/tests/__init__.py b/python/tests/__init__.py new file mode 100644 index 0000000..9f54269 --- /dev/null +++ b/python/tests/__init__.py @@ -0,0 +1,21 @@ +import os +import sys +import tempfile +import unittest + +base_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..') +sys.path.append(base_path) + +import analysis + +class TestAnalysisunittest.TestCase): + def test_FST(self): + with tempfile.NamedTemporaryFile('w') as input_file, tempfile.NamedTemporaryFile('r') as output_file: + input_text = 'cats\n' + automorf_path = "/usr/share/apertium/apertium-eng/eng.automorf.bin" + input_file.write(input_text) + input_file.flush() + x = analysis.FST() + x.init_analysis(automorf_path, input_file.name, output_file.name) + output = output_file.read() + self.assertEqual(output, "cats") \ No newline at end of file