[pyicu-dev] Subclassing Transliterators

Christoph Burgmer cburgmer at ira.uka.de
Sun Mar 14 10:21:39 PDT 2010


Am Samstag, 13. März 2010 schrieb Christoph Burgmer:
[...]
> Here is an example:
> >>> import PyICU
> >>> class VowelSubst(PyICU.Transliterator):
> 
> ...     def __init__(self, char='i'):
> ...         PyICU.Transliterator.__init__(self, 'My_ID')
> ...         self.char = char
> ...     def handleTransliterate(self, text, pos, incremental):
> ...         for i in range(pos.start, pos.limit):
> ...             if text[i] in u"aeiouüöä":
> ...                 text[i:i+1] = self.char
> ...         pos.start = pos.limit
> ...
> 
> >>> m = VowelSubst()
> >>> m.transliterate(u"Drei Chinesen mit dem Kontrabass")
> 
> u'Drii Chinisin mit dim Kintribiss'

I integrated this example into a test case.

> Filters are currently not supported - another class needs to be exposed.

For now I make the class return a NotImplementedError if given a second 
argument, which would be the UnicodeFilter instance. It seems this feature is 
not generally needed.

>  Also the method to register own classes is still missing.

Added.

> I'd  like to add Exception handling to handleTransliterate() but am unclear
> how to do this in a clean way. Currently Exceptions raised are not
>  transported through the C layer.

That turned out pretty easy, just a call to PyErr_Occurred().

I think the current implementation can be used already. If any additional 
feature is needed (e.g. UnicodeFilter) it could be exposed later.

-Christoph


More information about the pyicu-dev mailing list