2008-05-01

Jython本を注文した

Jython本を注文しました。

まだAmazonのレビューはない見たいだけど、ブログでは話題になっているようだ。

随所で最高の評価を受けているようなので、とても楽しみです!

今度「Python禁止!JAVAだけで書け!」みたいなプロジェクトに無理矢理入れられそうなので、ちょっとJythonの準備しとくかな。。。

2008-04-30

Python用の自動要約モジュールをcodereposにimportしたよ

けっこう前からのんびり作っていた自動要約モジュールをCodereposにimportしました。

http://coderepos.org/share/browser/lang/python/yoyaku

この自動要約モジュールはまだあまり精度はよくないです。
が、どのぐらい良くないかを知るには、以下のサイトでいちおうサービスとして設置していますので、お試しいただけます。

自動要約サービス Civory

今の所は単純なMMRアルゴリズムで文を抜粋するだけのものですので、まあ、あまり、、、。

今取り組んでいるのは、英文の自動要約の際に、文をまずトピックごとに分割して、その上で要約をかけようとしています。
トピックに分けるアルゴリズムは以下の論文を参考にしています。
Advances in domain independent linear text segmentation
いちおうこれの実装はひとまず終わっていて、それに対応するソースは以下になります。
TopicSegmentationEN.py
今後これにバグがないか注意しながらいろんな例で実験を重ねていく予定です。

NLTKでstemming

nltkにはステミングの有名なアルゴリズムであるporterアルゴリズムが用意されている。

>>> import nltk
>>> nltk.PorterStemmer().stem("application")
'applic'
>>>

2008-04-23

自分のサイトでのユーザーの視線の流れを調べてみる

プレゼンにも最適、ページ内のユーザーの視線をシミュレートする -Feng GUI heatmap

Feng GUI heatmapは、独自のアルゴリズムに基づいて、ユーザーの視線がどのようにページ内で移動するかとその頻度を表示するヒートマップを作成するサービスです。

ということなので、自分のサイトTekTekでもユーザーの視線をシミュレートしてみました。
対象ページ: 書籍「ハイパフォーマンスWebサイト」の詳細ページ

ぱっと見、書籍の画像とAmazonへのリンクに視線が集中するようだ。
しかし、このページのメインはレビューを眺めてもらうことなので、そちらがもっと読みやすくなるようにすべきかもしれない。
なんかよいアイデアはないだろうか。

ついでだが、上記書籍がまだ手元に届かない。早くしてよ〜。

2008-04-20

NLTKで英文の文末判定

英文の文末を判定する簡易なルールベースのアルゴリズム。

Separating sentences

1年前にこの手のアルゴリズムを実装しようとしたが、この問題は非常にやっかいです。
たとえばこんな例:

CELLULAR COMMUNICATIONS INC. sold 1,550,000 common shares at $21.75 each
yesterday, according to lead underwriter L.F. Rothschild & Co.

"INC."の直後や"$21.75"、"L.F."などのピリオドを文末と認識しては大間違いになるのです。

この問題を解決するのに自分が1年前に着目していた論文は以下のもの。

Unsupervised Multilingual Sentence Boundary Detection

この論文では、特に言語を英語だけに限定しない方法を提案しています。
大規模な生のテキストデータから得られる統計情報のみで、文末判定を行えます。
難点はルールベースなどとは違って、事前の綿密な統計の作成、統計処理後の各種特別処理の実装が面倒くさいこと。
昨年途中まで実装したが、Python用の自然言語処理ライブラリNLTKでどうも実装予定との情報を見つけて半端でやめていました。

それで久しぶりに調べてみたら、このアルゴリズムがすでにNLTKで実装されて公開されているではないですか!

早速インストールして使ってみました。

インストール
% wget http://prdownloads.sourceforge.net/nltk/nltk-0.9.2.tar.gz
% tar zxvf nltk-0.9.2.tar.gz
% cd nltk-0.9.2
% sudo python setup.py install
% cd ..
% sudo mkdir /usr/share/nltk
% cd /usr/share/nltk
% sudo wget http://prdownloads.sourceforge.net/nltk/nltk-data-0.9.2.zip
% sudo unzip nltk-data-0.9.2.zip
% sudo chmod -R g+r data
% export NLTK_DATA=/usr/share/nltk/data
% python
>>> import nltk
>>> nltk.corpus.brown.words()
['The', 'Fulton', 'County', 'Grand', 'Jury', 'said', ...]
>>>

ここまでできればインストール完了。

生テキストデータを用意
例えば、Google News(英語版)などからリンクをたどって、ひたすらニュース本文をファイルnews.txtにコピペする。
実験でやるんであれば1000行程度で十分でした。

実験!!
まず生テキストを食わせて学習
>>> from nltk.tokenize import PunktSentenceTokenizer
>>> p = PunktSentenceTokenizer()
>>> fp = file("news.txt")
>>> p.train(fp.read())


次に実際に判定してみる。
判定に使うテキストは次のような少々意地の悪い例
The Finland-based company expects a weaker dollar and slower economic growth in the U.S. and parts of Europe to dampen the overall handset market this year. About half of Nokia's (NOK) sales are in dollars or currencies tied to it; a weaker dollar makes imports more expensive.

"What spooked us was its outlook for the industry in general," said Rick Franklin, equities analyst at Edward Jones.

Nokia reiterated projections that the industry shipments of handsets will grow 10% this year over last. In the first quarter, though, global shipments rose 17%, suggesting a slowdown in the remainder of the year.

For the quarter that ended March 31, Nokia earned $1.9 billion (1.2 euros), up 25% from the same quarter last year but short of an expected $2.3 billion. Overall sales rose 28% to $20.1 billion (12.6 billion euros), roughly in line with views.


>>> a=p.tokenize("""The Finland-based company expects
a weaker dollar and slower economic growth in the U.S. and parts of Europe to
dampen the overall handset market this year. About half of Nokia's (NOK) sales
are in dollars or currencies tied to it; a weaker dollar makes imports more
expensive.

"What spooked us was its outlook for the industry in general," said Rick Franklin,
equities analyst at Edward Jones.

Nokia reiterated projections that the industry shipments of handsets will grow 10%
this year over last. In the first quarter, though, global shipments rose 17%,
suggesting a slowdown in the remainder of the year.

For the quarter that ended March 31, Nokia earned $1.9 billion (1.2 euros), up 25%
from the same quarter last year but short of an expected $2.3 billion. Overall
sales rose 28% to $20.1 billion (12.6 billion euros), roughly in line with views.""")
>>> for x in a:
... print x
... print "-"*20
The Finland-based company expects a weaker dollar and slower economic growth
in the U.S. and parts of Europe to dampen the overall handset market this year.
--------------------
About half of Nokia's (NOK) sales are in dollars or currencies tied to it;
a weaker dollar makes imports more expensive.
--------------------
"What spooked us was its outlook for the industry in general," said Rick Franklin,
equities analyst at Edward Jones.
--------------------
Nokia reiterated projections that the industry shipments of handsets will
grow 10% this year over last.
--------------------
In the first quarter, though, global shipments rose 17%, suggesting a slowdown
in the remainder of the year.
--------------------
For the quarter that ended March 31, Nokia earned $1.9 billion (1.2 euros), up
25% from the same quarter last year but short of an expected $2.3 billion.
--------------------
Overall sales rose 28% to $20.1 billion (12.6 billion euros), roughly in line
with views.
--------------------
>>>

きちんと、ピリオドで文をわけつつも、"U.S."や、"1.2 euros"などで区切るのは避けていることが分かります。

精度を上げるには、もっともっと大量の生テキストを食わせる必要があります。

モンティ・ホール問題

以下のリンク先の問題が興味深い。

Python: モンティ・ホール問題

「プレイヤーは、三つのドアを見せられる。ドアの一つの後ろにはプレイヤーが獲得できる車(アタリ)があり、一方、他の二つのドアには山羊(ハズレ)が 入っている。ホストであるモンティは、それぞれのドアの後ろに何があるか知っているのに対し、もちろんプレイヤーは知らない。

プレイヤーはまず三つのドアの一つを選ぶ。次にモンティは他の二つのドアのうち一つを開け、山羊をみせる。そしてモンティはプレイヤーに、初めの選択のま までよいか、もう一つの閉じているドアに変更するか、どちらかの選択権を提供する。プレイヤーは、選択を変更すべきだろうか?」


変更しない場合: 普通に3つのうちの一つが当たりなので、当たる確率は1/3

変更する場合:
 最初の選択でもしも当たっていたら、(これは1/3の確率でおこる)->変更するためかならずはずれになる。
 最初の選択でもしもはずれていたら、(これは2/3の確率でおこる)->モンティがもう一つのはずれを引いていてくれるので、変更することで必ず当たりになる。
 そのため、変更する場合に当たる確率は2/3

なので、変更したほうが当たる確率が二倍になる!!!

2008-04-14

partty

parttyが面白い。

自分のterminalを晒すことができるwebサービスだそうです。

面識がないにも関わらずtwitterでこっそりfollow している西尾さんが使っているのを見てシビレました。

自分はMacOS 10.4.11だが、上記サイトで配布されているparttyソフトがbus errorとやらで起動しなかったので、ソースをconfigure & makeしました。

gnu screen で複数画面を立ち上げていても、それらがきちんと公開されます。
mysql -uaaa -pxxx somedb
などとうっかり打つと、パスワードが晒されますのでご注意を。