Ejemplo n.º 1
0
    def renderJapanese(self, text, exact=True, feature=''):  #
        """
    @param  text  unicode  Japanese phrase
    @param  exact  bool  wheher do exact match  exact match faster but return less phrases
    @param* feature  unicode  MeCab feature
    @return  unicode not None  html
    """
        #if not d.japaneseLookupEnabled:
        #  return EMPTY_HTML
        #google = proxy.manager().google_search
        #feature = GrimoireBean.instance.lookupFeature(text)
        try:
            d = self.__d
            f = None
            if feature:
                f = mecabman.renderfeature(feature)
            else:
                roleName = mecabdef.role_name(text)
                if roleName:
                    f = ','.join((
                        text,
                        mecabdef.role_name_en(text)
                        or '',  # or part should never happen
                        roleName,
                    ))

                    if d.userLanguage == 'ko':
                        f += ',' + (mecabdef.role_name_ko(text) or ''
                                    )  # or part should never happen

            #with SkProfiler("en-vi"): # 1/8/2014: take 7 seconds for OVDP
            ret = rc.jinja_template('html/shiori').render({
                'language':
                'ja',
                'text':
                text,
                'feature':
                f,
                'kanji':
                d.renderHanzi(text, html=True),
                'tuples':
                d.lookupDB(text, exact=exact, feature=feature),
                'eb_strings':
                d.lookupEB(text, feature=feature
                           ),  # exact not used, since it is already very fast
                #'google': google,
                #'locale': d.locale,
            })
        except UnicodeDecodeError, e:
            dwarn(e)
            ret = rc.jinja_template('html/shiori').render({
                'text': text,
                'feature': feature,
                #'google': google,
                #'locale': d.locale,
            })
Ejemplo n.º 2
0
 def renderKorean(self, text):
     """
 @param  text  Korean phrase
 @return  unicode not None  html
 """
     l = [text]
     romaja = convutil.toroman(text, 'ko')
     if romaja:
         l.append(romaja)
     hanja = convutil.hangul2hanja(text)
     if hanja and hanja != text:
         l.append(hanja)
     feature = ', '.join(l)
     return rc.jinja_template('html/shiori').render({
         'language': 'ko',
         'text': text,
         'feature': feature,
     })
Ejemplo n.º 3
0
 def errorPageExtension(self, option, output):  # override
     if not option or not output:
         return False
     dprint("enter: error = %s, message = %s" %
            (option.error, option.errorString))
     output.encoding = "UTF-8"  # force UTF-8
     #output.baseUrl = option.url # local url
     #output.contentType = "text/html" # already automaticly detected
     output.content = rc.jinja_template('error').render({
         'code':
         self.extensionErrorCode(option.error),
         'message':
         option.errorString,
         'url':
         option.url.toString(),
         'tr':
         tr_,
         'rc':
         rc,
     }).encode('utf8', 'ignore')
     return True
Ejemplo n.º 4
0
 def _renderJson(type, id=0, source='', target='', **kwargs):
   """
   @param  type  str
   @param* id  long
   @param* source  unicode
   @param* target  unicode
   @return  unicode or None
   """
   if type == 'term' and id:
     td = dataman.manager().queryTermData(id)
     if td:
       return rc.jinja_template('html/term').render({
         'td': td,
         'userName': td.userName,
         'updateUserName': td.updateUserName,
         'source': source,
         'target': target,
         'i18n': i18n,
         'tr': i18n.autotr_,
       })
   elif type == 'tip':
     l = filter(bool, (source, target))
     return ': '.join(l)
Ejemplo n.º 5
0
    def _iterRenderAlignment(self,
                             text,
                             language,
                             align,
                             charPerLine=100,
                             rubySize=10,
                             colorize=False,
                             center=True):
        """
    @param  text  unicode
    @param  language  unicode
    @param  align  [(unicode source, unicode target)]
    @param* charPerLine  int  maximum number of characters per line
    @param* rubySize  float
    @param* colorsize  bool
    @param* center  bool
    @yield  unicode  HTML table
    """
        render = rc.jinja_template('html/alignment').render

        i = j = 0

        line = []
        lineCount = 0  # int  estimated line width, assume ruby has half width
        color = None
        #groupColor = '#ff8' # yellow

        COLORS = (
            'rgba(255,0,0,40)',  # red
            'rgba(0,255,0,40)',  # green
            'rgba(255,255,0,40)',  # yellow
            "rgba(0,0,255,40)",  # blue
        )

        PADDING_FACTOR = 0.1  # 1/10
        #LATIN_RUBY_WIDTH = 0.33 # = 2/6
        #KANJI_RUBY_WIDTH = 0.55 # = 1/2
        # ruby size / surface size

        # http://www.rapidtables.com/web/color/silver-color.htm
        #rubyColor = None
        #rubyColor = None if colorize else 'lightgray'
        rubyColor = None if colorize else 'silver'
        #rubyColor = None if colorize else 'darkgray'
        #rubyColor = None if colorize else 'gray'

        roundRubySize = int(round(rubySize)) or 1
        #paddingSize = int(round(rubySize * PADDING_FACTOR)) or 1 if invertRuby else 0
        paddingSize = int(round(rubySize * PADDING_FACTOR))

        #rubyWidth = LATIN_RUBY_WIDTH if not romaji else KANJI_RUBY_WIDTH
        rubyWidth = 0.8  # around 4/6, make it larger to be safer

        alignIndex = 0
        #for paragraph in text.split('\n'):
        # No longer split paragraph
        for group, (surface, ruby, alignIndex) in enumerate(
                self._iterParseAlignment(text, language, align, alignIndex)):
            if group == 0 and alignIndex >= len(align):  # aligning failed
                yield text
                line = []
                break

            if ruby and len(ruby) == 1 and textutil.ispunct(
                    ruby):  # skip punctuation
                ruby = None

            if colorize:
                color = COLORS[group %
                               len(COLORS)]  #if group is not None else None
                #if ch == cabochadef.TYPE_KANJI:
                #  i += 1
                #  color = 'rgba(255,0,0,40)' if i % 2 else 'rgba(255,255,0,40)' # red or yellow
                #elif ch == cabochadef.TYPE_RUBY:
                #  j += 1
                #  #color = "rgba(0,255,0,40)" if j % 2 else "rgba(255,0,255,40)" # green or magenta
                #  color = "rgba(0,255,0,40)" if j % 2 else "rgba(0,0,255,40)" # green or blue
                #else:
                #  color = None

            width = max(len(textutil.remove_html_tags(surface)),
                        len(ruby) * rubyWidth if ruby else 0)
            if width + lineCount <= charPerLine:
                pass
            elif line:
                yield render({
                    'tuples': line,
                    'rubySize': roundRubySize,
                    'rubyColor': rubyColor,
                    'paddingSize': paddingSize,
                    'center': center,
                    #'groupColor': groupColor,
                })
                line = []
                lineCount = 0
            line.append((surface, ruby, color))
            lineCount += width

        if line:
            yield render({
                'tuples': line,
                'rubySize': roundRubySize,
                'rubyColor': rubyColor,
                'paddingSize': paddingSize,
                'center': center,
                #'groupColor': groupColor,
            })
Ejemplo n.º 6
0
def _iterrendertable(text,
                     rubyType,
                     rubyKana=False,
                     features=None,
                     charPerLine=100,
                     rubySize=10,
                     colorize=False,
                     rubyInverted=False,
                     annotated=False,
                     highlight=False,
                     center=True,
                     fmt=mecabformat.UNIDIC_FORMATTER):
    """
  @param  text  unicode
  @param  rubyType  str
  @param* rubyKana  bool
  @param* charPerLine  int  maximum number of characters per line
  @param* rubySize  float
  @param* rubyInverted  bool
  @param* colorsize  bool
  @param* annotated  bool
  @param* highlight  bool
  @param* center  bool
  @param* features  {unicode surface:(unicode feature, fmt)} or None
  @param* fmt  mecabfmt
  @yield  unicode  HTML table
  """
    q = manager().parse(text, rubyType, rubyKana=rubyKana)
    if q:
        render = rc.jinja_template('html/furigana').render

        i = j = 0
        line = []
        lineCount = 0  # int  estimated line width, assume ruby has half width
        hasfeature = features is not None

        PADDING_FACTOR = 0.3
        LATIN_YOMI_WIDTH = 0.33  # = 2/6
        KANJI_YOMI_WIDTH = 0.55  # = 1/2

        ANNOT_ZOOM = 0.9
        ANNOT_WIDTH = LATIN_YOMI_WIDTH * ANNOT_ZOOM
        # yomi size / surface size

        #if rubyInverted:
        #  yomiWidth = 1 if mecabdef.rb_is_wide(rubyType) else 0.8
        #else:
        yomiWidth = KANJI_YOMI_WIDTH if mecabdef.rb_is_wide(
            rubyType) else LATIN_YOMI_WIDTH

        roundRubySize = int(round(rubySize)) or 1
        annotSize = int(round(rubySize * ANNOT_ZOOM)) or 1
        paddingSize = int(round(rubySize *
                                PADDING_FACTOR)) or 1 if rubyInverted else 0

        role = None
        roleExists = False
        rubyExists = False
        color = lastColor = None

        ANNOT_FONT = 'Tahoma'

        for surface, yomi, feature, surface_type in q:
            #colorChanged = False

            if hasfeature:
                features[surface] = feature

            if annotated or not colorize and highlight:
                role = fmt.getrole(feature)
                if role == mecabdef.ROLE_PUNCT:
                    role = None

            if colorize:
                if surface_type == mecabdef.SURFACE_KANJI:
                    i += 1
                    color = 'rgba(255,0,0,40)' if i % 2 else 'rgba(255,255,0,40)'  # red or yellow
                elif surface_type == mecabdef.SURFACE_KANA:
                    j += 1
                    #color = 'rgba(0,255,0,40)' if j % 2 else 'rgba(255,0,255,40)' # green or magenta
                    color = 'rgba(0,255,0,40)' if j % 2 else 'rgba(0,0,255,40)'  # green or blue
                else:
                    color = None
            elif highlight:
                if lastColor and role in (mecabdef.ROLE_PART,
                                          mecabdef.ROLE_AUX,
                                          mecabdef.ROLE_SUFFIX,
                                          mecabdef.ROLE_MARK):
                    color = lastColor
                else:
                    lastColor = color = ROLE_COLORS.get(role)
                    #if lastColor != color:
                    #  lastColor = color
                    #  colorChanged = True

            width = max(
                len(surface),
                (len(yomi) * yomiWidth + (1 if rubyInverted else 0))
                if yomi else 0,  # +1 when inverted for paddingSize
                len(surface) * yomiWidth +
                1 if rubyInverted else 0,  # in case yomi is empty
                (len(role) + 1) * ANNOT_WIDTH if annotated and role else 0,
            )
            if width + lineCount <= charPerLine:
                pass
            elif line:
                yield render({
                    'tuples': line,
                    'rubySize': roundRubySize,
                    'paddingSize': paddingSize,
                    'center': center,
                    'annotSize': annotSize,
                    'annotExists': annotated and roleExists,
                    'rubyExists': rubyExists,
                    'annotFont': ANNOT_FONT,
                })
                line = []
                lineCount = 0
                roleExists = False
                rubyExists = False
            if rubyInverted and yomi:
                #if surface:
                #  surface = wide2thin(surface)
                if mecabdef.rb_has_case(
                        rubyType) and len(yomi) > 2:  #or colorChanged)
                    yomi = yomi.title()
                t = yomi, surface, (role if annotated else None), color
            else:
                t = surface, yomi, (role if annotated else None), color
            if role:
                roleExists = True
            if yomi:
                rubyExists = True
            line.append(t)
            lineCount += width
        if line:
            yield render({
                'tuples': line,
                'center': center,
                'rubySize': roundRubySize,
                'paddingSize': paddingSize,
                'annotSize': annotSize,
                'annotExists': annotated and roleExists,
                'rubyExists': rubyExists,
                'annotFont': ANNOT_FONT,
            })
Ejemplo n.º 7
0
    def renderJapanese(self, text, exact=True, feature=''):  #
        """
    @param  text  unicode  Japanese phrase
    @param  exact  bool  wheher do exact match  exact match faster but return less phrases
    @param* feature  unicode  MeCab feature
    @return  unicode not None  html
    """
        #if not d.japaneseLookupEnabled:
        #  return EMPTY_HTML
        #google = proxy.manager().google_search
        #feature = GrimoireBean.instance.lookupFeature(text)
        try:
            ss = settings.global_()
            d = self.__d
            f = None
            if feature:
                f = mecabman.renderfeature(feature)
            else:
                roleName = mecabdef.role_name(text)
                if roleName:
                    f = ','.join((
                        text,
                        mecabdef.role_name_en(text)
                        or '',  # or part should never happen
                        roleName,
                    ))

                    if d.userLanguage == 'ko':
                        f += ',' + (mecabdef.role_name_ko(text) or ''
                                    )  # or part should never happen
            JMDictEnabled = True
            eb_strings = d.lookupEB(text, feature=feature)
            if ss.isJMDictRuEnabledJM():
                JMDictEnabled = False

            # Init jisho dict
            jisho = []
            if ss.isJishoOrgEnabled() and netman.manager().isOnline():
                jishomass = netman.manager().jisho_api(text)
                for it in jishomass:
                    word = ''
                    reading = ''
                    if 'word' in it['japanese'][0].keys():
                        word = it['japanese'][0]['word']
                    if 'reading' in it['japanese'][0].keys():
                        reading = it['japanese'][0]['reading']

                    jptext = ''
                    for jp in it['japanese']:
                        if 'word' in jp.keys():
                            jptext = jptext + jp['word']
                        if 'reading' in jp.keys():
                            jptext = jptext + "(" + jp['reading'] + ") "

                    alldefinitions = []
                    i = 1
                    for sens in it['senses']:
                        definitions = ''
                        if 'english_definitions' in sens.keys():
                            if len(sens['english_definitions']) > 0:
                                definitions = str(i) + ") " + ', '.join(
                                    sens['english_definitions'])
                            if len(sens['parts_of_speech']) > 0:
                                definitions = definitions + " (" + ', '.join(
                                    sens['parts_of_speech']) + ")"
                        if definitions != "":
                            alldefinitions.append(definitions)
                        i = i + 1
                    jisho.append({
                        'word': word,
                        'reading': reading,
                        'jptext': jptext,
                        'alldefinitions': alldefinitions
                    })

            # Init rus byars dict
            byars = ''
            if ss.isByarsEnabled() and netman.manager().isOnline():
                byars = netman.manager().byars_api(text)
                # Убираем лишнее
                byars = re.sub(r'<p class="dicname">Яркси</p>', '', byars)
                byars = re.sub(r'<p class="dicname">БЯРС</p>', '', byars)
                byars = re.sub(r'onclick=".*?"', '', byars)
                byars = re.sub(r'href=".*?"', 'href="#"', byars)
                byars = re.sub(r'<span class="id">〔.*?〕<\/span>', '', byars)
                byars = re.sub(r'<object .*?<\/object>', '', byars)
                byars = re.sub(
                    r'<div class=\"akusentoblock\">.*<\/ul>\s*<\/div>',
                    '',
                    byars,
                    flags=re.M | re.S)

            #with SkProfiler("en-vi"): # 1/8/2014: take 7 seconds for OVDP
            ret = rc.jinja_template('html/shiori').render({
                'language':
                'ja',
                'JMDictEnabled':
                JMDictEnabled,
                'text':
                text,
                'feature':
                f,
                'jisho':
                jisho,
                'byars':
                byars,
                'kanji':
                d.renderHanzi(text, html=True),
                'tuples':
                d.lookupDB(text, exact=exact, feature=feature),
                'eb_strings':
                eb_strings,  # exact not used, since it is already very fast
                #'google': google,
                #'locale': d.locale,
            })
        except UnicodeDecodeError, e:
            dwarn(e)
            ret = rc.jinja_template('html/shiori').render({
                'text': text,
                'feature': feature,
                #'google': google,
                #'locale': d.locale,
            })
Ejemplo n.º 8
0
def _iterrendertable(text,
                     language,
                     charPerLine=100,
                     rubySize=10,
                     colorize=False,
                     center=True,
                     invertRuby=False,
                     **kwargs):
    """
  @param  text  unicode
  @param  language  unicode
  @param* charPerLine  int  maximum number of characters per line
  @param* rubySize  float
  @param* colorsize  bool
  @param* center  bool
  @param* invertRuby  bool
  @param* features  {unicode surface:(unicode feature, fmt)} or None
  @param* kwargs  passed to Korean
  @yield  unicode  HTML table
  """
    render = rc.jinja_template('html/furigana').render

    i = j = 0

    line = []
    lineCount = 0  # int  estimated line width, assume ruby has half width
    color = None
    groupColor = None if language.startswith('zh') else '#ff8'  # yellow

    COLORS = (
        'rgba(255,0,0,40)',  # red
        'rgba(0,255,0,40)',  # green
        'rgba(255,255,0,40)',  # yellow
        "rgba(0,0,255,40)",  # blue
    )

    PADDING_FACTOR = 0.1
    LATIN_YOMI_WIDTH = 0.45  # 2/6 ~ 1/2 # tuned for Vietnamese
    KANJI_YOMI_WIDTH = 0.55  # = 1/2 + 0.05
    # yomi size / surface size

    romajiRuby = language.startswith('zh')  # use romaji width only for pinyin

    roundRubySize = int(round(rubySize)) or 1
    paddingSize = int(round(
        rubySize * PADDING_FACTOR)) or 1  # at least 1 pixel

    yomiWidth = LATIN_YOMI_WIDTH if romajiRuby else KANJI_YOMI_WIDTH
    if invertRuby:
        paddingSize *= 2  # increase padding size when invert
        yomiWidth *= 1.2  # increase yomi font size when invert

    #if paddingSize and language == 'ko' and not kwargs.get('romajaRubyEnabled'):
    #  paddingSize = 0
    rubyExists = False

    RUBY_FONT = 'Tahoma'

    for paragraph in text.split('\n'):
        for surface, yomi, group in _iterparseruby(paragraph, language,
                                                   **kwargs):

            if colorize:
                color = COLORS[group %
                               len(COLORS)] if group is not None else None
                #if ch == cabochadef.TYPE_KANJI:
                #  i += 1
                #  color = 'rgba(255,0,0,40)' if i % 2 else 'rgba(255,255,0,40)' # red or yellow
                #elif ch == cabochadef.TYPE_RUBY:
                #  j += 1
                #  #color = "rgba(0,255,0,40)" if j % 2 else "rgba(255,0,255,40)" # green or magenta
                #  color = "rgba(0,255,0,40)" if j % 2 else "rgba(0,0,255,40)" # green or blue
                #else:
                #  color = None

            width = max(
                len(surface),
                (len(yomi) * yomiWidth +
                 (1 if invertRuby else 0)) if yomi else 0,
            )
            if width + lineCount <= charPerLine:
                pass
            elif line:
                yield render({
                    'tuples': line,
                    'rubySize': roundRubySize,
                    'paddingSize': paddingSize,
                    'center': center,
                    'groupColor': groupColor,
                    'rubyExists': rubyExists,
                    'rubyFont': RUBY_FONT,
                })
                line = []
                lineCount = 0
                rubyExists = False
            if invertRuby and yomi:
                #if surface:
                #  surface = wide2thin(surface)
                #if romaji and len(yomi) > 2:
                #  yomi = yomi.title()
                t = yomi, surface, color, group
            else:
                t = surface, yomi, color, group
            line.append(t)
            lineCount += width
            if yomi:
                rubyExists = True

    if line:
        yield render({
            'tuples': line,
            'rubySize': roundRubySize,
            'paddingSize': paddingSize,
            'center': center,
            'groupColor': groupColor,
            'rubyExists': rubyExists,
            'rubyFont': RUBY_FONT,
        })
        rubyExists = False