3 CONTINUE_ON
= frozenset([
4 "Ll", "Lm", "Lo", "Lt", "Lu",
11 UNKNOWN
, LETTER
, NUMBER
= range(3)
14 """Generate a list of sortemes for the string.
16 A sorteme, by analogy with grapheme/morpheme/etc. is an atom of
17 sort information. This is larger than a word boundry but smaller
18 than a sentence boundry; roughly, a sorteme boundry occurs between
19 letters and numbers, between numbers and numbrs if 'too much'
20 punctuation exists in between, between lines.
22 There is no formal specification for sortemes; the goal of this
23 function is to provide good output for Collator.sortemekey.
29 string
= unicode(string
)
33 previous_mode
= UNKNOWN
35 for i
, c
in enumerate(string
):
37 prev_category
= category
39 category
= unicodedata
.category(c
)
41 # Split at the first letter following a number or
42 # non-continuing character.
43 if category
[0] == "L":
48 # Split at the first number following a non-number or
49 # non-continuing character.
50 elif category
[0] == "N":
55 # Split if we find a non-continuing character ("weird" ones).
56 elif category
not in CONTINUE_ON
:
60 # Only certain punctuation allowed in numbers.
61 elif mode
== NUMBER
and category
[0] == "P" and c
not in "',._":
65 # Split if we find two pieces of punctuation in a row, even
66 # if we should otherwise continue.
67 elif i
> 0 and prev_category
[0] == "P" and category
[0] == "P":
71 if broke
and start
is not None and last
is not None:
72 # If we read two strings separated by weird punctuation,
73 # pretend the punctuation isn't there.
74 if (this_mode
== previous_mode
== LETTER
75 and prev_category
[0] == "P"
77 words
[-1] += u
" " + string
[start
:last
+1]
79 # This ensures "foo2 bar" sorts as ["foo ", 2, "bar"]
80 # Which sorts after ["foo", "bar"].
81 if this_mode
== NUMBER
and previous_mode
== LETTER
and words
:
83 words
.append(string
[start
:last
+1])
84 previous_mode
= this_mode
89 if category
[0] in "LN":
91 if start
is not None and last
is not None:
92 if this_mode
== previous_mode
== LETTER
and words
:
93 words
[-1] += u
" " + string
[start
:last
+1]
95 if this_mode
== NUMBER
and previous_mode
== LETTER
and words
:
97 words
.append(string
[start
:last
+1])
100 def numeric(orig
, invalid
=float('inf')):
104 string
= unicode(orig
)
109 return (invalid
, orig
)
112 while string
[:1] == u
"-" or string
[:1] == u
"+":
113 if string
[:1] == u
"-":
117 if not string
[:1].isnumeric():
118 return (invalid
, orig
)
120 string
= normalize_punc(string
)
122 # Early out if possible.
124 return (float(string
) * mult
, orig
)
128 # Otherwise we need to do this the hard way.
129 def _numeric(string
):
132 v
= unicodedata
.numeric(c
)
139 whole
, frac
= string
.split(".")
140 whole
= _numeric(whole
)
141 frac
= _numeric(frac
) / (10.0 ** len(frac
))
142 return (mult
* (whole
+ frac
), orig
)
144 return (mult
* _numeric(string
), orig
)
146 def normalize_punc(string
):
147 string
= unicode(string
.strip(u
",.'"))
148 string
= filter(lambda u
: u
.isnumeric() or u
in u
",.'", string
)
149 commas
= string
.count(u
",")
150 stops
= string
.count(u
".")
151 quotes
= string
.count(u
"'")
153 # If anything occurs more than once, it's a separator.
155 string
= string
.replace(u
",", u
"")
158 string
= string
.replace(u
".", u
"")
161 string
= string
.replace(u
"'", u
"")
164 def normalize_two(a
, b
, string
):
165 # One of each - assume the first is grouping, second is point.
166 a_idx
= string
.rindex(a
)
167 b_idx
= string
.rindex(b
)
169 string
= string
.replace(b
, u
"").replace(a
, u
".")
171 string
= string
.replace(a
, u
"").replace(b
, u
".")
174 if commas
and stops
and quotes
:
175 # If all three, assume the middle is the decimal point.
180 # Not really valid, so do whatever we want...
183 comma_idx
= string
.index(u
",")
184 stops_idx
= string
.index(u
".")
185 quotes_idx
= string
.index(u
"'")
186 if (comma_idx
< stops_idx
< quotes_idx
187 or quotes_idx
< stops_idx
< comma_idx
):
188 string
= string
.replace(u
",", u
"").replace(u
"'", u
"")
189 elif (comma_idx
< quotes_idx
< stops_idx
190 or stops_idx
< quotes_idx
< comma_idx
):
191 string
= string
.replace(
196 string
= string
.replace(
201 elif stops
and quotes
:
202 string
= normalize_two(u
".", u
"'", string
)
204 elif commas
and quotes
:
205 string
= normalize_two(u
",", u
"'", string
)
207 elif commas
and stops
:
208 string
= normalize_two(u
",", u
".", string
)
211 if string
[-4:-3] == u
"," and len(string
) <= 7:
212 # Single comma as a thousands separator.
213 string
= string
.replace(u
",", u
"")
215 # Single comma, not thousands - probably a decimal point.
216 string
= string
.replace(u
",", u
".")
219 # Single quote, probably MM'SS", equivalent to a decimal point.
220 string
= string
.replace(u
"'", u
".")
222 elif stops
and string
[-4:] == ".000":
223 # Single stop, but no decimal - probably grouping.
224 string
= string
.replace(u
".", u
"")