| 1 |
Text::Ngram |
| 2 |
============= |
| 3 |
|
| 4 |
=head1 NAME |
| 5 |
|
| 6 |
Text::Ngram - Ngram analysis of text |
| 7 |
|
| 8 |
=head1 SYNOPSIS |
| 9 |
|
| 10 |
use Text::Ngram qw(ngram_counts add_to_counts); |
| 11 |
my $text = "abcdefghijklmnop"; |
| 12 |
my $hash_r = ngram_counts($text, 3); # Window size = 3 |
| 13 |
# $hash_r => { abc => 1, bcd => 1, ... } |
| 14 |
|
| 15 |
add_to_counts($more_text, 3, $hash_r); |
| 16 |
|
| 17 |
=head1 DESCRIPTION |
| 18 |
|
| 19 |
n-Gram analysis is a field in textual analysis which uses sliding window |
| 20 |
character sequences in order to aid topic analysis, language |
| 21 |
determination and so on. The n-gram spectrum of a document can be used |
| 22 |
to compare and filter documents in multiple languages, prepare word |
| 23 |
prediction networks, and perform spelling correction. |
| 24 |
|
| 25 |
The neat thing about n-grams, though, is that they're really easy to |
| 26 |
determine. For n=3, for instance, we compute the n-gram counts like so: |
| 27 |
|
| 28 |
the cat sat on the mat |
| 29 |
--- $counts{"the"}++; |
| 30 |
--- $counts{"he "}++; |
| 31 |
--- $counts{"e c"}++; |
| 32 |
... |
| 33 |
|
| 34 |
This module provides an efficient XS-based implementation of n-gram |
| 35 |
spectrum analysis. |
| 36 |
|
| 37 |
There are two functions which can be imported: |
| 38 |
|
| 39 |
=head2 ngram_counts |
| 40 |
|
| 41 |
This first function returns a hash reference with the n-gram histogram |
| 42 |
of the text for the given window size. The default window size is 5. |
| 43 |
|
| 44 |
$href = ngram_counts(\%config, $text, $window_size); |
| 45 |
|
| 46 |
The only necessary parameter is $text. |
| 47 |
|
| 48 |
The possible value for \%config are: |
| 49 |
|
| 50 |
=head3 flankbreaks |
| 51 |
|
| 52 |
If set to 1 (default), breaks are flanked by spaces; if set to 0, |
| 53 |
they're not. Breaks are punctuation and other non-alfabetic |
| 54 |
characters, which, unless you use C<punctuation => 0> in your |
| 55 |
configuration, do not make it into the returned hash. |
| 56 |
|
| 57 |
Here's an example, supposing you're using the default value |
| 58 |
for punctuation (1): |
| 59 |
|
| 60 |
my $text = "Hello, world"; |
| 61 |
my $hash = ngram_counts($text, 5); |
| 62 |
|
| 63 |
That produces the following ngrams: |
| 64 |
|
| 65 |
{ |
| 66 |
'Hello' => 1, |
| 67 |
'ello ' => 1, |
| 68 |
' worl' => 1, |
| 69 |
'world' => 1, |
| 70 |
} |
| 71 |
|
| 72 |
On the other hand, this: |
| 73 |
|
| 74 |
my $text = "Hello, world"; |
| 75 |
my $hash = ngram_counts({flankbreaks => 0}, $text, 5); |
| 76 |
|
| 77 |
Produces the following ngrams: |
| 78 |
|
| 79 |
{ |
| 80 |
'Hello' => 1, |
| 81 |
' worl' => 1, |
| 82 |
'world' => 1, |
| 83 |
} |
| 84 |
|
| 85 |
=head3 lowercase |
| 86 |
|
| 87 |
If set to 0, casing is preserved. If set to 1, all letters are |
| 88 |
lowercased before counting ngrams. Default is 1. |
| 89 |
|
| 90 |
# Get all ngrams of size 4 preserving case |
| 91 |
$href_p = ngram_counts( {lowercase => 0}, $text, 4 ); |
| 92 |
|
| 93 |
=head3 punctuation |
| 94 |
|
| 95 |
If set to 0 (default), punctuation is removed before calculating the |
| 96 |
ngrams. Set to 1 to preserve it. |
| 97 |
|
| 98 |
# Get all ngrams of size 2 preserving punctuation |
| 99 |
$href_p = ngram_counts( {punctuation => 1}, $text, 2 ); |
| 100 |
|
| 101 |
=head3 spaces |
| 102 |
|
| 103 |
If set to 0 default is 1, no ngrams contaning spaces will be returned. |
| 104 |
|
| 105 |
# Get all ngrams of size 3 that do not contain spaces |
| 106 |
$href = ngram_counts( {spaces => 0}, $text, 3); |
| 107 |
|
| 108 |
If you're going to request both types of ngrams, than the best way to |
| 109 |
avoid calculating the same thing twice is probably this: |
| 110 |
|
| 111 |
$href_with_spaces = ngram_counts($text[, $window]); |
| 112 |
$href_no_spaces = $href_with_spaces; |
| 113 |
for (keys %$href_no_spaces) { delete $href->{$_} if / / } |
| 114 |
|
| 115 |
=head2 add_to_counts |
| 116 |
|
| 117 |
This incrementally adds to the supplied hash; if C<$window> is zero or |
| 118 |
undefined, then the window size is computed from the hash keys. |
| 119 |
|
| 120 |
add_to_counts($more_text, $window, $href) |
| 121 |
|
| 122 |
=head1 TO DO |
| 123 |
|
| 124 |
=over 6 |
| 125 |
|
| 126 |
=item * Look further into the tests. Sort them and add more. |
| 127 |
|
| 128 |
=back |
| 129 |
|
| 130 |
=head1 SEE ALSO |
| 131 |
|
| 132 |
Cavnar, W. B. (1993). N-gram-based text filtering for TREC-2. In D. |
| 133 |
Harman (Ed.), I<Proceedings of TREC-2: Text Retrieval Conference 2>. |
| 134 |
Washington, DC: National Bureau of Standards. |
| 135 |
|
| 136 |
Shannon, C. E. (1951). Predication and entropy of printed English. |
| 137 |
I<The Bell System Technical Journal, 30>. 50-64. |
| 138 |
|
| 139 |
Ullmann, J. R. (1977). Binary n-gram technique for automatic correction |
| 140 |
of substitution, deletion, insert and reversal errors in words. |
| 141 |
I<Computer Journal, 20>. 141-147. |
| 142 |
|
| 143 |
=head1 AUTHOR |
| 144 |
|
| 145 |
Mainrained by Alberto Simoes, C<[email protected]>. |
| 146 |
|
| 147 |
Previously maintained by Jose Castro, C<[email protected]>. |
| 148 |
Originally created by Simon Cozens, C<[email protected]>. |
| 149 |
|
| 150 |
=head1 COPYRIGHT AND LICENSE |
| 151 |
|
| 152 |
Copyright 2006 by Alberto Simoes |
| 153 |
|
| 154 |
Copyright 2004 by Jose Castro |
| 155 |
|
| 156 |
Copyright 2003 by Simon Cozens |
| 157 |
|
| 158 |
This library is free software; you can redistribute it and/or modify |
| 159 |
it under the same terms as Perl itself. |
| 160 |
|