MediaLab Love Chapter 2

Assistance of MediaLab Love about Javascript test and more...

Google,Yahoo!でむりやり日本語検索をする


GoogleやYahoo!には日本語のページに絞って検索をする機能がありますが、なぜか日本語のページでないものも引っかかることがあります。*1特に、英単語のみで検索した場合はこの様なことが頻繁に起こります。日本語のページを探しているのにもかかわらず、このようなことがおきると非常に不便ですので、私は次の対策を考えました。
日本語のページだけを検索結果に出したいのであれば、日本語でしか使われない文字を検索単語に含めればよいのです。私の場合は、ひらがな、カタカナをOR条件でひっくるめた検索単語を追加しました。つまり、検索単語が
scandinavia
だとすると、
scandinavia い or の or て or か or な...(検索クエリ数の限界まで)
となります。
ここで追加した文字は
漢直ノート 個人による1-gramの差「雑記/えもじならべあそび」の文字頻度 - 雑記/えもじならべあそびを参考に文字の頻度の上位から抜き出しました。記号や漢字は省いてあります。

以下に作成したgreasemonkeyスクリプトとブックマークレットを示します。
Googleブックマークレット

javascript:(function(){if(document.domain.match(/google\.(co\.jp|com)$/)){var%20s=0;var%20sw=0;var%20eq=0;var%20maxq=31;var%20adduri;var%20newuri;var%20orh=new%20Array;var%20hira=new%20Array('\u3044','\u306e','\u3066','\u304b','\u306a','\u3067','\u3063','\u306a','\u3093','\u3068','\u3057','\u305f','\u306b','\u308b','\u3046','\u306f','\u3060','\u3089','\u304c','\u3059','\u30fc','\u3082','\u3092','\u307e','\u3042','\u3063\u3066','\u3057\u3066','\u3067\u3059','\u3066\u3044','\u306a\u3044','\u307e\u3059');var%20q=decodeURI(document.URL).match(/(as_(e|o|ep))?q=.*?(&|$)/g);for(var%20i=0;i<q.length;i++){var%20t=q[i].match(/[^=|&|\+|\u3000]+/g);s+=t.length-1;for(var%20j=0;j<t.length;j++){if(t[j].match(/^(OR|AND)$/)){sw++;}}}maxq=maxq-s-sw;for(var%20k=0;k<maxq;k++){orh.push(hira[k]);}if(document.URL.match(/as_oq=/)){adduri=encodeURI(orh.join(%22+%22));newuri=document.URL.replace(/([&\?]as_oq=.*?)((&|$))/,%22$1%22+'+'+adduri+%22$2%22);}else{adduri=encodeURI(orh.join(%22%20OR%20%22));newuri=document.URL.replace(/([&\?]q=.*?)((&|$))/,%22$1%22+'+'+adduri+%22$2%22);}document.location=newuri.replace(/([&\?]lr=)lang_ja(&|$)/,%22$1%22);}})();

Googlegreasemonkeyスクリプト
Google Another Japanese Search for Greasemonkey
Yahoo!のブックマークレット

javascript:(function(){if(document.domain.match(/search\.yahoo\.(co\.jp|com)$/)){var%20s=0;var%20sw=0;var%20eq=0;var%20maxq=47;var%20adduri;var%20newuri;var%20orh=new%20Array;var%20hira=new%20Array('\u3044','\u306e','\u3066','\u304b','\u306a','\u3067','\u3063','\u306a','\u3093','\u3068','\u3057','\u305f','\u306b','\u308b','\u3046','\u306f','\u3060','\u3089','\u304c','\u3059','\u30fc','\u3082','\u3092','\u307e','\u3042','\u308c','\u3053','\u30f3','\u308a','\u30eb','\u30b9','\u3064','\u304f','\u304d','\u3088','\u305d','\u3055','\u3051','\u3048','\u306d','\u3063\u3066','\u3057\u3066','\u3067\u3059','\u3066\u3044','\u306a\u3044','\u307e\u3059','\u3059\u3002','\u306e\u3067','\u3057\u305f','\u3059\u308b');var%20q=decodeURI(document.URL).match(/v?[apoe]=.*?(&|$)/g);for(var%20i=0;i<q.length;i++){var%20t=q[i].match(/[^=|&|\+|\u3000]+/g);s+=t.length-1;for(var%20j=0;j<t.length;j++){if(t[j].match(/^(OR|AND)$/)){sw++;}}}maxq=maxq-s-sw;for(var%20k=0;k<maxq;k++){orh.push(hira[k]);}if(document.URL.match(/[&\?]vo=/)){adduri=encodeURI(orh.join(%22+%22));newuri=document.URL.replace(/([&\?]vo=.*?)((&|$))/,%22$1%22+'+'+adduri+%22$2%22);}else if(document.URL.match(/[&\?]p=/)){adduri=encodeURI(orh.join(%22%20OR%20%22));newuri=document.URL.replace(/([&\?]p=.*?)((&|$))/,%22$1%22+'+'+adduri+%22$2%22);}else{adduri=encodeURI(orh.join(%22+%22));newuri=document.URL+'&vo='+adduri;}document.location=newuri;}})();||<

Yahoo!のgreasemonkeyスクリプト
Yahoo! Another Japanese Search for Greasemonkey

ブックマークレットは検索結果のページで実行してください。greasemonkeyスクリプトはインストールしておくと検索結果に"Another Japanese Search"というリンクが出るので、それを押すと新しい検索結果に飛びます。

なお、スクリプト中に検索クエリ数を示す部分があります。Googleは検索クエリ数の上限が32語のはずなのですが、ひらがなを使うと31語になってしまうので、31に設定してあります。おそらくGoogle側のバグなのでしょう。またストップ語の判定などは難しく、考慮しておりませんので、検索クエリ数の上限からはみだしてしまうかもしれません。ご了承ください。

Googleの方はウェブ検索だけではなく、ブック検索、コード検索、イメージ検索などほぼすべての検索結果に対して使用することが出来ます。ブログ検索でたまに実行できないときがあるようです。

Yahoo!は画像、音声、動画検索に使うことが出来ません。使えないというよりも検索結果が返ってきません。ブログ検索は検索クエリ数の上限が違うようなのでこれまた使うことが出来ません。

// ==UserScript==
// @name          Google Another Japanese Search
// @namespace     http://d.hatena.ne.jp/Koumei_S/
// @description   Google Japanese Search by inserting hiragana(Japanese letters)
// @include       http://*.google.tld/*
// @version       1.0
// ==/UserScript==

(function(){
	var s=0;
	var sw=0;
	var eq=0;
	var maxq=31;
	var adduri;
	var newuri;
	var link=document.createElement('a');
	var orh=new Array;
	var hira=new Array('\u3044','\u306e','\u3066','\u304b','\u306a','\u3067','\u3063','\u306a','\u3093','\u3068','\u3057','\u305f','\u306b','\u308b','\u3046','\u306f','\u3060','\u3089','\u304c','\u3059','\u30fc','\u3082','\u3092','\u307e','\u3042','\u3063\u3066','\u3057\u3066','\u3067\u3059','\u3066\u3044','\u306a\u3044','\u307e\u3059');
	var q=decodeURI(document.URL).match(/(as_(e|o|ep))?q=.*?(&|$)/g);
	for(var i=0;i<q.length;i++){
		var t=q[i].match(/[^=|&|\+|\u3000]+/g);s+=t.length-1;
		for(var j=0;j<t.length;j++){
			if(t[j].match(/^(OR|AND)$/)){
				sw++;
			}
		}
	}
	maxq=maxq-s-sw;
	for(var k=0;k<maxq;k++){
		orh.push(hira[k]);
	}
	if(document.URL.match(/as_oq=/)){
		adduri=encodeURI(orh.join("+"));
		newuri=document.URL.replace(/([&\?]as_oq=.*?)((&|$))/,"$1"+'+'+adduri+"$2");
	}
	else{
		adduri=encodeURI(orh.join(" OR "));
		newuri=document.URL.replace(/([&\?]q=.*?)((&|$))/,"$1"+'+'+adduri+"$2");
	}
	var insertpoint = document.evaluate("//td[@class='tc']//font", document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
	
	if(insertpoint.snapshotItem(2)){
		link.setAttribute('href',newuri);
		link.innerHTML='\u30fbAnother Japanese Search';
		insertpoint.snapshotItem(2).parentNode.appendChild(link);
	}
})();

*1:SEO業者がわざとそうしているのかもしれませんが…