text
stringlengths
0
828
.replace(u'\xe2\x80\xa9', '').replace(u'\xc2\x85', '') \
.encode('utf-8')"
4207,"def decode_to_unicode(text, default_encoding='utf-8'):
""""""Decode input text into Unicode representation.
Decode input text into Unicode representation by first using the default
encoding utf-8.
If the operation fails, it detects the type of encoding used in the
given text.
For optimal result, it is recommended that the 'chardet' module is
installed.
NOTE: Beware that this might be slow for *very* large strings.
If chardet detection fails, it will try to decode the string using the basic
detection function guess_minimum_encoding().
Also, bear in mind that it is impossible to detect the correct encoding at
all times, other then taking educated guesses. With that said, this function
will always return some decoded Unicode string, however the data returned
may not be the same as original data in some cases.
:param text: the text to decode
:type text: string
:param default_encoding: the character encoding to use. Optional.
:type default_encoding: string
:return: input text as Unicode
:rtype: string
""""""
if not text:
return """"
try:
return text.decode(default_encoding)
except (UnicodeError, LookupError):
pass
detected_encoding = None
if CHARDET_AVAILABLE:
# We can use chardet to perform detection
res = chardet.detect(text)
if res['confidence'] >= 0.8:
detected_encoding = res['encoding']
if detected_encoding is None:
# No chardet detection, try to make a basic guess
dummy, detected_encoding = guess_minimum_encoding(text)
return text.decode(detected_encoding)"
4208,"def to_unicode(text):
""""""Convert to unicode.""""""
if isinstance(text, unicode):
return text
if isinstance(text, six.string_types):
return decode_to_unicode(text)
return unicode(text)"
4209,"def translate_latex2unicode(text, kb_file=None):
""""""Translate latex text to unicode.
This function will take given text, presumably containing LaTeX symbols,
and attempts to translate it to Unicode using the given or default KB
translation table located under
CFG_ETCDIR/bibconvert/KB/latex-to-unicode.kb.
The translated Unicode string will then be returned.
If the translation table and compiled regular expression object is not
previously generated in the current session, they will be.
:param text: a text presumably containing LaTeX symbols.
:type text: string
:param kb_file: full path to file containing latex2unicode translations.
Defaults to CFG_ETCDIR/bibconvert/KB/latex-to-unicode.kb
:type kb_file: string
:return: Unicode representation of translated text
:rtype: unicode
""""""
if kb_file is None:
kb_file = get_kb_filename()
# First decode input text to Unicode
try:
text = decode_to_unicode(text)
except UnicodeDecodeError:
text = unicode(wash_for_utf8(text))
# Load translation table, if required
if CFG_LATEX_UNICODE_TRANSLATION_CONST == {}:
_load_latex2unicode_constants(kb_file)
# Find all matches and replace text
for match in CFG_LATEX_UNICODE_TRANSLATION_CONST['regexp_obj'] \
.finditer(text):
# If LaTeX style markers {, } and $ are before or after the
# matching text, it will replace those as well
text = re.sub(""[\{\$]?%s[\}\$]?"" % (re.escape(match.group()),),
CFG_LATEX_UNICODE_TRANSLATION_CONST[
'table'][match.group()],
text)
# Return Unicode representation of translated text
return text"
4210,"def _load_latex2unicode_constants(kb_file=None):
""""""Load LaTeX2Unicode translation table dictionary.
Load LaTeX2Unicode translation table dictionary and regular