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