JL ib^dZddlmZmZmZddlmZmZddlm Z m Z ddl m Z Gdde Z y) a! A general interface to the SENNA pipeline that supports any of the operations specified in SUPPORTED_OPERATIONS. Applying multiple operations at once has the speed advantage. For example, Senna will automatically determine POS tags if you are extracting named entities. Applying both of the operations will cost only the time of extracting the named entities. The SENNA pipeline has a fixed maximum size of the sentences that it can read. By default it is 1024 token/sentence. If you have larger sentences, changing the MAX_SENTENCE_SIZE value in SENNA_main.c should be considered and your system specific binary should be rebuilt. Otherwise this could introduce misalignment errors. The input is: - path to the directory that contains SENNA executables. If the path is incorrect, Senna will automatically search for executable file specified in SENNA environment variable - List of the operations needed to be performed. - (optionally) the encoding of the input data (default:utf-8) Note: Unit tests for this module can be found in test/unit/test_senna.py >>> from nltk.classify import Senna >>> pipeline = Senna('/usr/share/senna-v3.0', ['pos', 'chk', 'ner']) # doctest: +SKIP >>> sent = 'Dusseldorf is an international business center'.split() >>> [(token['word'], token['chk'], token['ner'], token['pos']) for token in pipeline.tag(sent)] # doctest: +SKIP [('Dusseldorf', 'B-NP', 'B-LOC', 'NNP'), ('is', 'B-VP', 'O', 'VBZ'), ('an', 'B-NP', 'O', 'DT'), ('international', 'I-NP', 'O', 'JJ'), ('business', 'I-NP', 'O', 'NN'), ('center', 'I-NP', 'O', 'NN')] )environpathsep) architecturesystem)PIPEPopen)TaggerIc4eZdZgdZddZdZdZdZdZy) Senna)poschknerc||_tj|tz|_|j |j}tj |srdtvrjtjtdtz|_|j |j}tj |std|d|d||_ y)NSENNAzSenna executable expected at z or z but not found) _encodingrnormpathr_path executableisfiler LookupError operations)self senna_pathrencoding exe_file_1 exe_file_2s Y/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/nltk/classify/senna.py__init__zSenna.__init__2s!]]:.4 __TZZ0 {{:&'!!]]77+;td}|dk(rtj|dStj|dS|dk(rtj|dS|dk(rtj|d Stj|d S) z The function that determines the system specific binary that should be used in the pipeline. In case, the system is not known the default senna binary will be used. Linuxr64bitz senna-linux64z senna-linux32Windowszsenna-win32.exeDarwinz senna-osxsenna)rrrjoin)r base_pathos_namebitss rrzSenna.executableGs ( g >!$DwyyO<<99Y8 8 i 99Y(9: : h 99Y 4 4yyG,,r cji}d}tjD]}||jvs|||<|dz }|S)z A method that calculates the order of the columns that SENNA pipeline will output the tags into. This depends on the operations being ordered. )r SUPPORTED_OPERATIONSr)r_mapi operations rr.z Senna._mapYsH  33 IDOO+"#YQ  r c,|j|gdS)zI Applies the specified operation(s) on a list of tokens. r) tag_sents)rtokenss rtagz Senna.tagfs~~vh'**r cZ|j}tj|j|js't d|j|jz|j|jd|jddg}|j |jDcgc]}d|z c}djd|Ddz}t|tr|r|j|}t|ttt}|j| \}}|} |jd k7rt!d |z|r|j#|} |j%} gg} d } d } | j'j)dD]u}|s| j+g| d z } d } |j)d }i}| D]}|| |j'||< || | |d<| dj+|| d z } w| Scc}w#t,$r}t-d| z|d}~wwxYw)z Applies the tag method over a list of sentences. This method will return a list of dictionaries. Every dictionary will contain a word with its calculated annotations/tags. z-Senna executable expected at %s but not foundz-pathz -usrtokensz-iobtags- c3>K|]}dj|yw) N)r').0xs r z"Senna.tag_sents..s:1388A;:s)stdinstdoutstderr)inputrz!Senna command failed! Details: %sr, wordzMisalignment error occurred at sentence number %d. Possible reason is that the sentence size exceeded the maximum size. Check the documentation of Senna class for more information.N)rrrrrrextendrr' isinstancestrencoder r communicate returncode RuntimeErrordecoder.stripsplitappend IndexError)r sentencesr _senna_cmdop_inputpr>r? senna_outputmap_tagged_sentencessentence_index token_index tagged_wordtagsresultr4es rr2zSenna.tag_sentslsE >>{{4??4::67?//$**-.  OODJJ '  JJ     doo>38>?: ::TA fc "x]]8,F *Dd C==v=6  <<1 BVKL L !==2Lyy{4 '--/55d; K ''+!# $$T*DF 6"49o335s  6 !*>!:;!Gv R ' ' / 1 K) * [?H  I%%   s% H H  H*H%%H*N)zutf-8) __name__ __module__ __qualname__r-rrr.r4r2r rr r /s"0%*-$ + C r r N)__doc__osrrrplatformrr subprocessrr nltk.tag.apir r rar rrgs*@"!)" @ G@ r