| 1 |
NAME |
| 2 |
Text::Context::EitherSide - Get n words either side of search keywords |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
use Text::Context::EitherSide; |
| 6 |
|
| 7 |
my $text = "The quick brown fox jumped over the lazy dog"; |
| 8 |
my $context = Text::Context::EitherSide->new($text); |
| 9 |
|
| 10 |
$context->as_string("fox") # "... quick brown fox jumped over ..." |
| 11 |
|
| 12 |
$context->as_string("fox", "jumped") |
| 13 |
# "... quick brown fox jumped over the ..." |
| 14 |
|
| 15 |
my $context = Text::Context::EitherSide->new($text, context => 1); |
| 16 |
# 1 word on either side |
| 17 |
|
| 18 |
$context->as_string("fox", "jumped", "dog"); |
| 19 |
# "... brown fox jumped over ... lazy dog", |
| 20 |
|
| 21 |
Or, if you don't believe in all this OO rubbish: |
| 22 |
|
| 23 |
use Text::Context::EitherSide qw(get_context); |
| 24 |
get_context(1, $text, "fox", "jumped", "dog") |
| 25 |
# "... brown fox jumped over ... lazy dog" |
| 26 |
|
| 27 |
DESCRIPTION |
| 28 |
Suppose you have a large piece of text - typically, say, a web page or a |
| 29 |
mail message. And now suppose you've done some kind of full-text search |
| 30 |
on that text for a bunch of keywords, and you want to display the |
| 31 |
context in which you found the keywords inside the body of the text. |
| 32 |
|
| 33 |
A simple-minded way to do that would be just to get the two words either |
| 34 |
side of each keyword. But hey, don't be too simple minded, because |
| 35 |
you've got to make sure that the list doesn't overlap. If you have |
| 36 |
|
| 37 |
the quick brown fox jumped over the lazy dog |
| 38 |
|
| 39 |
and you extract two words either side of "fox", "jumped" and "dog", you |
| 40 |
really don't want to end up with |
| 41 |
|
| 42 |
quick brown fox jumped over brown fox jumped over the the lazy dog |
| 43 |
|
| 44 |
so you need a small amount of smarts. This module has a small amount of |
| 45 |
smarts. |
| 46 |
|
| 47 |
EXPORTABLE |
| 48 |
get_context |
| 49 |
This is primarily an object-oriented module. If you don't care about |
| 50 |
that, just import the "get_context" subroutine, and call it like so: |
| 51 |
|
| 52 |
get_context($num_of_words, $text, @words_to_find) |
| 53 |
|
| 54 |
and you'll get back a string with ellipses as in the synopsis. That's |
| 55 |
all that most people need to know. But if you want to do clever stuff... |
| 56 |
|
| 57 |
METHODS |
| 58 |
new |
| 59 |
my $c = Text::Context::EitherSite->new($text [, context=> $n]); |
| 60 |
|
| 61 |
Create a new object storing some text to be searched, plus optionally |
| 62 |
some information about how many words on either side you want. (If you |
| 63 |
don't like the default of 2.) |
| 64 |
|
| 65 |
context |
| 66 |
$c->context(5); |
| 67 |
|
| 68 |
Allows you to get and set the number of the words on either side. |
| 69 |
|
| 70 |
as_sparse_list |
| 71 |
$c->as_sparse_list(@keywords) |
| 72 |
|
| 73 |
Returns the keywords, plus *n* words on either side, as a sparse list; |
| 74 |
the original text is split into an array of words, and non-contextual |
| 75 |
elements are replaced with "undef"s. (That's not actually how it works, |
| 76 |
but conceptually, it's the same.) |
| 77 |
|
| 78 |
as_list |
| 79 |
$c->as_list(@keywords) |
| 80 |
|
| 81 |
The same as "as_sparse_list", but single or multiple "undef"s are |
| 82 |
collapsed into a single ellipsis: |
| 83 |
|
| 84 |
(undef, "foo", undef, undef, undef, "bar") |
| 85 |
|
| 86 |
becomes |
| 87 |
|
| 88 |
("...", "foo", "...", "bar") |
| 89 |
|
| 90 |
as_string |
| 91 |
$c->as_string(@keywords) |
| 92 |
|
| 93 |
Takes the "as_list" output above and joins them all together into a |
| 94 |
string. This is what most people want from "Text::Context::EitherSide". |
| 95 |
|
| 96 |
EXPORT |
| 97 |
"get_context" is available as a shortcut for |
| 98 |
|
| 99 |
Text::Context::EitherSide->new($text, context => $n)->as_string(@words); |
| 100 |
|
| 101 |
but needs to be explicitly imported. Nothing is exported by default. |
| 102 |
|
| 103 |
SEE ALSO |
| 104 |
Text::Context is an even smarter way of extracting a contextual string. |
| 105 |
|
| 106 |
AUTHOR |
| 107 |
Current maintainer: Tony Bowden |
| 108 |
|
| 109 |
Original author: Simon Cozens |
| 110 |
|
| 111 |
BUGS and QUERIES |
| 112 |
Please direct all correspondence regarding this module to: |
| 113 |
[email protected] |
| 114 |
|
| 115 |
COPYRIGHT AND LICENSE |
| 116 |
Copyright 2002-2005 by Kasei Limited, http://www.kasei.com/ |
| 117 |
|
| 118 |
You may use and redistribute this module under the terms of the Artistic |
| 119 |
License 2.0. |
| 120 |
|
| 121 |
http://www.perlfoundation.org/artistic_license_2_0 |
| 122 |
|