From cd1898ec65314523e115636837afe9a82778ee71 Mon Sep 17 00:00:00 2001 From: Alexandre Courbot Date: Tue, 10 Nov 2009 10:21:29 +0900 Subject: [PATCH] Better calculation of multi-part components ancestor --- kanjivg.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/kanjivg.py b/kanjivg.py index c3c93af97..52487a0a4 100644 --- a/kanjivg.py +++ b/kanjivg.py @@ -34,8 +34,7 @@ Under the following conditions: See http://creativecommons.org/licenses/by-sa/3.0/ for more details.""" -def isKanji(c): - v = realord(c) +def isKanji(v): return (v >= 0x4E00 and v <= 0x9FC3) or (v >= 0x3400 and v <= 0x4DBF) or (v >= 0xF900 and v <= 0xFAD9) or (v >= 0x2E80 and v <= 0x2EFF) or (v >= 0x20000 and v <= 0x2A6DF) # Returns the unicode of a character in a unicode string, taking surrogate pairs into account @@ -252,6 +251,17 @@ class StructuredKanji: stk = [] self.__buildStructure(kanji.root, stk, None) + def __mostCommonAncestor(self, np, npp): + # Update the parent to the most common parent of all parts + npSave = np + if np != None: + while np != npp: + np = np.parent + if np == None: + npp = npp.parent + np = npSave + return np + def __buildStructure(self, group, stk, parent): # Find the component if it exists already, or create it as needed # Number exists and part is > 1, we must find a component which number matches. @@ -260,6 +270,7 @@ class StructuredKanji: for component in self.components: if component.element == group.element and component.number == group.number: newParent = component + component.parent = self.__mostCommonAncestor(component.parent, parent) break # Should never happen if not newParent: raise Exception("Unable to find component!") @@ -268,6 +279,7 @@ class StructuredKanji: for component in self.components: if component.element == group.element: newParent = component + component.parent = self.__mostCommonAncestor(component.parent, parent) break if not newParent: raise Exception("Unable to find component!") # Either a single part component or a first part - we need to create the component