cht電腦資訊gcingcin分享
adm Find login register

sync gcin 詞音詞庫的 script

elleryq

joined: 2007-09-27
posted: 117
promoted: 5
bookmarked: 0
Taipei, Taiwan
1subject: sync gcin 詞音詞庫的 scriptPromote 1 Bookmark 02011-07-15quote  

今天用 python 寫了兩個 script,配合 dropbox 之類的 syncing service 用的,想說就可以同步詞音的詞庫

程式在 https://github.com/elleryq/sync_gcin_tsin

push_gcin_tsin.py 就把詞庫轉成文字檔,其實跟用 tsd2a32 是一樣的,只是更簡化一點點,參數就帶 syncing service 所 sync 路徑的檔案名稱,例如 ~/Dropbox/tsin32.txt

merge_gcin_tsin_from.py 則是把文字檔跟目前詞音詞庫裡作比較(先看有無差集),有需要就先用 python 的 set 去做聯集,再用 tsa2d32 轉~用法一樣,就帶檔名。

未來有打算加的:

  • 讓 merge_gcin_tsin_from.py 支援 http/ftp
  • 加 exclude 設定,就在 push_gcin_tsin.py 出去前,先把 exclude 的詞給刪掉。
elleryq

joined: 2007-09-27
posted: 117
promoted: 5
bookmarked: 0
Taipei, Taiwan
2subject: Promote 0 Bookmark 02011-07-19quote  

現在支援可以從 http 或 ftp 下載檔案來merge了。

python merge_gcin_tsin_from.py http://your_host/your_path/tsin32.txt

dowba
joined: 2009-08-30
posted: 35
promoted:
bookmarked:
Taichung
3subject: Promote 0 Bookmark 02011-07-20quote  

請問設定好 init.py 並執行之後

出現

 print e.reason

          ^ 

SyntaxError: invalid syntax

錯誤訊息

 

請問何解??? 

 系統環境為WINXP 

 設定值為:

TSD2A32 = "C:\Program Files\gcin\bin\tsd2a32.exe"
TSA2D32 = "C:\Program Files\gcin\bin\tsa2d32.exe"
USER_GCIN_DIR = os.path.expanduser( "C:\Documents and Settings\computername\Application Data\gcin" )
USER_TSIN32 = os.path.join( USER_GCIN_DIR, "tsin32" ) 

edited: 2
elleryq

joined: 2007-09-27
posted: 117
promoted: 5
bookmarked: 0
Taipei, Taiwan
4subject: Promote 0 Bookmark 02011-07-20quote  

會不會是 indent 的問題啊?? 我用的都是 4 個空白,不是 tab ~

然後最好加上 r,'\' 才不會被當成 escape 字元

TSD2A32 = r"C:\Program Files\gcin\bin\tsd2a32.exe"
TSA2D32 = r"C:\Program Files\gcin\bin\tsa2d32.exe"
USER_GCIN_DIR = os.path.expanduser( r"C:\Documents and Settings\computername\Application Data\gcin" )

edited: 1
elleryq

joined: 2007-09-27
posted: 117
promoted: 5
bookmarked: 0
Taipei, Taiwan
5subject: Promote 0 Bookmark 02011-07-21quote  

我補上對 Windows 的支援了,再煩請更新以後,試試看有沒有問題。

還沒加上 exclude 特定詞的功能,另外想到要補上 setup.py,以利於安裝、部署。

dowba
joined: 2009-08-30
posted: 35
promoted:
bookmarked:
Taichung
6subject: Promote 0 Bookmark 02011-07-21quote  

仍有錯誤訊息:

init.py line 20 

AttributeError: "module" object has no attributes "create_unicode_buffer"

 

感激不盡! 

edited: 1
elleryq

joined: 2007-09-27
posted: 117
promoted: 5
bookmarked: 0
Taipei, Taiwan
7subject: Promote 0 Bookmark 02011-07-21quote  

咦? 奇怪,我有在我 Windows XP 上試過,是可以的。我 python 版本是 2.6。

  1. 請問你的 python 版本是?
  2. 煩請檢查一下 c:\python26\lib\ctypes 目錄下有無 wintypes.py (請把python26替換成你的python目錄)

補充:

改用乾淨的 python27 (因為我原來的python26有裝pywin32、pygtk 之類的函式庫,怕有影響) 來測試,是沒有問題的。

再補充:

試用了 python3,果然就出現問題了,正在處理中... 才處理完 function 問題,想不到字串的處理也是問題...  >_<

edited: 2
dowba
joined: 2009-08-30
posted: 35
promoted:
bookmarked:
Taichung
8subject: Promote 0 Bookmark 02011-07-21quote  

感謝辛勞!

使用的版本是 Python 3.2.1 

 

現在的錯誤訊息是:

TypeError: sequence item 0:expeted str instance, bytes found 

edited: 1
elleryq

joined: 2007-09-27
posted: 117
promoted: 5
bookmarked: 0
Taipei, Taiwan
9subject: Promote 0 Bookmark 02011-07-21quote  

看來要認真研究 python 3 了,library的名字更換這倒是小事,字串與檔案的處理真的是... ~_~

elleryq

joined: 2007-09-27
posted: 117
promoted: 5
bookmarked: 0
Taipei, Taiwan
10subject: Promote 0 Bookmark 02011-07-22quote  

已經完成 python3 的移植,目前 python2/python3 都可以執行,在我這邊的linux下沒問題,windows下還沒測試。煩請再試試看,謝謝。

dowba
joined: 2009-08-30
posted: 35
promoted:
bookmarked:
Taichung
11subject: Promote 0 Bookmark 02011-07-23quote  

沒有出現明顯的ERROR訊息

但是沒有成功

出現的訊息是 You need to install gcin 

 

沒有更改過任何設定

有沒有需要做任何更改呢?

 

另外,如果我想要追蹤home app_data_dir USER_GCIN_DIR這三個變數

要怎麼樣可以讓他們print出來呢?

謝謝! 

謝謝辛勞! 

edited: 2
elleryq

joined: 2007-09-27
posted: 117
promoted: 5
bookmarked: 0
Taipei, Taiwan
12subject: Promote 0 Bookmark 02011-07-23quote  

該死,我忘記 Windows Vista/7 了...

你可以在 line 39 後新增,就設置 USER_GCIN_DIR 後,加上


print( home, app_data_dir, USER_GCIN_DIR )
sys.exit(-1)  # 直接離開,不執行後面程式。


就可以印出。

dowba
joined: 2009-08-30
posted: 35
promoted:
bookmarked:
Taichung
13subject: Promote 0 Bookmark 02011-07-24quote  

elleryq

該死,我忘記 Windows Vista/7 了...

你可以在 line 39 後新增,就設置 USER_GCIN_DIR 後,加上


print( home, app_data_dir, USER_GCIN_DIR )
sys.exit(-1)  # 直接離開,不執行後面程式。


就可以印出。

 

忘了講

上面那個不成功訊息是Windows 7的 

Windows 7  之下目錄還是錯的

 會出現You need to install gcin的錯誤訊息

辛苦了!

edited: 1
elleryq

joined: 2007-09-27
posted: 117
promoted: 5
bookmarked: 0
Taipei, Taiwan
14subject: Promote 0 Bookmark 02011-07-25quote  

這版改去 registry 取得 gcin 的安裝目錄,並且在 linux/windows 2.6/2.7/3.0 交叉測試過,應該是沒問題了。煩請再試試看,感恩。

dowba
joined: 2009-08-30
posted: 35
promoted:
bookmarked:
Taichung
15subject: Promote 0 Bookmark 02011-07-25quote  

elleryq

這版改去 registry 取得 gcin 的安裝目錄,並且在 linux/windows 2.6/2.7/3.0 交叉測試過,應該是沒問題了。煩請再試試看,感恩。

 

還是有問題@@不是刻意找碴,是真的就有問題……

錯誤訊息為 WindowsError: [Error 2] The system cannot find the file specified

 

我的登錄檔中,HKEY_LOCAL_MACHINE 裡面的 gcin_dir 以及 install_dir 落在 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\gcin(64位元的系統

除此之外,HKEY_CURRENT_USER 裡面,HKEY_CURRENT_USER\Software\gcin 之下只有一個 Name=(Default),Type=REG_SZ,Data 為空的值,只有目錄,沒有任何值 。

其他有地方可以參考: 

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run 之下有一個  Name=gcin,Type=REG_SZ,Data="C:\Program Files (x86)\gcin\bin\gcin.exe" 的值

HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Compatibility Assistant\Persisted 之下有一個  Name=C:\Program Files (x86)\gcin\bin\gcin-setup.exe,Type=REG_DWORD,Data=0x0000000(1)  的值

 

 

elleryq

joined: 2007-09-27
posted: 117
promoted: 5
bookmarked: 0
Taipei, Taiwan
16subject: Promote 0 Bookmark 02011-07-26quote  

沒關係。

因為我手邊沒有 64bit 的 Windows,可以幫我試一下這段程式,然後告訴我執行結果嗎?

 

from winreg import HKEY_LOCAL_MACHINE, REG_SZ
from winreg import KEY_ALL_ACCESS, KEY_WOW64_64KEY, KEY_WOW64_32KEY
from winreg import OpenKeyEx, QueryValueEx
key1 = OpenKeyEx(HKEY_LOCAL_MACHINE, r'Software\gcin', 0, KEY_ALL_ACCESS | KEY_WOW64_64KEY )
gcin_install_dir, key_type = QueryValueEx(key1, "Install_Dir")
print( "key1", gcin_install_dir )
key2 = OpenKeyEx(HKEY_LOCAL_MACHINE, r'Software\gcin', 0, KEY_ALL_ACCESS | KEY_WOW64_32KEY )
gcin_install_dir, key_type = QueryValueEx(key2, "Install_Dir")
print( "key2", gcin_install_dir )

edited: 2
dowba
joined: 2009-08-30
posted: 35
promoted:
bookmarked:
Taichung
17subject: Promote 0 Bookmark 02011-07-27quote  

執行結果為:

WindowsError: [Error 2] The system cannot find the file specified

 

elleryq

joined: 2007-09-27
posted: 117
promoted: 5
bookmarked: 0
Taipei, Taiwan
18subject: Promote 0 Bookmark 02011-07-28quote  

那這樣呢?

from winreg import HKEY_LOCAL_MACHINE, REG_SZ
from winreg import KEY_ALL_ACCESS, KEY_WOW64_64KEY, KEY_WOW64_32KEY
from winreg import OpenKeyEx, QueryValueEx
try:
  key1 = OpenKeyEx(HKEY_LOCAL_MACHINE, r'Software\gcin', 0, KEY_ALL_ACCESS | KEY_WOW64_64KEY )
  gcin_install_dir, key_type = QueryValueEx(key1, "Install_Dir")
  print( "key1", gcin_install_dir )
except Exception as e:
  print( e )
try:
  key2 = OpenKeyEx(HKEY_LOCAL_MACHINE, r'Software\gcin', 0, KEY_ALL_ACCESS | KEY_WOW64_32KEY )
  gcin_install_dir, key_type = QueryValueEx(key2, "Install_Dir")
  print( "key2", gcin_install_dir )
except Exception as e:
  print( e )

dowba
joined: 2009-08-30
posted: 35
promoted:
bookmarked:
Taichung
19subject: Promote 0 Bookmark 02011-07-31quote  

elleryq

那這樣呢?

from winreg import HKEY_LOCAL_MACHINE, REG_SZ
from winreg import KEY_ALL_ACCESS, KEY_WOW64_64KEY, KEY_WOW64_32KEY
from winreg import OpenKeyEx, QueryValueEx
try:
  key1 = OpenKeyEx(HKEY_LOCAL_MACHINE, r'Software\gcin', 0, KEY_ALL_ACCESS | KEY_WOW64_64KEY )
  gcin_install_dir, key_type = QueryValueEx(key1, "Install_Dir")
  print( "key1", gcin_install_dir )
except Exception as e:
  print( e )
try:
  key2 = OpenKeyEx(HKEY_LOCAL_MACHINE, r'Software\gcin', 0, KEY_ALL_ACCESS | KEY_WOW64_32KEY )
  gcin_install_dir, key_type = QueryValueEx(key2, "Install_Dir")
  print( "key2", gcin_install_dir )
except Exception as e:
  print( e )

 

抱歉,回覆晚了

仍是出現錯誤訊息如下:

[Error 2] The system cannot find the file specified
[Error 5] Access is denied 

 

單獨第一個try的執行結果是  [Error 2] The system cannot find the file specified

單獨第二個try的執行結果是  [Error 5] Access is denied

以administrator身份執行第二個try,其結果為 "  key 2 C:\Program Files(x86)\gcin  "
表示有成功抓到! 

 

用上面的程式碼依樣畫葫蘆去改init.py,並且以administrator身份去執行,已經可以成功了,謝謝前輩辛苦! 

edited: 3
elleryq

joined: 2007-09-27
posted: 117
promoted: 5
bookmarked: 0
Taipei, Taiwan
20subject: Promote 0 Bookmark 02011-08-01quote  

抱歉,可以再請你試一次嗎?看這樣可不可以不需要 Administrator 權限。謝謝~

from winreg import HKEY_LOCAL_MACHINE, REG_SZ
from winreg import KEY_READ, KEY_WOW64_32KEY
from winreg import OpenKeyEx, QueryValueEx
try:
  key2 = OpenKeyEx(HKEY_LOCAL_MACHINE, r'Software\gcin', 0, KEY_READ | KEY_WOW64_32KEY )
  gcin_install_dir, key_type = QueryValueEx(key2, "Install_Dir")
  print( "key2", gcin_install_dir )
except Exception as e:
  print( e )

dowba
joined: 2009-08-30
posted: 35
promoted:
bookmarked:
Taichung
21subject: Promote 0 Bookmark 02011-08-01quote  

elleryq

抱歉,可以再請你試一次嗎?看這樣可不可以不需要 Administrator 權限。謝謝~

from winreg import HKEY_LOCAL_MACHINE, REG_SZ
from winreg import KEY_READ, KEY_WOW64_32KEY
from winreg import OpenKeyEx, QueryValueEx
try:
  key2 = OpenKeyEx(HKEY_LOCAL_MACHINE, r'Software\gcin', 0, KEY_READ | KEY_WOW64_32KEY )
  gcin_install_dir, key_type = QueryValueEx(key2, "Install_Dir")
  print( "key2", gcin_install_dir )
except Exception as e:
  print( e )

 這樣子不用administrator權限也可以成功抓到!

執行結果:

key2 C:\Program Files (x86)\gcin 

 

不過照這次的程式碼修改入init.py,使用 merge_gcin_tsin_from.py 會出現 C:\Users\J\Appdata\Roaming\gcin\tsin32 or D:\Documents\Dropbox\tsin32.txt is empty 的錯誤訊息。但是 gcin_install_dir 的值沒有錯誤,且確定這二個檔案都存在。

push_gcin_tsin.py 則是非常順利,成功顯示 Done 的訊息。

以下是已經成功的 init.py

if python_version == "3":
   from winreg import OpenKeyEx, QueryValueEx
   from winreg import HKEY_LOCAL_MACHINE, REG_SZ
   from winreg import KEY_ALL_ACCESS
   from winreg import KEY_READ, KEY_WOW64_64KEY, KEY_WOW64_32KEY
else:
   from _winreg import OpenKey, QueryValueEx
   from _winreg import HKEY_LOCAL_MACHINE, REG_SZ
   from _winreg import KEY_ALL_ACCESS
   from _winreg import KEY_WOW64_64KEY, KEY_WOW64_32KEY

sam_desired = KEY_ALL_ACCESS
if platform.architecture()[0]=="64bit":
   # TODO: Need to clarify
   #sam_desired = sam_desired | KEY_WOW64_64KEY
   sam_desired = sam_desired | KEY_WOW64_32KEY
try:
   key = OpenKeyEx(
                HKEY_LOCAL_MACHINE, r'Software\gcin', 0, KEY_READ | KEY_WOW64_32KEY )
   gcin_install_dir, key_type = QueryValueEx(key, "Install_Dir")  

謝謝辛勞! 

edited: 8
elleryq

joined: 2007-09-27
posted: 117
promoted: 5
bookmarked: 0
Taipei, Taiwan
22subject: Promote 0 Bookmark 02011-08-02quote  

我有更新,請再拉回去試試。如果 merge 仍然失敗的話,可以在 merge_gcin_tsin_from.py 的第 18 行之下,新加一行:print( 'len(current)=', len(current), 'len(cloud)=', len(cloud) )

然後把輸出結果貼上來看看。

dowba
joined: 2009-08-30
posted: 35
promoted:
bookmarked:
Taichung
23subject: Promote 0 Bookmark 02011-08-02quote  

elleryq

我有更新,請再拉回去試試。如果 merge 仍然失敗的話,可以在 merge_gcin_tsin_from.py 的第 18 行之下,新加一行:print( 'len(current)=', len(current), 'len(cloud)=', len(cloud) )

然後把輸出結果貼上來看看。

 

出現的結果是:D:\Documents\Dropbox\tsin32.txt is empty, skip to merge

上述程式碼執行結果為:len(current)= 38892 len(cloud)= 0

不知道是否有誤? 

dowba
joined: 2009-08-30
posted: 35
promoted:
bookmarked:
Taichung
24subject: Promote 0 Bookmark 02011-08-02quote  

elleryq

我有更新,請再拉回去試試。如果 merge 仍然失敗的話,可以在 merge_gcin_tsin_from.py 的第 18 行之下,新加一行:print( 'len(current)=', len(current), 'len(cloud)=', len(cloud) )

然後把輸出結果貼上來看看。

 

出現的結果是:D:\Documents\Dropbox\tsin32.txt is empty, skip to merge

上述程式碼執行結果為:len(current)= 38892 len(cloud)= 0

不知道是否有誤? 

elleryq

joined: 2007-09-27
posted: 117
promoted: 5
bookmarked: 0
Taipei, Taiwan
25subject: Promote 0 Bookmark 02011-08-03quote  

咦? 你的 D:\Documents\Dropbox\tsin32.txt 是空的嗎? 如果有內容,可否寄給我看看?? elleryq (at) gmail (dot) com , thanks

elleryq

joined: 2007-09-27
posted: 117
promoted: 5
bookmarked: 0
Taipei, Taiwan
26subject: Promote 0 Bookmark 02011-08-04quote  

謝謝你,我已經修改過程式,現在應該是沒問題了,煩請再試試看。

dowba
joined: 2009-08-30
posted: 35
promoted:
bookmarked:
Taichung
27subject: Promote 0 Bookmark 02011-08-04quote  

elleryq

謝謝你,我已經修改過程式,現在應該是沒問題了,煩請再試試看。

 

這次的結果為:

len(current)= 38892 len(cloud)= 38892
Same, skip to merge.

應該是沒問題了!感謝辛勞!

 

cht電腦資訊gcingcin分享
adm Find login register
views:28686