1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <meta content="text/html; charset=UTF-8" http-equiv="content-type">
5 <title>Stempel - Algorithmic Stemmer for Polish Language</title>
6 <meta content="Andrzej Bialecki" name="author">
8 content="stemming, stemmer, algorithmic stemmer, Polish stemmer">
10 content="This page describes a software package consisting of high-quality stemming tables for Polish, and a universal algorithmic stemmer, which operates using these tables."
13 <body style="font-family: Arial,SansSerif;">
14 <h1><i>Stempel</i> - Algorithmic Stemmer for Polish Language</h1>
16 <p>A method for conflation of different inflected word forms is an
17 important component of many Information Retrieval systems. It helps to
18 improve the system's recall and can significantly reduce the index
19 size. This is especially true for highly-inflectional languages like
20 those from the Slavic language family (Czech, Slovak, Polish, Russian,
22 <p>This page describes a software package consisting of high-quality
23 stemming tables for Polish, and a universal algorithmic stemmer, which
24 operates using these tables. The stemmer code is taken virtually
25 unchanged from the <a href="http://www.egothor.org">Egothor project</a>.</p>
26 <p>The software distribution includes stemmer
27 tables prepared using an extensive corpus of Polish language (see
29 <p>This work is available under Apache-style Open Source license - the
30 stemmer code is covered by Egothor License, the tables and other
31 additions are covered by Apache License 2.0. Both licenses allow to use
32 the code in Open Source as well as commercial (closed source) projects.</p>
34 <p>A short explanation is in order about the terminology used in this
36 <p>In the following sections I make a distinction between <b>stem</b>
38 <p>Lemma is a base grammatical form (dictionary form, headword) of a
39 word. Lemma is an existing, grammatically correct word in some human
41 <p>Stem on the other hand is just a unique token, not necessarily
42 making any sense in any human language, but which can serve as a unique
43 label instead of lemma for the same set of inflected forms. Quite often
44 stem is referred to as a "root" of the word - which is incorrect and
45 misleading (stems sometimes have very little to do with the linguistic
46 root of a word, i.e. a pattern found in a word which is common to all
47 inflected forms or within a family of languages).</p>
48 <p>For an IR system stems are usually sufficient, for a morphological
49 analysis system obviously lemmas are a must. In practice, various
50 stemmers produce a mix of stems and lemmas, as is the case with the
51 stemmer described here. Additionally, for some languages, which use
52 suffix-based inflection rules many stemmers based on suffix-stripping
53 will produce a large percentage of stems equivalent to lemmas. This is
54 however not the case for languages with complex, irregular inflection
55 rules (such as Slavic languages) - here simplistic suffix-stripping
56 stemmers produce very poor results.</p>
58 <p>Lemmatization is a process of finding the base, non-inflected form
59 of a word. The result of lemmatization is a correct existing word,
60 often in nominative case for nouns and infinitive form for verbs. A
61 given inflected form may correspond to several lemmas (e.g. "found"
62 -> find, found) - the correct choice depends on the context.<br>
64 Stemming is concerned mostly with finding a unique "root" of a word,
65 which not necessarily results in any existing word or lemma. The
66 quality of stemming is measured by the rate of collisions (overstemming
67 - which causes words with different lemmas to be incorrectly conflated
68 into one "root"), and the rate of superfluous word "roots"
69 (understemming - which assigns several "roots" to words with the same
72 Both stemmer and lemmatizer can be implemented in various ways. The two
73 most common approaches are:<br>
76 <li>dictionary-based: where the stemmer uses an extensive dictionary
77 of morphological forms in order to find the corresponding stem or lemma</li>
78 <li>algorithmic: where the stemmer uses an algorithm, based on
79 general morphological properties of a given language plus a set of
83 There are many existing and well-known implementations of stemmers for
84 English (Porter, Lovins, Krovetz) and other European languages
85 (<a href="http://snowball.tartarus.org">Snowball</a>). There are also
86 good quality commercial lemmatizers for Polish. However, there is only
88 freely available Polish stemmer, implemented by
90 href="http://www.cs.put.poznan.pl/dweiss/xml/projects/lametyzator/index.xml?lang=en">Dawid
91 Weiss</a>, based on the "ispell" dictionary and Jan Daciuk's <a
92 href="http://www.eti.pg.gda.pl/%7Ejandac/">FSA package</a>. That
93 stemmer is dictionary-based. This means that even
95 perfect accuracy for previously known word forms found in its
97 completely fails in case of all other word forms. This deficiency is
98 somewhat mitigated by the comprehensive dictionary distributed with
99 this stemmer (so there is a high probability that most of the words in
100 the input text will be found in the dictionary), however the problem
101 still remains (please see the page above for more detailed description).<br>
103 The implementation described here uses an algorithmic method. This
105 and particular algorithm implementation are described in detail in
107 The main advantage of algorithmic stemmers is their ability to process
109 unseen word forms with high accuracy. This particular algorithm uses a
112 transformation rules (patch commands), which describe how a word with a
113 given pattern should be transformed to its stem. These rules are first
114 learned from a training corpus. They don't
116 all possible cases, so there is always some loss of precision/recall
118 means that even the words from the training corpus are sometimes
119 incorrectly stemmed).<br>
120 <h2>Algorithm and implementation<span style="font-style: italic;"></span></h2>
121 The algorithm and its Java implementation is described in detail in the
122 publications cited below. Here's just a short excerpt from [2]:<br>
125 <div style="width: 80%;" align="justify">"The aim is separation of the
126 stemmer execution code from the data
127 structures [...]. In other words, a static algorithm configurable by
128 data must be developed. The word transformations that happen in the
129 stemmer must be then encoded to the data tables.<br>
131 The tacit input of our method is a sample set (a so-called dictionary)
132 of words (as keys) and their stems. Each record can be equivalently
133 stored as a key and the record of key's transformation to its
134 respective stem. The transformation record is termed a patch command
135 (P-command). It must be ensured that P-commands are universal, and that
136 P-commands can transform any word to its stem. Our solution[6,8] is
137 based on the Levenstein metric [10], which produces P-command as the
138 minimum cost path in a directed graph.<br>
140 One can imagine the P-command as an algorithm for an operator (editor)
141 that rewrites a string to another string. The operator can use these
142 instructions (PP-command's): <span style="font-weight: bold;">removal </span>-
143 deletes a sequence of characters starting at the current cursor
144 position and moves the cursor to the next character. The length of this
145 sequence is the parameter; <span style="font-weight: bold;">insertion </span>-
146 inserts a character ch, without moving the cursor. The character ch is
147 a parameter; <span style="font-weight: bold;">substitution </span>
148 - rewrites a character at the current cursor position to the character
149 ch and moves the cursor to the next character. The character ch is a
150 parameter; <span style="font-weight: bold;">no operation</span> (NOOP)
151 - skip a sequence of characters starting at the current cursor
152 position. The length of this sequence is the parameter.<br>
154 The P-commands are applied from the end of a word (right to left). This
155 assumption can reduce the set of P-command's, because the last NOOP,
156 moving the cursor to the end of a string without any changes, need not
160 Data structure used to keep the dictionary (words and their P-commands)
161 is a trie. Several optimization steps are applied in turn to reduce and
162 optimize the initial trie, by eliminating useless information and
163 shortening the paths in the trie.<br>
165 Finally, in order to obtain a stem from the input word, the word is
166 passed once through a matching path in the trie (applying at each node
167 the P-commands stored there). The result is a word stem.<br>
169 <p><i>(to be completed...)</i></p>
170 <p>The following Polish corpora have been used:</p>
173 href="http://sourceforge.net/project/showfiles.php?group_id=49316&package_id=65354">Polish
175 from ispell distribution</a></li>
176 <li><a href="http://www.mimuw.edu.pl/polszczyzna/">Wzbogacony korpus
177 słownika frekwencyjnego</a></li>
178 <!--<li><a href="http://www.korpus.pl">Korpus IPI PAN</a></li>-->
179 <!--<li>The Bible (so called "Warsaw Bible" or "Brytyjka")</li>--><li>The
180 Bible (so called "TysiÄ…clecia") - unauthorized electronic version</li>
182 href="http://www.mimuw.edu.pl/polszczyzna/Debian/sam34_3.4a.02-1_i386.deb">Analizator
183 morfologiczny SAM v. 3.4</a> - this was used to recover lemmas
184 missing from other texts</li>
186 <p>This step was the most time-consuming - and it would probably be
187 even more tedious and difficult if not for the
189 <a href="http://www.python.org/">Python</a>. The source texts had to be
190 brought to a common encoding (UTF-8) - some of them used quite ancient
191 encodings like Mazovia or DHN - and then scripts were written to
192 collect all lemmas and
193 inflected forms from the source texts. In cases when the source text
196 I used the SAM analyzer to produce lemmas. In cases of ambiguous
197 lemmatization I decided to put references to inflected forms from all
200 <p>All grammatical categories were allowed to appear in the corpus,
201 i.e. nouns, verbs, adjectives, numerals, and pronouns. The resulting
202 corpus consisted of roughly 87,000+ inflection sets, i.e. each set
203 consisted of one base form (lemma) and many inflected forms. However,
204 because of the nature of the training method I restricted these sets to
205 include only those where there were at least 4 inflected forms. Sets
206 with 3 or less inflected forms were removed, so that the final corpus
207 consisted of ~69,000 unique sets, which in turn contained ~1.5 mln
208 inflected forms. <br>
211 <p>I tested the stemmer tables produced using the implementation
212 described above. The following sections give some details about
215 <h3>Testing procedure</h3>
216 <p>The testing procedure was as follows:
219 <li>the whole corpus of ~69,000 unique sets was shuffled, so that the
220 input sets were in random order.</li>
221 <li>the corpus was split into two parts - one with 30,000 sets (Part
222 1), the other with ~39,000 sets (Part 2).</li>
223 <li>Training samples were drawn in sequential order from the Part 1.
224 Since the sets were already randomized, the training samples were also
225 randomized, but this procedure ensured that each larger training sample
226 contained all smaller samples.</li>
227 <li>Part 2 was used for testing. Note: this means that the testing
228 run used <em>only</em> words previously unseen during the training
229 phase. This is the worst scenario, because it means that stemmer must
230 extrapolate the learned rules to unknown cases. This also means that in
231 a real-life case (where the input is a mix between known and unknown
232 words) the F-measure of the stemmer will be even higher than in the
235 <h3>Test results</h3>
236 <p>The following table summarizes test results for varying sizes
237 of training samples. The meaning of the table columns is
241 <li><b>training sets:</b> the number of training sets. One set
242 consists of one lemma and at least 4 and up to ~80 inflected forms
243 (including pre- and suffixed forms).</li>
244 <li><b>testing forms:</b> the number of testing forms. Only inflected
245 forms were used in testing.</li>
246 <li><b>stem OK:</b> the number of cases when produced output was a
247 correct (unique) stem. Note: quite often correct stems were also
249 <li><b>lemma OK:</b> the number of cases when produced output was a
251 <li><b>missing:</b> the number of cases when stemmer was unable to
252 provide any output.</li>
253 <li><b>stem bad:</b> the number of cases when produced output was a
254 stem, but already in use identifying a different set.</li>
255 <li><b>lemma bad:</b> the number of cases when produced output was an
256 incorrect lemma. Note: quite often in such case the output was a
258 <li><b>table size:</b> the size in bytes of the stemmer table.</li>
261 <table border="1" cellpadding="2" cellspacing="0">
263 <tr bgcolor="#a0b0c0">
264 <th>Training sets</th>
265 <th>Testing forms</th>
271 <th>Table size [B]</th>
406 <p>I also measured the time to produce a stem (which involves
408 retrieving a patch command and applying the patch command to the input
410 On a machine running Windows XP (Pentium 4, 1.7 GHz, JDK 1.4.2_03
412 for tables ranging in size from 1,000 to 20,000 cells, the time to
414 single stem varies between 5-10 microseconds.<br>
416 <p>This means that the stemmer can process up to <span
417 style="font-weight: bold;">200,000 words per second</span>, an
418 outstanding result when compared to other stemmers (Morfeusz - ~2,000
419 w/s, FormAN (MS Word analyzer) - ~1,000 w/s).<br>
421 <p>The package contains a class <code>org.getopt.stempel.Benchmark</code>,
422 which you can use to produce reports
423 like the one below:<br>
425 <pre>--------- Stemmer benchmark report: -----------<br>Stemmer table: /res/tables/stemmer_2000.out<br>Input file: ../test3.txt<br>Number of runs: 3<br><br> RUN NUMBER: 1 2 3<br> Total input words 1378176 1378176 1378176<br> Missed output words 112 112 112<br> Time elapsed [ms] 6989 6940 6640<br> Hit rate percent 99.99% 99.99% 99.99%<br> Miss rate percent 00.01% 00.01% 00.01%<br> Words per second 197192 198584 207557<br> Time per word [us] 5.07 5.04 4.82<br></pre>
427 <p>The results of these tests are very encouraging. It seems that using
429 training corpus and the stemming algorithm described above results in a
430 high-quality stemmer useful for most applications. Moreover, it can
432 be used as a better than average lemmatizer.</p>
433 <p>Both the author of the implementation
434 (Leo Galambos, <leo.galambos AT egothor DOT org>) and the author
436 compilation (Andrzej Bialecki <ab AT getopt DOT org>) would
438 feedback and suggestions for further improvements.</p>
439 <h2>Bibliography</h2>
441 <li>Galambos, L.: Multilingual Stemmer in Web Environment, PhD
443 Faculty of Mathematics and Physics, Charles University in Prague, in
445 <li>Galambos, L.: Semi-automatic Stemmer Evaluation. International
446 Intelligent Information Processing and Web Mining Conference, 2004,
447 Zakopane, Poland.</li>
448 <li>Galambos, L.: Lemmatizer for Document Information Retrieval
449 Systems in JAVA.<span style="text-decoration: underline;"> </span><a
450 class="moz-txt-link-rfc2396E"
451 href="http://www.informatik.uni-trier.de/%7Eley/db/conf/sofsem/sofsem2001.html#Galambos01"><http://www.informatik.uni-trier.de/%7Eley/db/conf/sofsem/sofsem2001.html#Galambos01></a>
452 SOFSEM 2001, Piestany, Slovakia. <br>