def getVariantSection(self, inputString): variantEntries = self._dictionary.getVariantsForHeadword(inputString) variants = set([e.Headword for e in variantEntries if e.Headword != inputString]) variantLinks = [] for variant in variants: if variant == inputString: # e.g. 台 is listed as it's on variant, Unihan's policy continue variantLinks.append('<span class="character">' \ + '<a class="character" href="#lookup(%s)">%s</a>' \ % (util.encodeBase64(variant), variant) \ + '</span>') if not variantLinks: return '' else: return '<div class="variantSection">'\ + '<span class="meta">%s</span> ' \ % ngettext("See variant:", "See variants:", len(variants)) \ + ', '.join(variantLinks) \ + '</div>'
def getSimilarsSection(self, inputString): """Returns a section of headwords with similar shape.""" similarEntries = self._dictionary.getSimilarsForHeadword(inputString, orderBy=['Reading']) #orderBy=['Reading', 'Headword']) # TODO doesn't work for CEDICT similars = [e.Headword for e in similarEntries if e.Headword != inputString] similarLinks = [] for similar in similars: similarLinks.append('<span class="character">' \ + '<a class="character" href="#lookup(%s)">%s</a>' \ % (util.encodeBase64(similar), similar) \ + '</span>') if not similarLinks: return '' else: return '<div class="similarSection">'\ + '<span class="meta">%s</span> ' \ % ngettext("See similar headword:", "See similar headword:", len(similarLinks)) \ + ', '.join(similarLinks) \ + '</div>'
def getCharacterForRadical(self, radicalIndex, includeAllComponents=False): """Gets a list of characters classified under the given radical.""" # group by residual stroke count characterGroups = self.charDB.getCharacterListForKangxiRadicalIndex( radicalIndex, includeAllComponents=includeAllComponents) htmlList = [] # show main radical form htmlList.append('<h3>%s</h3>' \ % (gettext('Radical %(radical_index)d') \ % {'radical_index': radicalIndex})) charLinks = [] for strokeCount in sorted(characterGroups['radical'].keys()): for char in sorted(characterGroups['radical'][strokeCount]): charLinks.append('<span class="character">' \ + '<a class="character" href="#lookup(%s)">%s</a>' \ % (util.encodeBase64(char), char) \ + '</span>') htmlList.append(' '.join(charLinks)) radicalEntryDict = self.charDB.getRadicalDictionaryEntries() _, meaning = radicalEntryDict.get(radicalIndex, (None, None)) if meaning: htmlList.append(' <span class="translation">%s</span>' % meaning) radicalForms = self.charDB.kangxiRadicalForms _, strokeCount, _, _ = radicalForms[radicalIndex] if strokeCount: htmlList.append(' <span class="strokecount">(%s)</span>' \ % (ngettext('%(strokes)d stroke', '%(strokes)d strokes', strokeCount) % {'strokes': strokeCount})) htmlList.append('<h3>%s</h3>' % gettext('Characters')) # list sorted by residual stroke count if not characterGroups[None]: htmlList.append('<span class="meta">%s</span>' \ % gettext('no results found for the selected character domain')) else: htmlList.append('<table class="searchResult">') for strokeCount in sorted(characterGroups[None].keys()): if type(strokeCount) not in (type(0), type(0L)): # sort out non stroke count groups continue htmlList.append('<tr>' \ + '<th class="strokeCount">+%s</th><td>' % strokeCount) charLinks = [] for char in sorted(characterGroups[None][strokeCount]): charLinks.append('<span class="character">' \ + '<a class="character" href="#lookup(%s)">%s</a>' \ % (util.encodeBase64(char), char) \ + '</span>') htmlList.append(' '.join(charLinks)) htmlList.append('</td></tr>') # Add characters without stroke count information if None in characterGroups[None]: htmlList.append('<tr>' \ + '<th class="strokeCount">%s</th><td>' % gettext('Unknown')) charLinks = [] for char in sorted(characterGroups[None][None]): charLinks.append('<span class="character">' \ + '<a class="character" href="#lookup(%s)">%s</a>' \ % (util.encodeBase64(char), char) \ + '</span>') htmlList.append(' '.join(charLinks)) htmlList.append('</td></tr>') htmlList.append('</table>') return "\n".join(htmlList)
def getRadicalTable(self): """Gets a table of Kangxi radicals, sorted by radical index.""" htmlList = [] htmlList.append('<table id="radicaltable" class="radical">') lastStrokeCount = None radicalForms = self.charDB.kangxiRadicalForms radicalEntryDict = self.charDB.getRadicalDictionaryEntries() for radicalIdx in range(1, 215): mainForm, strokeCount, variants, _ = radicalForms[radicalIdx] if lastStrokeCount != strokeCount: lastStrokeCount = strokeCount htmlList.append( '<tr class="strokeCount" id="strokecount%d">' \ % strokeCount \ + '<td colspan="3"><h2>%s</h2></td>' \ % (ngettext('%(strokes)d stroke', '%(strokes)d strokes', strokeCount) % {'strokes': strokeCount}) + '</tr>') htmlList.append( '<tr class="radicalEntry" id="radical%d">' \ % radicalIdx \ + '<td class="radicalIndex">%s</td>' % radicalIdx) if variants: htmlList.append('<td class="radical">' \ + '<a class="character" href="#radical(%s)">' % radicalIdx \ + '<span class="radical">%s</span><br/>' % mainForm \ + '<span class="radicalVariant">%s</span>' \ % ''.join(variants) \ + '</a></td>') else: htmlList.append('<td class="radical">' \ + '<a class="character" href="#radical(%s)">' % radicalIdx \ + '<span class="radical">%s</span><br/>' % mainForm \ + '</a></td>') if radicalIdx in radicalEntryDict: _, meaning = radicalEntryDict[radicalIdx] # TODO remove reading htmlList.append( '<td class="translation">%s</td>' % meaning) else: htmlList.append('<td class="translation"></td>') htmlList.append('</tr>') htmlList.append('</table>') # TODO radicalEntries = {} for radicalIdx in range(1, 215): if radicalIdx in radicalEntryDict: _, meaning = radicalEntryDict[radicalIdx] # TODO remove reading else: meaning = None _, _, _, representatives = radicalForms[radicalIdx] radicalEntries[radicalIdx] = (representatives, meaning) return "\n".join(htmlList), radicalEntries