集合知プログラミングの第3章例をポチポチEmacsで書いていたらどうもスクリプトが動かない。
サンプルコードを落としてきたらそっちは動く。
なにかおかしい。
なんやかんやして数日たったけど、結局のところTypoだった………
キーッ!
#!/usr/bin/env python # -*- coding:utf-8 -*- import feedparser import re # Feedから文字をカウント def getwordcounts(url): # parse the feeds d = feedparser.parse(url) wc = {} # .でのアクセスからDictionaryのアクセスへ for e in d['entries']: if 'summary' in e: summary = e['summary'] else: summary = e['description'] words = getwords(e['title'] + ' ' + summary) for word in words: wc.setdefault(word, 0) wc[word] += 1 return d['feed']['title'], wc def getwords(html): # 全てのhtmlタグを削除 txt = re.compile(r'<[^>]+>').sub('',html) # 全てを非アルファベット文字で分割する words = re.compile(r'[^A-Z^a-z]+').split(txt) # 小文字に変換 return [word.lower() for word in words if word != ''] """ ここからメイン """ apcount = {} wordcounts = {} feedlist = [line for line in file('feedlist.txt')] for feedurl in feedlist: try: title, wc = getwordcounts(feedurl) wordcounts[title] = wc for word, count in wc.items(): apcount.setdefault(word, 0) if count > 1: apcount[word] += 1 except: print 'Failed to parse feed %s' % feedurl wordlist = [] for w, bc in apcount.items(): # 単語の頻出度/ブログ数 frac = float(bc) / len(feedlist) if frac > 0.1 and frac < 0.5: wordlist.append(w) # 単語をリストに追加 out = file('blogdata1.txt', 'w') out.write('Blog') for word in wordlist: out.write('\t%s' % word) out.write('\n') for blog, wc in wordcounts.items(): out.write(blog) for wird in wordlist: if word in wc: out.write('\t%d' % wc[word]) else: out.write('\t0') out.write('\n')