<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division , print_function , unicode_literals <EOL> import re <EOL> from cld2 import detect as cld2_detect <EOL> from textacy . compat import PY2 , str <EOL> from textacy . regexes_etc import ACRONYM_REGEX <EOL> def is_acronym ( token , exclude = None ) : <EOL> """<STR_LIT>""" <EOL> if exclude and token in exclude : <EOL> return False <EOL> if not token : <EOL> return False <EOL> if '<STR_LIT:U+0020>' in token : <EOL> return False <EOL> if len ( token ) == <NUM_LIT:2> and not token . isupper ( ) : <EOL> return False <EOL> if token . isdigit ( ) : <EOL> return False <EOL> if ( not any ( char . isupper ( ) for char in token ) <EOL> and not ( token [ <NUM_LIT:0> ] . isdigit ( ) or token [ - <NUM_LIT:1> ] . isdigit ( ) ) ) : <EOL> return False <EOL> if not <NUM_LIT:2> <= sum ( <NUM_LIT:1> for char in token if char . isalnum ( ) ) <= <NUM_LIT:10> : <EOL> return False <EOL> if not ACRONYM_REGEX . match ( token ) : <EOL> return False <EOL> return True <EOL> def detect_language ( text ) : <EOL> """<STR_LIT>""" <EOL> if PY2 : <EOL> is_reliable , _ , best_guesses = cld2_detect ( str ( text ) . encode ( '<STR_LIT:utf8>' ) , bestEffort = True ) <EOL> else : <EOL> is_reliable , _ , best_guesses = cld2_detect ( str ( text ) , bestEffort = True ) <EOL> if is_reliable is False : <EOL> msg = '<STR_LIT>' <EOL> print ( msg . format ( best_guesses ) ) <EOL> return best_guesses [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> def keyword_in_context ( text , keyword , ignore_case = True , <EOL> window_width = <NUM_LIT:50> , print_only = True ) : <EOL> """<STR_LIT>""" <EOL> flags = re . IGNORECASE if ignore_case is True else <NUM_LIT:0> <EOL> if print_only is True : <EOL> for match in re . finditer ( keyword , text , flags = flags ) : <EOL> print ( '<STR_LIT>' . format ( <EOL> pre = text [ max ( <NUM_LIT:0> , match . start ( ) - window_width ) : match . start ( ) ] . rjust ( window_width ) , <EOL> kw = match . group ( ) , <EOL> post = text [ match . end ( ) : match . end ( ) + window_width ] . ljust ( window_width ) ) ) <EOL> else : <EOL> return ( ( text [ max ( <NUM_LIT:0> , match . start ( ) - window_width ) : match . start ( ) ] , <EOL> match . group ( ) , <EOL> text [ match . end ( ) : match . end ( ) + window_width ] ) <EOL> for match in re . finditer ( keyword , text , flags = flags ) ) </s>
