| Use Case | Command | Description |
|---|---|---|
| Basic stemming | use Lingua::Stem qw(stem); | Stem a list of words using the default English locale. |
| OO with caching | my $stemmer = Lingua::Stem->new; | Create stemmer, enable level 2 caching for best performance. |
| Change locale | Lingua::Stem::set_locale('de'); | Set default locale for all subsequent stems (e.g., German). |
| Add exceptions | $stemmer->add_exceptions({ 'driven' => 'driven' }); | Override stemming for specific words. |
| Stem in place | my @words = qw(a list of words); | Stem words in-place (faster, modifies original array). |
| Clear cache | $stemmer->clear_stem_cache; | Clear the stemming cache for the current locale. |
| Get locale | my $loc = $stemmer->get_locale; | Retrieve the current locale of a stemmer instance. |
| Delete exceptions | $stemmer->delete_exceptions('gollum'); | Remove previously added exceptions. |
use Lingua::Stem qw(stem);
my $stemmmed_words_anon_array = stem(@words);
or for the OO inclined,
use Lingua::Stem;
my $stemmer = Lingua::Stem->new(-locale => 'EN-UK');
$stemmer->stem_caching({ -level => 2 });
my $stemmmed_words_anon_array = $stemmer->stem(@words);
use Lingua::Stem qw (stem clear_stem_cache stem_caching add_exceptions delete_exceptions
get_exceptions set_locale get_locale :all :locale :exceptions :stem :caching);
:all - imports stem add_exceptions delete_exceptions get_exceptions
set_locale get_locale
:stem - imports stem
:caching - imports stem_caching clear_stem_cache
:locale - imports set_locale get_locale
:exceptions - imports add_exceptions delete_exceptions get_exceptions
Currently supported locales are:
DA - Danish
DE - German
EN - English (also EN-US and EN-UK)
FR - French
GL - Galician
IT - Italian
NO - Norwegian
PT - Portuguese
RU - Russian (also RU-RU and RU-RU.KOI8-R)
SV - Swedish
If you have the memory and lots of stemming to do, I strongly suggest using cache level 2 and
processing lists in 'big chunks' (long lists) for best performance.
Comparision with Lingua::Stem::Snowball
It functions fairly similarly to the Lingua::Stem::Snowball suite of stemmers, with the most
significant differences being
+---------------------------------------------------------------+
| Language | ISO code | Lingua::Stem | Lingua::Stem::Snowball |
|---------------------------------------------------------------|
| Danish | da | yes | yes |
| Dutch | nl | no | yes |
| English | en | yes | yes |
| Finnish | fi | no | yes |
| French | fr | yes | yes |
| Galacian | gl | yes | no |
| German | de | yes | yes |
| Italian | it | yes | yes |
| Norwegian | no | yes | yes |
| Portuguese | pt | yes | yes |
| Russian | ru | yes | yes |
| Spanish | es | no | yes |
| Swedish | sv | yes | yes |
+---------------------------------------------------------------+
+------------------------------------------------------------------------+
| source: collected_works_poe.txt | words: 454691 | unique words: 22802 |
|------------------------------------------------------------------------|
| module | config | avg secs | words/sec |
|------------------------------------------------------------------------|
| Lingua::Stem 0.82 | no cache | 1.922 | 236560 |
| Lingua::Stem 0.82 | cache level 2 | 1.235 | 368292 |
| Lingua::Stem 0.82 | cachelv2, sip | 0.798 | 569494 |
| Lingua::Stem::Snowball 0.94 | stem | 1.622 | 280276 |
| Lingua::Stem::Snowball 0.94 | stem_in_place | 0.627 | 725129 |
+------------------------------------------------------------------------+
The script for the benchmark is included in the examples/ directory of this distribution as
benchmark_stemmers.plx.
# By default the locale is en
$us_stemmer = Lingua::Stem->new;
# Turn on the cache
$us_stemmer->stem_caching({ -level => 2 });
# Overriding the default for a specific instance
$uk_stemmer = Lingua::Stem->new({ -locale => 'en-uk' });
# Overriding the default for a specific instance and changing the default
$uk_stemmer = Lingua::Stem->new({ -default_locale => 'en-uk' });
# Change default locale
Lingua::Stem::set_locale('en-uk'); # UK's spellings
# Change instance locale
$self->set_locale('en-us'); # US's spellings
$default_locale = Lingua::Stem::get_locale;
Called as an instance method, returns the locale for the instance
$instance_locale = $stemmer->get_locale;
# adding default exceptions
Lingua::Stem::add_exceptions({ 'emily' => 'emily',
'driven' => 'driven',
});
Called as an instance method, adds exceptions only to the specific instance.
# adding instance exceptions
$stemmer->add_exceptions({ 'steely' => 'steely' });
The exceptions shortcut the normal stemming - if an exception matches no further stemming is
performed after the substitution.
Adding an exception with the same key value as an already defined exception replaces the
pre-existing exception with the new value.
# Deletion of exceptions from class default exceptions
Lingua::Stem::delete_exceptions('aragorn','frodo','samwise');
# Deletion of exceptions from instance
$stemmer->delete_exceptions('smaug','sauron','gollum');
# Deletion of all class default exceptions
delete_exceptions;
# Deletion of all exceptions from instance
$stemmer->delete_exceptions;
# Returns all class default exceptions
$exceptions = Lingua::Stem::get_exceptions;
As a class method with parameters, it returns the default exceptions listed in the
parameters as an anonymous hash of 'exception' => 'replace with' pairs. If a parameter
specifies an undefined 'exception', the value is set to undef.
# Returns class default exceptions for 'emily' and 'george'
$exceptions = Lingua::Stem::get_exceptions('emily','george');
As an instance method, with no parameters it returns the currently active exceptions for the
instance.
# Returns all instance exceptions
$exceptions = $stemmer->get_exceptions;
As an instance method with parameters, it returns the instance exceptions listed in the
parameters as an anonymous hash of 'exception' => 'replace with' pairs. If a parameter
specifies an undefined 'exception', the value is set to undef.
# Returns instance exceptions for 'lisa' and 'bart'
$exceptions = $stemmer->get_exceptions('lisa','bart');
# Default settings applied
my $anon_array_of_stemmed_words = Lingua::Stem::stem(@words);
Called as an instance method, it applies the instance's settings and stems the list of
passed words, returning an anonymous array with the stemmed words in the same order as the
passed list of words.
# Instance's settings applied
my $stemmed_words = $stemmer->stem(@words);
The stemmer performs best when handed long lists of words rather than one word at a time.
The cache also provides a huge speed up if you are processing lots of text.
my @words = ( 'a', 'list', 'of', 'words' );
my $stemmed_list_of_words = stem_in_place(@words);
# '$stemmed_list_of_words' refers to the @words list
# after 'stem_in_place' has executed
DO NOT use this method of stemming if you need to keep the original list of words. Its
performance gain derives entirely from the fact it does not make a copy the original list
but instead overwrites the original list.
If you try something like
my @words_for_stemming = @words;
my $stemmed_list_of_words = stem_in_place(@words_for_stemming);
thinking you will get a speed boost while keeping the original list, you won't: You wipe out
the speed gain completely with your copying of the original list. You should just use the
'stem' method instead on the original list of words if you need to keep the original list.
$stemmer->clear_stem_cache;
clear_stem_cache;
$stemmer->stem_caching({ -level => 1 });
stem_caching({ -level => 1 });
For the sake of maximum compatibility with previous versions, stem caching is set to '-level
=> 0' by default.
'-level' definitions
'0' means 'no caching'. This is the default level.
'1' means 'cache per run'. This caches stemming results during each
call to 'stem'.
'2' means 'cache indefinitely'. This caches stemming results until
either the process exits or the 'clear_stem_cache' method is called.
stem caching is global to the locale. If you turn on stem caching for one instance of a
locale stemmer, all instances using the same locale will have it turned on as well.
I STRONGLY suggest turning caching on if you have enough memory and are processing a lot of
data.
Generated by phpman v4.9.25-25-g40dbf62 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-17 00:43 @216.73.216.114
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format