Skip to content

Commit

Permalink
0. Init
Browse files Browse the repository at this point in the history
  • Loading branch information
singh-lokendra committed Jun 2, 2019
1 parent 510acea commit 3987d79
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
29 changes: 29 additions & 0 deletions 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)"

58 changes: 58 additions & 0 deletions python/analysis.i
@@ -0,0 +1,58 @@
%module analysis

%{
#define SWIG_FILE_WITH_INIT
#include <lttoolbox/fst_processor.h>
#include <lttoolbox/lttoolbox_config.h>
#include <lttoolbox/my_stdio.h>
#include <lttoolbox/lt_locale.h>

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 <lttoolbox/fst_processor.h>
#include <lttoolbox/lttoolbox_config.h>
#include <lttoolbox/my_stdio.h>
#include <lttoolbox/lt_locale.h>

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;
};
2 changes: 2 additions & 0 deletions python/notes.md
@@ -0,0 +1,2 @@
- add python & swig checkers in config.ac from libdivun
-
21 changes: 21 additions & 0 deletions 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<n><pl>")

0 comments on commit 3987d79

Please sign in to comment.