<s> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> from nltk . six import text_type <EOL> import os <EOL> import sys <EOL> import tempfile <EOL> import subprocess <EOL> import inspect <EOL> from nltk . data import ZipFilePathPointer <EOL> from nltk . internals import find_dir , find_file , find_jars_within_path <EOL> from nltk . parse . api import ParserI <EOL> from nltk . parse . dependencygraph import DependencyGraph <EOL> from nltk . parse . util import taggedsents_to_conll <EOL> def malt_regex_tagger ( ) : <EOL> from nltk . tag import RegexpTagger <EOL> _tagger = RegexpTagger ( <EOL> [ ( r'<STR_LIT>' , '<STR_LIT:.>' ) , ( r'<STR_LIT>' , '<STR_LIT:U+002C>' ) , ( r'<STR_LIT>' , '<STR_LIT:?>' ) , <EOL> ( r'<STR_LIT>' , '<STR_LIT:(>' ) , ( r'<STR_LIT>' , '<STR_LIT:)>' ) , <EOL> ( r'<STR_LIT>' , '<STR_LIT:[>' ) , ( r'<STR_LIT>' , '<STR_LIT:]>' ) , <EOL> ( r'<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( r'<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( r'<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( r'<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( r'<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( r'<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( r'<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( r'<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( r'<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( r'<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( r'<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( r'<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( r'<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( r'<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( r'<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( r'<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( r'<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( r'<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( r'<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( r'<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( r'<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] ) <EOL> return _tagger . tag <EOL> def find_maltparser ( parser_dirname ) : <EOL> """<STR_LIT>""" <EOL> if os . path . exists ( parser_dirname ) : <EOL> _malt_dir = parser_dirname <EOL> else : <EOL> _malt_dir = find_dir ( parser_dirname , env_vars = ( '<STR_LIT>' , ) ) <EOL> malt_dependencies = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> _malt_jars = set ( find_jars_within_path ( _malt_dir ) ) <EOL> _jars = set ( os . path . split ( jar ) [ <NUM_LIT:1> ] for jar in _malt_jars ) <EOL> malt_dependencies = set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert malt_dependencies . issubset ( _jars ) <EOL> assert any ( filter ( lambda i : i . startswith ( '<STR_LIT>' ) and i . endswith ( '<STR_LIT>' ) , _jars ) ) <EOL> return list ( _malt_jars ) <EOL> def find_malt_model ( model_filename ) : <EOL> """<STR_LIT>""" <EOL> if model_filename == None : <EOL> return '<STR_LIT>' <EOL> elif os . path . exists ( model_filename ) : <EOL> return model_filename <EOL> else : <EOL> return find_file ( model_filename , env_vars = ( '<STR_LIT>' , ) , verbose = False ) <EOL> class MaltParser ( ParserI ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parser_dirname , model_filename = None , tagger = None , additional_java_args = None ) : <EOL> """<STR_LIT>""" <EOL> self . malt_jars = find_maltparser ( parser_dirname ) <EOL> self . additional_java_args = additional_java_args if additional_java_args is not None else [ ] <EOL> self . model = find_malt_model ( model_filename ) <EOL> self . _trained = self . model != '<STR_LIT>' <EOL> self . working_dir = tempfile . gettempdir ( ) <EOL> self . tagger = tagger if tagger is not None else malt_regex_tagger ( ) <EOL> def parse_tagged_sents ( self , sentences , verbose = False , top_relation_label = '<STR_LIT:null>' ) : <EOL> """<STR_LIT>""" <EOL> if not self . _trained : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> with tempfile . NamedTemporaryFile ( prefix = '<STR_LIT>' , <EOL> dir = self . working_dir , mode = '<STR_LIT:w>' , delete = False ) as input_file : <EOL> with tempfile . NamedTemporaryFile ( prefix = '<STR_LIT>' , <EOL> dir = self . working_dir , mode = '<STR_LIT:w>' , delete = False ) as output_file : <EOL> for line in taggedsents_to_conll ( sentences ) : <EOL> input_file . write ( text_type ( line ) ) <EOL> input_file . close ( ) <EOL> cmd = self . generate_malt_command ( input_file . name , <EOL> output_file . name , mode = "<STR_LIT>" ) <EOL> _current_path = os . getcwd ( ) <EOL> try : <EOL> os . chdir ( os . path . split ( self . model ) [ <NUM_LIT:0> ] ) <EOL> except : <EOL> pass <EOL> ret = self . _execute ( cmd , verbose ) <EOL> os . chdir ( _current_path ) <EOL> if ret is not <NUM_LIT:0> : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( '<STR_LIT:U+0020>' . join ( cmd ) , ret ) ) <EOL> with open ( output_file . name ) as infile : <EOL> for tree_str in infile . read ( ) . split ( '<STR_LIT>' ) : <EOL> yield ( iter ( [ DependencyGraph ( tree_str , top_relation_label = top_relation_label ) ] ) ) <EOL> os . remove ( input_file . name ) <EOL> os . remove ( output_file . name ) <EOL> def parse_sents ( self , sentences , verbose = False , top_relation_label = '<STR_LIT:null>' ) : <EOL> """<STR_LIT>""" <EOL> tagged_sentences = ( self . tagger ( sentence ) for sentence in sentences ) <EOL> return self . parse_tagged_sents ( tagged_sentences , verbose , top_relation_label = top_relation_label ) <EOL> def generate_malt_command ( self , inputfilename , outputfilename = None , mode = None ) : <EOL> """<STR_LIT>""" <EOL> cmd = [ '<STR_LIT>' ] <EOL> cmd += self . additional_java_args <EOL> classpaths_separator = '<STR_LIT:;>' if sys . platform . startswith ( '<STR_LIT>' ) else '<STR_LIT::>' <EOL> cmd += [ '<STR_LIT>' , classpaths_separator . join ( self . malt_jars ) ] <EOL> cmd += [ '<STR_LIT>' ] <EOL> if os . path . exists ( self . model ) : <EOL> cmd += [ '<STR_LIT:-c>' , os . path . split ( self . model ) [ - <NUM_LIT:1> ] ] <EOL> else : <EOL> cmd += [ '<STR_LIT:-c>' , self . model ] <EOL> cmd += [ '<STR_LIT>' , inputfilename ] <EOL> if mode == '<STR_LIT>' : <EOL> cmd += [ '<STR_LIT>' , outputfilename ] <EOL> cmd += [ '<STR_LIT>' , mode ] <EOL> return cmd <EOL> @ staticmethod <EOL> def _execute ( cmd , verbose = False ) : <EOL> output = None if verbose else subprocess . PIPE <EOL> p = subprocess . Popen ( cmd , stdout = output , stderr = output ) <EOL> return p . wait ( ) <EOL> def train ( self , depgraphs , verbose = False ) : <EOL> """<STR_LIT>""" <EOL> with tempfile . NamedTemporaryFile ( prefix = '<STR_LIT>' , <EOL> dir = self . working_dir , mode = '<STR_LIT:w>' , delete = False ) as input_file : <EOL> input_str = ( '<STR_LIT:\n>' . join ( dg . to_conll ( <NUM_LIT:10> ) for dg in depgraphs ) ) <EOL> input_file . write ( text_type ( input_str ) ) <EOL> self . train_from_file ( input_file . name , verbose = verbose ) <EOL> os . remove ( input_file . name ) <EOL> def train_from_file ( self , conll_file , verbose = False ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( conll_file , ZipFilePathPointer ) : <EOL> with tempfile . NamedTemporaryFile ( prefix = '<STR_LIT>' , <EOL> dir = self . working_dir , mode = '<STR_LIT:w>' , delete = False ) as input_file : <EOL> with conll_file . open ( ) as conll_input_file : <EOL> conll_str = conll_input_file . read ( ) <EOL> input_file . write ( text_type ( conll_str ) ) <EOL> return self . train_from_file ( input_file . name , verbose = verbose ) <EOL> cmd = self . generate_malt_command ( conll_file , mode = "<STR_LIT>" ) <EOL> ret = self . _execute ( cmd , verbose ) <EOL> if ret != <NUM_LIT:0> : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( '<STR_LIT:U+0020>' . join ( cmd ) , ret ) ) <EOL> self . _trained = True <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> '''<STR_LIT>''' <EOL> import doctest <EOL> doctest . testmod ( ) </s>
