diff options
author | Wilker Aziz <wilker.aziz@gmail.com> | 2012-10-03 22:04:48 +0400 |
---|---|---|
committer | Wilker Aziz <wilker.aziz@gmail.com> | 2012-10-03 22:04:48 +0400 |
commit | 7f5b6b231000f52cf9280ee92262933ce270e4ff (patch) | |
tree | 09afa3156fc0e226642af665d08c7bf01537a822 /contrib | |
parent | 69a0fcde29abb7871706cbf2ce1d7f065401cfb1 (diff) |
better doc
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/python/README.md | 10 | ||||
-rw-r--r-- | contrib/python/binpt/binpt.cpp | 58 | ||||
-rw-r--r-- | contrib/python/binpt/binpt.pyx | 8 | ||||
-rw-r--r-- | contrib/python/setup.py | 21 |
4 files changed, 55 insertions, 42 deletions
diff --git a/contrib/python/README.md b/contrib/python/README.md index fa7d270c8..d9ef1cd27 100644 --- a/contrib/python/README.md +++ b/contrib/python/README.md @@ -10,9 +10,15 @@ The idea is to have some of Moses' internals exposed to Python (inspired on pycd ## Building -1. Build the python extension +1. Build the python extension: - python setup.py build_ext -i [--with-cmph] + You need to compile Moses with link=shared and (for while) without SRILM (for some reason SRILM prevents the compiler from generating libLM.so) + + ./bjam --libdir=path cxxflags=-fPIC link=shared + + Then you can build the extension (in case you used --libdir=path above, use --moses-lib=path below) + + python setup.py build_ext -i [--with-cmph] [--moses-lib=path] 3. Check the example code diff --git a/contrib/python/binpt/binpt.cpp b/contrib/python/binpt/binpt.cpp index 7de3058fc..0abf86ab1 100644 --- a/contrib/python/binpt/binpt.cpp +++ b/contrib/python/binpt/binpt.cpp @@ -1,4 +1,4 @@ -/* Generated by Cython 0.16 on Tue Sep 18 11:36:58 2012 */ +/* Generated by Cython 0.16 on Fri Sep 21 10:28:51 2012 */ #define PY_SSIZE_T_CLEAN #include "Python.h" @@ -692,7 +692,7 @@ static PyObject *__pyx_pf_5binpt_11QueryResult_2words(struct __pyx_obj_5binpt_Qu static PyObject *__pyx_pf_5binpt_11QueryResult_4scores(struct __pyx_obj_5binpt_QueryResult *__pyx_v_self); /* proto */ static PyObject *__pyx_pf_5binpt_11QueryResult_6wa(struct __pyx_obj_5binpt_QueryResult *__pyx_v_self); /* proto */ static PyObject *__pyx_lambda_funcdef_lambda1(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_r); /* proto */ -static PyObject *__pyx_pf_5binpt_11QueryResult_8desc(PyObject *__pyx_v_x, PyObject *__pyx_v_y, PyObject *__pyx_v_keys); /* proto */ +static PyObject *__pyx_pf_5binpt_11QueryResult_8desc(PyObject *__pyx_v_x, PyObject *__pyx_v_y, PyObject *__pyx_v_key); /* proto */ static PyObject *__pyx_pf_5binpt_11QueryResult_10__str__(struct __pyx_obj_5binpt_QueryResult *__pyx_v_self); /* proto */ static PyObject *__pyx_pf_5binpt_11QueryResult_12__repr__(struct __pyx_obj_5binpt_QueryResult *__pyx_v_self); /* proto */ static int __pyx_pf_5binpt_17BinaryPhraseTable___cinit__(struct __pyx_obj_5binpt_BinaryPhraseTable *__pyx_v_self, PyObject *__pyx_v_path, unsigned int __pyx_v_nscores, int __pyx_v_wa, PyObject *__pyx_v_delimiters); /* proto */ @@ -722,10 +722,10 @@ static char __pyx_k__y[] = "y"; static char __pyx_k__os[] = "os"; static char __pyx_k__wa[] = "wa"; static char __pyx_k__cmp[] = "cmp"; +static char __pyx_k__key[] = "key"; static char __pyx_k__top[] = "top"; static char __pyx_k__desc[] = "desc"; static char __pyx_k__join[] = "join"; -static char __pyx_k__keys[] = "keys"; static char __pyx_k__line[] = "line"; static char __pyx_k__path[] = "path"; static char __pyx_k__sort[] = "sort"; @@ -771,7 +771,7 @@ static PyObject *__pyx_n_s__encode; static PyObject *__pyx_n_s__isValidBinaryTable; static PyObject *__pyx_n_s__isfile; static PyObject *__pyx_n_s__join; -static PyObject *__pyx_n_s__keys; +static PyObject *__pyx_n_s__key; static PyObject *__pyx_n_s__line; static PyObject *__pyx_n_s__nscores; static PyObject *__pyx_n_s__os; @@ -1276,13 +1276,13 @@ static PyObject *__pyx_pf_5binpt_11QueryResult_6wa(struct __pyx_obj_5binpt_Query /* Python wrapper */ static PyObject *__pyx_pw_5binpt_11QueryResult_9desc(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ -static char __pyx_doc_5binpt_11QueryResult_8desc[] = "Returns the sign of keys(y) - keys(x).\n Can only be used if scores is not an empty vector as\n keys defaults to scores[0]"; +static char __pyx_doc_5binpt_11QueryResult_8desc[] = "Returns the sign of key(y) - key(x).\n Can only be used if scores is not an empty vector as\n keys defaults to scores[0]"; static PyMethodDef __pyx_mdef_5binpt_11QueryResult_9desc = {__Pyx_NAMESTR("desc"), (PyCFunction)__pyx_pw_5binpt_11QueryResult_9desc, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5binpt_11QueryResult_8desc)}; static PyObject *__pyx_pw_5binpt_11QueryResult_9desc(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { PyObject *__pyx_v_x = 0; PyObject *__pyx_v_y = 0; - PyObject *__pyx_v_keys = 0; - static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__x,&__pyx_n_s__y,&__pyx_n_s__keys,0}; + PyObject *__pyx_v_key = 0; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__x,&__pyx_n_s__y,&__pyx_n_s__key,0}; PyObject *__pyx_r = 0; __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("desc (wrapper)", 0); @@ -1313,7 +1313,7 @@ static PyObject *__pyx_pw_5binpt_11QueryResult_9desc(PyObject *__pyx_self, PyObj } case 2: if (kw_args > 0) { - PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s__keys); + PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s__key); if (value) { values[2] = value; kw_args--; } } } @@ -1331,7 +1331,7 @@ static PyObject *__pyx_pw_5binpt_11QueryResult_9desc(PyObject *__pyx_self, PyObj } __pyx_v_x = values[0]; __pyx_v_y = values[1]; - __pyx_v_keys = values[2]; + __pyx_v_key = values[2]; } goto __pyx_L4_argument_unpacking_done; __pyx_L5_argtuple_error:; @@ -1341,7 +1341,7 @@ static PyObject *__pyx_pw_5binpt_11QueryResult_9desc(PyObject *__pyx_self, PyObj __Pyx_RefNannyFinishContext(); return NULL; __pyx_L4_argument_unpacking_done:; - __pyx_r = __pyx_pf_5binpt_11QueryResult_8desc(__pyx_v_x, __pyx_v_y, __pyx_v_keys); + __pyx_r = __pyx_pf_5binpt_11QueryResult_8desc(__pyx_v_x, __pyx_v_y, __pyx_v_key); __Pyx_RefNannyFinishContext(); return __pyx_r; } @@ -1362,8 +1362,8 @@ static PyObject *__pyx_pw_5binpt_11QueryResult_4desc_lambda1(PyObject *__pyx_sel /* "binpt.pyx":52 * * @staticmethod - * def desc(x, y, keys = lambda r: r.scores[0]): # <<<<<<<<<<<<<< - * '''Returns the sign of keys(y) - keys(x). + * def desc(x, y, key = lambda r: r.scores[0]): # <<<<<<<<<<<<<< + * '''Returns the sign of key(y) - key(x). * Can only be used if scores is not an empty vector as */ @@ -1399,7 +1399,7 @@ static PyObject *__pyx_lambda_funcdef_lambda1(CYTHON_UNUSED PyObject *__pyx_self return __pyx_r; } -static PyObject *__pyx_pf_5binpt_11QueryResult_8desc(PyObject *__pyx_v_x, PyObject *__pyx_v_y, PyObject *__pyx_v_keys) { +static PyObject *__pyx_pf_5binpt_11QueryResult_8desc(PyObject *__pyx_v_x, PyObject *__pyx_v_y, PyObject *__pyx_v_key) { PyObject *__pyx_r = NULL; __Pyx_RefNannyDeclarations PyObject *__pyx_t_1 = NULL; @@ -1414,7 +1414,7 @@ static PyObject *__pyx_pf_5binpt_11QueryResult_8desc(PyObject *__pyx_v_x, PyObje /* "binpt.pyx":56 * Can only be used if scores is not an empty vector as * keys defaults to scores[0]''' - * return fsign(keys(y) - keys(x)) # <<<<<<<<<<<<<< + * return fsign(key(y) - key(x)) # <<<<<<<<<<<<<< * * def __str__(self): */ @@ -1424,7 +1424,7 @@ static PyObject *__pyx_pf_5binpt_11QueryResult_8desc(PyObject *__pyx_v_x, PyObje __Pyx_INCREF(__pyx_v_y); PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_y); __Pyx_GIVEREF(__pyx_v_y); - __pyx_t_2 = PyObject_Call(__pyx_v_keys, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_2 = PyObject_Call(__pyx_v_key, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_2); __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;} @@ -1432,7 +1432,7 @@ static PyObject *__pyx_pf_5binpt_11QueryResult_8desc(PyObject *__pyx_v_x, PyObje __Pyx_INCREF(__pyx_v_x); PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_x); __Pyx_GIVEREF(__pyx_v_x); - __pyx_t_3 = PyObject_Call(__pyx_v_keys, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_3 = PyObject_Call(__pyx_v_key, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_3); __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; __pyx_t_1 = PyNumber_Subtract(__pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;} @@ -1475,7 +1475,7 @@ static PyObject *__pyx_pw_5binpt_11QueryResult_11__str__(PyObject *__pyx_v_self) } /* "binpt.pyx":58 - * return fsign(keys(y) - keys(x)) + * return fsign(key(y) - key(x)) * * def __str__(self): # <<<<<<<<<<<<<< * '''Returns a string such as: <words> ||| <scores> [||| word-alignment info]''' @@ -3724,7 +3724,7 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = { {&__pyx_n_s__isValidBinaryTable, __pyx_k__isValidBinaryTable, sizeof(__pyx_k__isValidBinaryTable), 0, 0, 1, 1}, {&__pyx_n_s__isfile, __pyx_k__isfile, sizeof(__pyx_k__isfile), 0, 0, 1, 1}, {&__pyx_n_s__join, __pyx_k__join, sizeof(__pyx_k__join), 0, 0, 1, 1}, - {&__pyx_n_s__keys, __pyx_k__keys, sizeof(__pyx_k__keys), 0, 0, 1, 1}, + {&__pyx_n_s__key, __pyx_k__key, sizeof(__pyx_k__key), 0, 0, 1, 1}, {&__pyx_n_s__line, __pyx_k__line, sizeof(__pyx_k__line), 0, 0, 1, 1}, {&__pyx_n_s__nscores, __pyx_k__nscores, sizeof(__pyx_k__nscores), 0, 0, 1, 1}, {&__pyx_n_s__os, __pyx_k__os, sizeof(__pyx_k__os), 0, 0, 1, 1}, @@ -3774,8 +3774,8 @@ static int __Pyx_InitCachedConstants(void) { /* "binpt.pyx":52 * * @staticmethod - * def desc(x, y, keys = lambda r: r.scores[0]): # <<<<<<<<<<<<<< - * '''Returns the sign of keys(y) - keys(x). + * def desc(x, y, key = lambda r: r.scores[0]): # <<<<<<<<<<<<<< + * '''Returns the sign of key(y) - key(x). * Can only be used if scores is not an empty vector as */ __pyx_k_tuple_16 = PyTuple_New(3); if (unlikely(!__pyx_k_tuple_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; __pyx_clineno = __LINE__; goto __pyx_L1_error;} @@ -3786,9 +3786,9 @@ static int __Pyx_InitCachedConstants(void) { __Pyx_INCREF(((PyObject *)__pyx_n_s__y)); PyTuple_SET_ITEM(__pyx_k_tuple_16, 1, ((PyObject *)__pyx_n_s__y)); __Pyx_GIVEREF(((PyObject *)__pyx_n_s__y)); - __Pyx_INCREF(((PyObject *)__pyx_n_s__keys)); - PyTuple_SET_ITEM(__pyx_k_tuple_16, 2, ((PyObject *)__pyx_n_s__keys)); - __Pyx_GIVEREF(((PyObject *)__pyx_n_s__keys)); + __Pyx_INCREF(((PyObject *)__pyx_n_s__key)); + PyTuple_SET_ITEM(__pyx_k_tuple_16, 2, ((PyObject *)__pyx_n_s__key)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__key)); __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_16)); __pyx_k_codeobj_17 = (PyObject*)__Pyx_PyCode_New(3, 0, 3, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_16, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_18, __pyx_n_s__desc, 52, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; __pyx_clineno = __LINE__; goto __pyx_L1_error;} @@ -3987,8 +3987,8 @@ PyMODINIT_FUNC PyInit_binpt(void) /* "binpt.pyx":52 * * @staticmethod - * def desc(x, y, keys = lambda r: r.scores[0]): # <<<<<<<<<<<<<< - * '''Returns the sign of keys(y) - keys(x). + * def desc(x, y, key = lambda r: r.scores[0]): # <<<<<<<<<<<<<< + * '''Returns the sign of key(y) - key(x). * Can only be used if scores is not an empty vector as */ __pyx_t_1 = __Pyx_CyFunction_NewEx(&__pyx_mdef_5binpt_11QueryResult_4desc_lambda1, 0, NULL, __pyx_n_s__binpt, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; __pyx_clineno = __LINE__; goto __pyx_L1_error;} @@ -4001,8 +4001,8 @@ PyMODINIT_FUNC PyInit_binpt(void) * return self._wa * * @staticmethod # <<<<<<<<<<<<<< - * def desc(x, y, keys = lambda r: r.scores[0]): - * '''Returns the sign of keys(y) - keys(x). + * def desc(x, y, key = lambda r: r.scores[0]): + * '''Returns the sign of key(y) - key(x). */ __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_5binpt_11QueryResult_9desc, NULL, __pyx_n_s__binpt); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_1); @@ -4021,8 +4021,8 @@ PyMODINIT_FUNC PyInit_binpt(void) /* "binpt.pyx":52 * * @staticmethod - * def desc(x, y, keys = lambda r: r.scores[0]): # <<<<<<<<<<<<<< - * '''Returns the sign of keys(y) - keys(x). + * def desc(x, y, key = lambda r: r.scores[0]): # <<<<<<<<<<<<<< + * '''Returns the sign of key(y) - key(x). * Can only be used if scores is not an empty vector as */ __pyx_t_1 = __Pyx_GetName((PyObject *)__pyx_ptype_5binpt_QueryResult, __pyx_n_s__desc); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; __pyx_clineno = __LINE__; goto __pyx_L1_error;} diff --git a/contrib/python/binpt/binpt.pyx b/contrib/python/binpt/binpt.pyx index e66981df6..137b0bc8c 100644 --- a/contrib/python/binpt/binpt.pyx +++ b/contrib/python/binpt/binpt.pyx @@ -49,11 +49,11 @@ cdef class QueryResult(object): return self._wa @staticmethod - def desc(x, y, keys = lambda r: r.scores[0]): - '''Returns the sign of keys(y) - keys(x). + def desc(x, y, key = lambda r: r.scores[0]): + '''Returns the sign of key(y) - key(x). Can only be used if scores is not an empty vector as keys defaults to scores[0]''' - return fsign(keys(y) - keys(x)) + return fsign(key(y) - key(x)) def __str__(self): '''Returns a string such as: <words> ||| <scores> [||| word-alignment info]''' @@ -138,7 +138,7 @@ cdef class BinaryPhraseTable(object): def delimiters(self): return self._delimiters - def query(self, line, cmp = None, top = 0): + def query(self, line, cmp = None, key = lambda x: x.scores[0], top = 0): '''Queries the phrase table and returns a list of matches. Each match is a QueryResult. If 'cmp' is defined the return list is sorted. diff --git a/contrib/python/setup.py b/contrib/python/setup.py index 66042fbc8..bc501b360 100644 --- a/contrib/python/setup.py +++ b/contrib/python/setup.py @@ -3,31 +3,38 @@ from distutils.extension import Extension import os import sys -available_switches = ['--with-cmph'] +available_switches = ['--with-cmph', '--moses-lib'] with_cmph = False -while sys.argv[-1] in available_switches: - switch = sys.argv.pop() - if switch == '--with-cmph': +mosesdir = os.path.abspath('../../') +includes = [mosesdir, os.path.join(mosesdir, 'moses/src'), os.path.join(mosesdir, 'util')] +libdir = os.path.join(mosesdir, 'lib') + +while sys.argv[-1].split('=')[0] in available_switches: + param = sys.argv.pop().split('=') + if param[0] == '--with-cmph': with_cmph = True + if param[0] == '--moses-lib': + libdir = param[1] +print >> sys.stderr, 'mosesdir=%s\nincludes=%s\nlibdir=%s\ncmph=%s' % (mosesdir, includes, libdir, with_cmph) #### From here you probably don't need to change anything #### unless a new dependency shows up in Moses -mosesdir = os.path.abspath('../../') -includes = [mosesdir, os.path.join(mosesdir, 'moses/src'), os.path.join(mosesdir, 'util')] -libdir = os.path.join(mosesdir, 'lib') basic=['z', 'stdc++', 'pthread', 'm', 'gcc_s', 'c', 'boost_system', 'boost_thread', 'boost_filesystem', 'rt'] moses=['OnDiskPt', 'kenutil', 'kenlm', 'LM', 'mert_lib', 'moses_internal', 'CYKPlusParser', 'Scope3Parser', 'fuzzy-match', 'RuleTable', 'CompactPT', 'moses', 'dynsa', 'pcfg_common' ] additional=[] + if with_cmph: additional.append('cmph') exobj = [os.path.join(libdir, 'lib' + l + '.so') for l in moses] +print >> sys.stderr, 'basic=%s\nmoses=%s\nadditional=%s\nextra=%s' % (basic, moses, additional, exobj) + ext_modules = [ Extension(name = 'binpt', sources = ['binpt/binpt.cpp'], |