1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
import java.lang.Character
try:
# import from jarjar-ed version
from org.python.icu.text import Normalizer
from org.python.icu.lang import UCharacter, UProperty
from org.python.icu.util import VersionInfo
from org.python.icu.lang.UCharacter import EastAsianWidth, DecompositionType
from org.python.icu.lang.UCharacterEnums import ECharacterCategory, ECharacterDirection
except ImportError:
# development version of Jython, so use extlibs
from com.ibm.icu.text import Normalizer
from com.ibm.icu.lang import UCharacter, UProperty
from com.ibm.icu.util import VersionInfo
from com.ibm.icu.lang.UCharacter import EastAsianWidth, DecompositionType
from com.ibm.icu.lang.UCharacterEnums import ECharacterCategory, ECharacterDirection
__all__ = (
"bidirectional", "category", "combining", "decimal", "decomposition", "digit", "east_asian_width",
"lookup", "mirrored", "name", "normalize", "numeric", "unidata_version")
_forms = {
'NFC': Normalizer.NFC,
'NFKC': Normalizer.NFKC,
'NFD': Normalizer.NFD,
'NFKD': Normalizer.NFKD
}
Nonesuch = object() # to distinguish from None, which is a valid return value for some functions
def _validate_unichr(unichr):
if not(isinstance(unichr, unicode)):
raise TypeError("must be unicode, not {}".format(type(unichr).__name__))
if len(unichr) > 1 or len(unichr) == 0:
raise TypeError("need a single Unicode character as parameter")
def _get_codepoint(unichr):
_validate_unichr(unichr)
return ord(unichr)
def name(unichr, default=Nonesuch):
# handle None
n = UCharacter.getName(_get_codepoint(unichr))
if n is None:
if default is not Nonesuch:
return default
else:
raise ValueError("no such name")
return n
def lookup(name):
codepoint = UCharacter.getCharFromName(name)
if codepoint == -1:
raise KeyError("undefined character name '{}".format(name))
return unichr(codepoint)
def digit(unichr, default=Nonesuch):
d = UCharacter.digit(_get_codepoint(unichr))
if d == -1:
if default is not Nonesuch:
return default
else:
raise ValueError("not a digit")
return d
def decimal(unichr, default=Nonesuch):
d = UCharacter.getNumericValue(_get_codepoint(unichr))
if d < 0 or d > 9:
if default is not Nonesuch:
return default
else:
raise ValueError("not a decimal")
return d
def numeric(unichr, default=Nonesuch):
n = UCharacter.getUnicodeNumericValue(_get_codepoint(unichr))
if n == UCharacter.NO_NUMERIC_VALUE:
if default is not Nonesuch:
return default
else:
raise ValueError("not a numeric")
return n
_decomp = {
DecompositionType.CANONICAL: "canonical",
DecompositionType.CIRCLE: "circle",
DecompositionType.COMPAT: "compat",
DecompositionType.FINAL: "final",
DecompositionType.FONT: "font",
DecompositionType.FRACTION: "fraction",
DecompositionType.INITIAL: "initial",
DecompositionType.ISOLATED: "isolated",
DecompositionType.MEDIAL: "medial",
DecompositionType.NARROW: "narrow",
DecompositionType.NOBREAK: "nobreak",
DecompositionType.NONE: None,
DecompositionType.SMALL: "small",
DecompositionType.SQUARE: "square",
DecompositionType.SUB: "sub",
DecompositionType.SUPER: "super",
DecompositionType.VERTICAL: "vertical",
DecompositionType.WIDE: "wide"
}
def _get_decomp_type(unichr):
if unichr == u"\u2044": # FRACTION SLASH
# special case this for CPython compatibility even though this returns as not being combining, eg, see
# http://www.fileformat.info/info/unicode/char/2044/index.htm
return "fraction"
else:
return _decomp[UCharacter.getIntPropertyValue(ord(unichr), UProperty.DECOMPOSITION_TYPE)]
def decomposition(unichr):
_validate_unichr(unichr)
d = Normalizer.decompose(unichr, True)
decomp_type = None
if len(d) == 1:
decomp_type = _get_decomp_type(unichr)
else:
for c in d:
decomp_type = _get_decomp_type(c)
# print "Got a decomp_type %r %r %r" % (c, d, decomp_type)
if decomp_type is not None:
break
hexed = " ".join(("{0:04X}".format(ord(c)) for c in d))
if decomp_type:
return "<{}> {}".format(decomp_type, hexed)
elif len(d) == 1:
return ""
else:
return hexed
# To map from ICU4J enumerations for category, bidirection, and
# east_asian_width to the underlying property values that Python uses
# from UnicodeData.txt required a manual mapping between the following
# two files:
#
# http://icu-project.org/apiref/icu4j/constant-values.html
# http://www.unicode.org/Public/6.3.0/ucd/PropertyValueAliases.txt
_cat = {
ECharacterCategory.COMBINING_SPACING_MARK: "Mc",
ECharacterCategory.CONNECTOR_PUNCTUATION: "Pc",
ECharacterCategory.CONTROL: "Cc",
ECharacterCategory.CURRENCY_SYMBOL: "Sc",
ECharacterCategory.DASH_PUNCTUATION: "Pd",
ECharacterCategory.DECIMAL_DIGIT_NUMBER: "Nd",
ECharacterCategory.ENCLOSING_MARK: "Me",
ECharacterCategory.END_PUNCTUATION: "Pe",
ECharacterCategory.FINAL_PUNCTUATION: "Pf",
ECharacterCategory.FORMAT: "Cf",
# per http://icu-project.org/apiref/icu4j/com/ibm/icu/lang/UCharacterEnums.ECharacterCategory.html#GENERAL_OTHER_TYPES
# - no characters in [UnicodeData.txt] have this property
ECharacterCategory.GENERAL_OTHER_TYPES: "Cn Not Assigned",
ECharacterCategory.INITIAL_PUNCTUATION: "Pi",
ECharacterCategory.LETTER_NUMBER: "Nl",
ECharacterCategory.LINE_SEPARATOR: "Zl",
ECharacterCategory.LOWERCASE_LETTER: "Ll",
ECharacterCategory.MATH_SYMBOL: "Sm",
ECharacterCategory.MODIFIER_LETTER: "Lm",
ECharacterCategory.MODIFIER_SYMBOL: "Sk",
ECharacterCategory.NON_SPACING_MARK: "Mn",
ECharacterCategory.OTHER_LETTER: "Lo",
ECharacterCategory.OTHER_NUMBER: "No",
ECharacterCategory.OTHER_PUNCTUATION: "Po",
ECharacterCategory.OTHER_SYMBOL: "So",
ECharacterCategory.PARAGRAPH_SEPARATOR: "Zp",
ECharacterCategory.PRIVATE_USE: "Co",
ECharacterCategory.SPACE_SEPARATOR: "Zs",
ECharacterCategory.START_PUNCTUATION: "Ps",
ECharacterCategory.SURROGATE: "Cs",
ECharacterCategory.TITLECASE_LETTER: "Lt",
ECharacterCategory.UNASSIGNED: "Cn",
ECharacterCategory.UPPERCASE_LETTER: "Lu",
}
def category(unichr):
return _cat[UCharacter.getType(_get_codepoint(unichr))]
_dir = {
ECharacterDirection.ARABIC_NUMBER: "An",
ECharacterDirection.BLOCK_SEPARATOR: "B",
ECharacterDirection.BOUNDARY_NEUTRAL: "BN",
ECharacterDirection.COMMON_NUMBER_SEPARATOR: "CS",
ECharacterDirection.DIR_NON_SPACING_MARK: "NSM",
ECharacterDirection.EUROPEAN_NUMBER: "EN",
ECharacterDirection.EUROPEAN_NUMBER_SEPARATOR: "ES",
ECharacterDirection.EUROPEAN_NUMBER_TERMINATOR: "ET",
ECharacterDirection.FIRST_STRONG_ISOLATE: "FSI",
ECharacterDirection.LEFT_TO_RIGHT: "L",
ECharacterDirection.LEFT_TO_RIGHT_EMBEDDING: "LRE",
ECharacterDirection.LEFT_TO_RIGHT_ISOLATE: "LRI",
ECharacterDirection.LEFT_TO_RIGHT_OVERRIDE: "LRO",
ECharacterDirection.OTHER_NEUTRAL: "ON",
ECharacterDirection.POP_DIRECTIONAL_FORMAT: "PDF",
ECharacterDirection.POP_DIRECTIONAL_ISOLATE: "PDI",
ECharacterDirection.RIGHT_TO_LEFT: "R",
ECharacterDirection.RIGHT_TO_LEFT_ARABIC: "AL",
ECharacterDirection.RIGHT_TO_LEFT_EMBEDDING: "RLE",
ECharacterDirection.RIGHT_TO_LEFT_ISOLATE: "RLI",
ECharacterDirection.RIGHT_TO_LEFT_OVERRIDE: "RLO",
ECharacterDirection.SEGMENT_SEPARATOR: "S",
ECharacterDirection.WHITE_SPACE_NEUTRAL: "WS"
}
def bidirectional(unichr):
return _dir[UCharacter.getDirection(_get_codepoint(unichr))]
def combining(unichr):
return UCharacter.getCombiningClass(_get_codepoint(unichr))
def mirrored(unichr):
return UCharacter.isMirrored(_get_codepoint(unichr))
_eaw = {
# http://www.unicode.org/reports/tr11/
EastAsianWidth.AMBIGUOUS : "A",
EastAsianWidth.COUNT : "?", # apparently not used, see above TR
EastAsianWidth.FULLWIDTH : "F",
EastAsianWidth.HALFWIDTH : "H",
EastAsianWidth.NARROW : "Na",
EastAsianWidth.NEUTRAL : "N",
EastAsianWidth.WIDE : "W"
}
def east_asian_width(unichr):
return _eaw[UCharacter.getIntPropertyValue(_get_codepoint(unichr), UProperty.EAST_ASIAN_WIDTH)]
def normalize(form, unistr):
"""
Return the normal form 'form' for the Unicode string unistr. Valid
values for form are 'NFC', 'NFKC', 'NFD', and 'NFKD'.
"""
try:
normalizer_form = _forms[form]
except KeyError:
raise ValueError('invalid normalization form')
return Normalizer.normalize(unistr, normalizer_form)
def get_icu_version():
versions = []
for k in VersionInfo.__dict__.iterkeys():
if k.startswith("UNICODE_"):
v = getattr(VersionInfo, k)
versions.append((v.getMajor(), v.getMinor(), v.getMilli()))
return ".".join(str(x) for x in max(versions))
unidata_version = get_icu_version()
|