Commit 231fcc84d4f328954628c98bd39faeb99818d2b0

Authored by Michał Lenart
1 parent 27d0fb3c

- praca nad porządną wersją wrappera pythonowego

git-svn-id: svn://svn.nlp.ipipan.waw.pl/morfeusz/trunk@249 ff4e3ee1-f430-4e82-ade0-24591c43f1fd
CMakeLists.txt
... ... @@ -155,7 +155,7 @@ endif ()
155 155 include (CPack)
156 156  
157 157 ##### END initialize some vars #####
158   -file (COPY fsabuilder doTest.sh testJavaWrapper.sh DESTINATION .)
  158 +file (COPY fsabuilder doTest.sh testJavaWrapper.sh testPythonWrapper.sh morfeusz/wrappers/python/test.py DESTINATION .)
159 159  
160 160 #~ configure_file (
161 161 #~ "${PROJECT_SOURCE_DIR}/morfeusz/morfeusz2_version.h.in"
... ... @@ -184,4 +184,5 @@ endif()
184 184  
185 185 if (UNIX)
186 186 add_test(TestJavaWrapper ./testJavaWrapper.sh "${PROJECT_BINARY_DIR}")
  187 + add_test(TestPythonWrapper ./testPythonWrapper.sh "${PROJECT_BINARY_DIR}")
187 188 endif ()
... ...
morfeusz/wrappers/morfeusz_common.i
... ... @@ -16,8 +16,8 @@
16 16  
17 17 namespace morfeusz {
18 18  
19   - %ignore MorphInterpretation::createIgn(int startNode, int endNode, const std::string& orth, const std::string& lemma);
20   - %ignore MorphInterpretation::createWhitespace(int startNode, int endNode, const std::string& orth);
  19 +// %ignore MorphInterpretation::createIgn(int startNode, int endNode, const std::string& orth, const std::string& lemma);
  20 +// %ignore MorphInterpretation::createWhitespace(int startNode, int endNode, const std::string& orth);
21 21 %ignore Morfeusz::analyse(const char*) const;
22 22 %ignore Morfeusz::analyse(const std::string&) const;
23 23 %ignore Morfeusz::setCharset(Charset);
... ...
morfeusz/wrappers/morfeusz_python.i
1 1  
2   -%pythoncode %{
  2 +//%rename (_Morfeusz) morfeusz::Morfeusz;
  3 +//%rename (_ResultsIterator) morfeusz::ResultsIterator;
3 4  
4   -def _analyse(self, text):
5   - res = InterpsVector()
6   - _morfeusz2.Morfeusz_analyse(self, text.encode('utf8'), res)
7   - return list(res)
  5 +%rename (_generateByTagId) morfeusz::Morfeusz::generate(const std::string&, int, std::vector<std::string>&);
8 6  
9   -Morfeusz.analyse = _analyse
  7 +%extend morfeusz::ResultsIterator {
  8 + morfeusz::ResultsIterator& morfeusz::ResultsIterator::__iter__() {
  9 + return *($self);
  10 + }
  11 +};
10 12  
11   -def _generate(self, text):
12   - res = InterpsVector()
13   - _morfeusz2.Morfeusz_generate(self, text.encode('utf8'), res)
14   - return list(res)
  13 +%extend morfeusz::Morfeusz {
  14 + std::vector<morfeusz::MorphInterpretation> morfeusz::Morfeusz::_generateByTagId(const std::string& lemma, int tagId) const {
  15 + std::vector<morfeusz::MorphInterpretation> res;
  16 + $self->generate(lemma, tagId, res);
  17 + return res;
  18 + }
  19 +};
15 20  
16   -Morfeusz.generate = _generate
  21 +%feature("shadow") morfeusz::ResultsIterator::next %{
  22 +def next(self):
  23 + if self.hasNext():
  24 + return $action(self)
  25 + else:
  26 + raise StopIteration
  27 +%}
  28 +
  29 +%feature("shadow") morfeusz::Morfeusz::analyseAsIterator(const char*) %{
  30 +def analyse_iter(self, text):
  31 + return $action(self, text)
  32 +%}
17 33  
18   -def _getOrth(self):
19   - return _morfeusz2.MorphInterpretation_getOrth(self).decode('utf8')
  34 +%feature("shadow") morfeusz::Morfeusz::analyse %{
  35 +def analyse(self, text):
  36 + res = InterpsList()
  37 + $action(self, text, res)
  38 + return res
  39 +%}
20 40  
21   -def _getLemma(self):
22   - return _morfeusz2.MorphInterpretation_getLemma(self).decode('utf8')
  41 +%feature("shadow") morfeusz::Morfeusz::_generateByTagId %{
  42 +def _generateByTagId(self, lemma, tagId):
  43 + res = InterpsList()
  44 + $action(self, lemma, tagId, res)
  45 + return res
  46 +%}
23 47  
24   -def _getTag(self):
25   - return _morfeusz2.MorphInterpretation_getTag(self).decode('utf8')
  48 +%feature("shadow") morfeusz::Morfeusz::generate %{
  49 +def generate(self, lemma, tagId=None):
  50 + if tagId is not None:
  51 + return self._generateByTagId(lemma, tagId)
  52 + else:
  53 + res = InterpsList()
  54 + $action(self, lemma, res)
  55 + return res
  56 +%}
26 57  
27   -def _getName(self):
28   - return _morfeusz2.MorphInterpretation_getName(self).decode('utf8')
  58 +%rename (_orth) morfeusz::MorphInterpretation::orth;
  59 +
  60 +%extend morfeusz::MorphInterpretation {
  61 + %pythoncode %{
  62 + @property
  63 + def orth(self):
  64 + return self._orth.decode('utf8')
  65 +
  66 + @orth.setter
  67 + def orth(self, val):
  68 + self._orth = val.encode('utf8')
  69 + %}
  70 +};
  71 +
  72 +%rename (_lemma) morfeusz::MorphInterpretation::lemma;
  73 +
  74 +%extend morfeusz::MorphInterpretation {
  75 + %pythoncode %{
  76 + @property
  77 + def lemma(self):
  78 + return self._lemma.decode('utf8')
  79 +
  80 + @lemma.setter
  81 + def lemma(self, val):
  82 + self._lemma = val.encode('utf8')
  83 + %}
  84 +};
  85 +
  86 +%feature("shadow") morfeusz::IdResolver::getTag %{
  87 +def getTag(self, tagId):
  88 + return $action(self, tagId).decode('utf8')
  89 +%}
  90 +
  91 +%feature("shadow") morfeusz::IdResolver::getTagId %{
  92 +def getTagId(self, tag):
  93 + return $action(self, tag.encode('utf8'))
  94 +%}
  95 +
  96 +%feature("shadow") morfeusz::IdResolver::getName %{
  97 +def getName(self, nameId):
  98 + return $action(self, nameId).decode('utf8')
  99 +%}
  100 +
  101 +%feature("shadow") morfeusz::IdResolver::getNameId %{
  102 +def getNameId(self, name):
  103 + return $action(self, name.encode('utf8'))
  104 +%}
  105 +
  106 +%feature("shadow") morfeusz::IdResolver::getLabelsAsString %{
  107 +def getLabelsAsUnicode(self, labelsId):
  108 + return $action(self, labelsId).decode('utf8')
  109 +%}
  110 +
  111 +%feature("shadow") morfeusz::IdResolver::getLabels %{
  112 +def getLabels(self, labelsId):
  113 + return { l.decode('utf8') for l in $action(self, labelsId) }
  114 +%}
29 115  
30   -MorphInterpretation.getOrth = _getOrth
31   -MorphInterpretation.getLemma = _getLemma
32   -MorphInterpretation.getTag = _getTag
33   -MorphInterpretation.getName = _getName
  116 +%feature("shadow") morfeusz::IdResolver::getLabelsId %{
  117 +def getLabelsId(self, labelsStr):
  118 + return $action(self, labelsStr.encode('utf8'))
34 119 %}
35 120  
36 121 %include "std_vector.i"
... ...
morfeusz/wrappers/python/CMakeLists.txt
... ... @@ -19,7 +19,7 @@ add_custom_command (
19 19 COMMAND swig -python -c++ -o "${SWIG_PYTHON_OUTFILE_CXX}" "${CMAKE_SOURCE_DIR}/morfeusz/wrappers/morfeusz.i"
20 20 DEPENDS libmorfeusz
21 21 )
22   -add_custom_target (generate_python_wrapper
  22 +add_custom_target (generate_python_wrapper ALL
23 23 DEPENDS "${SWIG_PYTHON_OUTFILE_CXX}" "${SWIG_PYTHON_OUTFILE_PY}")
24 24  
25 25 set (SETUP_PY_IN "${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in")
... ... @@ -38,7 +38,7 @@ add_custom_command (OUTPUT ${OUTPUT}
38 38 ARGS ${SETUP_PY} build
39 39 DEPENDS generate_python_wrapper generate_python_setup_file libmorfeusz)
40 40  
41   -add_custom_target (build_python_wrapper
  41 +add_custom_target (build_python_wrapper ALL
42 42 DEPENDS ${OUTPUT})
43 43  
44 44 add_custom_target (pymorfeusz
... ...
morfeusz/wrappers/python/test.py 0 → 100644
  1 +
  2 +import morfeusz2
  3 +
  4 +m = morfeusz2.Morfeusz.createInstance()
  5 +for mi in m.analyse('Ala ma kota.'):
  6 + print mi.startNode, mi.endNode, mi.orth, mi.lemma, m.getIdResolver().getTag(mi.tagId), m.getIdResolver().getName(mi.nameId), m.getIdResolver().getLabels(mi.labelsId)
... ...
testPythonWrapper.sh 0 → 100755
  1 +#!/bin/bash
  2 +echo "testing python wrapper"
  3 +PROJECT_BINARY_DIR=$1
  4 +export LD_LIBRARY_PATH="$PROJECT_BINARY_DIR/morfeusz:$PROJECT_BINARY_DIR/morfeusz/wrappers/python/build/lib.linux-x86_64-2.7"
  5 +export PYTHONPATH="$PROJECT_BINARY_DIR/morfeusz/wrappers/python/build/lib.linux-x86_64-2.7"
  6 +python test.py
... ...