<s> import sys <EOL> import os . path <EOL> import logging <EOL> from sqlite import SQLite <EOL> class UMLS ( object ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def check_database ( cls ) : <EOL> """<STR_LIT>""" <EOL> umls_db = os . path . join ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not os . path . exists ( umls_db ) : <EOL> raise Exception ( "<STR_LIT>" <EOL> . format ( os . path . abspath ( umls_db ) ) ) <EOL> class UMLSLookup ( object ) : <EOL> """<STR_LIT>""" <EOL> sqlite = None <EOL> did_check_dbs = False <EOL> preferred_sources = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self ) : <EOL> absolute = os . path . dirname ( os . path . realpath ( __file__ ) ) <EOL> self . sqlite = SQLite . get ( os . path . join ( absolute , '<STR_LIT>' ) ) <EOL> def lookup_code ( self , cui , preferred = True ) : <EOL> """<STR_LIT>""" <EOL> if cui is None or len ( cui ) < <NUM_LIT:1> : <EOL> return [ ] <EOL> if not UMLSLookup . did_check_dbs : <EOL> UMLS . check_database ( ) <EOL> UMLSLookup . did_check_dbs = True <EOL> negated = '<STR_LIT:->' == cui [ <NUM_LIT:0> ] <EOL> if negated : <EOL> cui = cui [ <NUM_LIT:1> : ] <EOL> parts = cui . split ( '<STR_LIT:@>' , <NUM_LIT:1> ) <EOL> lookup_cui = parts [ <NUM_LIT:0> ] <EOL> if preferred : <EOL> sql = '<STR_LIT>' . format ( "<STR_LIT:U+002CU+0020>" . join ( UMLSLookup . preferred_sources ) ) <EOL> else : <EOL> sql = '<STR_LIT>' <EOL> arr = [ ] <EOL> for res in self . sqlite . execute ( sql , ( lookup_cui , ) ) : <EOL> if negated : <EOL> arr . append ( ( "<STR_LIT>" . format ( res [ <NUM_LIT:0> ] , res [ <NUM_LIT:1> ] , res [ <NUM_LIT:2> ] ) ) ) <EOL> else : <EOL> arr . append ( res ) <EOL> return arr <EOL> def lookup_code_meaning ( self , cui , preferred = True , no_html = True ) : <EOL> """<STR_LIT>""" <EOL> names = [ ] <EOL> for res in self . lookup_code ( cui , preferred ) : <EOL> if no_html : <EOL> names . append ( "<STR_LIT>" . format ( res [ <NUM_LIT:0> ] , res [ <NUM_LIT:1> ] , res [ <NUM_LIT:2> ] ) ) <EOL> else : <EOL> names . append ( "<STR_LIT>" . format ( res [ <NUM_LIT:0> ] , res [ <NUM_LIT:1> ] , res [ <NUM_LIT:2> ] ) ) <EOL> comp = "<STR_LIT:U+002CU+0020>" if no_html else "<STR_LIT>" <EOL> return comp . join ( names ) if len ( names ) > <NUM_LIT:0> else '<STR_LIT>' <EOL> def lookup_code_for_name ( self , name , preferred = True ) : <EOL> """<STR_LIT>""" <EOL> if name is None or len ( name ) < <NUM_LIT:1> : <EOL> return None <EOL> if not UMLSLookup . did_check_dbs : <EOL> UMLS . check_database ( ) <EOL> UMLSLookup . did_check_dbs = True <EOL> if preferred : <EOL> sql = '<STR_LIT>' . format ( "<STR_LIT:U+002CU+0020>" . join ( UMLSLookup . preferred_sources ) ) <EOL> else : <EOL> sql = '<STR_LIT>' <EOL> arr = [ ] <EOL> for res in self . sqlite . execute ( sql , ( '<STR_LIT:%>' + name + '<STR_LIT:%>' , ) ) : <EOL> arr . append ( res ) <EOL> return arr <EOL> if '<STR_LIT:__main__>' == __name__ : <EOL> UMLS . check_database ( ) <EOL> look = UMLSLookup ( ) <EOL> code = '<STR_LIT>' <EOL> meaning = look . lookup_code_meaning ( code ) <EOL> print ( '<STR_LIT>' . format ( code , meaning ) ) <EOL> name = '<STR_LIT>' <EOL> print ( '<STR_LIT>' . format ( name ) ) <EOL> codes = look . lookup_code_for_name ( name ) <EOL> for cd in codes : <EOL> print ( '<STR_LIT>' . format ( cd , look . lookup_code_meaning ( cd [ <NUM_LIT:0> ] ) ) ) </s>
