55from enum import Enum
66from typing import Iterable
77from typing import Optional
8+ from typing import Union
89
910from pysimstring import simstring
1011
@@ -29,8 +30,8 @@ class SimStringWrapper(StringDistance):
2930
3031 def __init__ (
3132 self ,
32- words = Iterable [str ],
33- measure = ESimStringMeasure .JACCARD ,
33+ words : Iterable [str ],
34+ measure : Union [ str , ESimStringMeasure ] = ESimStringMeasure .JACCARD ,
3435 name : str = None ,
3536 threshold = 0.5 ,
3637 min_nb_char = 5 ,
@@ -42,7 +43,7 @@ def __init__(
4243 An easy way to provide these words is to call
4344 :py:meth:`~iamsystem.Matcher.get_keywords_unigrams`.
4445 :param name: a name given to this algorithm. Default measure name.
45- :param measure: a similarity measure selected from
46+ :param measure: a similarity measure string or selected from
4647 :class:`~iamsystem.fuzzy.simstring.ESimStringMeasure`.
4748 Default JACCARD.
4849 :param threshold: similarity measure threshold.
@@ -51,6 +52,13 @@ def __init__(
5152 :param words2ignore: words that must be ignored by the algorithm to
5253 avoid false positives, for example English vocabulary words.
5354 """
55+ # If an error occured during initialization, file is not opened
56+ # the __del__ is called and return an error
57+ # I add this attribute to check if the file was opened before trying
58+ # to close it.
59+ self .__file_is_open = False
60+ if isinstance (measure , str ):
61+ measure = ESimStringMeasure [measure .upper ()]
5462 if name is None :
5563 name = measure .name
5664 super ().__init__ (
@@ -63,6 +71,7 @@ def __init__(
6371 for word in words :
6472 ss_db .insert (word )
6573 self .ss_reader = simstring .reader (abs_path )
74+ self .__file_is_open = True
6675 self .ss_reader .measure = getattr (simstring , measure .value )
6776 self .ss_reader .threshold = threshold
6877
@@ -79,7 +88,8 @@ def __del__(self):
7988 # call. However, it takes more time. It seems to be ok to close
8089 # the file here.
8190 # https://stackoverflow.com/questions/44142836/open-file-inside-class
82- self .ss_reader .close ()
91+ if self .__file_is_open :
92+ self .ss_reader .close ()
8393
8494
8595class SimstringWriter :
0 commit comments