2012-09-12 13:05:28 +04:00
|
|
|
# Python interface to Moses
|
2012-09-11 17:33:57 +04:00
|
|
|
|
2012-09-14 13:59:18 +04:00
|
|
|
The idea is to have some of Moses' internals exposed to Python (inspired on pycdec).
|
2012-09-12 13:05:28 +04:00
|
|
|
|
|
|
|
## What's been interfaced?
|
|
|
|
|
2012-11-15 16:02:50 +04:00
|
|
|
* Binary tables:
|
2012-09-12 13:05:28 +04:00
|
|
|
|
2012-11-15 15:58:04 +04:00
|
|
|
Moses::PhraseDictionaryTree
|
|
|
|
OnDiskPt::OnDiskWrapper
|
2012-09-12 13:05:28 +04:00
|
|
|
|
|
|
|
## Building
|
2012-09-11 17:33:57 +04:00
|
|
|
|
2012-10-03 22:04:48 +04:00
|
|
|
1. Build the python extension:
|
2012-09-11 17:33:57 +04:00
|
|
|
|
2012-11-15 15:58:04 +04:00
|
|
|
You need to compile Moses with link=shared
|
2012-10-03 22:04:48 +04:00
|
|
|
|
2012-11-15 15:58:04 +04:00
|
|
|
./bjam --libdir=path link=shared
|
2012-10-03 22:04:48 +04:00
|
|
|
|
|
|
|
Then you can build the extension (in case you used --libdir=path above, use --moses-lib=path below)
|
|
|
|
|
2012-11-13 14:43:36 +04:00
|
|
|
python setup.py build_ext -i [--with-cmph] [--moses-lib=PATH] [--cython] [--max-factors=NUM] [--max-kenlm-order=NUM]
|
|
|
|
|
2012-11-15 15:58:04 +04:00
|
|
|
Use `--cython` if you want to re-compile the pyx files, note that they already come compiled so that you don't need to have Cython installed
|
2012-09-11 17:33:57 +04:00
|
|
|
|
2012-11-15 15:58:04 +04:00
|
|
|
## Example
|
2012-09-11 17:33:57 +04:00
|
|
|
|
2012-11-15 15:58:04 +04:00
|
|
|
### Getting a phrase table
|
2012-11-14 21:14:46 +04:00
|
|
|
|
2012-11-15 15:58:04 +04:00
|
|
|
cd examples
|
|
|
|
export LC_ALL=C
|
|
|
|
cat phrase-table.txt | sort | ../../../bin/processPhraseTable -ttable 0 0 - -nscores 5 -alignment-info -out phrase-table
|
2012-11-14 21:14:46 +04:00
|
|
|
|
2012-11-15 15:58:04 +04:00
|
|
|
### Getting a rule table
|
2012-11-14 21:14:46 +04:00
|
|
|
|
2012-11-15 15:58:04 +04:00
|
|
|
cd examples
|
|
|
|
../../../bin/CreateOnDiskPt 0 0 5 20 2 rule-table.txt rule-table
|
|
|
|
|
|
|
|
### Querying
|
|
|
|
|
|
|
|
1. Phrase-based
|
|
|
|
|
2012-11-15 21:05:03 +04:00
|
|
|
echo "casa" | python example.py examples/phrase-table 5 20
|
|
|
|
echo "essa casa" | python example.py examples/phrase-table 5 20
|
2012-11-15 15:58:04 +04:00
|
|
|
|
|
|
|
2. Hierarchical
|
|
|
|
|
2012-11-15 21:05:03 +04:00
|
|
|
echo "i [X]" | python example.py examples/rule-table 5 20
|
|
|
|
echo "have [X]" | python example.py examples/rule-table 5 20
|
|
|
|
echo "[X][X] do not [X][X] [X]" | python example.py examples/rule-table 5 20
|
2012-11-15 15:58:04 +04:00
|
|
|
|
|
|
|
### Code
|
|
|
|
|
|
|
|
```python
|
2012-11-15 16:02:50 +04:00
|
|
|
from moses.dictree import load # load abstracts away the choice of implementation by checking the available files
|
2012-11-15 15:58:04 +04:00
|
|
|
import sys
|
|
|
|
|
2012-11-15 21:05:03 +04:00
|
|
|
if len(sys.argv) != 4:
|
|
|
|
print "Usage: %s table nscores tlimit < query > result" % (sys.argv[0])
|
2012-11-15 15:58:04 +04:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
path = sys.argv[1]
|
|
|
|
nscores = int(sys.argv[2])
|
2012-11-15 21:05:03 +04:00
|
|
|
tlimit = int(sys.argv[3])
|
2012-11-15 15:58:04 +04:00
|
|
|
|
2012-11-15 21:05:03 +04:00
|
|
|
table = load(path, nscores, tlimit)
|
2012-11-15 15:58:04 +04:00
|
|
|
|
|
|
|
for line in sys.stdin:
|
|
|
|
f = line.strip()
|
|
|
|
result = table.query(f)
|
|
|
|
# you could simply print the matches
|
|
|
|
# print '\n'.join([' ||| '.join((f, str(e))) for e in matches])
|
2012-11-15 16:02:50 +04:00
|
|
|
# or you can use their attributes
|
2012-11-15 15:58:04 +04:00
|
|
|
print result.source
|
|
|
|
for e in result:
|
|
|
|
if e.lhs:
|
|
|
|
print '\t%s -> %s ||| %s ||| %s' % (e.lhs,
|
|
|
|
' '.join(e.rhs),
|
|
|
|
e.scores,
|
|
|
|
e.alignment)
|
|
|
|
else:
|
|
|
|
print '\t%s ||| %s ||| %s' % (' '.join(e.rhs),
|
|
|
|
e.scores,
|
|
|
|
e.alignment)
|
|
|
|
```
|
2012-11-14 21:14:46 +04:00
|
|
|
|
2012-09-14 13:59:18 +04:00
|
|
|
|
|
|
|
## Changing the code
|
|
|
|
|
|
|
|
If you want to add your changes you are going to have to recompile the cython code.
|
|
|
|
|
2014-06-02 16:41:33 +04:00
|
|
|
1. Compile the cython code:
|
2012-09-14 13:59:18 +04:00
|
|
|
|
2012-11-13 14:43:36 +04:00
|
|
|
python setup.py build_ext -i --cython
|
|
|
|
|