集合知プログラミング4章のサンプルプログラム修正パッチ

公開されているサンプルプログラムをそのまま起動させるとエラーが発生してしまったので、
修正パッチを作成しました。

diff --git a/ColIntel/chapter4/nn.py b/ColIntel/chapter4/nn.py
index 1eece9a..c19b082 100755
--- a/ColIntel/chapter4/nn.py
+++ b/ColIntel/chapter4/nn.py
@@ -1,12 +1,12 @@
 from math import tanh
-from pysqlite2 import dbapi2 as sqlite
+import sqlite3
 
 def dtanh(y):
     return 1.0-y*y
 
 class searchnet:
     def __init__(self,dbname):
-      self.con=sqlite.connect(dbname)
+      self.con=sqlite3.connect(dbname)
   
     def __del__(self):
       self.con.close()
diff --git a/ColIntel/chapter4/searchengine.py b/ColIntel/chapter4/searchengine.py
index b88a018..9152b32 100755
--- a/ColIntel/chapter4/searchengine.py
+++ b/ColIntel/chapter4/searchengine.py
@@ -1,7 +1,7 @@
 import urllib2
 from BeautifulSoup import *
 from urlparse import urljoin
-from pysqlite2 import dbapi2 as sqlite
+import sqlite3
 import nn
 mynet=nn.searchnet('nn.db')
 
@@ -12,7 +12,7 @@ ignorewords={'the':1,'of':1,'to':1,'and':1,'a':1,'in':1,'is':1,'it':1}
 class crawler:
   # Initialize the crawler with the name of database
   def __init__(self,dbname):
-    self.con=sqlite.connect(dbname)
+    self.con=sqlite3.connect(dbname)
   
   def __del__(self):
     self.con.close()
@@ -58,7 +58,7 @@ class crawler:
   # Extract the text from an HTML page (no tags)
   def gettextonly(self,soup):
     v=soup.string
-    if v==Null:   
+    if v==None:
       c=soup.contents
       resulttext=''
       for t in c:
@@ -80,7 +80,7 @@ class crawler:
   
   # Add a link between two pages
   def addlinkref(self,urlFrom,urlTo,linkText):
-    words=self.separateWords(linkText)
+    words=self.separatewords(linkText)
     fromid=self.getentryid('urllist','url',urlFrom)
     toid=self.getentryid('urllist','url',urlTo)
     if fromid==toid: return
@@ -171,7 +171,7 @@ class crawler:
 
 class searcher:
   def __init__(self,dbname):
-    self.con=sqlite.connect(dbname)
+    self.con=sqlite3.connect(dbname)
 
   def __del__(self):
     self.con.close()
@@ -205,7 +205,6 @@ class searcher:
 
     # Create the query from the separate parts
     fullquery='select %s from %s where %s' % (fieldlist,tablelist,clauselist)
-    print fullquery
     cur=self.con.execute(fullquery)
     rows=[row for row in cur]
 

ざっくりな解説

  • pysqlite2をpython標準モジュールのsqlite3へ変更
  • Beautiful Soupのstring参照は、空の時Noneが返される
  • separatewords関数を呼び出すべきところがseparateWordsになっている

以上3点を直したパッチになっています。