星期日, 5月 30, 2010

新版 ydict ! update: 2013/07/10


最近 yahoo 的字典改版了, 害 4$ 改寫的 ydict 不能用了, 只好重新寫 parser ...
這邊是我重寫的 ydict, 配色和版面都參考之前的 ydict, 但是 parser 整個重新寫了, 以 MIT licence 釋出:

最新的版本可到這邊下載:
https://github.com/freehaha/ydict/releases


歡迎自由抓取修改, 但是使用後果自行負責!
-------------------------------
6/23 更新:
yahoo 字典的音標位置有點變, 造成音標抓不到, 改過來了
-------------------------------
6/27 更新:
改了一下切割英/中 example 的 regexp, 稍微改善一點排版

由於新版的 yahoo 字典的例句中英沒有明顯的分隔, 目前我只能由找到的第一個非英文字來分割中英文,還是有點瑕疵不過還算堪用啦 XD

-------------------------------
2012/12/07 更新
前幾天有發現 ydict 不能用了,但沒有特別注意,今天學長說不能用了我才發現還有人在用 ydict, 決定來改一改. 這個版本除了原本的功能還加上了變化型和同義字。可能還有些 bugs, 歡迎大家回報(雖然我可能不會馬上改就是了^^|||)。
-------------------------------
2013/04/02 更新
修正部份字詞 (colour, children) 會漏抓解釋的問題。
-------------------------------
2013/07/10
想說怎麼都查不到字, 結果是 yahoo 字典又改版了! 已更新.
另外開了 github 的 repo: https://github.com/freehaha/ydict, 歡迎任何 pull request.
-------------------------------
2013/07/25
yahoo 又改回之前的版本, 請到上面 github 的連結去下載最新的版本(其實是 20130402 的 版本, 上面前兩個連結將不會繼續更新, 以後請到 github 下載. 有問題的話也可以在那邊發 issue 給我.
-------------------------------
2013/08/14
後續就不在這邊更新了, 可以到以上 github 連結看 README 跟 release. 有問題還是歡迎在這邊留言, 或是到 github 發 issue ticket.

星期六, 5月 01, 2010

perl 的 'exists' in array, hash references

今天在寫 perl 發現奇怪的 bug,Glib::Timeout 收到 unhandled exception 就停掉了,看了 error log,這我明明就處裡的好好的阿!
簡化過的問題如下:
#!/usr/bin/perl
use strict;
                                                                                                                                                              
sub retundef {
    #something wrong
    return undef;
}  

my $ret = &retundef();
if(exists $ret->{dummy}) {
    print "gasp!?\n";
}  
exit 1 unless $ret;
if(@{$ret->{list}}) {
    local $, = ", ";
    print @{$ret->{list}};

}
exit 0;
看起來很簡單的 code 卻暗藏玄機,居然 return 0; why??? 我左看右看,才想到可能是 exists 的問題。perldoc 的 exists 是這樣寫的:

Although the mostly deeply nested array or hash will not spring into existence just because its existence was tested, any intervening ones will. .... This happens anywhere the arrow operator is used...
原來在 $ret->{dummy} 以後, $ret 就自動變成 hash reference 了,雖然 $ret->{dummy} 還是不存在,但是卻改變了 $ret。
------------
不得不說,喜歡 C 不是沒有原因的... perl 寫起來方便(光是 hash table 就大勝) 但是對於一些 reference,包括 GC 的處理上的確不太透明;看來我要學得東西還很多...

星期五, 4月 30, 2010

perl threads encoding

上一篇 講到了讓 DBI::SQLite 自動為 string 加上 utf-8 flag,其實是因為我最近在寫 gtk 的時候需要用到額外的 thread,但是 encoding (perl 內建的 package) 不是 thread-safe 而且只要一用,threading 就一定以 segmentation fault 收場。
我目前是沒有找到什麼解法,但是如果是使用不同編碼來寫 perl 那可能只能暫時用 package Encode 的 decode 或 encode 來把每個字串包起來吧 Orz. 天阿,好險我都用 utf-8

perl & utf-8 (SQLite issue)

事實上自從 perl 5.8.0 以後好像已經全面使用 unicode 了,根據我的了解 perl-gtk2 內部也都使用 utf-8,但是說也奇怪我的 gtk label 就是顯示不了中文,使用者輸入的中文卻又沒問題,找了許久才發現,perl 在 string 上會有一個 utf-8 flag,用來判斷這個 string 是不是使用 utf-8,不是的話 gtk2 的 module 會再做一次轉換,由於 db 讀出來的 string 都沒有設定,中文就爛掉了。

但是為什麼我 db 裡面明明存 utf-8 讀出來卻不是呢? DBI::SQLite 說道..
If the attribute $dbh->{sqlite_unicode} is set, strings coming from the database and passed to the collation function will be properly tagged with the utf8 flag; but this only works if the sqlite_unicode attribute is set before the first call to a perl collation sequence . The recommended way to activate unicode is to set the parameter at connection time :
所以只要確定 db 裡面是存著 utf-8 的資料,只要再連線時設定好 sqlite_unicode,sqlite 的 DBI 會幫你把 utf-8 flag turn on:
my $dbh = DBI->connect(
      "dbi:SQLite:dbname=foo", "", "",
      {
          RaiseError     => 1,
          sqlite_unicode => 1,
      }
  );
當然,總是有一些比較不一樣的方法,用 Encode::_utf8_on 可以直接把 flag turn on,可以在有用到中文的地方設定,但這樣不但瑣碎可能忘記,而且 gtk module 還會嘗試將其他不是 utf-8 的 string 再轉換一次,所以還是建議讓 DBI 把全部的 string set utf-8 flag。

星期一, 3月 29, 2010

latex 半自動 WYSIWYM

之前寫過的 iwatch 可以讓我寫 latex 的時候不用一直手動 make,然後只要編譯完用 foxit 顯示文件就可以了,但是這樣的效果差強人意,因為 foxit 沒辦法自動 reload,我只好每次 make 完就 kill 然後重開一個 foxit,心血來潮之下找到了 xpdf,他可以開一個 remote xpdf 像是一個 server ,然後 xpdf -remote preview -reload 就可以讓這個remote server 重新讀取 pdf!!
這樣的組合真是太完美了,搭配 iwatch,可以用我寫的 script:
#!/bin/bash
REMOTE=preview
TEX=sample.tex
PDF=$(basename $TEX .tex).pdf
xpdf -remote $REMOTE $PDF &                                                                                    
id=$!
while ./iwatch -e modify $TEX
do
    make
    xpdf -remote $REMOTE -reload &
    id=$!
done

這樣每次儲存以後,會自動編譯,然後在現有 xpdf 更新畫面!在我的桌面上執行起來像這樣:下面是 vim 上面是 xpdf,因為用 xmonad 剛好可以分成上下來顯示。