1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # EmailAddressExtractor - helper plugin that extracts email addresses from text
|
---|
4 | #
|
---|
5 | # A component of the Greenstone digital library software
|
---|
6 | # from the New Zealand Digital Library Project at the
|
---|
7 | # University of Waikato, New Zealand.
|
---|
8 | #
|
---|
9 | # Copyright (C) 2008 New Zealand Digital Library Project
|
---|
10 | #
|
---|
11 | # This program is free software; you can redistribute it and/or modify
|
---|
12 | # it under the terms of the GNU General Public License as published by
|
---|
13 | # the Free Software Foundation; either version 2 of the License, or
|
---|
14 | # (at your option) any later version.
|
---|
15 | #
|
---|
16 | # This program is distributed in the hope that it will be useful,
|
---|
17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | # GNU General Public License for more details.
|
---|
20 | #
|
---|
21 | # You should have received a copy of the GNU General Public License
|
---|
22 | # along with this program; if not, write to the Free Software
|
---|
23 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
24 | #
|
---|
25 | ###########################################################################
|
---|
26 |
|
---|
27 | package EmailAddressExtractor;
|
---|
28 |
|
---|
29 | use PrintInfo;
|
---|
30 | use strict;
|
---|
31 |
|
---|
32 | use gsprintf 'gsprintf';
|
---|
33 |
|
---|
34 | BEGIN {
|
---|
35 | @EmailAddressExtractor::ISA = ('PrintInfo');
|
---|
36 | }
|
---|
37 |
|
---|
38 | my $arguments = [
|
---|
39 | { 'name' => "extract_email",
|
---|
40 | 'desc' => "{EmailAddressExtractor.extract_email}",
|
---|
41 | 'type' => "flag",
|
---|
42 | 'reqd' => "no" },
|
---|
43 | { 'name' => "new_extract_email",
|
---|
44 | 'desc' => "",
|
---|
45 | 'type' => "flag",
|
---|
46 | 'reqd' => "no",
|
---|
47 | 'hiddengli' => "yes" }
|
---|
48 | ];
|
---|
49 |
|
---|
50 | my $options = { 'name' => "EmailAddressExtractor",
|
---|
51 | 'desc' => "{EmailAddressExtractor.desc}",
|
---|
52 | 'abstract' => "yes",
|
---|
53 | 'inherits' => "yes",
|
---|
54 | 'args' => $arguments };
|
---|
55 |
|
---|
56 |
|
---|
57 | sub new {
|
---|
58 | my ($class) = shift (@_);
|
---|
59 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
60 | push(@$pluginlist, $class);
|
---|
61 |
|
---|
62 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
63 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
64 |
|
---|
65 | my $self = new PrintInfo($pluginlist, $inputargs, $hashArgOptLists, 1);
|
---|
66 |
|
---|
67 | return bless $self, $class;
|
---|
68 |
|
---|
69 | }
|
---|
70 |
|
---|
71 | # extract metadata
|
---|
72 | sub extract_email_metadata {
|
---|
73 |
|
---|
74 | my $self = shift (@_);
|
---|
75 | my ($doc_obj) = @_;
|
---|
76 |
|
---|
77 | if ($self->{'extract_email'}) {
|
---|
78 | my $thissection = $doc_obj->get_top_section();
|
---|
79 | while (defined $thissection) {
|
---|
80 | my $text = $doc_obj->get_text($thissection);
|
---|
81 | $self->extract_email (\$text, $doc_obj, $thissection) if $text =~ /./;
|
---|
82 | $thissection = $doc_obj->get_next_section ($thissection);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | }
|
---|
87 |
|
---|
88 | sub extract_email {
|
---|
89 | my $self = shift (@_);
|
---|
90 | my ($textref, $doc_obj, $thissection) = @_;
|
---|
91 | my $outhandle = $self->{'outhandle'};
|
---|
92 |
|
---|
93 | gsprintf($outhandle, " {EmailAddressExtractor.extracting_emails}...\n")
|
---|
94 | if ($self->{'verbosity'} > 2);
|
---|
95 |
|
---|
96 | my @email = ($$textref =~ m/([-a-z0-9\.@+_=]+@(?:[-a-z0-9]+\.)+(?:com|org|edu|mil|int|net|[a-z][a-z]))/g);
|
---|
97 | @email = sort @email;
|
---|
98 |
|
---|
99 | # if($self->{"new_extract_email"} == 0)
|
---|
100 | # {
|
---|
101 | # my @email2 = ();
|
---|
102 | # foreach my $address (@email)
|
---|
103 | # {
|
---|
104 | # if (!(join(" ",@email2) =~ m/(^| )$address( |$)/ ))
|
---|
105 | # {
|
---|
106 | # push @email2, $address;
|
---|
107 | # $doc_obj->add_utf8_metadata ($thissection, "EmailAddress", $address);
|
---|
108 | # # print $outhandle " extracting $address\n"
|
---|
109 | # &gsprintf($outhandle, " {AutoExtractMetadata.extracting} $address\n")
|
---|
110 | # if ($self->{'verbosity'} > 3);
|
---|
111 | # }
|
---|
112 | # }
|
---|
113 | # }
|
---|
114 | # else
|
---|
115 | # {
|
---|
116 | my $hashExistMail = {};
|
---|
117 | foreach my $address (@email) {
|
---|
118 | if (!(defined $hashExistMail->{$address}))
|
---|
119 | {
|
---|
120 | $hashExistMail->{$address} = 1;
|
---|
121 | $doc_obj->add_utf8_metadata ($thissection, "EmailAddress", $address);
|
---|
122 | gsprintf($outhandle, " {AutoExtractMetadata.extracting} $address\n")
|
---|
123 | if ($self->{'verbosity'} > 3);
|
---|
124 | }
|
---|
125 | }
|
---|
126 | gsprintf($outhandle, " {EmailAddressExtractor.done_email_extract}\n")
|
---|
127 | if ($self->{'verbosity'} > 2);
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | 1;
|
---|