いつの間にか夏至もすぎ、
本連載でも何度か書籍の電子化の話題をとりあげたように、
そのような作業をしている中で
「きょうの料理」のテキスト
いうまでもなく
もっとも筆者自身はテレビを見なくなって久しく、

「きょうの料理」
1冊150ページ前後のテキストは、
もちろん
ページをOCRで読み取ってテキストファイル化できれば全文検索も簡単なのになぁ…、
何かいい方法はないかなぁ…、
ページごとのキーワード付け
ページ単位でアクセスするにはzipファイルで1冊の形を保つより、
各ページは"page_
$ cd 2009/01 $ ls page* | sed "s/jpg/jpg: /" > 2009-01.dat $ cat 2009-01.dat page_0001.jpg: page_0002.jpg: page_0003.jpg: ...
次に、
page_0001.jpg: 放送カレンダー
page_0002.jpg: 目次1
page_0003.jpg: 目次2
page_0004.jpg: CM タカラ本みりん
page_0005.jpg: 美しい味ことば 一月 雪花和 しめさば おから
page_0006.jpg: 冬のお手軽洋風おかず
page_0007.jpg: 冬のお手軽洋風おかず
page_0008.jpg: 冬のお手軽洋風おかず 塩田ノア バスク風おかずきんぴら 豚肩ロース ピーマン ごぼう にんにく
page_0009.jpg: 冬のお手軽洋風おかず 塩田ノア バスク風おかずきんぴら 写真
...
最近の横長ディスプレイだと、

当初は、
データベースとWebアプリの作成
システムは以前紹介した
SQLite3の場合、
$ sqlite3 knrdb.sql SQLite version 3.8.5 2014-06-04 14:06:34 Enter ".help" for usage hints. sqlite> create table pages(page text, keyword text); sqlite> .q $
先に作ったデータファイルをこのデータベースに登録する処理は、
1 #!/usr/bin/python
2 # -*- coding: euc-jp -*-
3
4 import sys,os,sqlite3
5
6 def insert_db(cursor, t):
7 try:
8 print("inserting {}".format(t))
9 cursor.execute('insert into pages values(?, ?)', t)
10 except sqlite3.Error, e:
11 print("An error occurred:{}".format(e.args[0]))
12
13 def main():
14 dbname = 'knrdb.sql'
15
16 connection = sqlite3.connect(dbname)
17 cursor = connection.cursor()
18
19 file = sys.argv[1]
20 (year, month) = file.replace('.dat','').split('-')
21
22 with open(file, 'r') as f:
23 lines = f.readlines()
24
25 for l in lines:
26 (short_page, conts) = l.strip().split(':')
27 long_page = year + '-' + month + '-' + short_page
28
29 conts_utf = conts.decode('euc-jp')
30 insert_db(cursor, (long_page, conts_utf))
31 connection.commit()
32
33 if __name__ == "__main__":
34 main()
このコードをinsert.
$ python insert.py 2009-01.dat inserting ('2009-01-page_0001.jpg', u' \u76ee\u6b21 \u30ab\u30ec\u30f3\u30c0\u30fc') inserting ('2009-01-page_0002.jpg', u' \u76ee\u6b211') inserting ('2009-01-page_0003.jpg', u' \u76ee\u6b212') inserting ('2009-01-page_0004.jpg', u' CM \u30bf\u30ab\u30e9\u672c\u307f\u308a\u3093') ...
データが正しく登録されたかsqlite3コマンドで確認します。この際、
$ sqlite3 knrdb.sql SQLite version 3.8.5 2014-06-04 14:06:34 Enter ".help" for usage hints. sqlite> select * from pages; 2009-01-page_0001.jpg| 目次 カレンダー 2009-01-page_0002.jpg| 目次1 2009-01-page_0003.jpg| 目次2 2009-01-page_0004.jpg| CM タカラ本みりん ... sqlite> select * from pages where keyword like '%ベーコン%'; 2009-01-page_0012.jpg| 冬のお手軽洋風おかず 塩田ノア タルティフレット じゃがいも たまねぎ カリフラワー ベーコン カマンベールチーズ 2009-01-page_0017.jpg| 冬のお手軽洋風おかず 城川朝 レンジミネストローネ キャベツ にんじん たまねぎ にんにく じゃがいも ベーコン 赤いんげん豆 ミックスハーブ トマトの水煮 2009-01-page_0019.jpg| 冬のお手軽洋風おかず 城川朝 たらのベーコン巻き 生だら ベーコン ...
データベースはきちんと作成できているようなので、
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta http-equiv="Content-Language" content="ja">
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <title>KNR keyword search</title>
7 </head>
8 <body>
9
10 <?php
11 function DBconnect($name) {
12 $dbname = sprintf("sqlite:%s", $name);
13 try {
14 $db = new PDO($dbname);
15 echo"DB connected<br>\n";
16 }
17 catch (PDOException $e) {
18 exit("cannot connect to DB".$e->getMessage());
19 }
20 return $db;
21 }
22
23 function query_db($db, $key) {
24 $sql = sprintf("select * from pages where keyword like '%%%s%%' order by page", $key);
25 echo "sql:$sql<br>\n";
26 foreach ($db->query($sql) as $line) {
27 $item = array(
28 "page" => trim($line['page']),
29 "keyword" => trim($line['keyword'])
30 );
31 $results[] = $item;
32 }
33 return $results;
34 }
35
36 $dbname = 'knrdb.sql';
37
38 $key = $_GET['key'];
39 echo "key:$key <br>";
40 $db = DBconnect($dbname);
41 $res = query_db($db, $key);
42 foreach ($res as $line) {
43 printf("<a href=\"show.php?id=%s\">%s : %s </a> <br>\n", $line['page'], $line['page'], $line['keyword']);
44 }
45 printf("<br><a href=\"index.html\">検索ページへ戻る </a><br>\n");
46 ?>
47
48 </body>
49 </html>
search.
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta http-equiv="Content-Language" content="ja">
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6
7 <title>KNR search</title>
8 </head>
9 <body>
10 <H2>キーワード入力</H2>
11 <form action="search.php">
12 <p>
13 キーワード<input type="text" name="key">
14 </p>
15 </form>
16
17 </body>
18 </html>
search.
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta http-equiv="Content-Language" content="ja">
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <title>KNR page show</title>
7 </head>
8 <body>
9 <?php
10 $id = $_GET['id'];
11 list($year, $month, $page) = explode('-', $id);
12 printf("<img src=\"Pages/%s/%s/%s\" width=\"800\">\n", $year, $month, $page);
13 ?>
14 </body>
15 </html>
先に作ったSQLite3のデータベースファイルとこれら3つのファイルは、
一方、
$ mkdir ./public_html/KNR_Test/Pages $ sudo mount --bind /Document/Scans/KNR ./public_html/KNR_Test/Pages
動作テスト
以上の環境を整えて、

「卵」

適当なリンクを開いてやると、

今回のコードは必要最低限の機能しか実装していないので、