搜索安然电子邮件的语料库,查找往返于证券欺诈者Ken Lay的电子邮件。
一封名为workdocs
的500k +电子邮件的电子邮件文档的结构如下:
一个这样的文件:
{'headers': {'To': 'eric.bass@enron.com', 'Subject': 'Re: Plays and other information', 'X-cc': '', 'X-To': 'Eric Bass', 'Date': 'Tue, 14 Nov 2000 08:22:00 -0800 (PST)', 'Message-ID': '<6884142.1075854677416.JavaMail.evans@thyme>', 'From': 'michael.simmons@enron.com', 'X-From': 'Michael Simmons', 'X-bcc': ''}, 'subFolder': 'notes_inbox', 'mailbox': 'bass-e', '_id': ObjectId('4f16fc97d1e2d32371003e27'), 'body': "the scrimmage is still up in the air...\n\n\nwebb said that they didnt want to scrimmage...\n\nthe aggies are scrimmaging each other... (the aggie teams practiced on \nSunday)\n\nwhen I called the aggie captains to see if we could use their field.... they \nsaid that it was tooo smalll for us to use...\n\n\nsounds like bullshit to me... but what can we do....\n\n\nanyway... we will have to do another practice Wed. night.... and I dont' \nknow where we can practice.... any suggestions...\n\n\nalso, we still need one more person..."}
我感兴趣的字段为{'To':...,'From':...,'X-cc':...,'X-bcc':...}
,可在字段'headers'
中找到。
在整个文档中搜索'klay@enron'
似乎可以使用workdocs.find({'$text':{'$search':'klay@enron.com'}})
,但我有兴趣使用正则表达式捕获许多可能的电子邮件别名。如何在字段ken_email
,To
,From
和X-bcc
中找到与正则表达式X-cc
(下方)匹配的文档?
from pymongo import MongoClient
import re
re_email = '^(K|Ken|Kenneth)[A-Z0-9._%+-]*Lay@[A-Z0-9._%+-]+\.[A-Z]{2,4}$'
ken_email = re.compile(re_email, re.IGNORECASE)
答案 0 :(得分:1)
要仅搜索这四个字段,您可以使用:
(?:to|from|x-b?cc)'\s*:\s*'K[A-Z0-9._%+-]*Lay@[A-Z0-9._%+-]+\.[A-Z]{2,4}
该版本删除了他的名字周围的捕获组,这对于匹配发生是不必要的。 (在正则表达式完成后提取会更快。)
我也不相信验证电子邮件地址是必要的。您已经在查看除了电子邮件地址之外什么都没有的字段了。你可以进一步缩短正则表达式:
(?:to|from|x-b?cc)'\s*:\s*'K[A-Z0-9._%+-]*Lay
这将增加匹配klay123@example.com
效率不高(特别是长串文字),但有一些方法可以加快速度。最简单的方法是事先移除身体。 (这也可能有助于防止误报。)您可以在第一个}
之后删除所有内容。
只是为了踢,这里有一个匹配的正则表达式:
\}.*
只需用空字符串替换即可将其删除。