3 CONTINUE_ON
= frozenset([
4 "Ll", "Lm", "Lo", "Lt", "Lu",
11 UNKNOWN
, LETTER
, NUMBER
= range(3)
16 """Generate a list of sortemes for the string.
18 A sorteme, by analogy with grapheme/morpheme/etc. is an atom of
19 sort information. This is larger than a word boundry but smaller
20 than a sentence boundry; roughly, a sorteme boundry occurs between
21 letters and numbers, between numbers and numbrs if 'too much'
22 punctuation exists in between, between lines.
24 There is no formal specification for sortemes; the goal of this
25 function is to provide good output for Collator.sortemekey.
31 string
= unicode(string
)
35 previous_mode
= UNKNOWN
38 # TODO(jfw): This kind of evolved over time, there's probably a much
39 # faster / more concise way to express it now.
40 for i
, c
in enumerate(string
):
42 prev_category
= category
44 category
= unicodedata
.category(c
)
46 # Split at the first letter following a number or
47 # non-continuing character.
48 if category
[0] == "L":
53 # Split at the first number following a non-number or
54 # non-continuing character.
55 elif category
[0] == "N":
60 # Split if we find a non-continuing character ("weird" ones).
61 elif category
not in CONTINUE_ON
:
65 # Only certain punctuation allowed in numbers.
66 elif mode
== NUMBER
and category
[0] == "P" and c
not in "',._":
70 # Split if we find two pieces of punctuation in a row, even
71 # if we should otherwise continue.
72 elif prev_category
[0] in "P" and category
[0] in "P":
76 if broke
and start
is not None and last
is not None:
77 # If we read two strings separated by weird punctuation,
78 # pretend the punctuation isn't there.
79 if this_mode
== previous_mode
== LETTER
:
80 words
[-1] += BREAKER
+ string
[start
:last
+1]
82 if this_mode
== NUMBER
and previous_mode
== LETTER
:
84 words
.append(string
[start
:last
+1])
85 previous_mode
= this_mode
90 if category
[0] in "LN":
93 if start
is not None and last
is not None:
94 if this_mode
== LETTER
and previous_mode
== LETTER
and words
:
95 words
[-1] += BREAKER
+ string
[start
:last
+1]
97 if this_mode
== NUMBER
and previous_mode
== LETTER
and words
:
99 words
.append(string
[start
:last
+1])
102 def numeric(orig
, invalid
=float('inf')):
106 string
= unicode(orig
)
111 return (invalid
, orig
)
114 while string
[:1] == u
"-" or string
[:1] == u
"+":
115 if string
[:1] == u
"-":
119 if not string
[:1].isnumeric():
120 return (invalid
, orig
)
122 string
= normalize_punc(string
)
124 # Early out if possible.
126 return (float(string
) * mult
, orig
)
130 # Otherwise we need to do this the hard way.
131 def _numeric(string
):
134 v
= unicodedata
.numeric(c
)
141 whole
, frac
= string
.split(".")
142 whole
= _numeric(whole
)
143 frac
= _numeric(frac
) / (10.0 ** len(frac
))
144 return (mult
* (whole
+ frac
), orig
)
146 return (mult
* _numeric(string
), orig
)
148 def normalize_punc(string
):
149 string
= unicode(string
.strip(u
",.'"))
150 string
= filter(lambda u
: u
.isnumeric() or u
in u
",.'", string
)
151 commas
= string
.count(u
",")
152 stops
= string
.count(u
".")
153 quotes
= string
.count(u
"'")
155 # If anything occurs more than once, it's a separator.
157 string
= string
.replace(u
",", u
"")
160 string
= string
.replace(u
".", u
"")
163 string
= string
.replace(u
"'", u
"")
166 def normalize_two(a
, b
, string
):
167 # One of each - assume the first is grouping, second is point.
168 a_idx
= string
.rindex(a
)
169 b_idx
= string
.rindex(b
)
171 string
= string
.replace(b
, u
"").replace(a
, u
".")
173 string
= string
.replace(a
, u
"").replace(b
, u
".")
176 if commas
and stops
and quotes
:
177 # If all three, assume the middle is the decimal point.
182 # Not really valid, so do whatever we want...
185 comma_idx
= string
.index(u
",")
186 stops_idx
= string
.index(u
".")
187 quotes_idx
= string
.index(u
"'")
188 if (comma_idx
< stops_idx
< quotes_idx
189 or quotes_idx
< stops_idx
< comma_idx
):
190 string
= string
.replace(u
",", u
"").replace(u
"'", u
"")
191 elif (comma_idx
< quotes_idx
< stops_idx
192 or stops_idx
< quotes_idx
< comma_idx
):
193 string
= string
.replace(
198 string
= string
.replace(
203 elif stops
and quotes
:
204 string
= normalize_two(u
".", u
"'", string
)
206 elif commas
and quotes
:
207 string
= normalize_two(u
",", u
"'", string
)
209 elif commas
and stops
:
210 string
= normalize_two(u
",", u
".", string
)
213 if string
[-4:-3] == u
"," and len(string
) <= 7:
214 # Single comma as a thousands separator.
215 string
= string
.replace(u
",", u
"")
217 # Single comma, not thousands - probably a decimal point.
218 string
= string
.replace(u
",", u
".")
221 # Single quote, probably MM'SS", equivalent to a decimal point.
222 string
= string
.replace(u
"'", u
".")
224 elif stops
and string
[-4:] == ".000":
225 # Single stop, but no decimal - probably grouping.
226 string
= string
.replace(u
".", u
"")