I'm writing a script right now that has to search through an email inbox with the intent of moving out emails that are close enough to one another (using Text::Levenshtein). Right now I'm keeping one array that keeps track of emails we've examined so far and have found no matches for, and as I go along I compare each new email in the INBOX to each item in the array, either marking it to be moved if found or pushing it onto the array if not.
As can be seen, this is going to run in O((n-1)!) (if my 5 year old math is right) which can get a bit bogged down since we're talking about very large numbers of emails here (on the order of 7000). Anyone have any ideas on ways to make this more efficient? I know hashes would search faster than an array, but the text of the emails aren't going to be exactly the same, just very similar.
for my $itemIndex (1..$items->Count) {
my $didWeMove = 0;
(my $body) = ($items->Item($itemIndex)->Body =~ /MessageBody>(.*)<
+\/Mess/s);
if ($body eq " ") {
die "\nError: Found a message not in the proper format. (No <M
+essageBody> tag)";
}
foreach $letterArray (@letterCache) {
my $letter = @$letterArray[0];
my $len = (length($body)>length($letter)) ? length($body) : le
+ngth($letter);
if (distance($body, $letter)/$len < $threshold) {
push(@toBeMoved, $itemIndex); #If we found one, push the i
+ndex onto the list...
push(@toBeMoved, @$letterArray[1]); #...and push the origi
+nal onto the list...
$didWeMove = 1; #And set this to true.
}
}
if (!$didWeMove) { #If we did not find a match, this is a unique
+email (so far)
push (@letterCache, [$body, $itemIndex]); }
Any help?
Thanks,
Ross
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.