示例#1
0
文件: opencc.py 项目: Rougnt/VNR-Core
def makeconverter(fr, to):
    """
  @param  fr  str
  @param  to  str
  @return  SimpleChineseConverter or None
  """
    from pycc import SimpleChineseConverter
    ret = SimpleChineseConverter()
    reverse = False
    key = OPENCC_LANG_DELIM.join((fr, to))
    path = OPENCC_DIC_PATHS.get(key)
    if not path:
        key = OPENCC_LANG_DELIM.join((to, fr))
        path = OPENCC_DIC_PATHS.get(key)
        reverse = True
    if path:
        #print path, reverse
        if os.path.exists(path):
            #from sakurakit.skprof import SkProfiler
            #with SkProfiler(): # 10/19/2014: 0.006 seconds for zhs2zht
            ret.addFile(path, reverse)
            return ret
        else:
            from sakurakit.skdebug import derror
            derror("dic path does not exist:", path)
示例#2
0
def _iteralign(data, source, reverse=True):
    """
  @param  data  list  json
  @param  source  unicode  input text
  @param* reverse  bool  by default, align is sorted based on source,
                         instead of translation like Google/Naver/Baidu
  @yield  (unicode surface, unicode translation)
  """
    texts = filter(bool, source.split('\n'))
    if texts and len(texts) == len(data):
        try:
            for i, l in enumerate(data):
                align = l['Alignment']  # this is empty string if not supported
                if align:  # str
                    trans = l['TranslatedText']
                    surf = texts[i]
                    it = _parsealign(align)
                    if reverse:
                        it = sorted(it, key=lambda it: it[2])
                    for ss, se, ts, te in it:
                        s = surf[ss:se + 1]
                        t = trans[ts:te + 1]
                        yield s, t
        except Exception, e:
            derror(e)
示例#3
0
    def requestPixmap(self, path, rsize, size):
        """@reimp @public
    @param[in]  providerId  unicode  unused
    @param[out]  rsize  QSize
    @param[in]  size  QSize
    @return  QPixmap not None

    virtual QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
    """

        ret = QPixmap(QUrl(path).toLocalFile())
        if ret.isNull():
            derror("failed to load image: '%s'" % path)
        elif size != ret.size() and not size.isEmpty() and not ret.size(
        ).isEmpty():
            if ret.width() * size.height() > ret.height() * size.width():
                ret = ret.scaledToHeight(min(800, size.height()),
                                         Qt.SmoothTransformation)
            else:
                w = 1000 if ret.width() > ret.height() else 600
                ret = ret.scaledToWidth(min(w, size.width()),
                                        Qt.SmoothTransformation)
        #elif size != ret.size():
        #  ret = (ret.scaled(size, Qt.KeepAspectRatio, Qt.SmoothTransformation) if not size.isEmpty() else
        #         ret.scaledToWidth(size.width(), Qt.SmoothTransformation) if size.width() > 0 else
        #         ret.scaledToHeight(size.height(), Qt.SmoothTransformation) if size.height() > 0 else
        #         ret)
        rsize.setWidth(ret.width())
        rsize.setHeight(ret.height())
        return ret
示例#4
0
def _iteralign(data, source, reverse=True):
  """
  @param  data  dict  json
  @param  source  unicode  the original input text
  @param* reverse  bool  sort align based on source if false
  @yield  (unicode surface, unicode translation)
  """
  # Reverse is not enabled since there are cases one word mapping to two translation.
  # ex. 悠真くんを攻略すれば210円か。
  # ば => 如果...的话
  equiv = data.get('equiv')
  if equiv:
    try:
      slist = equiv['org']
      tlist = equiv['txn']
      trans = data['text']

      m = OrderedDict() # {int index, [unicode s, unicode t])  mapping from s to t

      if not reverse: # order in source by default
        for i, start, size in slist:
          s = source[start:start + size]
          l = m.get(i)
          if l:
            l[0] = s
          else:
            m[i] = [s, ''] # use list instead of tuple so that it is modifiable

        for i, start, size in tlist:
          t = trans[start:start + size]
          l = m.get(i)
          if l:
            l[1] = t
          else:
            m[i] = ['', t]

      else: # order in translation instead
        tlist.sort(key=lambda it:it[1])

        for i, start, size in tlist:
          t = trans[start:start + size]
          l = m.get(i)
          if l:
            l[1] = t
          else:
            m[i] = ['', t]

        for i, start, size in slist:
          s = source[start:start + size]
          l = m.get(i)
          if l:
            l[0] = s
          else:
            m[i] = [s, ''] # use list instead of tuple so that it is modifiable

      #for k in sorted(m.iterkeys()):
      for s,t in m.itervalues():
        yield s, t
    except Exception, e:
      derror(e)
示例#5
0
def translate(text, to='zhs', fr='ja'):
    try:
        query = text
        salt = random.randint(32768, 65536)
        sign = appid + query + str(salt) + secretKey
        sign = hashlib.md5(sign.encode('utf-8')).hexdigest()

        headers = {
            'Content-Type': 'application/x-www-form-urlencoded',
        }
        data = {
            "from": "jp",
            "to": "zh",
            "q": query,
            "salt": str(salt),
            "sign": sign,
            "appid": appid
        }
        url = 'https://api.fanyi.baidu.com/api/trans/vip/translate'
        res = requests.post(url=url, headers=headers, data=data, timeout=4)
        if res.ok:
            result = json.loads(res.text)['trans_result'][0]['dst']
            return result
        else:
            derror('error')
            pass

    except requests.ConnectionError, e:
        dwarn("connection error", e.args)
示例#6
0
def hangul2hanja():
  import os
  from pyhanja import QHangulHanjaConverter
  ret = QHangulHanjaConverter()
  path = os.path.join(HANGUL_DIC_DIR, 'dic6.txt')
  if not os.path.exists(path) or not ret.loadFile(path):
    derror("failed to load dic:", path)
  return ret
示例#7
0
 def unlock(self):
     """@reimp"""
     try:
         if fcntl:  # it will be None if deleted
             fcntl.lockf(self._fp, fcntl.LOCK_UN)
         #os.close(self._fp)
         if os.path.isfile(self.path):
             os.unlink(self.path)
     except Exception, e:
         derror(e)
示例#8
0
def converter():
    import os
    from pypinyin import PinyinConverter
    ret = PinyinConverter()
    path = PINYIN_DIC_PATH
    if os.path.exists(path):
        ret.addFile(path)
    else:
        from sakurakit.skdebug import derror
        derror("dic path does not exist:", path)
    return ret
示例#9
0
def run_batch(path, cwd=None):
  """
  @param  path  str  path to local file
  @return  if succeed
  """
  dprint("path = %s" % path)
  if not path or not os.path.exists(path):
    derror("batch script does not exist: %s" % path)
    return
  cwd = cwd or os.path.dirname(path)
  subprocess.Popen(path, cwd=cwd).communicate()
示例#10
0
 def loadFile(self, path):
     """
 @param  path  unicode
 @return  bool
 """
     try:
         self.__d.clear()
         self.__d.addFile(path)
         return True
     except Exception, e:
         derror(e)
         return False
示例#11
0
    def notify(self, receiver, event):
        """@reimp @protected
    virtual bool notify(QObject *receiver, QEvent *e)

    See: http://www.02.246.ne.jp/~torutk/cxx/qt/QtMemo.html
    """
        try:
            if isinstance(
                    receiver,
                    QObject):  # receiver could be QCursor. Only notify QObject
                return super(Application, self).notify(receiver, event)
        except KeyboardInterrupt, e:
            derror(e)
示例#12
0
class Application(QApplication):
    def __init__(self, argv):
        super(Application, self).__init__(argv)

        self.setApplicationName(mytr_("Visual Novel Reader"))
        self.setApplicationVersion(str(config.VERSION_TIMESTAMP))
        self.setOrganizationName(config.VERSION_ORGANIZATION)
        self.setOrganizationDomain(config.VERSION_DOMAIN)
        self.setWindowIcon(rc.icon('logo-reader'))

        #if skos.WIN:
        #  ignoreWindowsExceptions()

        dprint("pass")

    def setFontFamily(self, family):
        dprint(family)
        font = self.font()
        font.setFamily(family)
        self.setFont(font)

    # FIXME: wParam does not exist in PySide MSG
    # Bug: https://bugreports.qt-project.org/browse/PYSIDE-84
    #def winEventFilter(self, msg):
    #  """
    #  @param  msg  PySide.QtCore.MSG
    #  @return  bool
    #  """
    #  # See: http://stackoverflow.com/questions/19195935/how-to-detect-windows-shutdown-or-logoff-in-qt
    #  if msg.wParam in (WM_QUIT, WM_ENDSESSION, WM_QUERYENDSESSION):
    #    dprint("quit")
    #  return super(Application, self).winEventFilter(msg)

    def notify(self, receiver, event):
        """@reimp @protected
    virtual bool notify(QObject *receiver, QEvent *e)

    See: http://www.02.246.ne.jp/~torutk/cxx/qt/QtMemo.html
    """
        try:
            if isinstance(
                    receiver,
                    QObject):  # receiver could be QCursor. Only notify QObject
                return super(Application, self).notify(receiver, event)
        except KeyboardInterrupt, e:
            derror(e)
        except Exception, e:
            derror(e)
            if skos.WIN:
                import win32api, win32con
                win32api.MessageBox(
                    None, """\
I am sorry that VNR got an unexpected error m(_ _)m
I am not sure what is happening, and whether you have to restart VNR T_T
Feel free to complain to me ([email protected]) if this error keeps bothering you.

ERROR MESSAGE BEGIN
%s
ERROR MESSAGE END""" % e, "VNR Error", win32con.MB_OK | win32con.MB_ICONERROR)
示例#13
0
def hanja2hangul():
  import os
  from pyhanja import QHanjaHangulConverter
  ret = QHanjaHangulConverter()
  #path = os.path.join(HANGUL_DIC_DIR, HANGUL_DIC_CONV)
  path = os.path.join(HANGUL_DIC_DIR, 'dic4.txt')
  if not os.path.exists(path) or not ret.addWordDictionary(path):
    derror("failed to load word dic:", path)

  # dic1: Korean hanzi in unihan
  # dic3: Chinese hanzi converted from unihan
  # dic5: hanzi frequent in Chinese but not in unihan
  for f in 'dic1.txt', 'dic3.txt', 'dic5.txt':
    path = os.path.join(HANGUL_DIC_DIR, f)
    if not os.path.exists(path) or not ret.addCharacterDictionary(path):
      derror("failed to load char dic:", path)
  return ret
示例#14
0
 def _iteralign(self, data):
     """
 @param  data  list  gson list
 @yield  (unicode surface, unicode translation)
 """
     try:
         for l in data:
             surface = l[0]
             #index = l[1]
             trans = l[2][0][
                 0]  # Actually, there are a list of possible meaning
             if '(' in surface:
                 for annot in ('(aux:relc)' '(null:pronoun)'):
                     surface = surface.replace(
                         annot, '').strip()  # remove annotations
             yield surface, trans
     except Exception, e:
         derror(e)
示例#15
0
def _iteralign(data, encoding='utf8'):
  """
  @param  data  list  json['data']
  @param* encoding  unicoding of raw json bytes for offset
  @yield  (unicode surface, unicode translation)
  """
  try:
    for sentence in data:
      src = sentence['src'].encode(encoding) #, errors='ignore') # get raw bytes
      for res in sentence['result']:
        #index = res[0] # int
        trans = res[1] # unicode
        offset = res[2][0] # such as "0|6"
        left, mid, right = offset.partition('|')
        left = int(left)
        right = int(right)
        surf = src[left:left+right].decode(encoding) #, errors='ignore')
        if surf:
          yield surf, trans
  except Exception, e:
    derror(e)
示例#16
0
 def lookupChar(self, ch):
     """
 @param  ch  unicode
 @return  [unicode or list] or None
 """
     ret = self.chars.get(ch)
     if ret:
         try:
             indirect = False
             for it in ret:
                 if isinstance(it, int):
                     indirect = True
                     break
             if indirect:
                 ret = list(ret)
                 for i, it in enumerate(ret):
                     if isinstance(it, int):
                         ret[i] = self._lookupRad(it)
             return ret
         except Exception, e:
             derror(e)
示例#17
0
  def dropMimeData(mime):
    """
    @param  mime  QMimeData
    """
    try:
      url = mime.urls()[0]
      path = url.toLocalFile()
      suf = os.path.splitext(path)[1]
      suf = suf[1:].lower()
      if suf in IMAGE_SUFFICES:
        dprint("drop image")
        settings.global_().setSpringBoardWallpaperUrl(url.toString())
      elif suf in ('exe', 'lnk'):
        dprint("drop game")

        import main
        #m = main.manager()
        #f = m.openGame if settings.global_().springBoardLaunchesGame() else m.addGame
        skevents.runlater(partial(main.manager().addGame, path=path), 200)
      else:
        derror("unreachable unknown suffix: %s" % suf)
    except (AttributeError, IndexError): pass
示例#18
0
    def requestPixmap(self, name, rsize, size):
        """@reimp @public
    @param[in]  providerId  unicode  unused
    @param[out]  rsize  QSize
    @param[in]  size  QSize
    @return  QPixmap not None

    virtual QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
    """
        ret = QPixmap(rc.image_path(name))
        if ret.isNull():
            derror("failed to load image: '%s'" % name)
        elif ret.size() != size:
            ret = (
                ret.scaled(size, Qt.KeepAspectRatio, Qt.SmoothTransformation)
                if not size.isEmpty() else ret.scaledToWidth(
                    size.width(), Qt.SmoothTransformation) if size.width() > 0
                else ret.scaledToHeight(size.height(), Qt.SmoothTransformation)
                if size.height() > 0 else ret)
        rsize.setWidth(ret.width())
        rsize.setHeight(ret.height())
        return ret
示例#19
0
    def requestPixmap(self, path, rsize, size):
        """@reimp @public
    @param[in]  providerId  unicode  unused
    @param[out]  rsize  QSize
    @param[in]  size  QSize
    @return  QPixmap not None

    virtual QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
    """

        icon = rc.file_icon(path)
        if icon.isNull():
            derror("failed to load image: '%s'" % path)
            ret = QPixmap()
        elif not size.isEmpty():
            ret = icon.pixmap(size)
        else:
            #sizes = icon.availableSizes(QIcon.Selected, QIcon.Off) # crash for executable
            ret = icon.pixmap(*FileImageProvider.ICON_SIZE)

        rsize.setWidth(ret.width())
        rsize.setHeight(ret.height())
        return ret
示例#20
0
def translate(text, to='zhs', fr='ja'):
    try:
        query = text.encode('utf-8')

        data = {
            'app_id':appid,
            'source':'jp',
            'target':'zh',
            'text':query,
            'time_stamp':str(int(time.time())),
            'nonce_str': str(random.randint(10000000, 90000000))
        }
        data['sign'] = get_req_sign(data, appkey)
        url = 'https://api.ai.qq.com/fcgi-bin/nlp/nlp_texttranslate'
        res = requests.post(url=url, data=data, timeout=5)
        if res.ok:
            result = json.loads(res.text)['data']['target_text']
            return result
        else:
            derror('error')
            return ''

    except requests.ConnectionError, e:
        dwarn("connection error", e.args)
示例#21
0
 def exceptionFilter(e):
     """
 LONG WINAPI exceptionFilter(PEXCEPTION_POINTERS e)
 """
     derror("Application exception")
示例#22
0
def _iteralign(data, source, target, encoding='utf8'):
    """
  @param  data  list  json['align']
  @param  source  unicode  original text
  @param  target  unicode  result translation
  @param* encoding  unicoding of raw json bytes for offset
  @yield  (unicode surface, unicode translation)
  """
    try:
        # {
        #   "ss": 48 # source start
        #   "se": 59 # source end
        #   "te": 39 # target start
        #   "ts": 34 # target end
        # },
        source = source.encode(encoding)
        target = target.encode(encoding)
        if isinstance(data, list):  # English -> Korean
            for align in data:
                ss = int(align['ss'])
                se = int(align['se'])
                ts = int(align['ts'])
                te = int(align['te'])

                s = source[ss:se + 1].decode(encoding)
                t = target[ts:te + 1].decode(encoding)
                if s:
                    yield s, t
        elif isinstance(data, dict):  # Japanese -> Korean
            slist = data['src']
            tlist = data['tar']
            m = OrderedDict(
            )  # {int group, ([unicode s], [unicode t])}  mapping from s to t

            for it in tlist:
                group = it['g']
                fr = it['f']  #
                to = it['t']  #
                t = target[fr:to + 1].decode(encoding)
                l = m.get(group)
                if l:
                    l[1].append(t)
                else:
                    m[group] = [], [t]

            for it in slist:
                group = it['g']
                fr = it['f']  #
                to = it['t']  #
                s = source[fr:to + 1].decode(encoding)
                l = m.get(group)
                if l:
                    l[0].append(s)
                else:
                    m[group] = [s], []

            #for k in sorted(m.iterkeys()):
            for s, t in m.itervalues():
                yield s, t
    except Exception, e:
        derror(e)
示例#23
0
        }
        data = {
            "from": "jp",
            "to": "zh",
            "q": query,
            "salt": str(salt),
            "sign": sign,
            "appid": appid
        }
        url = 'https://api.fanyi.baidu.com/api/trans/vip/translate'
        res = requests.post(url=url, headers=headers, data=data, timeout=4)
        if res.ok:
            result = json.loads(res.text)['trans_result'][0]['dst']
            return result
        else:
            derror('error')
            pass

    except requests.ConnectionError, e:
        dwarn("connection error", e.args)
    except requests.HTTPError, e:
        dwarn("http error", e.args)
    except UnicodeDecodeError, e:
        dwarn("unicode decode error", e)
    except (ValueError, KeyError, IndexError, TypeError), e:
        dwarn("json format error", e)
    except Exception, e:
        derror('error', e)
    else:
        pass
示例#24
0
        if r.ok and len(ret) > 100:
            ret = _parse(ret)
        else:
            dwarn("return content too short")
        return ret.decode('utf8', errors='ignore')

    #except socket.error, e:
    #  dwarn("socket error", e.args)
    except requests.ConnectionError, e:
        dwarn("connection error", e.args)
    except requests.HTTPError, e:
        dwarn("http error", e.args)
    #except KeyError, e:
    #  dwarn("invalid response header", e.args)
    except Exception, e:
        derror(e)

    dwarn("failed")

    try:
        dwarn(r.url)
    except:
        pass

    return ""


if __name__ == '__main__':
    #t = u"あのね  すもももももももものうち><" # Fixme: illegal html characters does not work
    #t = u"あのね  すもももももももものうち"
    #print translate(t, fr='ja', to='ar')