在成功安装cjklib和CEDICT之后,当我使用它时,它会抛出错误,为什么?

时间:2012-05-07 02:50:26

标签: python cjk

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

自从我开始学习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”更新为“from 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?我猜,只有这样,它可能适用于两种情况而不会抛出任何错误。

有没有人遇到类似的情况?你是怎么解决的?或者,你还有什么可以尝试的吗?请点亮一些灯!

提前致谢!

2 个答案:

答案 0 :(得分:0)

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

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。

答案 1 :(得分:0)

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

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