| 1 | # calculate and stores ngrams of various types | ||
| 2 | |||
| 3 | package IDS::Algorithm::Ngram; | ||
| 4 | use base qw(IDS::Algorithm); | ||
| 5 | $IDS::Algorithm::Ngram::VERSION = "1.0"; | ||
| 6 | |||
| 7 | # BUG BUG BUG | ||
| 8 | # Need to be able to handle either token list or strings. | ||
| 9 | # choose which by a parameter | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | use IO::Handle; | ||
| 14 | use Carp qw(cluck carp confess); | ||
| 15 | |||
| 16 | sub param_options { | ||
| 17 | my $self = shift; | ||
| 18 | |||
| 19 | return ( | ||
| 20 | "ngram_verbose=i" => \${$self->{"params"}}{"verbose"}, | ||
| 21 | "ids_state=s" => \${$self->{"params"}}{"state_file"}, | ||
| 22 | "length=i" => \${$self->{"params"}}{"length"}, | ||
| 23 | ); | ||
| 24 | } | ||
| 25 | |||
| 26 | sub default_parameters { | ||
| 27 | my $self = shift; | ||
| 28 | $self->{"params"} = { | ||
| 29 | "verbose" => 0, | ||
| 30 | "length" => 3, | ||
| 31 | "state_file" => 0, | ||
| 32 | }; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub length { | ||
| 36 | my $self = shift; | ||
| 37 | return ${$self->{"params"}}{"length"}; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub ngrams { | ||
| 41 | my $self = shift; | ||
| 42 | my $data = shift or | ||
| 43 | confess *add_instance{PACKAGE} . "::add_instance missing data to add"; | ||
| 44 | my $length = ${$self->{"params"}}{"length"}; | ||
| 45 | my @ngrams = (); | ||
| 46 | |||
| 47 | my $ngram = ""; | ||
| 48 | my @tokens = @{$data}; | ||
| 49 | while (@tokens >= $length) { | ||
| 50 | $ngram = join "; ", @tokens[0..($length-1)]; | ||
| 51 | push @ngrams, $ngram; | ||
| 52 | shift @tokens; | ||
| 53 | } | ||
| 54 | return @ngrams; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub add { | ||
| 58 | my $self = shift; | ||
| 59 | my $data = shift or | ||
| 60 | confess *add_instance{PACKAGE} . "::add_instance missing data to add"; | ||
| 61 | my $length = ${$self->{"params"}}{"length"}; | ||
| 62 | |||
| 63 | for my $ngram ($self->ngrams($data)) { | ||
| 64 | ${$self->{"ngrams"}}{$ngram}++; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | sub test { | ||
| 69 | my $self = shift; | ||
| 70 | my $data = shift or # ref to a list of the data to test | ||
| 71 | confess *test_instance{PACKAGE} . "::test_instance missing tokenref"; | ||
| 72 | my ($ngram, @ngrams, $misfires, $count); | ||
| 73 | my $verbose = ${$self->{"params"}}{"verbose"}; | ||
| 74 | |||
| 75 | $misfires = 0; | ||
| 76 | @ngrams = $self->ngrams($data); | ||
| 77 | $count = scalar(@ngrams); | ||
| 78 | for $ngram (@ngrams) { | ||
| 79 | if ($verbose > 1) { | ||
| 80 | print ${$self->{"ngrams"}}{$ngram} ? "Yes" : "No"; | ||
| 81 | print ": '$ngram'\n"; | ||
| 82 | } | ||
| 83 | ${$self->{"ngrams"}}{$ngram} or $misfires++; | ||
| 84 | } | ||
| 85 | ### assume if we got no n-grams that it is not similar at all | ||
| 86 | return $count == 0 ? 0 : ($count - $misfires) / $count; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub save { | ||
| 90 | my $self = shift; | ||
| 91 | my $fname = shift or confess *save{PACKAGE} . "::save missing filename"; | ||
| 92 | my $fh = new IO::File ">$fname" or warn "Unable to open $fname: $!\n"; | ||
| 93 | my $length = ${$self->{"params"}}{"length"}; | ||
| 94 | |||
| 95 | my ($ngram, $count); | ||
| 96 | |||
| 97 | print $fh "$length\n"; | ||
| 98 | while (($ngram, $count) = each %{$self->{"ngrams"}}) { | ||
| 99 | print $fh "$count $ngram\n"; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | sub load { | ||
| 104 | my $self = shift; | ||
| 105 | my $fname = shift; | ||
| 106 | defined($fname) && $fname or | ||
| 107 | $fname = ${$self->{"params"}}{"state_file"}; | ||
| 108 | defined($fname) && $fname or | ||
| 109 | confess *load{PACKAGE} . "::load missing filename"; | ||
| 110 | |||
| 111 | my ($ngram, $count, $n, $fh); | ||
| 112 | |||
| 113 | $fh = new IO::File "<$fname" or | ||
| 114 | confess *load{PACKAGE} . "::load Unable to open $fname for reading: $!"; | ||
| 115 | |||
| 116 | ${$self->{"params"}}{"length"} = <$fh>; | ||
| 117 | chomp(${$self->{"params"}}{"length"}); | ||
| 118 | ${$self->{"params"}}{"length"} or | ||
| 119 | confess *load{PACKAGE} . "::load from $fname missing length"; | ||
| 120 | |||
| 121 | $n = 0; | ||
| 122 | %{$self->{"ngrams"}} = (); | ||
| 123 | while (<$fh>) { | ||
| 124 | chomp; | ||
| 125 | $n++; | ||
| 126 | ($count, $ngram) = split /\s+/, $_, 2; | ||
| 127 | print "load count $count gram $ngram\n" if ${$self->{"params"}}{"verbose"} > 2; | ||
| 128 | ${$self->{"ngrams"}}{$ngram} = $count; | ||
| 129 | } | ||
| 130 | print "Loaded $n " . ${$self->{"params"}}{"length"} . "-grams\n" if | ||
| 131 | ${$self->{"params"}}{"verbose"}; | ||
| 132 | my @foo = %{$self->{"ngrams"}}; | ||
| 133 | my $foo = scalar(@foo) / 2; | ||
| 134 | print "DB has $foo entries\n"; | ||
| 135 | } | ||
| 136 | |||
| 137 | =head1 AUTHOR INFORMATION | ||
| 138 | |||
| 139 | Copyright 2005-2007, Kenneth Ingham. All rights reserved. | ||
| 140 | |||
| 141 | This library is free software; you can redistribute it and/or modify | ||
| 142 | it under the same terms as Perl itself. | ||
| 143 | |||
| 144 | Address bug reports and comments to: ids_test at i-pi.com. When sending | ||
| 145 | bug reports, please provide the versions of IDS::Test.pm, IDS::Algorithm.pm, | ||
| 146 | IDS::DataSource.pm, the version of Perl, and the name and version of the | ||
| 147 | operating system you are using. Since Kenneth is a PhD student, the | ||
| 148 | speed of the reponse depends on how the research is proceeding. | ||
| 149 | |||
| 150 | =head1 BUGS | ||
| 151 | |||
| 152 | Please report them. | ||
| 153 | |||
| 154 | =head1 SEE ALSO | ||
| 155 | |||
| 156 | L<IDS::Test>, L<IDS::DataSource>, L<IDS::Algorithm> | ||
| 157 | |||
| 158 | =cut | ||
| 159 | |||
| 160 | 1; |