X-Git-Url: https://git.korewanetadesu.com/?p=python-collate.git;a=blobdiff_plain;f=collate%2F_abcollator.py;h=622766d3b5056c27ca9b9a98ce4609961dd11c1d;hp=02cb733c79d889462a03e4680ece539e664cb696;hb=7644110ce07ec8a78003ee7db9dcdfe5cbca3854;hpb=d46d035bdc1ef7276af7c41880034226d0cdfbfc diff --git a/collate/_abcollator.py b/collate/_abcollator.py index 02cb733..622766d 100644 --- a/collate/_abcollator.py +++ b/collate/_abcollator.py @@ -1,23 +1,57 @@ -import collate._strings +"""Abstract base collator.""" + +import collate.strings class Collator(object): + """Abstract base class for Collators. + + Attributes: + locale - the collator follows rules for this locale + encoding - assumed string encoding + """ + + locale = "C" + encoding = "ascii" + + def __init__(self, locale=None, encoding=None): + pass + def cmp(self, string1, string2): """Return negative if a < b, zero if a == b, positive if a > b.""" return cmp(self.key(string1), self.key(string2)) + def key(self, string): + """Return a good sorting key for the string. + + The sort key should be considered an opaque value which is + only meaningful when compared to other sort keys from the same + collator. + """ + if isinstance(string, str): + string = string.decode(self.encoding, 'replace') + return string + def words(self, string): - """Split the string into separate words. + """Split the string along word boundries.""" + if isinstance(string, str): + string = string.decode(self.encoding, 'replace') + return string.split() - This split is done using Unicode's definition of whitespace. + def sortemekey(self, string): + """Return a key based on sortemes of a string. + + A sorteme, by analogy with grapheme/morpheme/etc. is an atom + of sort information. This is larger than a word boundry but + smaller than a sentence boundry; roughly, a sorteme boundry + occurs between letters and numbers, between numbers and + numbers if 'too much' punctuation exists in between, between + lines. """ - return string.split() + if isinstance(string, str): + string = string.decode(self.encoding, 'replace') - def sortemes(self, string): - return collate._strings.alnumsplit(string) + # Shove the sortkeyed original string on the end to resolve + # ties intelligently. + return (collate.strings.sortemes(string, self.key), + self.key(string)) - def sortemekey(self, string, invalid=float('inf')): - words = [] - for sorteme in self.sortemes(string): - num, alpha = collate._strings.numeric(sorteme, invalid) - words.append((num, self.key(alpha))) - return words