(The original question was about generating all forms for one given word. This answer focuses on the harder problem of generating all forms for all words of a dictionary. I post this here as this is what comes up when searching for the harder problem.)
Update on unmunch
ing
As of 2021, Hunspell provides two tools which are called unmunch
and wordforms
for generating word forms. Their respective usage is:
# print all forms for all words whose stems are given in `stems.dic`
# and make use of affix rules defined in `affixes.aff`:
unmunch stems.dic affixes.aff
# print the forms of ONE given word (a single stem with no affix flag)
# which are allowed by the reference dictionary defined by the pair of
# `stems.dic` and `affixes.aff`:
wordforms affixes.aff stems.dic word
So affixes.aff
would be given by your language, and stems.dic
would be either a reference dictionary for your language, or a custom dictionary with the stems of the new words you want to generate.
Unfortunately, Hunspell’s unmunch
is deprecated¹ and does not work properly. It is inherited from MySpell, and my guess is that it does not support all features of Hunspell. When I tried using it with the reference French dictionary (Dicollecte, v7.0), it generated garbage words by applying affix rules it was not supposed to apply (such as: conjugating non-verbs), and failed to generate many expected words. Some of the defects I could pinpoint:
- apparently it does not properly support UTF-8?
- it does not understand
FLAG long
, which leads to many affix rules being applied out of the blue;
- it wrongly parses metadata attached to stems as affix flags, leading to even more arbitrary rules being applied;
- it does not understand
0
as meaning the empty string and thus generate garbage words containing 0
;
- it seems to limit derivations to at most 2 rules, and thus misses many expected words.
wordforms
should be more up-to-date, so you might try to emulate unmunch
with wordforms
(as the README suggests), but the latter only takes one unqualified stem, and compares it against the whole dictionary implied by stems.dic
and affixes.aff
. This takes a lot of time per stem and, worst, you would have to call wordforms
in turn with all the stems in stems.dic
. So you would have a quadratic time. For me, with the reference set of affixes for French, this is slow to the point of being unusable—even with only 10 stems! The unusable Bash code is, for illustration:
# /!\ EXTREMELY SLOW
aff='affixes.aff'
dic='stems.dic'
cat "$dic" | while read -r stem ; do # read each stem of the file
stem="${stem%%/*}" # strip the stem from the optional slash (attached affix flags)
wordforms "$aff" "$dic" "$stem" # generate all forms for this stem
done \
| sort -u # sort (according to the locale) and remove duplicates
Also, note that wordforms
produces bare words, while unmunch
was able to attach derived metadata (such as part-of-speech or gender), so with wordforms
you lose information (which may or may not matter to you).
The lack of a replacement for unmunch
is a known issue. Apparently Hunspell developers will not address it in a predictable future (something about funding?). This has led to several people reimplementing the functionality, you’ll find pointers throughout GitHub issues.
- In 2012 someone wrote an sh/awk script by adapting the source code of
wordforms
; maybe severely outdated, but I haven’t tried it.
- In 2014 someone wrote another sh/awk script to treat a Hindi dictionary; for me it worked slightly better than the built-in
unmunch
, as it does support FLAG long
, but still has the other defects mentioned above.
- In December 2020 someone wrote a Perl module and a Perl program; looks great, but I’m not sure how to use them.
- In 2021 I wrote my own version, that does fix the defects mentioned above, and works for French. It does not support every feature though, see the comments in the source code for a list of unsupported features.
¹ From the repo’s README.