Monday, April 27, 2009

三步驟讓 Google 只搜尋網路書籤連結內容



三步驟讓 Google 只搜尋網路書籤連結內容


How to do



  1. 將網路書籤 RSS Feed 輸入到下面第一欄位。

    譬如我的 Delicious 網址 http://feeds.delicious.com/v2/rss/migi?count=999 (記得把 count 改大一點

  2. 將產生的文字複製到這個連結第一欄 XML specification URL,接著點 Refresh

  3. 在下面 "Try searching for" 隨便輸入,接著點後面的 Search


這時就可以儘量搜尋了,試著搜尋最近加入的文章內容。也可以把這個網頁Bookmark起來,以後直接開來使用




RSS網址:













覺得太簡單沒什麼功能可以調整嗎?這裡有更複雜完整版

Sunday, April 26, 2009

Let Google Custom Search search in your online bookmarks

This idea has been staying in my mind for 1 year. Today I found GCS(google custom search) added a new function: Linked CSE

Linked CSE makes adding searching links open.  That is, you can have your search list outside the CSE server.  You can let CSE search in your own bookmarks!

What's the difference with traditional bookmark search:
Old bookmark search engines only record and search for page titles.  But what if we want to search the contents we bookmarked?  Or if we just forgot the title but we remember its content?  When our bookmarks are in a huge number, it's not easy to find anything we really want.

Let me show you how big different it will be.  For the first example, I bookmarked a site www.freedownloadaday.com in my Decilious bookmarks.  Using Delicious's search function I can only find this site from 3 keywords: free, download, and day.  However go to freedownloadaday's site, today's post is Clean up toolbar clutter.  I can go to my  GCS Homepage and search for "Clean up toolbar clutter".  This post is in the first result. This is what bookmark servers can't do.

You can try searching any other texts in my bookmark.

How it works
Now combining 3 main atoms by Google and Del.icio.us, I can let any online bookmark go with Google-supported context search feature.
The 3 atoms are:
  1. RSS Feed in that onlien bookmark
  2. GCS's advanced function: Linked CSE
  3. URL-based Tools for Linked CSE's

How to do
Firstly find the RSS feed of your boookmark.
My RSS feed is (default): http://feeds.delicious.com/v2/rss/migi?count=15 

Notice if we want more bookmarks to be fetched, simply change the count parameter to 999 (That's big enough I think :) -- The parameter name count varies depending on different provides.  Google Bookmarks uses num.

Log-in to your GCS website -> create/edit your engine -> In Control panel on the left, go to page Advanced. 
Locate Upload annotations, remember the bold text in that paragraph _cse_XXXXXXX
Locate Add nnotations feed, after adding a proper link in the textbox, you are done.

The GCS's URL-based tool can extract links from any web page or RSS and generate annotations(XML-based link description file for GCS).  So, we feed the RSS into the tool:
www.google.com/cse/tools/makeannotations?url=feeds.delicious.com%2Fv2%2Frss%2FMiGi%3Fcount%3D999&label=__your_label__&pattern=path
Here the parameters are:
  • url = feeds.delicious.com/v2/rss/migi?count=999 You have to encode symbols(/?=&) by yourself.  as following:
  • label = (important: You must change this value to that specified in your GCS advanced setting page.)  Find text: Label sites with _cse_XXXXXXXX if you want to include them in your Custom Search Engine
  • pattern = path
See here for more options
The encoding:

original characterescaped character
/%2F
?%3F
=%3D
&%26


More interesting CSE topic


Can I also search in my Google Bookmarks?
Yes. Use and modify this link
Unfortunately Google Bookmarks is not public, even its RSS feed.  Unless you build your own transmitter between RSS feeds and annotations with automatic logging into Google account,  you cannot have dynamic google bookmark update.

Can I search in a specific tag/label corresponding to my bookmark setting?
Technically Yes! But it needs more tune and may not be easy.  See the following relative work for the Delicious convert tool by a smart guy. (You have to provide your delicious account)

related works:
Further Reading:



Wednesday, April 22, 2009

CodeProject: Explorer Shell Context Menu. Free source code and programming help

CodeProject: Explorer Shell Context Menu. Free source code and programming help: "Screenshot -"

Thanks to this article. I finally can show explorer shell context menu in my vb.net program.

Screenshot -

Language Auto Detector works with lang attribute自動語言選擇--現在直接使用 lang="zh" 即可

This is English in <span lang="en">
這是<span lang="zh">下看到的中文
請使用左方下拉表單選擇語言Please choose your language in the drop-down list on the left

Thursday, April 16, 2009

Friday, April 03, 2009

Language Auto Detector works with lang attribute自動語言選擇--現在直接使用 lang="zh" 即可

This is English in <span lang="en">
這是<span lang="zh">下看到的中文



Using this format is much easier than using the class attribute when posting articles.
PS. Note that the post title can also include span tag!! It changes as you change the language setting!!


這樣撰寫文章還是比較方便。原理是由 javascripti 搜索 span 及 div tag,再增加對應語言的 className。也就是說,就方法 class="lang_zh" 還是有效。

要注意的是為了不要讓瀏覽器 loading 太重,搜尋tag只含 div 和 span,也就是說,如果要在其他tag內有語言自動選擇功能,還是要乖乖寫 class="lang_zh"...不過這狀況總是沒有使用 span 的來得多。
PS. 連標題都可以寫span tag!! bogger真是太寬鬆了!

code:

// for IE6 or before: change <span lang="en"> to <span class="lang_en">
modList = document.getElementsByTagName("span");
for (var i=0; i<modList.length; i+=1) {lang2class(modList[i]);}
modList = document.getElementsByTagName("div");
for (var i=0; i<modList.length; i+=1) {lang2class(modList[i]);}


function lang2class(elm)
{
if (attr=elm.getAttribute("lang")) {elm.className+=" lang_"+attr;}
}

Thursday, April 02, 2009

中英對照測試 (請使用上方下拉表單)

From: 自由電子報-國際新聞
Music fans turn up volume of legal downloads:study 研究:樂迷合法下載量增多

◎ 鄭寺音

Music fans worldwide downloaded 420 million songs legally over the Internet in 2005, a 20-fold increase from two years ago, research showed.

研究指出,全球音樂迷2005年在網路上合法下載4億2千萬首歌曲,兩年來增加了20倍。

Record labels reaped a 1.1-billion-dollar harvest from digital downloads, up from 380 million dollars the previous year, with about 40 percent of this revenue earned from music for mobile phones.

唱片業者從數位下載獲得11億美元的收益,前一年則有3億8千萬美元,這項收益約4成是從手機音樂賺來的。

...



Content:

<span lang="en">Music fans turn up volume of legal downloads:study</span&tt;<span lang="zh"> 研究:樂迷合法下載量增多</span>

◎ 鄭寺音
<div lang="en">
Music fans worldwide downloaded 420 million songs legally over the Internet in 2005, a 20-fold increase from two years ago, research showed.
</div><div lang="zh">
研究指出,全球音樂迷2005年在網路上合法下載4億2千萬首歌曲,兩年來增加了20倍。
</div><div lang="en">
Record labels reaped a 1.1-billion-dollar harvest from digital downloads, up from 380 million dollars the previous year, with about 40 percent of this revenue earned from music for mobile phones.
</div><div lang="zh">
唱片業者從數位下載獲得11億美元的收益,前一年則有3億8千萬美元,這項收益約4成是從手機音樂賺來的。
</div>

!! Simplest Automatic Javascript+CSS Multilingual Selector!!(號外)最簡單的 Javascript+CSS 網站語言自動選擇器



測試:Test:
This is English

這是中文





讓頁面自動選擇browser預設語言,一直是非英語主流部落客要將網站改成雙語環境最重要的功能
標題已經下英文了(我現在英文想得比中文快),當然這篇文章之後也要變成提供雙語

It has always been needed for a multi-lingual blogger to build a site with automatic language selector. Now I have devised a very good way (I believe):


我的目標是 My Aim


blog只要寫一遍,中英文都放在同一篇文章中
但是post出去後,瀏覽器會自動選擇語言
此外,利用cookie做到讓使用者可以選擇希望看到的語言,甚至讓所有語言都呈現出來

We want to write blog once and only once. We want to put all languages into the same post, so it's easy to write/update.
Besdies, auto selection, we also want to select language manually.



原理 How To Do



1. 在文件中使用不同class 的tag包住不同語言:

1. Use tags with different class names to distinguish language.


<div class="lang_en">
This is English
</div>
<div class="lang_zh">
這是中文
</div>



2. 利用 CSS 之 display:none 來隱藏其中一種或多種語言。
Javascript可以讀取browser語言設定: browser.language (FF) / browser.browserLanguage(IE)

定義CSS如下:

2. Then, make use of CSS's display:none to hide text without the language we choose. Javascript can get the browser's language setting: browser.language (FF) / browser.browserLanguage(IE)

The language-selecting CSS:


.lang_zh_off .lang_zh
{ display:none; }

.lang_en_off .lang_en
{ display:none; }



如何利用Javascript修改CSS?最簡單的方法就是修改body(或其他大範圍tag)使其擁有不同className。

But how to modify CSS settings by Javascript? The simplest way it to modify the class name of large-range tag like body.



譬如若要讓body下只顯示中文,那就修改<body class="lang_en_off">
若要兩個都消失,則為 <body class="lang_en_off lang_zh_off">
為什麼要指定消失,而不指定顯示的語言? 因為如果要指定顯示,則必須先預設所有語言文字消失。一旦程式出錯,則整篇文章都沒有了。所以先使預設為全顯示,比較安全。

If we want to show jsut English in body, then change the body tag as:<body class="lang_en_off">. It can be done by Javascript. If we wnat to hide two languages: <body class="lang_en_off lang_zh_off">

A note for the reason why I define the trigger for "off" but not "on": If we let everything "off" but only display some languages triggered by "on", once a program error occurs, no trigger is set. Then nothing will display. We would rather display all text once the program does not work.



注意:class比id好用,因為一個tag可以很容易利用空白分隔,使其包含不同className

PS.
原想利用 <div lang="en"> 這種正規方式來指定語言,但因 IE6 不支援 CSS attribute selector而作罷 (*[lang="en"])

3. 再來重頭戲便是javascript:

3. The most important, the javascript code:




function __set_lang()
{
/*
//simple test:
alert (document.body.className);
document.body.className= "lang_zh_off lang_en_off";

alert (document.body.className);
return;
*/
/*
detectList['zh']='zh';
detectList['en']='en';
detectList['default'] ='en';
*/

// detect browser default language
var langCode = navigator.language || navigator.browserLanguage;
var lang = langCode.toLowerCase();

// detect language
var attr="",found=false;
lang = trim(lang).substr(0,2);

if (lang!="zh") attr+=" lang_zh_off"; else found=true;
//default:en
if (found) attr+=" lang_en_off";

// write body class
var body_attr = body_original_attr;
if (body_attr==null) body_attr="";
document.body.className= body_attr+attr;

// alert ("done:"+document.body.className);
}
function trim(str)
{
return str.replace(/^\s+/, "").replace(/\s+$/, "");
}



利用 .className 修改及讀取body的class屬性。
(原本我用 setAttribute('class',...), 在IE6下竟發現這樣改不能讓CSS正確判斷修改過的class屬性!)



4.最後是把javascript 加到HTML中:

4. Finally, here's the HTML sample code:


<pre>
<body>
<script language="javascript" src="lang.js"></script>
...



這個部份的javascript 一定要加在body之後! (若要使用新版可以直接使用 lang attribute的code, 必須加到HTML最下面</body>附近!!)
另一種方法是修改 body 為 body onload="__set_lang()"

Be aware that this code must be added in the last line before </body> Another way is to modify body tag as: body onload="__set_lang()"



5. 此外cookie和下拉式表單比較簡單 請自行發揮, 或參考我包好的:

5. In addition, we can use cookie to store user preferred language. Display a drop-down list on the page. It is easier and I skip the details. See my packed codes here:

http://www-scf.usc.edu/~chunming/lang.css
http://www-scf.usc.edu/~chunming/lang.js


此blog 和我的首頁就是用這兩個檔案

This site and my personal website are examples that uses the two files above.

Python Basics

Python Basics
Functions
  • Definition: Functions in Python are defined with the following syntax:
    def funct(arg_11, arg_2, ..., arg_n):
    print "This is a function."
    return value
    Any Python code, including other function and class definitions, may appear inside a function. Functions may also be defined within a conditional, but in that case the function's definition must be processed prior to its being called. Python does not support function overloading but does support variable number of arguments, default arguments, and keyword arguments. Return types are not specified by functions.
  • Arguments: Function arguments are passed by value so that if you change the value of the argument within the function, it does not get changed outside of the function. If you want the function to be able to modify non-local variables, you must declare them as global in the first line of the function. Note that if you declare any variables as global, that name cannot be reused in the argument list, i.e. this would cause an error:
    function double(x):
    global x
    x = x*2
    return
    double(x)
    Instead this could be done
    function double(n):
    n = n * 2
    return n
    x = double(x)

    Or
    function doubleX():
    global x
    x = x * 2
    return
    doubleX()

  • Default Arguments: A function may define default values for arguments. The default must be a constant expression or array and any defaults should be on the right side of any non-default arguments.
    def square(x = 5):
    return x*x
    If this function is called with square(), it will return 25. Otherwise, if it is called with square(n) , it will return n^2.
  • Variable length argument lists: Variable length arguments are supported by being wrapped up in a tuple. Before the variable number of arguments, zero or more normal arguments may occur:
    def var_args(arg1, arg2, *args):
  • Keyword arguments: Functions can also be called using arguments of the form keyword = value:
    def player(name, number, team="Florida"):
    print(name + "wears number " + str(number) + "for " + team)
    player("Matt Walsh", 44)
    player(number = 44, name = "David Lee")
    player("Anthony Roberson", number = 1)
    player(name = "J.J. Redick", number = 4, team = "Duke")
  • Return: Values are returned from the function with the return command: return var. You can not return multiple values, but that can be achieved by returning an array or object. Return immediately ends execution of the function and passes control back to the line from which it was called.
  • Variable Functions: Python supports the concept of variable functions. That means that if a variable can point to a function instead of a value. Objects within a method can be called similarly.
    def test():
    print 'This is a test.'
    var = test
    var()
    #this calles test()
    var = circle.setRadius
    var(3)
    #this calls circle.setRadius(3)

Wednesday, April 01, 2009

Automatic Language Selection

Automatic Language Selection

var langcodes=["fr", "es"];

// Browser Language Redirect script
// copyright 3rd January 2006, Stephen Chapman
// permission to use this Javascript on your web page is granted
// provided that all of the code in this script (with the sole exception
// of the langcodes array entries) is used without any alteration

var langCode = navigator.language || navigator.systemLanguage;
var lang = langCode.toLowerCase();
lang = lang.substr(0,2);
var dest = window.location.href;
for (i=langcodes.length-1;i >= 0; i--)
{
if (lang==langcodes[i]){
dest = dest.substr(0,dest.lastIndexOf('.')) + '-' + lang.substr(0,2) + dest.substr(dest.lastIndexOf('.'));
window.location.replace ?window.location.replace(dest) :window.location=dest;
}
}