3

I am looking for a Python package that can take a text as input and return the same text with any "non-ASCII Unicode symbol" converted into a word that represents it, if such conversion makes sense.

For example, given the text:

This text is © John Smith. He likes √2 and €. 

the output could be:

This text is copyright John Smith. He likes square root 2 and euro. 

Any OS, license and price is fine.

(I have to convert some Unicode texts to ASCII, and want to avoid using too much information. The texts are medical papers written in English.)

1
  • "using too much information" or "losing too much information"? Jul 26, 2016 at 19:38

1 Answer 1

2

Not a package, but two simple functions that may already do what you want to achieve:

import unicodedata

def InsertUnicodeCharNames(inputstring):
    result = ""
    for ch in inputstring:
        if ord(ch) > 127:
            result += TrimSign(unicodedata.name(ch)).lower()
        else:
            result += ch
    return result

# remove trailing string " SIGN" from EURO SIGN, COPYRIGHT SIGN etc.
def TrimSign(inputstring):
    if inputstring.endswith(" SIGN"):
        return inputstring[:len(inputstring) - len(" SIGN")]
    else:
        return inputstring

This is a usage example:

print(InsertUnicodeCharNames("This text is © John Smith. He likes √2 and €."))

returns

This text is copyright John Smith. He likes square root2 and euro.

Unfortunately, this approach will produce ugly results for accented characters:

print(InsertUnicodeCharNames("Beyoncé planning to perform with Motörhead!"))

returns

Beyonclatin small letter e with acute planning to perform with Motlatin small letter o with diaeresisrhead!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.