0

这是我在这里的第一篇文章。

自从我开始学习 python 以来,我一直在这里阅读帖子,这对我的学习过程有很大的帮助。所以,在我提出问题之前,只想对大家说一声非常感谢!

问题:

我成功安装了 cjklib 包。然后,我也成功安装了 CEDICT 字典。但是当我尝试使用 CEDICT 时,它总是会抛出如下错误:

>>> d = CEDICT()
......
  File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/default.py", line 335, in do_execute
sqlalchemy.exc.OperationalError: (OperationalError) unknown database "cedict_0" 'PRAGMA "cedict_0".table_info("CEDICT")' ()
>>> 

要重现问题:

安装 cjklib 包:

下载 cjklib-0.3.tar.gz,解压并更新目录 Cjklib-0.3/cjklib/build/*.py 中的文件(具体来说,builder.py 和init .py):

将“来自 sqlalchemy.exceptions”更新为“来自 sqlalchemy.exc”

$cd djklib-0.3/cjklib/build/
$sudo python setup.py install
$sudo installcjkdict CEDICT
$python
Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from cjklib.dictionary import CEDICT
>>> d = CEDICT()

发生错误,具体如下:

>>> d = CEDICT()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/cjklib/dictionary/__init__.py", line 605, in __init__
    super(CEDICT, self).__init__(**options)
  File "/usr/local/lib/python2.7/dist-packages/cjklib/dictionary/__init__.py", line 532, in __init__
    super(EDICTStyleEnhancedReadingDictionary, self).__init__(**options)
  File "/usr/local/lib/python2.7/dist-packages/cjklib/dictionary/__init__.py", line 269, in __init__
    if not self.available(self.db):
  File "/usr/local/lib/python2.7/dist-packages/cjklib/dictionary/__init__.py", line 276, in available
    and dbConnectInst.hasTable(cls.DICTIONARY_TABLE))
  File "/usr/local/lib/python2.7/dist-packages/cjklib/dbconnector.py", line 444, in hasTable
    schema = self._findTable(tableName)
  File "/usr/local/lib/python2.7/dist-packages/cjklib/dbconnector.py", line 429, in _findTable
    if hasTable(tableName, schema=schema):
  File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/base.py", line 2525, in has_table
  File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/base.py", line 2412, in run_callable
  File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/base.py", line 1959, in run_callable
  File "build/bdist.linux-x86_64/egg/sqlalchemy/dialects/sqlite/base.py", line 567, in has_table
  File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/base.py", line 1450, in execute
  File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/base.py", line 1627, in _execute_text
  File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/base.py", line 1697, in _execute_context
  File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/base.py", line 1690, in _execute_context
  File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/default.py", line 335, in do_execute
sqlalchemy.exc.OperationalError: (OperationalError) unknown database "cedict_0" 'PRAGMA "cedict_0".table_info("CEDICT")' ()
>>>

试用: 我自己尝试了一些解决方案,例如:

如错误所示,它在 sqlite 数据库文件中找不到表,因此我通过添加以下行来编辑 cjklib.conf 文件,告诉它该表就在此处:

url = sqlite:////usr/local/share/cjklib/cedict.db

然后,它找到了表 CEDICT 并停止抛出错误。但不幸的是,当我运行下面的代码时,它开始抛出另一种错误:

>>> from cjklib import characterlookup
>>> cjk = characterlookup.CharacterLookup('T')

错误:

>>> cjk = characterlookup.CharacterLookup('T')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/cjklib/characterlookup.py", line 118, in __init__
    self.hasComponentLookup = self.db.hasTable('ComponentLookup')
  File "/usr/local/lib/python2.7/dist-packages/cjklib/dbconnector.py", line 444, in hasTable
    schema = self._findTable(tableName)
  File "/usr/local/lib/python2.7/dist-packages/cjklib/dbconnector.py", line 429, in _findTable
    if hasTable(tableName, schema=schema):
  File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/base.py", line 2525, in has_table
  File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/base.py", line 2412, in run_callable
  File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/base.py", line 1959, in run_callable
  File "build/bdist.linux-x86_64/egg/sqlalchemy/dialects/sqlite/base.py", line 567, in has_table
  File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/base.py", line 1450, in execute
  File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/base.py", line 1627, in _execute_text
  File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/base.py", line 1697, in _execute_context
  File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/base.py", line 1690, in _execute_context
  File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/default.py", line 335, in do_execute
sqlalchemy.exc.OperationalError: (OperationalError) unknown database "cjklib_0" 'PRAGMA "cjklib_0".table_info("ComponentLookup")' ()

如果我删除我添加的行,它将再次按预期工作。

解决方案:

如何让它同时读取 cedict.db 和 cjklib.db ?我猜,只有这样,它才可能适用于这两种情况而不会引发任何错误。

有没有人遇到过类似的情况?你是怎么解决的?或者,您还有什么想尝试的吗?请放一些灯!

提前致谢!

4

2 回答 2

0

(1)您使CEDICT工作的方法不完整。虽然 "d = CEDICT()" 不会引发任何错误,但是当你使用字典函数时会遇到错误,例如 "d.getFor(u'Friend')"。您应该在配置文件中添加一个项目:

attach = sqlite:////usr/local/share/cjklib/cjklib.db

但是,这种方法仍然会给出一个“characterlookup”错误(正如你提到的)。

(2) 真正的解决方案:为字典显式创建“dbconnection”。

>>> from cjklib.dictionary import CEDICT
>>> from cjklib.dbconnector import getDBConnector
>>> db = getDBConnector({'sqlalchemy.url': 'sqlite://', 'attach': ['cjklib']})
>>> d=CEDICT(dbConnectInst=db)
>>> it=d.getFor(u'朋友')

编辑:

将 cjklib.conf 保留为默认配置。

于 2012-05-08T21:36:23.200 回答
0

当您在 python 中全局初始化 CEDICT() 对象时,此错误也可能发生在多线程环境中,但随后您尝试在新创建的线程中使用相同的对象。

解决方案是在您打算进行字典查找的线程内创建对象。

于 2014-02-28T16:37:08.673 回答