Text::Positional::Ngram
This document provides a general introduction to the Text::Positional::Ngram module.
The Text::Positional::Ngram module is a module that allows for the retrieval of variable length ngrams. An ngram is defined as a sequence of 'n' tokens that occur within a window of at leaste 'n' tokens in the text. What constitutes as a 'token' can be defined by the user.
An ngram is a sequence of n tokens. The tokens in the ngrams are delimited by the diamond symbol, "<>". Therefore "to<>be<>" is a bigram whose tokens consist of "to" and "be". Similarly, "or<>not<>to<>" is a trigram whose tokens consist of "or", "not", and "to".
Given a piece of text, Ngrams are usually formed of contiguous tokens. For example, if we take the phrase:
to be or not to be
The bigrams for this phrase would be:
to<>be<> be<>or<> or<>not<>
The trigrams for this phrase would be:
to<>be<>or<> be<>or<>not<> or<>not<>to<> not<>to<>be<>
We define a token as a contiguous sequence of characters that match one of a set of regular expressions. These regular expressions may be user-provided, or, if not provided, are assumed to be the following two regular expressions:
\w+ -> this matches a contiguous sequence of alpha-numeric characters [\.,;:\?!] -> this matches a single punctuation mark
For example, assume the following is a line of text:
"to be or not to be, that is the question!"
Then, using the above regular expressions, we get the following tokens:
to be or not to be , that is the question !
If we assume that the user provides the following regular expression:
[a-zA-Z]+ -> this matches a contiguous sequence of alphabetic characters
Then, we get the following tokens:
to be or not to be that is the question
use Text::Positional::Ngram;
use Text::Positional::Ngram; # create an instance of Text::Positional::Ngram my $text = Text::Positional::Ngram->new(); # create the files needed and specify which # file you would like to get the ngrams from $text->create_files("my_file.txt"); # get the ngrams $text->get_ngrams();
Takes an array of files in which the ngrams are to be obtained from. This function will creates the files that are required for the ngrams to be created. These files are defined as the name of the first file entered in the FILE array and timestamped. 1. vocabulary file : converts tokens to integers prefix: 2. snt file : integer representation of corpus 3. sntngram file : integer representation of the ngrams and their frequency counts 4. ngram file : ngrams and their frequencies
Obtains ngrams of size two and their frequencies storing them in the given ngram file.
Removes n-grams containing at least one (in OR mode) stop word or all stop words (in AND mode). The default is OR mode. Each stop word should be a regular expression in this FILE and should be on a line of its own. These should be valid Perl regular expressions, which means that any occurrence of the forward slash '/' within the regular expression must be 'escaped'.
OR mode removes n-grams containing at least one stop word and AND mode removes n-grams that consists of entirely of stop words. Default: AND
Each regular expression in this FILE should be on a line of its own, and should be delimited by the forward slash '/'. These should be valid Perl regular expressions, which means that any occurrence of the forward slash '/' within the regular expression must be 'escaped'. NOTE: This function should be called before the function that creates the main files ie before create_files(FILE).
The set_nontoken_file function can be used when there are predictable sequences of characters that you know should not be included as tokens. NOTE: This function should be called before the function that creates the main files ie before create_files(FILE).
Ignores Ignores n-grams that occur less than N times. Ignored n-grams are not counted and so do not affect counts and frequencies. NOTE: Should be set before you retrieve the ngrams, ie before you call the get_ngrams() function.
The marginal frequencies consist of the frequencies of the individual tokens in their respective positions in the n-gram. NOTE: Should be set before you retrieve the ngrams, ie before you call the get_ngrams() function.
Prevents n-grams from spanning across the new-line character
Does not display n-grams that occur less than N times NOTE: Should be set before you retrieve the ngrams, ie before you call the get_ngrams() function.
Finds n-grams greater than or equal to size N. Default: 2 NOTE: Should be set before you retrieve the ngrams, ie before you call the get_ngrams() function.
Finds n-grams less than or equal to size N Default: 2 NOTE: Should be set before you retrieve the ngrams, ie before you call the get_ngrams() function.
Finds ngrams equal to size N Default : 2 NOTE: Should be set before you retrieve the ngrams, ie before you call the get_ngrams() function.
Prints the ngrams to FILE. The hidden files that get erased when program is completed are named: <FILE>.<ext>. If this is not set the files will be named default.<ext>
Returns the number of n-grams.
Removes the snt, sntngram and the vocab file.
Sets the size of the window in which positional ngram can be found in. =head1 AUTHOR
Bridget Thomson McInnes, bthomson@d.umn.edu
Limitations of this package are:
Copyright (C) 2004-2007, Bridget Thomson McInnes
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Note: a copy of the GNU Free Documentation License is available on the web at http://www.gnu.org/copyleft/fdl.html and is included in this distribution as FDL.txt.
perl(1)
4 POD Errors
The following errors were encountered while parsing the POD:
'=item' outside of any '=over'
You forgot a '=back' before '=head1'
To install Text::Positional::Ngram, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Text::Positional::Ngram
CPAN shell
perl -MCPAN -e shell install Text::Positional::Ngram
For more information on module installation, please visit the detailed CPAN module installation guide.