Training a new Stanford part-of-speech tagger from within the NLTK
Question: I've trained a part-of-speech tagger for an uncommon language (Uyghur) using
the Stanford POS tagger and some self-collected training data. I've been using
the NLTK's `nltk.tag.stanford.POSTagger` interface to tag individual sentences
in Python. This works well for most of my purposes: (running from
`/usr/share/stanford-postagger`)

    
    
    >>> from nltk.tag.stanford import POSTagger
    >>> uy = POSTagger('models/uyghur.tagger', 'stanford-postagger.jar')
    >>> uy.tag('Men méning mantini yégenlikimni bilimen .'.split())
    [[(u'Men', u'PN1s'), (u'm\xe9ning', u'PN1s.GEN'), (u'mantini', u'N-ACC'), (u'y\xe9genlikimni', u'Vt-PST.dir-1s2'), (u'bilimen', u'Vt-PRS-1s1'), (u'.', u'PUNCT')]]
    

I would like to do a ten-fold cross-validation to get a better sense of the
accuracy of this tagger, i.e., use each tenth of my complete training data as
test data for a tagger trained on the other nine-tenths of the data. Splitting
the data set ten ways is no problem in Python, but I don't know if there's a
way to train a new tagger from within Python. When I've done it, it's been
from the command line using `java -mx1g -classpath /usr/share/stanford-
postagger/stanford-postagger.jar edu.stanford.nlp.tagger.maxent.MaxentTagger
-props uyghurtagger.props`.

Is it possible to train a new Stanford tagger from within Python using the
NLTK interface, or will I need to create it manually via the command line each
time?


Answer: This was much simpler to implement than I had thought.

    
    
    import subprocess32
    subprocess32.call(
        ['java', '-mx1g', '-classpath',
         '/usr/share/stanford-postagger/stanford-postagger.jar',
         'edu.stanford.nlp.tagger.maxent.MaxentTagger', '-props',
         'uyghurtagger.props'])
    

It's really just as simple as passing a list of the command line arguments to
`subprocess32.call()`. (I am using `subprocess32` instead of `subprocess` per
the recommendation in the [`subprocess`
docs](https://docs.python.org/2/library/subprocess.html)).

