次は日本語の文書を検索して結果をkwic表示してみようと考えた。日本語は分かち書きされていないので、形態素解析などする必要がある。「する」と「した」を同じ動詞として並べようとしたらそれなりの手間が必要になってくる。日本語形態素解析はいつものようにMeCabで。Pythonを含むスクリプト言語のバインディングも用意されているので、それをインストールして利用する。とりあえず下のようなのを書いてみた。何だかずいぶん要領が悪そうなものになってみっともないが、ここはほとんど見に来る人もいないからまあいいか。自分用の覚書である。
#!/usr/bin/ruby
# vim: set fileencoding=utf-8 :
import sys
import re
import MeCab
t = MeCab.Tagger()
word = sys.argv[1]
file = sys.argv[2]
wpos = int(sys.argv[3])
txt = open(file,'r').read()
txt = re.sub('\x0D\x0A|\x0A|\x0D', '\n', txt)
txt = re.sub('\n', '', txt)
txt = re.sub('――', 'ーー', txt)
txt = re.sub('……', '・・', txt)
txt = re.sub('《.+?》', '', txt)
txt = re.sub('[#.+?]', '', txt)
txt = re.sub('「', '', txt)
txt = re.sub('」', '。', txt)
lno = 1
sentences = txt.split('。')
reslist = []
for sentence in sentences:
stlist = []
m = t.parse(sentence)
res = m.rstrip(" \n").split("\n")
for rs in res:
elements = rs.split(',')
if elements.count(word) > 0:
for rs2 in res:
elms = rs2.split(',')
if len(elms) > 6:
stlist.append(elms[6])
if len(stlist) > 0:
stlist.append(sentence)
reslist.append(stlist)
newlist = []
for res in reslist:
pos = res.index(word)
if pos == 0:
nlist = ['','',res[0],res[1],res[2]]
elif pos == 1:
nlist = ['',res[0],res[1],res[2],res[3]]
else:
nlist = res[pos-2:pos+2]
nlist.append(res[-1])
newlist.append(nlist)
freqlist = []
for result in newlist:
freqlist.append(result[wpos])
wdict = {}
get = wdict.get
for fw in freqlist:
wdict[fw] = get(fw, 0) + 1
for item in newlist:
item.append(wdict[item[wpos]])
item.reverse()
newlist.sort()
newlist.reverse()
for line in newlist:
pos = line[1].find(word)
if pos >= 0:
if pos > 30:
left = line[1][pos-30:pos]
else:
left = line[1][0:pos]
left = ' '*((30-len(left))/3) + left
right = line[1][pos:(pos + len(word) + 30)]
output = left + right
if len(str(line[0])) == 1:
nr = ' ' + str(line[0])
else:
nr = str(line[0])
print nr, output
これをkmecab.pyという名前で保存したので、python kmecab.py keyword filename [0, 1, 3, or 4] とすれば、0ではキーワードの二つ前の語で並べ替え、1では一つ前、3で一つ後ろ、4で二つ後ろで並べ替えるようになる。並べ替えは頻度順である。夏目漱石の『吾輩は猫である』を検索対象として試してみると、
python kmecab.py 変化 neko8.txt 3 13 事は禅寺の筍が若竹に変化する勢で大きくなる 13 かえってこれを電力に変化して立派に役に立たせ 13 明は自分以外の状態を変化させて満足を求めるの 13 じ吾輩の眼玉のごとく変化する 13 門君一人の運命がどう変化しようと、主人の朝夕 13 て、あれは上段の間の変化したもので、上使が坐 13 眼球のように間断なく変化している 13 とも薬缶とも銅壺とも変化するだろう 13 の曾呂崎を天然居士に変化させたのは一体誰の所 13 られて細い高い隆起と変化して参りますーー実に 13 いの事で君子の挙動の変化する訳がない 13 降りようと企てた者が変化して落ちる事になる 13 いかに流行が変化し易くったって、横に 7 製作の上に変化をあらわすのが困難で 7 成算があってかほどの変化を示したものか、また 7 、目まぐるしきまでに変化を生ぜしめたのはかえ 7 両端を叩いて黒白の変化を同一物の上に起こす 7 画工が精力を消耗して変化を求めた顔でも十二三 7 ならば世界の表面に大変化を来したろうと 7 つうち、発音に自然と変化を生じてくるのは、彼 4 の第二則に曰く運動の変化は、加えられたる力に 4 脳漿一勺の化学的変化はとにかく意志の動い 4 想像力がないとこんな変化は出来んのである 4 由に肉が骨に一足飛に変化は出来ますまい 2 相違ないが、何となく変化に乏しい 2 吾輩はまだかかる心的変化に遭逢した事はない 1 吾輩も彼等の変化なき雑談を終日聞かね 1 物の形や、色の精細な変化などがよく分るようだ後ろに続く語で並べ替えた結果である。これはまあうまくできたように見える。「する」「を」「は」「に」「なき」「など」という順である。前の語で並べると、
python kmecab.py 変化 neko8.txt 1 6 の第二則に曰く運動の変化は、加えられたる力に 6 製作の上に変化をあらわすのが困難で 6 成算があってかほどの変化を示したものか、また 6 事は禅寺の筍が若竹に変化する勢で大きくなる 6 かえってこれを電力に変化して立派に役に立たせ 6 、目まぐるしきまでに変化を生ぜしめたのはかえ 6 吾輩も彼等の変化なき雑談を終日聞かね 6 て、あれは上段の間の変化したもので、上使が坐 6 両端を叩いて黒白の変化を同一物の上に起こす 6 由に肉が骨に一足飛に変化は出来ますまい 6 の曾呂崎を天然居士に変化させたのは一体誰の所 6 いの事で君子の挙動の変化する訳がない 2 られて細い高い隆起と変化して参りますーー実に 2 降りようと企てた者が変化して落ちる事になる 2 つうち、発音に自然と変化を生じてくるのは、彼 2 いかに流行が変化し易くったって、横に 1 相違ないが、何となく変化に乏しい 1 脳漿一勺の化学的変化はとにかく意志の動い 1 明は自分以外の状態を変化させて満足を求めるの 1 じ吾輩の眼玉のごとく変化する 1 門君一人の運命がどう変化しようと、主人の朝夕 1 物の形や、色の精細な変化などがよく分るようだ 1 眼球のように間断なく変化している 1 画工が精力を消耗して変化を求めた顔でも十二三 1 想像力がないとこんな変化は出来んのである 1 とも薬缶とも銅壺とも変化するだろう 1 ならば世界の表面に大変化を来したろうと 1 吾輩はまだかかる心的変化に遭逢した事はない何だか変だ。実は「の」と「に」の頻度が同じなので混ざってしまっているのだ。「が」と「と」もそうだ。頻度が同じもの同士の中でまとめてくれない。そういう考慮をしていないから、当然なのだが。二つ前、二つ後ろでやってみたら、全然並べ替えられなかった。キーワードが活用する語だと、頻度と結果が一致しないことになってしまうことにも気がついた。最後の表示用の整形のときには活用を考慮していないから。実際に作ってみるといろいろ考えなければならないことが判ってくる。
キーワードの前後の単語の出現頻度の順で並べ替えてみる。
#!/usr/bin/ruby
# vim: set fileencoding=utf-8 :
import sys
import re
word = sys.argv[1]
file = sys.argv[2]
bf = sys.argv[3]
length = len(word)
txt = open(file,'r').read()
txt = re.sub('\x0D\x0A|\x0A|\x0D', '\n', txt)
txt = re.sub('(\n)+', ' ', txt)
txt = re.sub('\t', ' ', txt)
txt = re.sub('<.+?>', ' ', txt)
lines = []
bfwds = []
strt = 1
while strt > 0:
pos = txt.find(word,strt)
if pos == -1: break
left = txt[pos-30:pos]
right = txt[pos:(pos + len(word) + 30)]
bwords = left.split(' ')
bwords.reverse()
if bf == 'f':
rstring = right[length:]
fwords = rstring.split(' ')
if len(fwords[0]) == 0:
fword = fwords[1]
else:
fword = fwords[0]
lines.append([str(pos),right,left,fword])
bfwds.append(fword)
else:
lines.append([str(pos),right,left,bwords[1]])
bfwds.append(bwords[1])
strt = pos + 1
wdict = {}
get = wdict.get
for bf in bfwds:
wdict[bf] = get(bf, 0) + 1
for line in lines:
line.append(wdict[line[3]])
line.reverse()
lines.sort()
lines.reverse()
for line in lines:
print line[2].rjust(25) + line[3] + ":" + line[4]
本当はスクリプトの中に説明をきちんと書くべきなんだろう。wdict={}のところで、キーワードの前あるいは次の単語リストを集計している。その集計値を二次元リストに追加してソートして表示する。やってみると呆気ない。結果は次のとおり。
$ python kwicfq.py moonlight drodr10.txt b w, or whether it was only the moonlight, he never knew. Their spirits:356000 traits that were clear in the moonlight eyed him with absolute apathy:217201 re was glowing redly into the moonlight through the wide door made fo:199239 rafina was moving through the moonlight as though its rays were her s:218946 oks they had given him in the moonlight, and all looked back at him w:355650 o danced, so sparkled. In the moonlight also one makes no unworthy st:19383 merry play of my sword in the moonlight was often a joy to see, it so:19303 longer than man's. And if the moonlight streamed on untroubled, and t:204443 layed languorous music in the moonlight and sang soft by her low balc:44267 ho might be familiar with the moonlight in that shadowy chamber shoul:42272 he sunset grew dimmer and the moonlight stole in softly, as a cat mig:193054 hat now approached him in the moonlight round a corner of the house w:205397 hat had frowned at him in the moonlight when he came here before, fro:355299 grow dimmer, and glide to the moonlight again that streamed through a:218073 elf in a long hall lit by the moonlight only, which was looking in th:216337 ch lies always so near to the moonlight, and was not in front of him :215294 at, if interrupters come, the moonlight is better suited to the play :19175 a sheer wall that even in the moonlight fell into blackness. Rodrigue:38713 . He saw her pass through the moonlight and grow dimmer, and glide to:218029 panish gardens, remembered by moonlight in Spring, for the other end :20838 apiers crossing each other by moonlight begun to gleam in the street :206461 when her lover came again by moonlight had cast them all from her fr:44481 n white, and all shining with moonlight, came Serafina. Rodriguez in :217931 a chamber partly shining with moonlight. "In there," said the man tha:216854 er, lurking along the edge of moonlight and darkness, disappearing an:217555 For some moments the spell of moonlight on sunlight hovered: the air :194333 enough, it was not yet wholly moonlight when cantering hooves came do:350318 night, all drenched in white moonlight, sheltering huge darkness in :121534 rily towards him in the clear moonlight with a sword. Morano was frig:210798 all alone with disaster, and moonlight pouring down, and the black g:122007 all down the long room across moonlight and blackness, came the lady :221914 And choose, my son, rather a moonlight night when you sing under tho:18413次に、キーワードの次の単語で並べ替えた場合。
$ python kwicfq.py moonlight drodr10.txt f w, or whether it was only the moonlight, he never knew. Their spirits:356000 oks they had given him in the moonlight, and all looked back at him w:355650 n white, and all shining with moonlight, came Serafina. Rodriguez in :217931 ch lies always so near to the moonlight, and was not in front of him :215294 night, all drenched in white moonlight, sheltering huge darkness in :121534 layed languorous music in the moonlight and sang soft by her low balc:44267 er, lurking along the edge of moonlight and darkness, disappearing an:217555 all down the long room across moonlight and blackness, came the lady :221914 . He saw her pass through the moonlight and grow dimmer, and glide to:218029 hat had frowned at him in the moonlight when he came here before, fro:355299 enough, it was not yet wholly moonlight when cantering hooves came do:350318 panish gardens, remembered by moonlight in Spring, for the other end :20838 ho might be familiar with the moonlight in that shadowy chamber shoul:42272 rily towards him in the clear moonlight with a sword. Morano was frig:210798 merry play of my sword in the moonlight was often a joy to see, it so:19303 re was glowing redly into the moonlight through the wide door made fo:199239 longer than man's. And if the moonlight streamed on untroubled, and t:204443 he sunset grew dimmer and the moonlight stole in softly, as a cat mig:193054 hat now approached him in the moonlight round a corner of the house w:205397 all alone with disaster, and moonlight pouring down, and the black g:122007 elf in a long hall lit by the moonlight only, which was looking in th:216337 For some moments the spell of moonlight on sunlight hovered: the air :194333 And choose, my son, rather a moonlight night when you sing under tho:18413 at, if interrupters come, the moonlight is better suited to the play :19175 when her lover came again by moonlight had cast them all from her fr:44481 a sheer wall that even in the moonlight fell into blackness. Rodrigue:38713 traits that were clear in the moonlight eyed him with absolute apathy:217201 apiers crossing each other by moonlight begun to gleam in the street :206461 rafina was moving through the moonlight as though its rays were her s:218946 o danced, so sparkled. In the moonlight also one makes no unworthy st:19383 grow dimmer, and glide to the moonlight again that streamed through a:218073 a chamber partly shining with moonlight. "In there," said the man tha:216854もっと手間をかければ、二つ前、二つ先といった単語でも並べ替えられる。
私はPHPでこういったことができるようになるのに、一年近くかかっている。しかも、MySQLと組み合わせないと頻度順の並べ替えができなかった。
再びコマンドラインに戻ってkwic。今度は前後の単語のアルファベット順に並べてみる。
#!/usr/bin/ruby
# vim: set fileencoding=utf-8 :
import sys
import re
word = sys.argv[1]
file = sys.argv[2]
bf = sys.argv[3]
txt = open(file,'r').read()
txt = re.sub('\x0D\x0A|\x0A|\x0D', '\n', txt)
txt = re.sub('(\n)+', ' ', txt)
txt = re.sub('\t', ' ', txt)
txt = re.sub('<.+?>', ' ', txt)
lines = []
strt = 1
while strt > 0:
pos = txt.find(word,strt)
if pos == -1: break
left = txt[pos-30:pos]
right = txt[pos:(pos + len(word) + 30)]
bwords = left.split(' ')
bwords.reverse()
fwords = right.split(' ')
if bf == 'f':
lines.append([fwords[1],left,right,str(pos)])
else:
lines.append([bwords[1],left,right,str(pos)])
strt = pos + 1
lines.sort()
for line in lines:
print line[1].rjust(25) + line[2] + ":" + line[3]
キーワードの前の単語あるいは後ろ(一つ先)の単語を先頭にして、続いて検索語の前30文字、後ろ30文字、出現位置という順のリストを項目とする二次元リストを作って、ソートしてから表示させるだけ。前回と同じファイルを検索対象文書としてmoonlightを調べてみる。まずはキーワードの前の単語で:
$ python kwic.py moonlight drodr10.txt b And choose, my son, rather a moonlight night when you sing under tho:18413 all down the long room across moonlight and blackness, came the lady :221914 all alone with disaster, and moonlight pouring down, and the black g:122007 when her lover came again by moonlight had cast them all from her fr:44481 apiers crossing each other by moonlight begun to gleam in the street :206461 panish gardens, remembered by moonlight in Spring, for the other end :20838 rily towards him in the clear moonlight with a sword. Morano was frig:210798 For some moments the spell of moonlight on sunlight hovered: the air :194333 er, lurking along the edge of moonlight and darkness, disappearing an:217555 . He saw her pass through the moonlight and grow dimmer, and glide to:218029 a sheer wall that even in the moonlight fell into blackness. Rodrigue:38713 at, if interrupters come, the moonlight is better suited to the play :19175 ch lies always so near to the moonlight, and was not in front of him :215294 elf in a long hall lit by the moonlight only, which was looking in th:216337 grow dimmer, and glide to the moonlight again that streamed through a:218073 hat had frowned at him in the moonlight when he came here before, fro:355299 hat now approached him in the moonlight round a corner of the house w:205397 he sunset grew dimmer and the moonlight stole in softly, as a cat mig:193054 ho might be familiar with the moonlight in that shadowy chamber shoul:42272 layed languorous music in the moonlight and sang soft by her low balc:44267 longer than man's. And if the moonlight streamed on untroubled, and t:204443 merry play of my sword in the moonlight was often a joy to see, it so:19303 o danced, so sparkled. In the moonlight also one makes no unworthy st:19383 oks they had given him in the moonlight, and all looked back at him w:355650 rafina was moving through the moonlight as though its rays were her s:218946 re was glowing redly into the moonlight through the wide door made fo:199239 traits that were clear in the moonlight eyed him with absolute apathy:217201 w, or whether it was only the moonlight, he never knew. Their spirits:356000 night, all drenched in white moonlight, sheltering huge darkness in :121534 enough, it was not yet wholly moonlight when cantering hooves came do:350318 a chamber partly shining with moonlight. "In there," said the man tha:216854 n white, and all shining with moonlight, came Serafina. Rodriguez in :217931次にキーワードの一つ先の語で:
python kwic.py moonlight drodr10.txt f a chamber partly shining with moonlight. "In there," said the man tha:216854 grow dimmer, and glide to the moonlight again that streamed through a:218073 o danced, so sparkled. In the moonlight also one makes no unworthy st:19383 . He saw her pass through the moonlight and grow dimmer, and glide to:218029 all down the long room across moonlight and blackness, came the lady :221914 ch lies always so near to the moonlight, and was not in front of him :215294 er, lurking along the edge of moonlight and darkness, disappearing an:217555 layed languorous music in the moonlight and sang soft by her low balc:44267 oks they had given him in the moonlight, and all looked back at him w:355650 rafina was moving through the moonlight as though its rays were her s:218946 apiers crossing each other by moonlight begun to gleam in the street :206461 n white, and all shining with moonlight, came Serafina. Rodriguez in :217931 traits that were clear in the moonlight eyed him with absolute apathy:217201 a sheer wall that even in the moonlight fell into blackness. Rodrigue:38713 when her lover came again by moonlight had cast them all from her fr:44481 w, or whether it was only the moonlight, he never knew. Their spirits:356000 ho might be familiar with the moonlight in that shadowy chamber shoul:42272 panish gardens, remembered by moonlight in Spring, for the other end :20838 at, if interrupters come, the moonlight is better suited to the play :19175 And choose, my son, rather a moonlight night when you sing under tho:18413 For some moments the spell of moonlight on sunlight hovered: the air :194333 elf in a long hall lit by the moonlight only, which was looking in th:216337 all alone with disaster, and moonlight pouring down, and the black g:122007 hat now approached him in the moonlight round a corner of the house w:205397 night, all drenched in white moonlight, sheltering huge darkness in :121534 he sunset grew dimmer and the moonlight stole in softly, as a cat mig:193054 longer than man's. And if the moonlight streamed on untroubled, and t:204443 re was glowing redly into the moonlight through the wide door made fo:199239 merry play of my sword in the moonlight was often a joy to see, it so:19303 enough, it was not yet wholly moonlight when cantering hooves came do:350318 hat had frowned at him in the moonlight when he came here before, fro:355299 rily towards him in the clear moonlight with a sword. Morano was frig:210798前が「back」のb、先が「forward」のfで指示しているが、実はbは不要。省略すると検索語の前の単語の順で並べるようになっている。この場合、文末や文頭を区別できないので、前の文や次の文の単語がでてきてしまう。そこで次のようにしてみた。キーワードの後ろの8文字を取得するようにしたのである。単語を認識していない。
#!/usr/bin/ruby
# vim: set fileencoding=utf-8 :
import sys
import re
word = sys.argv[1]
file = sys.argv[2]
bf = sys.argv[3]
length = len(word)
txt = open(file,'r').read()
txt = re.sub('\x0D\x0A|\x0A|\x0D', '\n', txt)
txt = re.sub('(\n)+', ' ', txt)
txt = re.sub('\t', ' ', txt)
txt = re.sub('<.+?>', ' ', txt)
lines = []
strt = 1
while strt > 0:
pos = txt.find(word,strt)
if pos == -1: break
left = txt[pos-30:pos]
right = txt[pos:(pos + len(word) + 30)]
bwords = left.split(' ')
bwords.reverse()
fwords = right.split(' ')
if bf == 'f':
lines.append([right[length:length + 8],left,right,str(pos)])
else:
lines.append([bwords[1],left,right,str(pos)])
strt = pos + 1
lines.sort()
for line in lines:
print line[1].rjust(25) + line[2] + ":" + line[3]
すると次のような結果になる。
$ python kwic.py moonlight drodr10.txt f grow dimmer, and glide to the moonlight again that streamed through a:218073 o danced, so sparkled. In the moonlight also one makes no unworthy st:19383 all down the long room across moonlight and blackness, came the lady :221914 er, lurking along the edge of moonlight and darkness, disappearing an:217555 . He saw her pass through the moonlight and grow dimmer, and glide to:218029 layed languorous music in the moonlight and sang soft by her low balc:44267 rafina was moving through the moonlight as though its rays were her s:218946 apiers crossing each other by moonlight begun to gleam in the street :206461 traits that were clear in the moonlight eyed him with absolute apathy:217201 a sheer wall that even in the moonlight fell into blackness. Rodrigue:38713 when her lover came again by moonlight had cast them all from her fr:44481 panish gardens, remembered by moonlight in Spring, for the other end :20838 ho might be familiar with the moonlight in that shadowy chamber shoul:42272 at, if interrupters come, the moonlight is better suited to the play :19175 And choose, my son, rather a moonlight night when you sing under tho:18413 For some moments the spell of moonlight on sunlight hovered: the air :194333 elf in a long hall lit by the moonlight only, which was looking in th:216337 all alone with disaster, and moonlight pouring down, and the black g:122007 hat now approached him in the moonlight round a corner of the house w:205397 he sunset grew dimmer and the moonlight stole in softly, as a cat mig:193054 longer than man's. And if the moonlight streamed on untroubled, and t:204443 re was glowing redly into the moonlight through the wide door made fo:199239 merry play of my sword in the moonlight was often a joy to see, it so:19303 enough, it was not yet wholly moonlight when cantering hooves came do:350318 hat had frowned at him in the moonlight when he came here before, fro:355299 rily towards him in the clear moonlight with a sword. Morano was frig:210798 oks they had given him in the moonlight, and all looked back at him w:355650 ch lies always so near to the moonlight, and was not in front of him :215294 n white, and all shining with moonlight, came Serafina. Rodriguez in :217931 w, or whether it was only the moonlight, he never knew. Their spirits:356000 night, all drenched in white moonlight, sheltering huge darkness in :121534 a chamber partly shining with moonlight. "In there," said the man tha:216854コンマやピリオドは下の方にまとめられる。どちらがいいかは好みの問題だと思う。
次は頻度順に並べるにはどうしたらいいかを考えてみる。
かろうじてhtmlの表示ができるようになったので、前に作ったkwic表示スクリプトをオンラインで利用できるようにしてみることにした。今回は、対象テキストはロード・ダンセイのDon Rodriguez; chronicles of Shadow Valleyである。『影の谷年代記』として邦訳がある。しかし、何ともみっともないなあ。全然判っていないことが一目瞭然だろう。一応、動くことは動く。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import re
fname = '/path/drodr10.txt'
txt = open(fname,'r').read()
txt = re.sub('\x0D\x0A|\x0A|\x0D', '\n', txt)
txt = re.sub('(\n)+', ' ', txt)
txt = re.sub('\t', ' ', txt)
txt = re.sub('<.+?>', ' ', txt)
def index(req,word):
html = '''
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>kwic</title>
</head>
<style type="text/css">
strong { color:blue; }
</style>
<body>
<h3>kwic</h3>
<p>入力された文字は、「%s」です。</p>
<form action="test099.py" method="get">
<input type="text" name="word" />
<input type="submit" />
</form>
'''
if len(word)>3:
table = "<table border='0'>\n";
strt = 1
while strt > 0:
pos = txt.find(word,strt)
if pos == -1: break
left = txt[pos-30:pos]
right = txt[pos:(pos + len(word) + 30)]
table = table + "<tr><td align='right'>" + left + "</td><td>" + right + "</td><td align='right'>" + str(pos) + "</td></tr>\n"
strt = pos + 1
table = table + "</table>\n"
table = re.sub(word,'<strong>' + word + '</strong>',table)
html = html + table + "<hr />\n</body>\n</html>\n"
else:
html = html + "<hr />\n</body>\n</html>\n"
return html %(word)
table = table + xxxというのはどうにかならないのか。PHPの".="みたいなものはないのか。前にも書いたけど。この作品からmoonlightを検索すると以下のような結果になった。
このページは、最初に何も入力せずに開こうとするとエラーが出てしまう。TypeError: index() takes exactly 2 non-keyword arguments (1 given)というエラーである。どうすればいいんですか!
ubuntuにmod_pythonをインストールしてみた。ubuntuの場合、synapticなどで、libapache2-mod-pythonをインストールするだけである。その後、スクリプトを動かしたい場所(たとえば/var/www)の<Directory>の項に以下の3行を追加。
AddHandler mod_python .py PythonHandler mod_python.publisher PythonDebug Onそして、Apacheの再起動。この設定は、.htaccessに書いてもいいらしい。
試しに、時刻を表示させてみる。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
def index(req):
html = '''
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>現在の日時を表示する</title>
</head>
<body>
<h1>現在の日時を表示する</h1>
<p>現在の日時は、「%s」です。</p>
</body>
</html>
'''
now = time.strftime('%Y年%m月%d日 %H時%M分%S秒')
return html % now
問題なく動いたようなので、次に文字列を送ってそれを受け取り、表示させてみた。
#!/usr/bin/python
# -*- coding: utf-8 -*-
def index(req,words):
html = '''
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>テキスト入力フィールドに入力された文字を取得する</title>
</head>
<body>
<h1>テキスト入力フィールドに入力された文字を取得する</h1>
<p>入力された文字は、「%s」です。</p>
<form action="test09.py" method="get">
<input type="text" name="words" />
<input type="submit" />
</form>
</body>
</html>
'''
return html %(words)
実はこれに何時間もかかっている。上のようにGETだと受け取れるのだが、POSTだと受け取れない。どうしてなのだろう。どうしたらいいのか判らない。ネットで調べても、mod_pythonの日本語の例がほとんど見つからない。皆、使っていないのだろうか。こういうhtmlページの書き出しはPHPの方が楽なような気がする。慣れの問題かも知れないけど。PHPやMySQLで"strrev(string)"とか"reverse(string)"とすれば簡単に文字列を逆転させることができるのに、Pythonにはない。なぜなんだ! と思っていたが、以下のようにして簡単に定義できると判った。こうしておけば、strrev(string)でPHPなどと同じように文字列を逆転できる。
def strrev(s): return s[::-1]でもどうしてこれで逆転するんだ。[::-1]なんて私には理解できない呪文である。