| 1 | package Email::Store::NamedEntity; | ||
| 2 | use 5.006; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | our $VERSION = '1.3'; | ||
| 6 | use Email::Store::DBI; | ||
| 7 | use base 'Email::Store::DBI'; | ||
| 8 | use Email::Store::Mail; | ||
| 9 | |||
| 10 | |||
| 11 | Email::Store::NamedEntity->table("named_entity"); | ||
| 12 | Email::Store::NamedEntity->columns(All => qw/id mail thing description score/); | ||
| 13 | Email::Store::NamedEntity->columns(Primary => qw/id/); | ||
| 14 | Email::Store::NamedEntity->has_a(mail => "Email::Store::Mail"); | ||
| 15 | Email::Store::Mail->has_many( named_entities => "Email::Store::NamedEntity" ); | ||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 | sub on_store_order { 80 } | ||
| 20 | |||
| 21 | sub on_store { | ||
| 22 | my ($self, $mail) = @_; | ||
| 23 | my $simple = $mail->simple; | ||
| 24 | require Lingua::EN::NamedEntity; | ||
| 25 | |||
| 26 | foreach my $e (Lingua::EN::NamedEntity::extract_entities($simple->body)) | ||
| 27 | { | ||
| 28 | |||
| 29 | my $class = $e->{class}; | ||
| 30 | my $score = $e->{scores}->{$class} || 0; | ||
| 31 | Email::Store::NamedEntity->create({ | ||
| 32 | mail => $mail->id, | ||
| 33 | thing => $e->{entity}, | ||
| 34 | description => $class, | ||
| 35 | score => $score, | ||
| 36 | }); | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | sub on_gather_plucene_fields_order { 80 } | ||
| 41 | |||
| 42 | # Bet you weren't expecting that! | ||
| 43 | sub on_gather_plucene_fields { | ||
| 44 | my ($self, $mail, $hash) = @_; | ||
| 45 | |||
| 46 | my %topics; | ||
| 47 | foreach my $e ($mail->named_entities) { | ||
| 48 | push @{$topics{lc($e->description)}}, lc($e->thing); | ||
| 49 | } | ||
| 50 | |||
| 51 | foreach my $key (keys %topics) { | ||
| 52 | $hash->{$key} = join ' ', @{$topics{$key}}; | ||
| 53 | } | ||
| 54 | |||
| 55 | } | ||
| 56 | |||
| 57 | =head1 NAME | ||
| 58 | |||
| 59 | Email::Store::NamedEntity - Provides a list of named entities for an email | ||
| 60 | |||
| 61 | =head1 SYNOPSIS | ||
| 62 | |||
| 63 | Remember to create the database table: | ||
| 64 | |||
| 65 | % make install | ||
| 66 | % perl -MEmail::Store="..." -e 'Email::Store->setup' | ||
| 67 | |||
| 68 | And now: | ||
| 69 | |||
| 70 | foreach my $e ($mail->named_entities) { | ||
| 71 | print $e->thing," which is a ", $e->description,"(score=",$e->score(),")\n"; | ||
| 72 | } | ||
| 73 | |||
| 74 | =head1 DESCRIPTION | ||
| 75 | |||
| 76 | C<Named entities> is the NLP jargon for proper nouns which represent people, | ||
| 77 | places, organisations, and so on. Clearly this is useful meta data to extract | ||
| 78 | from a body of emails. | ||
| 79 | |||
| 80 | This extension for C<Email::Store> adds the C<named_entity> table, and exports | ||
| 81 | the C<named_entities> method to the C<Email::Store::Mail> class which returns | ||
| 82 | a list of C<Email::Store::NamedEntity> objects. | ||
| 83 | |||
| 84 | A C<Email::Store::NamedEntity> object has three fields - | ||
| 85 | |||
| 86 | =over 4 | ||
| 87 | |||
| 88 | =item thing | ||
| 89 | |||
| 90 | The entity we've extracted e.g "Bob Smith" or "London" w | ||
| 91 | |||
| 92 | =item description | ||
| 93 | |||
| 94 | What class of entity it is e.g "person", "organisation" or "place" | ||
| 95 | |||
| 96 | =item score | ||
| 97 | |||
| 98 | How likely like it is to be that class. | ||
| 99 | |||
| 100 | =back | ||
| 101 | |||
| 102 | C<Email::Store::NamedEntity> will also attempt to index each field | ||
| 103 | so that if you ahve the C<Email::Store::Plucene> module installed | ||
| 104 | then you could search using something like | ||
| 105 | |||
| 106 | place:London | ||
| 107 | |||
| 108 | |||
| 109 | =head1 SEE ALSO | ||
| 110 | |||
| 111 | L<Email::Store::Mail>, L<Lingua::EN::NamedEntity>. | ||
| 112 | |||
| 113 | =head1 AUTHOR | ||
| 114 | |||
| 115 | Simon Wistow, C<simon@thegestalt.org> | ||
| 116 | |||
| 117 | This module is distributed under the same terms as Perl itself. | ||
| 118 | |||
| 119 | =cut | ||
| 120 | |||
| 121 | 1; | ||
| 122 | __DATA__ |