[libvoikko] Changes in Python interface / support for multiple instances

Harri Pitkänen hatapitk at iki.fi
Wed Mar 17 20:06:56 EET 2010


Thread safety work within libvoikko has now progressed far enough that basic 
spell checking operations work correctly with multiple open instances.

While working on this I also decided to change our Python interface a bit. 
Since we have not yet promised that the interface would remain fixed I hope 
this change will not cause too much trouble for anyone. If you for some reason 
need to use the old Python interface with libvoikko 3.0, just take the Python 
module from libvoikko 2.3 and use it with the new library, that should work.

Anyway, the notable change was that the init() method is now gone, 
initialization is done directly in the constructor. terminate() is still there 
but you don't necessarily need to call it, the resources are also released 
when the Voikko object gets deleted by Python garbage collector. You can still 
call terminate() if you wish to immediately release the resources allocated 
for transducers or other backend linguistic stuff. In fact Voikko.terminate() 
is in many ways similar to close() method for Python or Java file/network 
streams.

The new Python module is in SVN with name libvoikkoNew.py. I will replace the 
old libvoikko.py with it once it is a complete replacement.

Below is an example Python script that demonstrates how the new module can be 
used to find all four letter strings consisting of letters 
"aehijklmnopstuvyäö" that are only accepted by one of two spellers. You could 
not do that efficiently in one process with previous version of libvoikko 
because of the limitation of one open Voikko instance at a time. The example 
code compares two configurations of Suomi-malaga (standard and dialect) but 
the same should work for comparing two HFST spellers, or an HFST speller with 
a Malaga speller, or two Lttoolbox spellers etc.

Harri


from libvoikkoNew import Voikko

CHARS = u"aehijklmnopstuvyäö"
WORDLENGTH = 4

speller1 = Voikko(variant=u"standard")
speller2 = Voikko(variant=u"dialect")

def compareSpellers(word):
	ok1 = speller1.spell(word)
	ok2 = speller2.spell(word)
	if ok1 != ok2:
		if ok1:
			print u"Accepted only by speller1: " + word
		else:
			print u"Accepted only by speller2: " + word

def tryStrings(prefix, length):
	if length == 0:
		compareSpellers(prefix)
		return
	for char in CHARS:
		tryStrings(prefix + char, length - 1)

tryStrings(u"", WORDLENGTH)



More information about the Libvoikko mailing list