Lingua::Stem(3pm)
π NAME π
Lingua::Stem β stemming of words. Yep bestie, this is the Lingua::Stem(3pm) manual, and we are absolutely getting into it π
β¨
β¨ SYNOPSIS β¨
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);
π¦ DESCRIPTION π¦
okay so this routine applies stemming algorithms to its parameters, and it returns the stemmed words as appropriate to the selected locale. Thatβs the whole main character vibe fr fr.
You can import some or all of the class methods. Iconic, honestly:
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β importsstemadd_exceptionsdelete_exceptionsget_exceptionsset_localeget_locale:stemβ importsstem:cachingβ importsstem_cachingclear_stem_cache:localeβ importsset_localeget_locale:exceptionsβ importsadd_exceptionsdelete_exceptionsget_exceptions
Currently supported locales are:
DAβ DanishDEβ GermanENβ English (alsoEN-USandEN-UK)FRβ FrenchGLβ GalicianITβ ItalianNOβ NorwegianPTβ PortugueseRUβ Russian (alsoRU-RUandRU-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. No cap, thatβs the performance slay. π₯
π§ Comparision with Lingua::Stem::Snowball π§
It functions fairly similarly to the Lingua::Stem::Snowball suite of stemmers, with the most significant differences being:
Lingua::Stemis a 'pure perl' (no compiled XS code is needed) suite.Lingua::Stem::Snowballis XS based (must be compiled).Lingua::Stemworks with Perl 5.6 or later.Lingua::Stem::Snowballworks with Perl 5.8 or later.Lingua::Stemhas an 'exceptions' system allowing you to override stemming on a 'case by case' basis.Lingua::Stem::Snowballdoes not have an 'exceptions' system.- A somewhat different set of supported languages:
daDanish βLingua::Stem: yes;Lingua::Stem::Snowball: yesnlDutch βLingua::Stem: no;Lingua::Stem::Snowball: yesenEnglish βLingua::Stem: yes;Lingua::Stem::Snowball: yesfiFinnish βLingua::Stem: no;Lingua::Stem::Snowball: yesfrFrench βLingua::Stem: yes;Lingua::Stem::Snowball: yesglGalacian βLingua::Stem: yes;Lingua::Stem::Snowball: nodeGerman βLingua::Stem: yes;Lingua::Stem::Snowball: yesitItalian βLingua::Stem: yes;Lingua::Stem::Snowball: yesnoNorwegian βLingua::Stem: yes;Lingua::Stem::Snowball: yesptPortuguese βLingua::Stem: yes;Lingua::Stem::Snowball: yesruRussian βLingua::Stem: yes;Lingua::Stem::Snowball: yesesSpanish βLingua::Stem: no;Lingua::Stem::Snowball: yessvSwedish βLingua::Stem: yes;Lingua::Stem::Snowball: yes
Lingua::Stemis faster for 'stem' (circa 30% faster thanLingua::Stem::Snowball).Lingua::Stem::Snowballis faster for 'stem_in_place' (circa 30% faster thanLingua::Stem).Lingua::Stem::Snowballis more consistent with regard to character set issues.Lingua::Stem::Snowballis under active development.Lingua::Stemis currently fairly static.
Some benchmarks using Lingua::Stem 0.82 and Lingua::Stem::Snowball 0.94 gives an idea of how various options impact performance. The dataset was The Works of Edgar Allen Poe, volumes 1-5 from the Gutenberg Project processed 10 times in a row as single batch of words (processing a long text one word at a time is very inefficient and drops the performance of Lingua::Stem by about 90%: So "Don't Do That" ;) ).
The benchmarks were run on a 3.06 Ghz P4 with HT on Fedora Core 5 Linux using Perl 5.8.8.
- source:
collected_works_poe.txtβ words:454691β unique words:22802 Lingua::Stem 0.82β config:no cacheβ avg secs:1.922β words/sec:236560Lingua::Stem 0.82β config:cache level 2β avg secs:1.235β words/sec:368292Lingua::Stem 0.82β config:cachelv2, sipβ avg secs:0.798β words/sec:569494Lingua::Stem::Snowball 0.94β config:stemβ avg secs:1.622β words/sec:280276Lingua::Stem::Snowball 0.94β config:stem_in_placeβ avg secs:0.627β words/sec:725129
The script for the benchmark is included in the examples/ directory of this distribution as benchmark_stemmers.plx. iykyk π«Ά
πΈ CHANGES πΈ
2.312020.09.26β Fix for Latin1/UTF8 issue in documentation2.302020.06.20β Version renumber for module consistency0.842010.04.29β Documentation fixes to the En stemmer and removal of the accidentally includedlib/Lingua/test.plfile. Thanks goes to Aaron Naiman for bringing the documentation error to my attention and to Alexandr Ciornii and'kmx'for the pointing out the problem with thetest.plfile.0.832007.06.23β Disabled Italian locale build tests due to changes inLingua::Stem::Itbreaking the tests.0.822006.07.23β Added 'stem_in_place' to base package. Tweaks to documentation and build tests.0.812004.07.26β Minor documentation tweak. No functional change.0.802004.07.25β Added 'RU', 'RU_RU', 'RU_RU.KOI-8' locale. Added support forLingua::Stem::RutoMakefile.PLand autoloader. Added documentation stressing use of caching and batches for performance. Added support for '_' as a seperator in the locale strings. Added example benchmark script. Expanded copyright credits.0.702004.04.26β Added FR locale and documentation fixes toLingua::Stem::Gl0.612003.09.28β Documentation fixes. No functional changes.0.602003.04.05β Added more locales by wrappering various stemming implementations. Documented currently supported list of locales.0.502000.09.14β Fixed major implementation error. Starting with version 0.30 I forgot to include rulesets 2,3 and 4 for Porter's algorithm. The resulting stemming results were very poor. Thanks go to<csyap@netfision.com>for bringing the problem to my attention. Unfortunately, the fix inherently generates different stemming results than 0.30 and 0.40 did. If you need identically broken output - use locale 'en-broken'.0.402000.08.25β Added stem caching support as an option. This can provide a large speedup to the operation of the stemmer. Caching is default turned off to maximize compatibility with previous versions.0.301999.06.24β Replaced core of 'En' stemmers with code fromJim Richardson <jimr@maths.usyd.edu.au>Aliased 'en-us' and 'en-uk' to 'en' Fixed 'SYNOPSIS' to correct return value type for stemmed words (SYNOPIS error spotted by<Arved_37@chebucto.ns.ca>)0.201999.06.15β Changed to '.pm' module, moved intoLingua::namespace, added OO interface, optionalized the export of routines into the caller's namespace, added named parameter initialization, stemming exceptions, autoloaded locale support and isolated case flattening to localized stemmers prevent i18n problems later.
Input and output text are assumed to be in UTF8 encoding (no operational impact right now, but will be important when extending the module to non-English).
β¨ METHODS β¨
new(...);β makes a shiny newLingua::Stemobject, and you can pick the locale while you're at it. The default isen, bestie π
# 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' });
set_locale($locale);β sets the locale to one of the recognized locales. Locale identifiers get converted to lowercase, no cap. π
Called as a class method, it changes the default locale for all subsequently generated object instances. Called as an instance method, it only changes the locale for that particular instance.
It croaks if you pass it an unknown locale β so don't try to be cute with fake locales, bestie π
# Change default locale
Lingua::Stem::set_locale('en-uk'); # UK's spellings
# Change instance locale
$self->set_locale('en-us'); # US's spellings
get_locale;β as a class method, returns the current default locale. As an instance method, returns the locale for that instance. Easy peek, iconic behavior π
$default_locale = Lingua::Stem::get_locale;
$instance_locale = $stemmer->get_locale;
add_exceptions($exceptions_hash_ref);β exceptions let you override the stemming algorithm on a case-by-case basis. It's an exact match and substitution situation: if a passed word is identical to the exception, it gets replaced by the specified value. No case adjustments are performed. Periodt. π
Called as a class method, it adds exceptions to the default exceptions list used for subsequent instantiations of Lingua::Stem objects.
# adding default exceptions
Lingua::Stem::add_exceptions({ 'emily' => 'emily',
'driven' => 'driven',
});
Called as an instance method, it 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. Slay.
delete_exceptions(@exceptions_list);β the mirror ofadd_exceptions, this allows the _removal_ of exceptions from either the defaults for the class or from the instance. Bye-bye, exceptions π«Ά
# 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;
get_exceptions;β as a class method with no parameters, it returns all the default exceptions as an anonymous hash of'exception' => 'replace with'pairs.
# 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');
stem(@list);β called as a class method, it applies the default 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.
# 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. Literally give it the whole group chat, not one text at a time πβ¨
stem_in_place(@list);β stems the passed list of words in place. It returns a reference to the modified list. This is about 60% faster than thestemmethod, but it modifies the original list. This currently only works for the English locales.
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 of the original list but instead overwrites the original list. This is giving dangerous main character energy π
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. No cap.
clear_stem_cache;β clears the stemming cache for the current locale. Can be called as either a class method or an instance method. Fresh start, cleanse the timeline π§
$stemmer->clear_stem_cache;
clear_stem_cache;
stem_caching ({ -level => 0|1|2 });β sets stemming cache level for the current locale. Can be called as either a class method or an instance method.
$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. Shared cache, shared vibes π
I STRONGLY suggest turning caching on if you have enough memory and are processing a lot of data. It's giving speed, it's giving efficiency, it's giving slay π₯
β VERSION β
2.31 2020.09.26
π NOTES π
It started with the 'Text::Stem' module, which has been adapted into a more general framework and moved into the more language-oriented 'Lingua' namespace and re-organized to support an OOP interface as well as switch core 'En' locale stemmers. Glow-up fr fr π
Version 0.40 added a cache for stemmed words. This can provide up to a several-fold performance improvement. Obsessed.
Organization is such that extending this module to any number of languages should be direct and simple. Very demure, very mindful.
Case flattening is a function of the language, so the 'exceptions' methods have to be used appropriately to the language. For 'En' family stemming, use lower case words, only, for exceptions. Don't fight the lowercase agenda π
π AUTHORS π
- Jerilyn Franz
<cpan@jerilyn.info> - Jim Richardson
<imr@maths.usyd.edu.au>
π CREDITS π
- Jim Richardson
<imr@maths.usyd.edu.au> - Ulrich Pfeifer
<pfeifer@ls6.informatik.uni-dortmund.de> - Aldo Calpini
<dada@perl.it> - xern
<xern@cpan.org> - Ask Solem Hoel
<ask@unixmonks.net> - Dennis Haney
<davh@davh.dk> - SΓ©bastien Darribere-Pleyt
<sebastien.darribere@lefute.com> - Aleksandr Guidrevitch
<pillgrim@mail.ru>
π¦ SEE ALSO π¦
Lingua::Stem::EnLingua::Stem::EnLingua::Stem::DaLingua::Stem::DeLingua::Stem::GlLingua::Stem::NoLingua::Stem::PtLingua::Stem::SvLingua::Stem::ItLingua::Stem::FrLingua::Stem::RuText::GermanLingua::PT::StemmerLingua::GL::StemmerLingua::Stem::Snowball::NoLingua::Stem::Snowball::SeLingua::Stem::Snowball::DaLingua::Stem::Snowball::SvLingua::Stemmer::GLLingua::Stem::Snowball
Β©οΈ COPYRIGHT Β©οΈ
Copyright 1999-2004
Freerun Technologies, Inc (Freerun), Jim Richardson, University of Sydney <imr@maths.usyd.edu.au> and Jerilyn Franz <cpan@jerilyn.info>. All rights reserved.
Text::German was written and is copyrighted by Ulrich Pfeifer.
Lingua::Stem::Snowball::Da was written and is copyrighted by Dennis Haney and Ask Solem Hoel.
Lingua::Stem::It was written and is copyrighted by Aldo Calpini.
Lingua::Stem::Snowball::No, Lingua::Stem::Snowball::Se, Lingua::Stem::Snowball::Sv were written and are copyrighted by Ask Solem Hoel.
Lingua::Stemmer::GL and Lingua::PT::Stemmer were written and are copyrighted by Xern.
Lingua::Stem::Fr was written and is copyrighted by Aldo Calpini and SΓ©bastien Darribere-Pley.
Lingua::Stem::Ru was written and is copyrighted by Aleksandr Guidrevitch.
This software may be freely copied and distributed under the same terms and conditions as Perl. Free to share, bestie π«Ά
π BUGS π
None known. (Iconic, honestly.)
β TODO β
Add more languages. Extend regression tests. Add support for the Lingua::Stem::Snowball family of stemmers as an alternative core stemming engine. Extend 'stem_in_place' functionality to non-English stemmers. Future plans are cute π
That's the whole page, bestie β brought to you by perl v5.42.0, dated 2020-09-27, and the manual page Lingua::Stem(3pm). Go forth and stem responsibly πΈβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Lingua-Stem 2.31-bp160.1.11 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms