Tree::Lexicon - Object class for storing and retrieving a lexicon in a tree of affixes
Version 0.01
use Tree::Lexicon; my $lexicon = Tree::Lexicon->new(); $lexicon->insert( 'apply', '', 'Apple', 'Windows', 'Linux', 'app', 'all day' ); # Warns of strings not matching /^\w+/ without inserting if ($lexicon->contains( 'WiNdOwS' )) { $lexicon->remove( 'wInDoWs' ); $lexicon->insert( 'Vista' ); } my @words = $lexicon->vocabulary; # Same as: @words = ( 'Apple', 'Linux', 'Windows', 'app', 'apply' ); @words = $lexicon->auto_complete( 'ap' ); # Same as: @words = ( 'app', 'apply' ); my $regexp = $lexicon->as_regexp(); # Same as: $regexp = qr/\b(?:Apple|Linux|Windows|app(?:ly)?)\b/; my $caseless->Tree::Lexicon->new( 0 )->insert( 'apply', '', 'Apple', 'Windows', 'Linux', 'app', 'all day' ); # Warns of strings not matching /^\w+/ without inserting if ($caseless->contains( 'WiNdOwS' )) { $caseless->remove( 'wInDoWs' ); $caseless->insert( 'Vista' ); } @words = $caseless->vocabulary; # Same as: @words = ( 'APP', 'APPLE', 'APPLY', 'LINUX', 'VISTA' ); @words = $caseless->auto_complete( 'ap' ); # Same as: @words = ( 'APP', 'APPLE', 'APPLY' ); my $regexp = $caseless->as_regexp(); # Same as: $regexp = qr/\b(?:[Aa][Pp[Pp](?:[Ll](?:[Ee]|[Yy]))?|[Ll][Ii][Nn][Uu][X]|[Vv][Ii][Ss][Tt][Aa])\b/; use Tree::Lexicon qw( cs_regexp ci_regexp ); my $cs_regexp = cs_regexp( @words ); # Same as: $cs_regexp = Tree::Lexicon->new()->insert( @words )->as_regexp(); my $ci_regexp = ci_regexp( @words ); # Same as: $ci_regexp = Tree::Lexicon->new( 0 )->insert( @words )->as_regexp();
The purpose of this module is to provide a simple and effective means to store a lexicon. It is intended to aid parsers in identifying keywords and interactive applications in identifying user-provided words.
Convenience function for generating a case sensitive regular expression from list of words.
my $cs_regexp = cs_regexp( @words ); # Same as: $cs_regexp = Tree::Lexicon->new( 1 )->insert( @words )->as_regexp();
Convenience function for generating a case insensitive regular expression from list of words.
my $ci_regexp = cs_regexp( @words ); # Same as: $ci_regexp = Tree::Lexicon->new( 0 )->insert( @words )->as_regexp();
Passing a string not matching /^\w+/ as an argument to insert, remove, contains or auto_complete yields a warning to STDERR and nothing else.
/^\w+/
insert
remove
contains
auto_complete
Returns a new empty Tree::Lexicon object. By default, the tree's contents are case-sensitive. Passing a single false argument to the constuctor makes its contents case-insensitive.
Tree::Lexicon
$lexicon = Tree::Lexicon->new(); # Same as: $lexicon = Tree::Lexicon->new( 1 ); # or # $lexicon = Tree::Lexicon->new( 0 );
Inserts zero or more words into the lexicon tree and returns the object.
$lexicon->insert( 'list', 'of', 'words' );
If you already have an initial list of words, then you can chain this method up with the constructor.
my $lexicon = Tree::Lexicon->new()->insert( @words );
Removes zero or more words from the lexicon tree and returns them (or undef if not found).
undef
@removed = $lexicon->remove( 'these', 'words' );
Returns 1 or '' for each word as to its presence or absense, respectively.
1
''
@verify = $lexicon->contains( 'these', 'words' );
Returns all words beginning with the string passed.
@words = $lexicon->auto_complete( 'a' );
Returns all words in the lexicon.
@words = $lexicon->vocabulary();
Returns a regular expression equivalent to the lexicon tree. The regular expression has the form qr/\b(?: ... )\b/.
qr/\b(?: ... )\b/
$regexp = $lexicon->as_regexp();
S. Randall Sawyer, <srandalls at cpan.org>
<srandalls at cpan.org>
Please report any bugs or feature requests to bug-tree-lexicon at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Tree-Lexicon. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-tree-lexicon at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Tree::Lexicon
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Tree-Lexicon
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Tree-Lexicon
CPAN Ratings
http://cpanratings.perl.org/d/Tree-Lexicon
Search CPAN
http://search.cpan.org/dist/Tree-Lexicon/
This module's framework generated with module-starter.
module-starter
Copyright 2013 S. Randall Sawyer.
This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:
http://www.perlfoundation.org/artistic_license_2_0
To install Tree::Lexicon, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Tree::Lexicon
CPAN shell
perl -MCPAN -e shell install Tree::Lexicon
For more information on module installation, please visit the detailed CPAN module installation guide.