2009年3月12日木曜日

mlのアーカイブからなんかを抽出する(2)

このエントリーをブックマークに追加 このエントリーを含むはてなブックマーク
ここから先は正規表現の時間。position idとmatch idのペアを捕まえて、message idとさらにpairにしてあげればよい。

GNU Backgammon Position ID: bgAAALYOjAoMAA
Match ID : MAHgAEAAIAAA
+24-23-22-21-20-19------18-17-16-15-14-13-+
OO | X O O | | X X X X |
OO | X O O | | X |
OO | O | | |
OO | | | |
OO | | | |
| |BAR| |v
| | | |
| | | |
| | | X |
| X X | | X |
| X X X | | X |
+-1--2--3--4--5--6-------7--8--9-10-11-12-+

1つのメッセージの中には1つしかpositionが出てこないと仮定してsearchしgroupdictしてあげる。



import re
import email
import email.Iterators
import mailbox

r = re.compile((r"(Position +ID *: *(?P<position>[a-zA-Z0-9]{14}))"
r"|(Match +ID *: *(?P<match>[a-zA-Z0-9]{12}))"
r"|(24-23-22-21-20-19------18-17-16-15-14-13-)"))

f= open('all')#2002-06')
mbox = mailbox.UnixMailbox(f, email.message_from_file)
for msg in mbox:
for part in email.Iterators.typed_subpart_iterator(msg):
b = part.get_payload()
enc = part.get_content_charset()
if not enc:
enc = 'us-ascii'
try:
u = b.decode(enc)
except (UnicodeError, LookupError):
continue
found = r.search(u)
if found:
d = found.groupdict()
print msg['message-id'], "%s:%s"%(d['position'], d['match'])


IDをまったく書かないでpositionを投稿なさっている方がいるらしい。勘弁して。

[nori@asama]~/Desktop/work/gnubg/bug-ml% grep 'None:None' positions.txt | wc
17 34 1104

0 件のコメント: