2008-03-12 23:30:27
かろうじて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)というエラーである。どうすればいいんですか!